|
1 | 1 | use std::fmt;
|
2 | 2 | use std::ops::Index;
|
3 | 3 |
|
| 4 | +use rustc_data_structures::graph::scc; |
4 | 5 | use rustc_index::{IndexSlice, IndexVec};
|
5 | 6 | use rustc_middle::mir::ConstraintCategory;
|
6 | 7 | use rustc_middle::ty::{RegionVid, TyCtxt, VarianceDiagInfo};
|
7 | 8 | use rustc_span::Span;
|
8 |
| -use tracing::{debug, instrument}; |
| 9 | +use tracing::debug; |
9 | 10 |
|
10 |
| -use crate::region_infer::{ConstraintSccs, RegionDefinition, RegionTracker}; |
11 | 11 | use crate::type_check::Locations;
|
12 |
| -use crate::universal_regions::UniversalRegions; |
13 | 12 |
|
14 | 13 | pub(crate) mod graph;
|
15 | 14 |
|
@@ -57,105 +56,18 @@ impl<'tcx> OutlivesConstraintSet<'tcx> {
|
57 | 56 | /// Computes cycles (SCCs) in the graph of regions. In particular,
|
58 | 57 | /// find all regions R1, R2 such that R1: R2 and R2: R1 and group
|
59 | 58 | /// them into an SCC, and find the relationships between SCCs.
|
60 |
| - pub(crate) fn compute_sccs( |
| 59 | + pub(crate) fn compute_sccs< |
| 60 | + A: scc::Annotation, |
| 61 | + AA: scc::Annotations<RegionVid, ConstraintSccIndex, A>, |
| 62 | + >( |
61 | 63 | &self,
|
62 | 64 | static_region: RegionVid,
|
63 |
| - definitions: &IndexVec<RegionVid, RegionDefinition<'tcx>>, |
64 |
| - ) -> ConstraintSccs { |
65 |
| - let constraint_graph = self.graph(definitions.len()); |
| 65 | + num_region_vars: usize, |
| 66 | + annotations: &mut AA, |
| 67 | + ) -> scc::Sccs<RegionVid, ConstraintSccIndex> { |
| 68 | + let constraint_graph = self.graph(num_region_vars); |
66 | 69 | let region_graph = &constraint_graph.region_graph(self, static_region);
|
67 |
| - ConstraintSccs::new_with_annotation(®ion_graph, |r| { |
68 |
| - RegionTracker::new(r, &definitions[r]) |
69 |
| - }) |
70 |
| - } |
71 |
| - |
72 |
| - /// This method handles Universe errors by rewriting the constraint |
73 |
| - /// graph. For each strongly connected component in the constraint |
74 |
| - /// graph such that there is a series of constraints |
75 |
| - /// A: B: C: ... : X where |
76 |
| - /// A's universe is smaller than X's and A is a placeholder, |
77 |
| - /// add a constraint that A: 'static. This is a safe upper bound |
78 |
| - /// in the face of borrow checker/trait solver limitations that will |
79 |
| - /// eventually go away. |
80 |
| - /// |
81 |
| - /// For a more precise definition, see the documentation for |
82 |
| - /// [`RegionTracker::has_incompatible_universes()`]. |
83 |
| - /// |
84 |
| - /// This edge case used to be handled during constraint propagation |
85 |
| - /// by iterating over the strongly connected components in the constraint |
86 |
| - /// graph while maintaining a set of bookkeeping mappings similar |
87 |
| - /// to what is stored in `RegionTracker` and manually adding 'sttaic as |
88 |
| - /// needed. |
89 |
| - /// |
90 |
| - /// It was rewritten as part of the Polonius project with the goal of moving |
91 |
| - /// higher-kindedness concerns out of the path of the borrow checker, |
92 |
| - /// for two reasons: |
93 |
| - /// |
94 |
| - /// 1. Implementing Polonius is difficult enough without also |
95 |
| - /// handling them. |
96 |
| - /// 2. The long-term goal is to handle higher-kinded concerns |
97 |
| - /// in the trait solver, where they belong. This avoids |
98 |
| - /// logic duplication and allows future trait solvers |
99 |
| - /// to compute better bounds than for example our |
100 |
| - /// "must outlive 'static" here. |
101 |
| - /// |
102 |
| - /// This code is a stop-gap measure in preparation for the future trait solver. |
103 |
| - /// |
104 |
| - /// Every constraint added by this method is an |
105 |
| - /// internal `IllegalUniverse` constraint. |
106 |
| - #[instrument(skip(self, universal_regions, definitions))] |
107 |
| - pub(crate) fn add_outlives_static( |
108 |
| - &mut self, |
109 |
| - universal_regions: &UniversalRegions<'tcx>, |
110 |
| - definitions: &IndexVec<RegionVid, RegionDefinition<'tcx>>, |
111 |
| - ) -> ConstraintSccs { |
112 |
| - let fr_static = universal_regions.fr_static; |
113 |
| - let sccs = self.compute_sccs(fr_static, definitions); |
114 |
| - |
115 |
| - // Changed to `true` if we added any constraints to `self` and need to |
116 |
| - // recompute SCCs. |
117 |
| - let mut added_constraints = false; |
118 |
| - |
119 |
| - for scc in sccs.all_sccs() { |
120 |
| - // No point in adding 'static: 'static! |
121 |
| - // This micro-optimisation makes somewhat sense |
122 |
| - // because static outlives *everything*. |
123 |
| - if scc == sccs.scc(fr_static) { |
124 |
| - continue; |
125 |
| - } |
126 |
| - |
127 |
| - let annotation = sccs.annotation(scc); |
128 |
| - |
129 |
| - // If this SCC participates in a universe violation, |
130 |
| - // e.g. if it reaches a region with a universe smaller than |
131 |
| - // the largest region reached, add a requirement that it must |
132 |
| - // outlive `'static`. |
133 |
| - if annotation.has_incompatible_universes() { |
134 |
| - // Optimisation opportunity: this will add more constraints than |
135 |
| - // needed for correctness, since an SCC upstream of another with |
136 |
| - // a universe violation will "infect" its downstream SCCs to also |
137 |
| - // outlive static. |
138 |
| - added_constraints = true; |
139 |
| - let scc_representative_outlives_static = OutlivesConstraint { |
140 |
| - sup: annotation.representative, |
141 |
| - sub: fr_static, |
142 |
| - category: ConstraintCategory::IllegalUniverse, |
143 |
| - locations: Locations::All(rustc_span::DUMMY_SP), |
144 |
| - span: rustc_span::DUMMY_SP, |
145 |
| - variance_info: VarianceDiagInfo::None, |
146 |
| - from_closure: false, |
147 |
| - }; |
148 |
| - self.push(scc_representative_outlives_static); |
149 |
| - } |
150 |
| - } |
151 |
| - |
152 |
| - if added_constraints { |
153 |
| - // We changed the constraint set and so must recompute SCCs. |
154 |
| - self.compute_sccs(fr_static, definitions) |
155 |
| - } else { |
156 |
| - // If we didn't add any back-edges; no more work needs doing |
157 |
| - sccs |
158 |
| - } |
| 70 | + scc::Sccs::new_with_annotation(®ion_graph, annotations) |
159 | 71 | }
|
160 | 72 | }
|
161 | 73 |
|
|
0 commit comments