Skip to content

Commit 4bb6bd0

Browse files
committed
Auto merge of rust-lang#130227 - amandasystems:remove-placeholders-completely, r=<try>
[WIP] Remove placeholders completely This PR does shotgun surgery on borrowck to remove all special handling of placeholders, completely replacing them with a preprocessing step that rewrites placeholder leaks into constraints, removing constraint propagation of placeholders and all logic used to detect placeholder violations during error reporting. This finishes what rust-lang#123720 started. The new method works like this: 1. during SCC construction, some information about SCC membership and reachability is retained 2. just after SCC construction, a constraint `r - (from: to_invalid) - > 'static` is added when `r` is the representative of an SCC and 1. that SCC either has had its universe shrunk because it reaches a region with a smaller one (in which case `to_invalid` is the smallest-universed region it reaches), or if it reaches a region with a too large universe that isn't part of the SCC (in which case `to_invalid` is the region with a too large universe). In either case, `from` is also `r`. 2. some region `reaches` in `r`'s SCC reaches another placeholder, `reached`, in which case the added constraint is `r -> (reaches: reached) 'static`. Through clever choice of defaults (chosing minimum elements), `reached` will be `r` if at all possible. When tracing errors for diagnostics one of these special constraints along a path are treated much like a HTTP redirect: if we are explaining `from: to` and reach an edge with `reaches: invalid` we stop the search and start following `reaches: invalid` instead. When doing this the implicit edges `x: 'static` for every region `x` are ignored, since the search would otherwise be able to cheat by going through 'static and re-find the same edge again. A bunch of optimisations are possible: - ~~Conservatively add constraints, e.g. one per SCC. May worsen error tracing!~~ - as a final pass, allow fusing the annotations for the SCC after adding the extra constraints to remove unnecessary information and save memory. This could be done cheaply since we already iterate over the entire set of SCCs. - currently, if constraints are added the entire set of SCCs are recomputed. This is of course rather wasteful, and we could do better. Especially since SCCs are added in dependency order. This would require a fully separate SCC module since the dynamic SCC combo we'd need now shares almost no properties with regular SCC computation. Given that this is meant to be a temporary work-around, that seems like too much work. There are a bunch of rather nice bonuses: - We now don't need to expose region indices in `MirTypeckRegionConstraints` to the entire crate. The only entry point is `placeholder_region()` so correctness of the indices is now guaranteed - A lot of things that were previously iterations over lists is now a single lookup - The constraint graph search functions are simple and at least one of them can now take a proper region as target rather than a predicate function. The only case that needs the predicate argument to `find_constraint_path_to()` is `find_sub_region_live_at()`, which may or may not be possible to work around. r​? nikomatsakis
2 parents ecf2d1f + 40f3aea commit 4bb6bd0

File tree

14 files changed

+777
-658
lines changed

14 files changed

+777
-658
lines changed

compiler/rustc_borrowck/src/constraints/graph.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,17 @@ impl<D: ConstraintGraphDirection> ConstraintGraph<D> {
106106
}
107107

