rustc_borrowck/constraints/
graph.rs

1use rustc_data_structures::graph;
2use rustc_index::IndexVec;
3use rustc_middle::mir::ConstraintCategory;
4use rustc_middle::ty::{RegionVid, VarianceDiagInfo};
5use rustc_span::DUMMY_SP;
6
7use crate::constraints::{OutlivesConstraint, OutlivesConstraintIndex, OutlivesConstraintSet};
8use crate::type_check::Locations;
9
10/// The construct graph organizes the constraints by their end-points.
11/// It can be used to view a `R1: R2` constraint as either an edge `R1
12/// -> R2` or `R2 -> R1` depending on the direction type `D`.
13pub(crate) struct ConstraintGraph<D: ConstraintGraphDirection> {
14    _direction: D,
15    first_constraints: IndexVec<RegionVid, Option<OutlivesConstraintIndex>>,
16    next_constraints: IndexVec<OutlivesConstraintIndex, Option<OutlivesConstraintIndex>>,
17}
18
19pub(crate) type NormalConstraintGraph = ConstraintGraph<Normal>;
20
21pub(crate) type ReverseConstraintGraph = ConstraintGraph<Reverse>;
22
23/// Marker trait that controls whether a `R1: R2` constraint
24/// represents an edge `R1 -> R2` or `R2 -> R1`.
25pub(crate) trait ConstraintGraphDirection: Copy + 'static {
26    fn start_region(c: &OutlivesConstraint<'_>) -> RegionVid;
27    fn end_region(c: &OutlivesConstraint<'_>) -> RegionVid;
28    fn is_normal() -> bool;
29}
30
31/// In normal mode, a `R1: R2` constraint results in an edge `R1 ->
32/// R2`. This is what we use when constructing the SCCs for
33/// inference. This is because we compute the value of R1 by union'ing
34/// all the things that it relies on.
35#[derive(Copy, Clone, Debug)]
36pub(crate) struct Normal;
37
38impl ConstraintGraphDirection for Normal {
39    fn start_region(c: &OutlivesConstraint<'_>) -> RegionVid {
40        c.sup
41    }
42
43    fn end_region(c: &OutlivesConstraint<'_>) -> RegionVid {
44        c.sub
45    }
46
47    fn is_normal() -> bool {
48        true
49    }
50}
51
52/// In reverse mode, a `R1: R2` constraint results in an edge `R2 ->
53/// R1`. We use this for optimizing liveness computation, because then
54/// we wish to iterate from a region (e.g., R2) to all the regions
55/// that will outlive it (e.g., R1).
56#[derive(Copy, Clone, Debug)]
57pub(crate) struct Reverse;
58
59impl ConstraintGraphDirection for Reverse {
60    fn start_region(c: &OutlivesConstraint<'_>) -> RegionVid {
61        c.sub
62    }
63
64    fn end_region(c: &OutlivesConstraint<'_>) -> RegionVid {
65        c.sup
66    }
67
68    fn is_normal() -> bool {
69        false
70    }
71}
72
73impl<D: ConstraintGraphDirection> ConstraintGraph<D> {
74    /// Creates a "dependency graph" where each region constraint `R1:
75    /// R2` is treated as an edge `R1 -> R2`. We use this graph to
76    /// construct SCCs for region inference but also for error
77    /// reporting.
78    pub(crate) fn new(
79        direction: D,
80        set: &OutlivesConstraintSet<'_>,
81        num_region_vars: usize,
82    ) -> Self {
83        let mut first_constraints = IndexVec::from_elem_n(None, num_region_vars);
84        let mut next_constraints = IndexVec::from_elem(None, &set.outlives);
85
86        for (idx, constraint) in set.outlives.iter_enumerated().rev() {
87            let head = &mut first_constraints[D::start_region(constraint)];
88            let next = &mut next_constraints[idx];
89            debug_assert!(next.is_none());
90            *next = *head;
91            *head = Some(idx);
92        }
93
94        Self { _direction: direction, first_constraints, next_constraints }
95    }
96
97    /// Given the constraint set from which this graph was built
98    /// creates a region graph so that you can iterate over *regions*
99    /// and not constraints.
100    pub(crate) fn region_graph<'a, 'tcx>(
101        &'a self,
102        set: &'a OutlivesConstraintSet<'tcx>,
103        static_region: RegionVid,
104    ) -> RegionGraph<'a, 'tcx, D> {
105        RegionGraph::new(set, self, static_region)
106    }
107
108    /// Given a region `R`, iterate over all constraints `R: R1`.
109    pub(crate) fn outgoing_edges<'a, 'tcx>(
110        &'a self,
111        region_sup: RegionVid,
112        constraints: &'a OutlivesConstraintSet<'tcx>,
113        static_region: RegionVid,
114    ) -> Edges<'a, 'tcx, D> {
115        //if this is the `'static` region and the graph's direction is normal,
116        //then setup the Edges iterator to return all regions #53178
117        if region_sup == static_region && D::is_normal() {
118            Edges {
119                graph: self,
120                constraints,
121                pointer: None,
122                next_static_idx: Some(0),
123                static_region,
124            }
125        } else {
126            //otherwise, just setup the iterator as normal
127            let first = self.first_constraints[region_sup];
128            Edges { graph: self, constraints, pointer: first, next_static_idx: None, static_region }
129        }
130    }
131}
132
133pub(crate) struct Edges<'a, 'tcx, D: ConstraintGraphDirection> {
134    graph: &'a ConstraintGraph<D>,
135    constraints: &'a OutlivesConstraintSet<'tcx>,
136    pointer: Option<OutlivesConstraintIndex>,
137    next_static_idx: Option<usize>,
138    static_region: RegionVid,
139}
140
141impl<'a, 'tcx, D: ConstraintGraphDirection> Iterator for Edges<'a, 'tcx, D> {
142    type Item = OutlivesConstraint<'tcx>;
143
144    fn next(&mut self) -> Option<Self::Item> {
145        if let Some(p) = self.pointer {
146            self.pointer = self.graph.next_constraints[p];
147
148            Some(self.constraints[p])
149        } else if let Some(next_static_idx) = self.next_static_idx {
150            self.next_static_idx = if next_static_idx == (self.graph.first_constraints.len() - 1) {
151                None
152            } else {
153                Some(next_static_idx + 1)
154            };
155
156            Some(OutlivesConstraint {
157                sup: self.static_region,
158                sub: next_static_idx.into(),
159                locations: Locations::All(DUMMY_SP),
160                span: DUMMY_SP,
161                category: ConstraintCategory::Internal,
162                variance_info: VarianceDiagInfo::default(),
163                from_closure: false,
164            })
165        } else {
166            None
167        }
168    }
169}
170
171/// This struct brings together a constraint set and a (normal, not
172/// reverse) constraint graph. It implements the graph traits and is
173/// usd for doing the SCC computation.
174pub(crate) struct RegionGraph<'a, 'tcx, D: ConstraintGraphDirection> {
175    set: &'a OutlivesConstraintSet<'tcx>,
176    constraint_graph: &'a ConstraintGraph<D>,
177    static_region: RegionVid,
178}
179
180impl<'a, 'tcx, D: ConstraintGraphDirection> RegionGraph<'a, 'tcx, D> {
181    /// Creates a "dependency graph" where each region constraint `R1:
182    /// R2` is treated as an edge `R1 -> R2`. We use this graph to
183    /// construct SCCs for region inference but also for error
184    /// reporting.
185    pub(crate) fn new(
186        set: &'a OutlivesConstraintSet<'tcx>,
187        constraint_graph: &'a ConstraintGraph<D>,
188        static_region: RegionVid,
189    ) -> Self {
190        Self { set, constraint_graph, static_region }
191    }
192
193    /// Given a region `R`, iterate over all regions `R1` such that
194    /// there exists a constraint `R: R1`.
195    pub(crate) fn outgoing_regions(&self, region_sup: RegionVid) -> Successors<'a, 'tcx, D> {
196        Successors {
197            edges: self.constraint_graph.outgoing_edges(region_sup, self.set, self.static_region),
198        }
199    }
200}
201
202pub(crate) struct Successors<'a, 'tcx, D: ConstraintGraphDirection> {
203    edges: Edges<'a, 'tcx, D>,
204}
205
206impl<'a, 'tcx, D: ConstraintGraphDirection> Iterator for Successors<'a, 'tcx, D> {
207    type Item = RegionVid;
208
209    fn next(&mut self) -> Option<Self::Item> {
210        self.edges.next().map(|c| D::end_region(&c))
211    }
212}
213
214impl<'a, 'tcx, D: ConstraintGraphDirection> graph::DirectedGraph for RegionGraph<'a, 'tcx, D> {
215    type Node = RegionVid;
216
217    fn num_nodes(&self) -> usize {
218        self.constraint_graph.first_constraints.len()
219    }
220}
221
222impl<'a, 'tcx, D: ConstraintGraphDirection> graph::Successors for RegionGraph<'a, 'tcx, D> {
223    fn successors(&self, node: Self::Node) -> impl Iterator<Item = Self::Node> {
224        self.outgoing_regions(node)
225    }
226}