Skip to content

Commit 264683d

Browse files
committed
Remove precise placeholder tracking from region inference
1 parent 6f76769 commit 264683d

File tree

13 files changed

+178
-338
lines changed

13 files changed

+178
-338
lines changed

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

Lines changed: 23 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -224,64 +224,38 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
224224
&self,
225225
diag: &mut Diag<'_>,
226226
lower_bound: RegionVid,
227-
) {
227+
) -> Option<()> {
228228
let tcx = self.infcx.tcx;
229229

230230
// find generic associated types in the given region 'lower_bound'
231-
let gat_id_and_generics = self
232-
.regioncx
233-
.placeholders_contained_in(lower_bound)
234-
.map(|placeholder| {
235-
if let Some(id) = placeholder.bound.kind.get_id()
236-
&& let Some(placeholder_id) = id.as_local()
237-
&& let gat_hir_id = tcx.local_def_id_to_hir_id(placeholder_id)
238-
&& let Some(generics_impl) =
239-
tcx.parent_hir_node(tcx.parent_hir_id(gat_hir_id)).generics()
240-
{
241-
Some((gat_hir_id, generics_impl))
242-
} else {
243-
None
244-
}
245-
})
246-
.collect::<Vec<_>>();
247-
debug!(?gat_id_and_generics);
231+
let scc = self.regioncx.constraint_sccs().scc(lower_bound);
232+
let placeholder: ty::PlaceholderRegion = self.regioncx.placeholder_representative(scc)?;
233+
let placeholder_id = placeholder.bound.kind.get_id()?.as_local()?;
234+
let gat_hir_id = self.infcx.tcx.local_def_id_to_hir_id(placeholder_id);
235+
let generics_impl =
236+
self.infcx.tcx.parent_hir_node(self.infcx.tcx.parent_hir_id(gat_hir_id)).generics()?;
248237

249238
// Look for the where-bound which introduces the placeholder.
250239
// As we're using the HIR, we need to handle both `for<'a> T: Trait<'a>`
251240
// and `T: for<'a> Trait`<'a>.
252241
let mut hrtb_bounds = vec![];
253-
gat_id_and_generics.iter().flatten().for_each(|&(gat_hir_id, generics)| {
254-
for pred in generics.predicates {
255-
let BoundPredicate(WhereBoundPredicate { bound_generic_params, bounds, .. }) =
256-
pred.kind
257-
else {
258-
continue;
259-
};
260-
if bound_generic_params
261-
.iter()
262-
.rfind(|bgp| tcx.local_def_id_to_hir_id(bgp.def_id) == gat_hir_id)
263-
.is_some()
264-
{
265-
for bound in *bounds {
266-
hrtb_bounds.push(bound);
267-
}
268-
} else {
269-
for bound in *bounds {
270-
if let Trait(trait_bound) = bound {
271-
if trait_bound
272-
.bound_generic_params
273-
.iter()
274-
.rfind(|bgp| tcx.local_def_id_to_hir_id(bgp.def_id) == gat_hir_id)
275-
.is_some()
276-
{
277-
hrtb_bounds.push(bound);
278-
return;
279-
}
280-
}
281-
}
242+
243+
for pred in generics_impl.predicates {
244+
let BoundPredicate(WhereBoundPredicate { bound_generic_params, bounds, .. }) =
245+
pred.kind
246+
else {
247+
continue;
248+
};
249+
if bound_generic_params
250+
.iter()
251+
.rfind(|bgp| self.infcx.tcx.local_def_id_to_hir_id(bgp.def_id) == gat_hir_id)
252+
.is_some()
253+
{
254+
for bound in *bounds {
255+
hrtb_bounds.push(bound);
282256
}
283257
}
284-
});
258+
}
285259
debug!(?hrtb_bounds);
286260

287261
let mut suggestions = vec![];
@@ -327,6 +301,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
327301
Applicability::MaybeIncorrect,
328302
);
329303
}
304+
Some(())
330305
}
331306

332307
/// Produces nice borrowck error diagnostics for all the errors collected in `nll_errors`.

compiler/rustc_borrowck/src/handle_placeholders.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use tracing::{debug, instrument, trace};
1414
use crate::constraints::{ConstraintSccIndex, OutlivesConstraintSet};
1515
use crate::consumers::OutlivesConstraint;
1616
use crate::diagnostics::{RegionErrorKind, RegionErrors, UniverseInfo};
17-
use crate::region_infer::values::{LivenessValues, PlaceholderIndices};
17+
use crate::region_infer::values::LivenessValues;
1818
use crate::region_infer::{ConstraintSccs, RegionDefinition, Representative, TypeTest};
1919
use crate::ty::VarianceDiagInfo;
2020
use crate::type_check::free_region_relations::UniversalRegionRelations;
@@ -32,7 +32,6 @@ pub(crate) struct LoweredConstraints<'tcx> {
3232
pub(crate) type_tests: Vec<TypeTest<'tcx>>,
3333
pub(crate) liveness_constraints: LivenessValues,
3434
pub(crate) universe_causes: FxIndexMap<UniverseIndex, UniverseInfo<'tcx>>,
35-
pub(crate) placeholder_indices: PlaceholderIndices,
3635
}
3736

