Skip to content

Commit 3375984

Browse files
committed
Only flag the error for placeholder-existential failures if there is
actually an existential. Closes: #146467
1 parent 0a281ca commit 3375984

File tree

3 files changed

+24
-19
lines changed

3 files changed

+24
-19
lines changed

compiler/rustc_borrowck/src/handle_placeholders.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::ty::VarianceDiagInfo;
2020
use crate::type_check::free_region_relations::UniversalRegionRelations;
2121
use crate::type_check::{Locations, MirTypeckRegionConstraints};
2222
use crate::universal_regions::UniversalRegions;
23-
use crate::{BorrowckInferCtxt, NllRegionVariableOrigin, bug};
23+
use crate::{BorrowckInferCtxt, NllRegionVariableOrigin};
2424

2525
/// A set of outlives constraints after rewriting to remove
2626
/// higher-kinded constraints.
@@ -188,13 +188,23 @@ impl RegionTracker {
188188
/// the SCC itself.
189189
///
190190
/// Returns *a* culprit (there may be more than one).
191-
fn reaches_existential_that_cannot_name_us(&self) -> Option<RegionVid> {
191+
fn reaches_existential_that_cannot_name_us(
192+
&self,
193+
region_definitions: &IndexVec<RegionVid, RegionDefinition<'_>>,
194+
) -> Option<RegionVid> {
192195
let Representative::Placeholder(_p) = self.representative else {
193196
return None;
194197
};
195198

196199
let (reachable_lowest_max_u, reachable_lowest_max_u_rvid) = self.max_nameable_universe;
197200

201+
let NllRegionVariableOrigin::Existential { .. } =
202+
region_definitions[reachable_lowest_max_u_rvid].origin
203+
else {
204+
debug!("SCC universe wasn't lowered by an existential; skipping.");
205+
return None;
206+
};
207+
198208
(!self.reachable_placeholders.can_be_named_by(reachable_lowest_max_u))
199209
.then_some(reachable_lowest_max_u_rvid)
200210
}
@@ -442,19 +452,7 @@ fn rewrite_placeholder_outlives<'tcx>(
442452
variance_info: VarianceDiagInfo::None,
443453
from_closure: false,
444454
});
445-
} else if !(annotation.reaches_existential_that_cannot_name_us().is_some()
446-
|| annotation.reaches_other_placeholder(annotation.representative.rvid()).is_some())
447-
{
448-
// The SCC's representative is not nameable from some region
449-
// that ends up in the SCC. This means there is nothing for us to do.
450-
// However, this is only possible under circumstances that produce
451-
// errors, so we make sure that we catch them here. Otherwise,
452-
// there might actually be soundness issues!
453-
bug!(
454-
"Universe of SCC {scc:?} should have been lowered by an existential or at least another placeholder but was lowered by {:?}, which is neither.",
455-
annotation.max_nameable_universe
456-
);
457-
};
455+
}
458456
}
459457
added_constraints
460458
}
@@ -486,12 +484,14 @@ fn find_placeholder_mismatch_errors<'tcx>(
486484
let scc = sccs.scc(rvid);
487485
let annotation = annotations.scc_to_annotation[scc];
488486

489-
if let Some(existential_that_cannot_name_rvid) =
490-
annotation.reaches_existential_that_cannot_name_us()
487+
if let Some(cannot_name_rvid) =
488+
annotation.reaches_existential_that_cannot_name_us(definitions)
491489
{
490+
debug!("Existential {cannot_name_rvid:?} lowered our universe...");
491+
492492
errors_buffer.push(RegionErrorKind::PlaceholderOutlivesExistentialThatCannotNameIt {
493493
longer_fr: rvid,
494-
existential_that_cannot_name_longer: existential_that_cannot_name_rvid,
494+
existential_that_cannot_name_longer: cannot_name_rvid,
495495
placeholder: origin_a,
496496
})
497497
}

compiler/rustc_borrowck/src/nll.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ pub(crate) fn compute_regions<'tcx>(
136136
&mut placeholder_errors,
137137
);
138138

139+
debug!("Placeholder errors: {placeholder_errors:?}");
140+
139141
// If requested, emit legacy polonius facts.
140142
polonius::legacy::emit_facts(
141143
&mut polonius_facts,
@@ -188,6 +190,7 @@ pub(crate) fn compute_regions<'tcx>(
188190

189191
let nll_errors = if region_inference_errors.is_empty() {
190192
// Only flag the higher-kinded bounds errors if there are no borrowck errors.
193+
debug!("No region inference errors, using placeholder errors: {placeholder_errors:?}");
191194
placeholder_errors
192195
} else {
193196
debug!("Errors already reported, skipping these: {placeholder_errors:?}");

compiler/rustc_borrowck/src/region_infer/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1566,7 +1566,9 @@ impl<'tcx> RegionInferenceContext<'tcx> {
15661566
from_region_origin: NllRegionVariableOrigin,
15671567
to_region: RegionVid,
15681568
) -> (BlameConstraint<'tcx>, Vec<OutlivesConstraint<'tcx>>) {
1569-
assert!(from_region != to_region, "Trying to blame a region for itself!");
1569+
if from_region == to_region {
1570+
bug!("Trying to blame {from_region:?} for itself!");
1571+
}
15701572

15711573
let path = self.constraint_path_between_regions(from_region, to_region).unwrap();
15721574

0 commit comments

Comments
 (0)