Skip to content

Commit

Permalink
further clean up best_blame_constraint
Browse files Browse the repository at this point in the history
This gets rid of `categorized_path`, as it was redundant given the `OutlivesConstraint`s in `path`
already have a category field.
  • Loading branch information
dianne committed Jan 6, 2025
1 parent aa8d5bf commit 2249232
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 50 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_borrowck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#![feature(assert_matches)]
#![feature(box_patterns)]
#![feature(file_buffered)]
#![feature(if_let_guard)]
#![feature(let_chains)]
#![feature(never_type)]
#![feature(rustc_attrs)]
Expand Down
95 changes: 45 additions & 50 deletions compiler/rustc_borrowck/src/region_infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1983,18 +1983,6 @@ impl<'tcx> RegionInferenceContext<'tcx> {
})
.unwrap_or_else(|| ObligationCauseCode::Misc);

// Classify each of the constraints along the path.
let mut categorized_path: Vec<BlameConstraint<'tcx>> = path
.iter()
.map(|constraint| BlameConstraint {
category: constraint.category,
from_closure: constraint.from_closure,
cause: ObligationCause::new(constraint.span, CRATE_DEF_ID, cause_code.clone()),
variance_info: constraint.variance_info,
})
.collect();
debug!("categorized_path={:#?}", categorized_path);

// To find the best span to cite, we first try to look for the
// final constraint that is interesting and where the `sup` is
// not unified with the ultimate target region. The reason
Expand All @@ -2015,7 +2003,6 @@ impl<'tcx> RegionInferenceContext<'tcx> {
// we still want to screen for an "interesting" point to
// highlight (e.g., a call site or something).
let target_scc = self.constraint_sccs.scc(target_region);
let mut range = 0..path.len();

// As noted above, when reporting an error, there is typically a chain of constraints
// leading from some "source" region which must outlive some "target" region.
Expand Down Expand Up @@ -2059,13 +2046,11 @@ impl<'tcx> RegionInferenceContext<'tcx> {
| NllRegionVariableOrigin::Existential { from_forall: true } => false,
};

let find_region = |i: &usize| {
let constraint = &path[*i];

let interesting_to_blame = |constraint: &OutlivesConstraint<'tcx>| {
let constraint_sup_scc = self.constraint_sccs.scc(constraint.sup);

if blame_source {
match categorized_path[*i].category {
match constraint.category {
ConstraintCategory::OpaqueType
| ConstraintCategory::Boring
| ConstraintCategory::BoringNoLocation
Expand All @@ -2078,7 +2063,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
}
} else {
!matches!(
categorized_path[*i].category,
constraint.category,
ConstraintCategory::OpaqueType
| ConstraintCategory::Boring
| ConstraintCategory::BoringNoLocation
Expand All @@ -2088,49 +2073,59 @@ impl<'tcx> RegionInferenceContext<'tcx> {
}
};

let best_choice =
if blame_source { range.rev().find(find_region) } else { range.find(find_region) };
let best_choice = if blame_source {
path.iter().rposition(interesting_to_blame)
} else {
path.iter().position(interesting_to_blame)
};

debug!(?best_choice, ?blame_source);

if let Some(i) = best_choice {
if let Some(next) = categorized_path.get(i + 1) {
if matches!(categorized_path[i].category, ConstraintCategory::Return(_))
&& next.category == ConstraintCategory::OpaqueType
{
// The return expression is being influenced by the return type being
// impl Trait, point at the return type and not the return expr.
return (next.clone(), path);
}
let best_constraint = match best_choice {
Some(i)
if let Some(next) = path.get(i + 1)
&& matches!(path[i].category, ConstraintCategory::Return(_))
&& next.category == ConstraintCategory::OpaqueType =>
{
// The return expression is being influenced by the return type being
// impl Trait, point at the return type and not the return expr.
*next
}

if categorized_path[i].category == ConstraintCategory::Return(ReturnConstraint::Normal)
Some(i)
if path[i].category == ConstraintCategory::Return(ReturnConstraint::Normal)
&& let Some(field) = path.iter().find_map(|p| {
if let ConstraintCategory::ClosureUpvar(f) = p.category {
Some(f)
} else {
None
}
}) =>
{
let field = categorized_path.iter().find_map(|p| {
if let ConstraintCategory::ClosureUpvar(f) = p.category {
Some(f)
} else {
None
}
});

if let Some(field) = field {
categorized_path[i].category =
ConstraintCategory::Return(ReturnConstraint::ClosureUpvar(field));
OutlivesConstraint {
category: ConstraintCategory::Return(ReturnConstraint::ClosureUpvar(field)),
..path[i]
}
}

return (categorized_path[i].clone(), path);
}
Some(i) => path[i],

// If that search fails, that is.. unusual. Maybe everything
// is in the same SCC or something. In that case, find what
// appears to be the most interesting point to report to the
// user via an even more ad-hoc guess.
categorized_path.sort_by_key(|p| p.category);
debug!("sorted_path={:#?}", categorized_path);
None => {
// If that search fails, that is.. unusual. Maybe everything
// is in the same SCC or something. In that case, find what
// appears to be the most interesting point to report to the
// user via an even more ad-hoc guess.
*path.iter().min_by_key(|p| p.category).unwrap()
}
};

(categorized_path.remove(0), path)
let blame_constraint = BlameConstraint {
category: best_constraint.category,
from_closure: best_constraint.from_closure,
cause: ObligationCause::new(best_constraint.span, CRATE_DEF_ID, cause_code.clone()),
variance_info: best_constraint.variance_info,
};
(blame_constraint, path)
}

pub(crate) fn universe_info(&self, universe: ty::UniverseIndex) -> UniverseInfo<'tcx> {
Expand Down

0 comments on commit 2249232

Please sign in to comment.