Skip to content

Commit 1b2281a

Browse files
committed
point out unblamed constraints from Copy/Sized bounds in region errors
1 parent 2c5815b commit 1b2281a

File tree

5 files changed

+31
-1
lines changed

5 files changed

+31
-1
lines changed

Diff for: compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

+1
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ impl<'tcx> BorrowExplanation<'tcx> {
331331
};
332332

333333
cx.add_placeholder_from_predicate_note(err, &path);
334+
cx.add_sized_or_copy_bound_info(err, category, &path);
334335

335336
if let ConstraintCategory::Cast {
336337
is_implicit_coercion: true,

Diff for: compiler/rustc_borrowck/src/diagnostics/mod.rs

+22
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use super::MirBorrowckCtxt;
3737
use super::borrow_set::BorrowData;
3838
use crate::constraints::OutlivesConstraint;
3939
use crate::fluent_generated as fluent;
40+
use crate::nll::ConstraintDescription;
4041
use crate::session_diagnostics::{
4142
CaptureArgLabel, CaptureReasonLabel, CaptureReasonNote, CaptureReasonSuggest, CaptureVarCause,
4243
CaptureVarKind, CaptureVarPathUseCause, OnClosureNote,
@@ -647,6 +648,27 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
647648
err.span_note(span, "due to current limitations in the borrow checker, this implies a `'static` lifetime");
648649
}
649650
}
651+
652+
/// Add a label to region errors and borrow explanations when outlives constraints arise from
653+
/// proving a type implements `Sized` or `Copy`.
654+
fn add_sized_or_copy_bound_info(
655+
&self,
656+
err: &mut Diag<'_>,
657+
blamed_category: ConstraintCategory<'tcx>,
658+
path: &[OutlivesConstraint<'tcx>],
659+
) {
660+
for sought_category in [ConstraintCategory::SizedBound, ConstraintCategory::CopyBound] {
661+
if sought_category != blamed_category
662+
&& let Some(sought_constraint) = path.iter().find(|c| c.category == sought_category)
663+
{
664+
let label = format!(
665+
"requirement occurs due to {}",
666+
sought_category.description().trim_end()
667+
);
668+
err.span_label(sought_constraint.span, label);
669+
}
670+
}
671+
}
650672
}
651673

652674
/// The span(s) associated to a use of a place.

Diff for: compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+1
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
554554
}
555555

556556
self.add_placeholder_from_predicate_note(&mut diag, &path);
557+
self.add_sized_or_copy_bound_info(&mut diag, category, &path);
557558

558559
self.buffer_error(diag);
559560
}

Diff for: tests/ui/impl-trait/multiple-lifetimes/error-handling.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ LL | fn foo<'a, 'b, 'c>(x: &'static i32, mut y: &'a i32) -> E<'b, 'c> {
66
| |
77
| lifetime `'a` defined here
88
...
9+
LL | let u = v;
10+
| - requirement occurs due to copying this value
11+
...
912
LL | let _: &'b i32 = *u.0;
1013
| ^^^^^^^ type annotation requires that `'a` must outlive `'b`
1114
|

Diff for: tests/ui/lifetimes/copy_modulo_regions.stderr

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

0 commit comments

Comments
 (0)