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);
}

View File

@ -6,6 +6,9 @@ LL | fn foo<'a, 'b, 'c>(x: &'static i32, mut y: &'a i32) -> E<'b, 'c> {
| |
| lifetime `'a` defined here
...
LL | let u = v;
| - requirement occurs due to copying this value
...
LL | let _: &'b i32 = *u.0;
| ^^^^^^^ type annotation requires that `'a` must outlive `'b`
|

View File

@ -4,7 +4,10 @@ error: lifetime may not live long enough
LL | fn foo<'a>() -> [Foo<'a>; 100] {
| -- lifetime `'a` defined here
LL | [mk_foo::<'a>(); 100]
| ^^^^^^^^^^^^^^^^^^^^^ returning this value requires that `'a` must outlive `'static`
| ^^^^^^^^^^^^^^^^^^^^^
| |
| returning this value requires that `'a` must outlive `'static`
| requirement occurs due to copying this value
|
= note: requirement occurs because of the type `Foo<'_>`, which makes the generic argument `'_` invariant
= note: the struct `Foo<'a>` is invariant over the parameter `'a`