rustc_borrowck/constraints/
mod.rs1use std::fmt;
2use std::ops::Index;
3
4use rustc_index::{IndexSlice, IndexVec};
5use rustc_middle::mir::ConstraintCategory;
6use rustc_middle::ty::{RegionVid, TyCtxt, VarianceDiagInfo};
7use rustc_span::Span;
8use tracing::debug;
9
10use crate::type_check::Locations;
11
12pub(crate) mod graph;
13
14#[derive(Clone, Debug, Default)]
19pub(crate) struct OutlivesConstraintSet<'tcx> {
20 outlives: IndexVec<OutlivesConstraintIndex, OutlivesConstraint<'tcx>>,
21}
22
23impl<'tcx> OutlivesConstraintSet<'tcx> {
24 pub(crate) fn push(&mut self, constraint: OutlivesConstraint<'tcx>) {
25 debug!("OutlivesConstraintSet::push({:?})", constraint);
26 if constraint.sup == constraint.sub {
27 return;
29 }
30 self.outlives.push(constraint);
31 }
32
33 pub(crate) fn graph(&self, num_region_vars: usize) -> graph::NormalConstraintGraph {
40 graph::ConstraintGraph::new(graph::Normal, self, num_region_vars)
41 }
42
43 pub(crate) fn reverse_graph(&self, num_region_vars: usize) -> graph::ReverseConstraintGraph {
46 graph::ConstraintGraph::new(graph::Reverse, self, num_region_vars)
47 }
48
49 pub(crate) fn outlives(
50 &self,
51 ) -> &IndexSlice<OutlivesConstraintIndex, OutlivesConstraint<'tcx>> {
52 &self.outlives
53 }
54}
55
56impl<'tcx> Index<OutlivesConstraintIndex> for OutlivesConstraintSet<'tcx> {
57 type Output = OutlivesConstraint<'tcx>;
58
59 fn index(&self, i: OutlivesConstraintIndex) -> &Self::Output {
60 &self.outlives[i]
61 }
62}
63
64#[derive(Copy, Clone, PartialEq, Eq)]
65pub struct OutlivesConstraint<'tcx> {
66 pub sup: RegionVid,
72
73 pub sub: RegionVid,
75
76 pub locations: Locations,
78
79 pub span: Span,
84
85 pub category: ConstraintCategory<'tcx>,
87
88 pub variance_info: VarianceDiagInfo<TyCtxt<'tcx>>,
90
91 pub from_closure: bool,
93}
94
95impl<'tcx> fmt::Debug for OutlivesConstraint<'tcx> {
96 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
97 write!(
98 formatter,
99 "({:?}: {:?}) due to {:?} ({:?}) ({:?})",
100 self.sup, self.sub, self.locations, self.variance_info, self.category,
101 )
102 }
103}
104
105rustc_index::newtype_index! {
106 #[debug_format = "OutlivesConstraintIndex({})"]
107 pub(crate) struct OutlivesConstraintIndex {}
108}
109
110rustc_index::newtype_index! {
111 #[orderable]
112 #[debug_format = "ConstraintSccIndex({})"]
113 pub struct ConstraintSccIndex {}
114}