3837
impl<'d, 'tcx, A: scc::Annotation> SccAnnotations<'d, 'tcx, A> {
@@ -62,7 +61,7 @@ impl scc::Annotations<RegionVid> for SccAnnotations<'_, '_, RegionTracker> {
6261
}
6362

6463
#[derive(Copy, Debug, Clone, PartialEq, Eq)]
65-
enum PlaceholderReachability {
64+
pub(crate) enum PlaceholderReachability {
6665
/// This SCC reaches no placeholders.
6766
NoPlaceholders,
6867
/// This SCC reaches at least one placeholder.
@@ -120,7 +119,7 @@ impl PlaceholderReachability {
120119
/// the values of its elements. This annotates a single SCC.
121120
#[derive(Copy, Debug, Clone)]
122121
pub(crate) struct RegionTracker {
123-
reachable_placeholders: PlaceholderReachability,
122+
pub(crate) reachable_placeholders: PlaceholderReachability,
124123

125124
/// The smallest max nameable universe of all
126125
/// regions reachable from this SCC.
@@ -245,6 +244,16 @@ impl RegionTracker {
245244
PlaceholderReachability::Placeholders { min_placeholder, .. } => Some(min_placeholder),
246245
}
247246
}
247+
248+
/// If this SCC reaches at least one placeholder, return
249+
/// its region vid. If there's more than one, return the one
250+
/// with the smallest vid.
251+
pub(crate) fn reached_placeholder(&self) -> Option<RegionVid> {
252+
match self.reachable_placeholders {
253+
PlaceholderReachability::NoPlaceholders => None,
254+
PlaceholderReachability::Placeholders { min_placeholder, .. } => Some(min_placeholder),
255+
}
256+
}
248257
}
249258

250259
impl scc::Annotation for RegionTracker {
@@ -350,8 +359,6 @@ pub(crate) fn compute_sccs_applying_placeholder_outlives_constraints<'tcx>(
350359
let (definitions, has_placeholders) = region_definitions(infcx, universal_regions);
351360

352361
let MirTypeckRegionConstraints {
353-
placeholder_indices,
354-
placeholder_index_to_region: _,
355362
liveness_constraints,
356363
mut outlives_constraints,
357364
universe_causes,
@@ -385,7 +392,6 @@ pub(crate) fn compute_sccs_applying_placeholder_outlives_constraints<'tcx>(
385392
outlives_constraints: Frozen::freeze(outlives_constraints),
386393
liveness_constraints,
387394
universe_causes,
388-
placeholder_indices,
389395
};
390396
}
391397
debug!("Placeholders present; activating placeholder handling logic!");
@@ -426,7 +432,6 @@ pub(crate) fn compute_sccs_applying_placeholder_outlives_constraints<'tcx>(
426432
type_tests,
427433
liveness_constraints,
428434
universe_causes,
429-
placeholder_indices,
430435
}
431436
}
432437

compiler/rustc_borrowck/src/lib.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ use crate::region_infer::opaque_types::DeferredOpaqueTypeError;
7474
use crate::renumber::RegionCtxt;
7575
use crate::session_diagnostics::VarNeedNotMut;
7676
use crate::type_check::free_region_relations::UniversalRegionRelations;
77-
use crate::type_check::{Locations, MirTypeckRegionConstraints, MirTypeckResults};
77+
use crate::type_check::{
78+
Locations, MirTypeckRegionConstraints, MirTypeckResults, PlaceholderToRegion,
79+
};
7880

7981
mod borrow_set;
8082
mod borrowck_errors;
@@ -303,6 +305,7 @@ struct CollectRegionConstraintsResult<'tcx> {
303305
deferred_opaque_type_errors: Vec<DeferredOpaqueTypeError<'tcx>>,
304306
polonius_facts: Option<AllFacts<RustcFacts>>,
305307
polonius_context: Option<PoloniusContext>,
308+
placeholder_to_region: PlaceholderToRegion<'tcx>,
306309
}
307310

308311
/// Start borrow checking by collecting the region constraints for
@@ -353,6 +356,7 @@ fn borrowck_collect_region_constraints<'tcx>(
353356
known_type_outlives_obligations,
354357
deferred_closure_requirements,
355358
polonius_context,
359+
placeholder_to_region,
356360
} = type_check::type_check(
357361
root_cx,
358362
&infcx,
@@ -382,6 +386,7 @@ fn borrowck_collect_region_constraints<'tcx>(
382386
deferred_opaque_type_errors: Default::default(),
383387
polonius_facts,
384388
polonius_context,
389+
placeholder_to_region,
385390
}
386391
}
387392

@@ -406,6 +411,7 @@ fn borrowck_check_region_constraints<'tcx>(
406411
deferred_opaque_type_errors,
407412
polonius_facts,
408413
polonius_context,
414+
placeholder_to_region: _,
409415
}: CollectRegionConstraintsResult<'tcx>,
410416
) -> PropagatedBorrowCheckResults<'tcx> {
411417
assert!(!infcx.has_opaque_types_in_storage());

0 commit comments

Comments
 (0)