1
Fork 0

point out unblamed constraints from Copy/Sized bounds in region errors

This commit is contained in:
dianne 2024-12-20 21:14:16 -08:00
parent 2c5815b285
commit 1b2281a493
5 changed files with 31 additions and 1 deletions

View file

@ -331,6 +331,7 @@ impl<'tcx> BorrowExplanation<'tcx> {
};
cx.add_placeholder_from_predicate_note(err, &path);
cx.add_sized_or_copy_bound_info(err, category, &path);
if let ConstraintCategory::Cast {
is_implicit_coercion: true,

View file

@ -37,6 +37,7 @@ use super::MirBorrowckCtxt;
use super::borrow_set::BorrowData;
use crate::constraints::OutlivesConstraint;
use crate::fluent_generated as fluent;
use crate::nll::ConstraintDescription;
use crate::session_diagnostics::{
CaptureArgLabel, CaptureReasonLabel, CaptureReasonNote, CaptureReasonSuggest, CaptureVarCause,
CaptureVarKind, CaptureVarPathUseCause, OnClosureNote,
@ -647,6 +648,27 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
err.span_note(span, "due to current limitations in the borrow checker, this implies a `'static` lifetime");
}
}
/// Add a label to region errors and borrow explanations when outlives constraints arise from
/// proving a type implements `Sized` or `Copy`.
fn add_sized_or_copy_bound_info(
&self,
err: &mut Diag<'_>,
blamed_category: ConstraintCategory<'tcx>,
path: &[OutlivesConstraint<'tcx>],
) {
for sought_category in [ConstraintCategory::SizedBound, ConstraintCategory::CopyBound] {
if sought_category != blamed_category
&& let Some(sought_constraint) = path.iter().find(|c| c.category == sought_category)
{
let label = format!(
"requirement occurs due to {}",
sought_category.description().trim_end()
);
err.span_label(sought_constraint.span, label);
}
}
}
}
/// The span(s) associated to a use of a place.

View file

@ -554,6 +554,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
}
self.add_placeholder_from_predicate_note(&mut diag, &path);
self.add_sized_or_copy_bound_info(&mut diag, category, &path);
self.buffer_error(diag);
}