108108
/// Given a region `R`, iterate over all constraints `R: R1`.
109+
/// if `static_region` is `None`, do not yield implicit
110+
/// `'static -> a` edges.
109111
pub(crate) fn outgoing_edges<'a, 'tcx>(
110112
&'a self,
111113
region_sup: RegionVid,
112114
constraints: &'a OutlivesConstraintSet<'tcx>,
113-
static_region: RegionVid,
115+
static_region: Option<RegionVid>,
114116
) -> Edges<'a, 'tcx, D> {
115117
//if this is the `'static` region and the graph's direction is normal,
116118
//then setup the Edges iterator to return all regions #53178
117-
if region_sup == static_region && D::is_normal() {
119+
if Some(region_sup) == static_region && D::is_normal() {
118120
Edges {
119121
graph: self,
120122
constraints,
@@ -135,7 +137,7 @@ pub(crate) struct Edges<'a, 'tcx, D: ConstraintGraphDirection> {
135137
constraints: &'a OutlivesConstraintSet<'tcx>,
136138
pointer: Option<OutlivesConstraintIndex>,
137139
next_static_idx: Option<usize>,
138-
static_region: RegionVid,
140+
static_region: Option<RegionVid>,
139141
}
140142

141143
impl<'a, 'tcx, D: ConstraintGraphDirection> Iterator for Edges<'a, 'tcx, D> {
@@ -153,8 +155,12 @@ impl<'a, 'tcx, D: ConstraintGraphDirection> Iterator for Edges<'a, 'tcx, D> {
153155
Some(next_static_idx + 1)
154156
};
155157

158+
let Some(static_region) = self.static_region else {
159+
return None;
160+
};
161+
156162
Some(OutlivesConstraint {
157-
sup: self.static_region,
163+
sup: static_region,
158164
sub: next_static_idx.into(),
159165
locations: Locations::All(DUMMY_SP),
160166
span: DUMMY_SP,
@@ -194,7 +200,11 @@ impl<'a, 'tcx, D: ConstraintGraphDirection> RegionGraph<'a, 'tcx, D> {
194200
/// there exists a constraint `R: R1`.
195201
pub(crate) fn outgoing_regions(&self, region_sup: RegionVid) -> Successors<'a, 'tcx, D> {
196202
Successors {
197-
edges: self.constraint_graph.outgoing_edges(region_sup, self.set, self.static_region),
203+
edges: self.constraint_graph.outgoing_edges(
204+
region_sup,
205+
self.set,
206+
Some(self.static_region),
207+
),
198208
}
199209
}
200210
}

compiler/rustc_borrowck/src/constraints/mod.rs

+101-42
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
use std::fmt;
22
use std::ops::Index;
33

4+
use rustc_data_structures::fx::FxHashSet;
5+
use rustc_data_structures::graph::scc;
46
use rustc_index::{IndexSlice, IndexVec};
7+
use rustc_infer::infer::NllRegionVariableOrigin;
58
use rustc_middle::mir::ConstraintCategory;
69
use rustc_middle::ty::{RegionVid, TyCtxt, VarianceDiagInfo};
710
use rustc_span::Span;
811
use tracing::{debug, instrument};
912

10-
use crate::region_infer::{ConstraintSccs, RegionDefinition, RegionTracker};
13+
use crate::region_infer::{PlaceholderTracking, RegionDefinition, RegionTracker, SccAnnotations};
1114
use crate::type_check::Locations;
1215
use crate::universal_regions::UniversalRegions;
1316

@@ -57,16 +60,39 @@ impl<'tcx> OutlivesConstraintSet<'tcx> {
5760
/// Computes cycles (SCCs) in the graph of regions. In particular,
5861
/// find all regions R1, R2 such that R1: R2 and R2: R1 and group
5962
/// them into an SCC, and find the relationships between SCCs.
60-
pub(crate) fn compute_sccs(
63+
pub(crate) fn compute_sccs<
64+
A: scc::Annotation,
65+
AA: scc::Annotations<RegionVid, ConstraintSccIndex, A>,
66+
>(
6167
&self,
6268
static_region: RegionVid,
63-
definitions: &IndexVec<RegionVid, RegionDefinition<'tcx>>,
64-
) -> ConstraintSccs {
65-
let constraint_graph = self.graph(definitions.len());
69+
num_region_vars: usize,
70+
annotations: &mut AA,
71+
) -> scc::Sccs<RegionVid, ConstraintSccIndex> {
72+
let constraint_graph = self.graph(num_region_vars);
6673
let region_graph = &constraint_graph.region_graph(self, static_region);
67-
ConstraintSccs::new_with_annotation(&region_graph, |r| {
68-
RegionTracker::new(r, &definitions[r])
69-
})
74+
scc::Sccs::new_with_annotation(&region_graph, annotations)
75+
}
76+
77+
/// There is a placeholder violation; add a requirement
78+
/// that some SCC outlive static and explain which region
79+
/// reaching which other region caused that.
80+
fn add_placeholder_violation_constraint(
81+
&mut self,
82+
outlives_static: RegionVid,
83+
blame_from: RegionVid,
84+
blame_to: RegionVid,
85+
fr_static: RegionVid,
86+
) {
87+
self.push(OutlivesConstraint {
88+
sup: outlives_static,
89+
sub: fr_static,
90+
category: ConstraintCategory::IllegalPlaceholder(blame_from, blame_to),
91+
locations: Locations::All(rustc_span::DUMMY_SP),
92+
span: rustc_span::DUMMY_SP,
93+
variance_info: VarianceDiagInfo::None,
94+
from_closure: false,
95+
});
7096
}
7197

7298
/// This method handles Universe errors by rewriting the constraint
@@ -79,7 +105,7 @@ impl<'tcx> OutlivesConstraintSet<'tcx> {
79105
/// eventually go away.
80106
///
81107
/// For a more precise definition, see the documentation for
82-
/// [`RegionTracker::has_incompatible_universes()`].
108+
/// [`RegionTracker`] and its methods!.
83109
///
84110
/// This edge case used to be handled during constraint propagation
85111
/// by iterating over the strongly connected components in the constraint
@@ -104,57 +130,90 @@ impl<'tcx> OutlivesConstraintSet<'tcx> {
104130
/// Every constraint added by this method is an
105131
/// internal `IllegalUniverse` constraint.
106132
#[instrument(skip(self, universal_regions, definitions))]
107-
pub(crate) fn add_outlives_static(
133+
pub(crate) fn add_outlives_static<'d>(
108134
&mut self,
109135
universal_regions: &UniversalRegions<'tcx>,
110-
definitions: &IndexVec<RegionVid, RegionDefinition<'tcx>>,
111-
) -> ConstraintSccs {
136+
definitions: &'d IndexVec<RegionVid, RegionDefinition<'tcx>>,
137+
) -> (scc::Sccs<RegionVid, ConstraintSccIndex>, PlaceholderTracking) {
112138
let fr_static = universal_regions.fr_static;
113-
let sccs = self.compute_sccs(fr_static, definitions);
139+
let mut annotations = SccAnnotations::init(definitions);
140+
let sccs = self.compute_sccs(fr_static, definitions.len(), &mut annotations);
114141

115-
// Changed to `true` if we added any constraints to `self` and need to
116-
// recompute SCCs.
117-
let mut added_constraints = false;
142+
// Is this SCC already outliving static directly or transitively?
143+
let mut outlives_static = FxHashSet::default();
118144

119145
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*.
146+
let annotation: RegionTracker = annotations.scc_to_annotation[scc];
123147
if scc == sccs.scc(fr_static) {
148+
// No use adding 'static: 'static.
124149
continue;
125150
}
126151

127-
let annotation = sccs.annotation(scc);
128-
129-
// If this SCC participates in a universe violation,
152+
// If this SCC participates in a universe violation
130153
// e.g. if it reaches a region with a universe smaller than
131154
// 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);
155+
// outlive `'static`. Here we get to know which reachable region
156+
// caused the violation.
157+
if let Some(to) = annotation.universe_violation() {
158+
outlives_static.insert(scc);
159+
self.add_placeholder_violation_constraint(
160+
annotation.representative_rvid(),
161+
annotation.representative_rvid(),
162+
to,
163+
fr_static,
164+
);
165+
}
166+
}
167+
168+
// Note: it's possible to sort this iterator by SCC and get dependency order,
169+
// which makes it easy to only add only one constraint per future cycle.
170+
// However, this worsens diagnostics and requires iterating over
171+
// all successors to determine if we outlive static transitively,
172+
// a cost you pay even if you have no placeholders.
173+
let placeholders_and_sccs =
174+
definitions.iter_enumerated().filter_map(|(rvid, definition)| {
175+
if matches!(definition.origin, NllRegionVariableOrigin::Placeholder { .. }) {
176+
Some((sccs.scc(rvid), rvid))
177+
} else {
178+
None
179+
}
180+
});
181+
182+
// The second kind of violation: a placeholder reaching another placeholder.
183+
for (scc, rvid) in placeholders_and_sccs {
184+
let annotation = annotations.scc_to_annotation[scc];
185+
186+
if sccs.scc(fr_static) == scc || outlives_static.contains(&scc) {
187+
debug!("{:?} already outlives (or is) static", annotation.representative_rvid());
188+
continue;
149189
}
190+
191+
if let Some(other_placeholder) = annotation.reaches_other_placeholder(rvid) {
192+
debug!(
193+
"Placeholder {rvid:?} of SCC {scc:?} reaches other placeholder {other_placeholder:?}"
194+
);
195+
outlives_static.insert(scc);
196+
self.add_placeholder_violation_constraint(
197+
annotation.representative_rvid(),
198+
rvid,
199+
other_placeholder,
200+
fr_static,
201+
);
202+
};
150203
}
151204

152-
if added_constraints {
205+
if !outlives_static.is_empty() {
206+
debug!("The following SCCs had :'static constraints added: {:?}", outlives_static);
207+
let mut annotations = SccAnnotations::init(definitions);
208+
153209
// We changed the constraint set and so must recompute SCCs.
154-
self.compute_sccs(fr_static, definitions)
210+
(
211+
self.compute_sccs(fr_static, definitions.len(), &mut annotations),
212+
PlaceholderTracking::On(annotations.scc_to_annotation),
213+
)
155214
} else {
156215
// If we didn't add any back-edges; no more work needs doing
157-
sccs
216+
(sccs, PlaceholderTracking::On(annotations.scc_to_annotation))
158217
}
159218
}
160219
}

compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs

+3-15
Original file line numberDiff line numberDiff line change
@@ -181,24 +181,12 @@ trait TypeOpInfo<'tcx> {
181181
bound: placeholder.bound,
182182
});
183183

184-
let error_region =
185-
if let RegionElement::PlaceholderRegion(error_placeholder) = error_element {
186-
let adjusted_universe =
187-
error_placeholder.universe.as_u32().checked_sub(base_universe.as_u32());
188-
adjusted_universe.map(|adjusted| {
189-
ty::Region::new_placeholder(tcx, ty::Placeholder {
190-
universe: adjusted.into(),
191-
bound: error_placeholder.bound,
192-
})
193-
})
194-
} else {
195-
None
196-
};
197-
198184
debug!(?placeholder_region);
199185

186+
// FIXME: this is obviously weird; this whole argument now does nothing and maybe
187+
// it should?
200188
let span = cause.span;
201-
let nice_error = self.nice_error(mbcx, cause, placeholder_region, error_region);
189+
let nice_error = self.nice_error(mbcx, cause, placeholder_region, None);
202190

203191
debug!(?nice_error);
204192

compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
488488
let (blame_constraint, extra_info) = self.regioncx.best_blame_constraint(
489489
borrow_region,
490490
NllRegionVariableOrigin::FreeRegion,
491-
|r| self.regioncx.provides_universal_region(r, borrow_region, outlived_region),
491+
outlived_region,
492492
);
493493
let BlameConstraint { category, from_closure, cause, .. } = blame_constraint;
494494

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+25-43
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl<'tcx> ConstraintDescription for ConstraintCategory<'tcx> {
6161
| ConstraintCategory::Boring
6262
| ConstraintCategory::BoringNoLocation
6363
| ConstraintCategory::Internal
64-
| ConstraintCategory::IllegalUniverse => "",
64+
| ConstraintCategory::IllegalPlaceholder(..) => "",
6565
}
6666
}
6767
}
@@ -206,52 +206,35 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
206206
&self,
207207
diag: &mut Diag<'_>,
208208
lower_bound: RegionVid,
209-
) {
209+
) -> Option<()> {
210210
let mut suggestions = vec![];
211211
let hir = self.infcx.tcx.hir();
212212

213-
// find generic associated types in the given region 'lower_bound'
214-
let gat_id_and_generics = self
215-
.regioncx
216-
.placeholders_contained_in(lower_bound)
217-
.map(|placeholder| {
218-
if let Some(id) = placeholder.bound.kind.get_id()
219-
&& let Some(placeholder_id) = id.as_local()
220-
&& let gat_hir_id = self.infcx.tcx.local_def_id_to_hir_id(placeholder_id)
221-
&& let Some(generics_impl) = self
222-
.infcx
223-
.tcx
224-
.parent_hir_node(self.infcx.tcx.parent_hir_id(gat_hir_id))
225-
.generics()
226-
{
227-
Some((gat_hir_id, generics_impl))
228-
} else {
229-
None
230-
}
231-
})
232-
.collect::<Vec<_>>();
233-
debug!(?gat_id_and_generics);
234-
235213
// find higher-ranked trait bounds bounded to the generic associated types
214+
let scc = self.regioncx.constraint_sccs().scc(lower_bound);
215+
let placeholder: ty::PlaceholderRegion = self.regioncx.placeholder_representative(scc)?;
216+
let placeholder_id = placeholder.bound.kind.get_id()?.as_local()?;
217+
let gat_hir_id = self.infcx.tcx.local_def_id_to_hir_id(placeholder_id);
218+
let generics_impl =
219+
self.infcx.tcx.parent_hir_node(self.infcx.tcx.parent_hir_id(gat_hir_id)).generics()?;
220+
236221
let mut hrtb_bounds = vec![];
237-
gat_id_and_generics.iter().flatten().for_each(|(gat_hir_id, generics)| {
238-
for pred in generics.predicates {
239-
let BoundPredicate(WhereBoundPredicate { bound_generic_params, bounds, .. }) = pred
240-
else {
241-
continue;
242-
};
243-
if bound_generic_params
244-
.iter()
245-
.rfind(|bgp| self.infcx.tcx.local_def_id_to_hir_id(bgp.def_id) == *gat_hir_id)
246-
.is_some()
247-
{
248-
for bound in *bounds {
249-
hrtb_bounds.push(bound);
250-
}
222+
223+
for pred in generics_impl.predicates {
224+
let BoundPredicate(WhereBoundPredicate { bound_generic_params, bounds, .. }) = pred
225+
else {
226+
continue;
227+
};
228+
if bound_generic_params
229+
.iter()
230+
.rfind(|bgp| self.infcx.tcx.local_def_id_to_hir_id(bgp.def_id) == gat_hir_id)
231+
.is_some()
232+
{
233+
for bound in *bounds {
234+
hrtb_bounds.push(bound);
251235
}
252236
}
253-
});
254-
debug!(?hrtb_bounds);
237+
}
255238

256239
hrtb_bounds.iter().for_each(|bound| {
257240
let Trait(PolyTraitRef { trait_ref, span: trait_span, .. }, _) = bound else {
@@ -300,6 +283,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
300283
Applicability::MaybeIncorrect,
301284
);
302285
}
286+
Some(())
303287
}
304288

305289
/// Produces nice borrowck error diagnostics for all the errors collected in `nll_errors`.
@@ -446,9 +430,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
446430
debug!("report_region_error(fr={:?}, outlived_fr={:?})", fr, outlived_fr);
447431

448432
let (blame_constraint, extra_info) =
449-
self.regioncx.best_blame_constraint(fr, fr_origin, |r| {
450-
self.regioncx.provides_universal_region(r, fr, outlived_fr)
451-
});
433+
self.regioncx.best_blame_constraint(fr, fr_origin, outlived_fr);
452434
let BlameConstraint { category, cause, variance_info, .. } = blame_constraint;
453435

454436
debug!("report_region_error: category={:?} {:?} {:?}", category, cause, variance_info);

0 commit comments

Comments
 (0)