Skip to content

Commit

Permalink
point out unblamed constraints from Copy/Sized bounds in region e…
Browse files Browse the repository at this point in the history
…rrors
  • Loading branch information
dianne committed Jan 7, 2025
1 parent 2c5815b commit 1b2281a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
22 changes: 22 additions & 0 deletions compiler/rustc_borrowck/src/diagnostics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_borrowck/src/diagnostics/region_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/impl-trait/multiple-lifetimes/error-handling.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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`
|
Expand Down
5 changes: 4 additions & 1 deletion tests/ui/lifetimes/copy_modulo_regions.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down

0 comments on commit 1b2281a

Please sign in to comment.