1
Fork 0

best_blame_constraint: avoid blaming assignments without user-provided types

This commit is contained in:
dianne 2024-12-19 01:16:53 -08:00
parent 31e4d8175a
commit 50222dba2e
19 changed files with 88 additions and 64 deletions

View file

@ -2911,7 +2911,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
(
name,
BorrowExplanation::MustBeValidFor {
category: ConstraintCategory::Assignment,
category: ConstraintCategory::Assignment { .. },
from_closure: false,
region_name:
RegionName {

View file

@ -41,7 +41,7 @@ impl<'tcx> ConstraintDescription for ConstraintCategory<'tcx> {
fn description(&self) -> &'static str {
// Must end with a space. Allows for empty names to be provided.
match self {
ConstraintCategory::Assignment => "assignment ",
ConstraintCategory::Assignment { .. } => "assignment ",
ConstraintCategory::Return(_) => "returning this value ",
ConstraintCategory::Yield => "yielding this value ",
ConstraintCategory::UseAsConst => "using this value as a constant ",
@ -481,7 +481,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
(ConstraintCategory::Return(kind), true, false) if self.is_closure_fn_mut(fr) => {
self.report_fnmut_error(&errci, kind)
}
(ConstraintCategory::Assignment, true, false)
(ConstraintCategory::Assignment { .. }, true, false)
| (ConstraintCategory::CallArgument(_), true, false) => {
let mut db = self.report_escaping_data_error(&errci);
@ -672,7 +672,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
// Revert to the normal error in these cases.
// Assignments aren't "escapes" in function items.
if (fr_name_and_span.is_none() && outlived_fr_name_and_span.is_none())
|| (*category == ConstraintCategory::Assignment
|| (matches!(category, ConstraintCategory::Assignment { .. })
&& self.regioncx.universal_regions().defining_ty.is_fn_def())
|| self.regioncx.universal_regions().defining_ty.is_const()
{

View file

@ -2034,6 +2034,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
| ConstraintCategory::BoringNoLocation
| ConstraintCategory::Internal
| ConstraintCategory::Predicate(_)
| ConstraintCategory::Assignment { has_interesting_ty: false }
) && constraint.span.desugaring_kind().is_none_or(|kind| {
// Try to avoid blaming constraints from desugarings, since they may not clearly
// clearly match what users have written. As an exception, allow blaming returns

View file

@ -892,7 +892,18 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
Some(l) if !body.local_decls[l].is_user_variable() => {
ConstraintCategory::Boring
}
_ => ConstraintCategory::Assignment,
Some(l) => ConstraintCategory::Assignment {
has_interesting_ty: body.local_decls[l].user_ty.is_some()
|| matches!(
body.local_decls[l].local_info(),
LocalInfo::User(BindingForm::Var(VarBindingForm {
opt_ty_info: Some(_),
..
}))
),
},
// Assignments to projections should be considered interesting.
_ => ConstraintCategory::Assignment { has_interesting_ty: true },
};
debug!(
"assignment category: {:?} {:?}",
@ -1226,7 +1237,8 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
Some(l) if !body.local_decls[l].is_user_variable() => {
ConstraintCategory::Boring
}
_ => ConstraintCategory::Assignment,
// The return type of a call is interesting for diagnostics.
_ => ConstraintCategory::Assignment { has_interesting_ty: true },
};
let locations = term_location.to_locations();