Skip to content

Commit d8528c6

Browse files
committed
Some drive-by housecleaning in rustc_borrowck
This commit picks up a few odd ends discovered during the work on #130227. It adds some documentation and renames a few methods with too generic names to describe what they actually do. It also adds some debug output that was helpful during bug hunting.
1 parent c4b38a5 commit d8528c6

File tree

4 files changed

+39
-45
lines changed

4 files changed

+39
-45
lines changed

compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs

+18-21
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl<'tcx> UniverseInfo<'tcx> {
4949
UniverseInfo::RelateTys { expected, found }
5050
}
5151

52-
pub(crate) fn report_error(
52+
pub(crate) fn report_erroneous_element(
5353
&self,
5454
mbcx: &mut MirBorrowckCtxt<'_, '_, 'tcx>,
5555
placeholder: ty::PlaceholderRegion,
@@ -68,7 +68,7 @@ impl<'tcx> UniverseInfo<'tcx> {
6868
mbcx.buffer_error(err);
6969
}
7070
UniverseInfo::TypeOp(ref type_op_info) => {
71-
type_op_info.report_error(mbcx, placeholder, error_element, cause);
71+
type_op_info.report_erroneous_element(mbcx, placeholder, error_element, cause);
7272
}
7373
UniverseInfo::Other => {
7474
// FIXME: This error message isn't great, but it doesn't show
@@ -145,8 +145,11 @@ pub(crate) trait TypeOpInfo<'tcx> {
145145
error_region: Option<ty::Region<'tcx>>,
146146
) -> Option<Diag<'infcx>>;
147147

148+
/// Constraints require that `error_element` appear in the
149+
/// values of `placeholder`, but this cannot be proven to
150+
/// hold. Report an error.
148151
#[instrument(level = "debug", skip(self, mbcx))]
149-
fn report_error(
152+
fn report_erroneous_element(
150153
&self,
151154
mbcx: &mut MirBorrowckCtxt<'_, '_, 'tcx>,
152155
placeholder: ty::PlaceholderRegion,
@@ -190,12 +193,7 @@ pub(crate) trait TypeOpInfo<'tcx> {
190193
let nice_error = self.nice_error(mbcx, cause, placeholder_region, error_region);
191194

192195
debug!(?nice_error);
193-
194-
if let Some(nice_error) = nice_error {
195-
mbcx.buffer_error(nice_error);
196-
} else {
197-
mbcx.buffer_error(self.fallback_error(tcx, span));
198-
}
196+
mbcx.buffer_error(nice_error.unwrap_or_else(|| self.fallback_error(tcx, span)));
199197
}
200198
}
201199

@@ -450,7 +448,8 @@ fn try_extract_error_from_region_constraints<'a, 'tcx>(
450448
ty::ReVar(vid) => universe_of_region(vid),
451449
_ => ty::UniverseIndex::ROOT,
452450
};
453-
let matches =
451+
// Are the two regions the same?
452+
let regions_the_same =
454453
|a_region: Region<'tcx>, b_region: Region<'tcx>| match (a_region.kind(), b_region.kind()) {
455454
(RePlaceholder(a_p), RePlaceholder(b_p)) => a_p.bound == b_p.bound,
456455
_ => a_region == b_region,
@@ -459,7 +458,7 @@ fn try_extract_error_from_region_constraints<'a, 'tcx>(
459458
|constraint: &Constraint<'tcx>, cause: &SubregionOrigin<'tcx>, exact| match *constraint {
460459
Constraint::RegSubReg(sub, sup)
461460
if ((exact && sup == placeholder_region)
462-
|| (!exact && matches(sup, placeholder_region)))
461+
|| (!exact && regions_the_same(sup, placeholder_region)))
463462
&& sup != sub =>
464463
{
465464
Some((sub, cause.clone()))
@@ -468,23 +467,21 @@ fn try_extract_error_from_region_constraints<'a, 'tcx>(
468467
if (exact
469468
&& sup == placeholder_region
470469
&& !universe_of_region(vid).can_name(placeholder_universe))
471-
|| (!exact && matches(sup, placeholder_region)) =>
470+
|| (!exact && regions_the_same(sup, placeholder_region)) =>
472471
{
473472
Some((ty::Region::new_var(infcx.tcx, vid), cause.clone()))
474473
}
475474
_ => None,
476475
};
477-
let mut info = region_constraints
478-
.constraints
479-
.iter()
480-
.find_map(|(constraint, cause)| check(constraint, cause, true));
481-
if info.is_none() {
482-
info = region_constraints
476+
477+
let mut find_culprit = |exact_match: bool| {
478+
region_constraints
483479
.constraints
484480
.iter()
485-
.find_map(|(constraint, cause)| check(constraint, cause, false));
486-
}
487-
let (sub_region, cause) = info?;
481+
.find_map(|(constraint, cause)| check(constraint, cause, exact_match))
482+
};
483+
484+
let (sub_region, cause) = find_culprit(true).or_else(|| find_culprit(false))?;
488485

489486
debug!(?sub_region, "cause = {:#?}", cause);
490487
let error = match (error_region, *sub_region) {

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
404404
let universe = placeholder.universe;
405405
let universe_info = self.regioncx.universe_info(universe);
406406

407-
universe_info.report_error(self, placeholder, error_element, cause);
407+
universe_info.report_erroneous_element(self, placeholder, error_element, cause);
408408
}
409409

410410
RegionErrorKind::RegionError { fr_origin, longer_fr, shorter_fr, is_reported } => {

compiler/rustc_borrowck/src/region_infer/mod.rs

+19-22
Original file line numberDiff line numberDiff line change
@@ -1623,30 +1623,23 @@ impl<'tcx> RegionInferenceContext<'tcx> {
16231623
let longer_fr_scc = self.constraint_sccs.scc(longer_fr);
16241624
debug!("check_bound_universal_region: longer_fr_scc={:?}", longer_fr_scc,);
16251625

1626-
for error_element in self.scc_values.elements_contained_in(longer_fr_scc) {
1627-
match error_element {
1628-
RegionElement::Location(_) | RegionElement::RootUniversalRegion(_) => {}
1629-
// If we have some bound universal region `'a`, then the only
1630-
// elements it can contain is itself -- we don't know anything
1631-
// else about it!
1632-
RegionElement::PlaceholderRegion(placeholder1) => {
1633-
if placeholder == placeholder1 {
1634-
continue;
1635-
}
1636-
}
1637-
}
1638-
1626+
// If we have some bound universal region `'a`, then the only
1627+
// elements it can contain is itself -- we don't know anything
1628+
// else about it!
1629+
if let Some(error_element) = self
1630+
.scc_values
1631+
.elements_contained_in(longer_fr_scc)
1632+
.find(|e| *e != RegionElement::PlaceholderRegion(placeholder))
1633+
{
1634+
// Stop after the first error, it gets too noisy otherwise, and does not provide more information.
16391635
errors_buffer.push(RegionErrorKind::BoundUniversalRegionError {
16401636
longer_fr,
16411637
error_element,
16421638
placeholder,
16431639
});
1644-
1645-
// Stop after the first error, it gets too noisy otherwise, and does not provide more
1646-
// information.
1647-
break;
1640+
} else {
1641+
debug!("check_bound_universal_region: all bounds satisfied");
16481642
}
1649-
debug!("check_bound_universal_region: all bounds satisfied");
16501643
}
16511644

16521645
#[instrument(level = "debug", skip(self, infcx, errors_buffer))]
@@ -2066,7 +2059,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
20662059
constraint.category
20672060
};
20682061

2069-
match category {
2062+
let interest = match category {
20702063
// Returns usually provide a type to blame and have specially written diagnostics,
20712064
// so prioritize them.
20722065
ConstraintCategory::Return(_) => 0,
@@ -2118,9 +2111,13 @@ impl<'tcx> RegionInferenceContext<'tcx> {
21182111
// specific, and are not used for relations that would make sense to blame.
21192112
ConstraintCategory::BoringNoLocation => 6,
21202113
// Do not blame internal constraints.
2121-
ConstraintCategory::Internal => 7,
2122-
ConstraintCategory::IllegalUniverse => 8,
2123-
}
2114+
ConstraintCategory::IllegalUniverse => 7,
2115+
ConstraintCategory::Internal => 8,
2116+
};
2117+
2118+
debug!("constraint {constraint:?} category: {category:?}, interest: {interest:?}");
2119+
2120+
interest
21242121
};
21252122

21262123
let best_choice = if blame_source {

compiler/rustc_borrowck/src/region_infer/values.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ rustc_index::newtype_index! {
2121

2222
/// An individual element in a region value -- the value of a
2323
/// particular region variable consists of a set of these elements.
24-
#[derive(Debug, Clone)]
24+
#[derive(Debug, Clone, PartialEq)]
2525
pub(crate) enum RegionElement {
2626
/// A point in the control-flow graph.
2727
Location(Location),

0 commit comments

Comments
 (0)