Skip to content

Commit 05d58ab

Browse files
committed
Remove precise placeholder tracking from region inference
1 parent 6f76769 commit 05d58ab

File tree

7 files changed

+124
-323
lines changed

7 files changed

+124
-323
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: 14 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,12 +359,11 @@ 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,
358365
type_tests,
366+
placeholder_to_region: _
359367
} = constraints;
360368

361369
let fr_static = universal_regions.fr_static;
@@ -385,7 +393,6 @@ pub(crate) fn compute_sccs_applying_placeholder_outlives_constraints<'tcx>(
385393
outlives_constraints: Frozen::freeze(outlives_constraints),
386394
liveness_constraints,
387395
universe_causes,
388-
placeholder_indices,
389396
};
390397
}
391398
debug!("Placeholders present; activating placeholder handling logic!");
@@ -426,7 +433,6 @@ pub(crate) fn compute_sccs_applying_placeholder_outlives_constraints<'tcx>(
426433
type_tests,
427434
liveness_constraints,
428435
universe_causes,
429-
placeholder_indices,
430436
}
431437
}
432438

0 commit comments

Comments
 (0)