Point at lifetime requirement origin in more cases

This commit is contained in:
Esteban Küber 2025-08-29 20:14:01 +00:00
parent c3e0b29e79
commit 7a0319f01d
21 changed files with 93 additions and 51 deletions

View file

@ -480,7 +480,7 @@ impl<'infcx, 'tcx> crate::MirBorrowckCtxt<'_, 'infcx, 'tcx> {
}
pub(crate) fn temporary_value_borrowed_for_too_long(&self, span: Span) -> Diag<'infcx> {
struct_span_code_err!(self.dcx(), span, E0716, "temporary value dropped while borrowed",)
struct_span_code_err!(self.dcx(), span, E0716, "temporary value dropped while borrowed")
}
}

View file

@ -2992,6 +2992,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
self.buffer_error(err);
}
#[tracing::instrument(level = "debug", skip(self, explanation))]
fn report_local_value_does_not_live_long_enough(
&self,
location: Location,
@ -3001,13 +3002,6 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
borrow_spans: UseSpans<'tcx>,
explanation: BorrowExplanation<'tcx>,
) -> Diag<'infcx> {
debug!(
"report_local_value_does_not_live_long_enough(\
{:?}, {:?}, {:?}, {:?}, {:?}\
)",
location, name, borrow, drop_span, borrow_spans
);
let borrow_span = borrow_spans.var_or_use_path_span();
if let BorrowExplanation::MustBeValidFor {
category,
@ -3031,15 +3025,6 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
let mut err = self.path_does_not_live_long_enough(borrow_span, &name);
if let BorrowExplanation::MustBeValidFor { ref path, region_name, .. } = explanation {
for constraint in path {
if let ConstraintCategory::Predicate(pred) = constraint.category
&& !pred.is_dummy()
{
err.span_note(pred, format!("requirement that {name} is borrowed for `{region_name}` introduced here"));
}
}
}
if let Some(annotation) = self.annotate_argument_and_return_for_borrow(borrow) {
let region_name = annotation.emit(self, &mut err);

View file

@ -416,6 +416,19 @@ impl<'tcx> BorrowExplanation<'tcx> {
{
self.add_object_lifetime_default_note(tcx, err, unsize_ty);
}
for constraint in path {
if let ConstraintCategory::Predicate(pred) = constraint.category
&& !pred.is_dummy()
{
err.span_note(
pred,
format!("requirement for `{region_name}` introduced here"),
);
break;
}
}
self.add_lifetime_bound_suggestion_to_diagnostic(err, &category, span, region_name);
}
_ => {}

View file

@ -29,7 +29,7 @@ LL | outlives::<'a>(call_once(c));
LL | }
| - `c` dropped here while still borrowed
|
note: requirement that `c` is borrowed for `'a` introduced here
note: requirement for `'a` introduced here
--> $DIR/without-precise-captures-we-are-powerless.rs:7:33
|
LL | fn outlives<'a>(_: impl Sized + 'a) {}
@ -80,7 +80,7 @@ LL | outlives::<'a>(call_once(c));
LL | }
| - `c` dropped here while still borrowed
|
note: requirement that `c` is borrowed for `'a` introduced here
note: requirement for `'a` introduced here
--> $DIR/without-precise-captures-we-are-powerless.rs:7:33
|
LL | fn outlives<'a>(_: impl Sized + 'a) {}
@ -101,6 +101,12 @@ LL | outlives::<'a>(c());
| argument requires that `c` is borrowed for `'a`
LL | outlives::<'a>(call_once(c));
| ^ move out of `c` occurs here
|
note: requirement for `'a` introduced here
--> $DIR/without-precise-captures-we-are-powerless.rs:7:33
|
LL | fn outlives<'a>(_: impl Sized + 'a) {}
| ^^
error[E0597]: `x` does not live long enough
--> $DIR/without-precise-captures-we-are-powerless.rs:36:13
@ -142,7 +148,7 @@ LL | outlives::<'a>(call_once(c));
LL | }
| - `c` dropped here while still borrowed
|
note: requirement that `c` is borrowed for `'a` introduced here
note: requirement for `'a` introduced here
--> $DIR/without-precise-captures-we-are-powerless.rs:7:33
|
LL | fn outlives<'a>(_: impl Sized + 'a) {}
@ -190,7 +196,7 @@ LL | T.outlives::<'a>(call_once(c));
LL | }
| - `c` dropped here while still borrowed
|
note: requirement that `c` is borrowed for `'a` introduced here
note: requirement for `'a` introduced here
--> $DIR/without-precise-captures-we-are-powerless.rs:49:47
|
LL | fn outlives<'a>(&'a self, _: impl Sized + 'a) {}

View file

@ -27,6 +27,12 @@ LL | want(&String::new(), extend_lt);
| | |
| | creates a temporary value which is freed while still in use
| argument requires that borrow lasts for `'static`
|
note: requirement for `'static` introduced here
--> $DIR/fn-item-check-type-params.rs:47:33
|
LL | fn want<I, O>(_: I, _: impl Fn(I) -> O) {}
| ^^^^^^^^^^
error[E0716]: temporary value dropped while borrowed
--> $DIR/fn-item-check-type-params.rs:54:26
@ -36,6 +42,12 @@ LL | let val = extend_lt(&String::from("blah blah blah"));
| | |
| | creates a temporary value which is freed while still in use
| argument requires that borrow lasts for `'static`
|
note: requirement for `'static` introduced here
--> $DIR/fn-item-check-type-params.rs:22:21
|
LL | (T, Option<U>): Displayable,
| ^^^^^^^^^^^
error: aborting due to 4 previous errors

View file

@ -23,7 +23,7 @@ LL | force_send(async_load(&not_static));
LL | }
| - `not_static` dropped here while still borrowed
|
note: requirement that `not_static` is borrowed for `'1` introduced here
note: requirement for `'1` introduced here
--> $DIR/implementation-not-general-enough-ice-133252.rs:16:18
|
LL | fn force_send<T: Send>(_: T) {}

View file

@ -10,6 +10,9 @@ LL | | ));
| | -- temporary value is freed at the end of this statement
| |______|
| argument requires that borrow lasts for `'a`
|
note: requirement for `'a` introduced here
--> $SRC_DIR/core/src/ops/function.rs:LL:COL
error: aborting due to 1 previous error

View file

@ -14,6 +14,11 @@ note: due to a current limitation of the type system, this implies a `'static` l
|
LL | for<'a> I::Item<'a>: Debug,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
note: requirement for `'static` introduced here
--> $DIR/hrtb-implied-1.rs:26:26
|
LL | for<'a> I::Item<'a>: Debug,
| ^^^^^
error: aborting due to 1 previous error

View file

@ -32,7 +32,7 @@ fn needs_static() {
//~| NOTE borrowed value does not live long enoug
fn needs_static(_: impl Sized + 'static) {}
//~^ NOTE requirement that `x` is borrowed for `'static` introduced here
//~^ NOTE requirement for `'static` introduced here
needs_static(a);
//~^ NOTE argument requires that `x` is borrowed for `'static`
}
@ -80,7 +80,7 @@ fn needs_static_mut() {
//~| NOTE borrowed value does not live long enough
fn needs_static(_: impl Sized + 'static) {}
//~^ NOTE requirement that `x` is borrowed for `'static` introduced here
//~^ NOTE requirement for `'static` introduced here
needs_static(a);
//~^ NOTE argument requires that `x` is borrowed for `'static`
}

View file

@ -50,7 +50,7 @@ LL |
LL | }
| - `x` dropped here while still borrowed
|
note: requirement that `x` is borrowed for `'static` introduced here
note: requirement for `'static` introduced here
--> $DIR/migration-note.rs:34:37
|
LL | fn needs_static(_: impl Sized + 'static) {}
@ -131,7 +131,7 @@ LL |
LL | }
| - `x` dropped here while still borrowed
|
note: requirement that `x` is borrowed for `'static` introduced here
note: requirement for `'static` introduced here
--> $DIR/migration-note.rs:82:37
|
LL | fn needs_static(_: impl Sized + 'static) {}

View file

@ -70,7 +70,7 @@ LL | })
LL | }
| - `a` dropped here while still borrowed
|
note: requirement that `a` is borrowed for `'static` introduced here
note: requirement for `'static` introduced here
--> $DIR/propagate-approximated-shorter-to-static-comparing-against-free.rs:13:8
|
LL | F: for<'x> FnOnce(Cell<&'a u32>, Cell<&'x u32>),

View file

@ -14,7 +14,7 @@ LL | z = &local_arr;
LL | }
| - `local_arr` dropped here while still borrowed
|
note: requirement that `local_arr` is borrowed for `'static` introduced here
note: requirement for `'static` introduced here
--> $DIR/propagate-multiple-requirements.rs:4:21
|
LL | fn once<S, T, U, F: FnOnce(S, T) -> U>(f: F, s: S, t: T) -> U {

View file

@ -12,16 +12,16 @@ LL | assert_static_via_hrtb_with_assoc_type(&&local);
LL | }
| - `local` dropped here while still borrowed
|
note: requirement that `local` is borrowed for `'static` introduced here
--> $DIR/local-outlives-static-via-hrtb.rs:15:53
|
LL | fn assert_static_via_hrtb<G>(_: G) where for<'a> G: Outlives<'a> {}
| ^^^^^^^^^^^^
note: due to a current limitation of the type system, this implies a `'static` lifetime
--> $DIR/local-outlives-static-via-hrtb.rs:15:42
|
LL | fn assert_static_via_hrtb<G>(_: G) where for<'a> G: Outlives<'a> {}
| ^^^^^^^^^^^^^^^^^^^^^^^
note: requirement for `'static` introduced here
--> $DIR/local-outlives-static-via-hrtb.rs:15:53
|
LL | fn assert_static_via_hrtb<G>(_: G) where for<'a> G: Outlives<'a> {}
| ^^^^^^^^^^^^
error[E0597]: `local` does not live long enough
--> $DIR/local-outlives-static-via-hrtb.rs:25:45
@ -37,16 +37,16 @@ LL | assert_static_via_hrtb_with_assoc_type(&&local);
LL | }
| - `local` dropped here while still borrowed
|
note: requirement that `local` is borrowed for `'static` introduced here
--> $DIR/local-outlives-static-via-hrtb.rs:19:30
|
LL | for<'a> &'a T: Reference<AssociatedType = &'a ()>,
| ^^^^^^^^^^^^^^^^^^^^^^^
note: due to a current limitation of the type system, this implies a `'static` lifetime
--> $DIR/local-outlives-static-via-hrtb.rs:19:5
|
LL | for<'a> &'a T: Reference<AssociatedType = &'a ()>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: requirement for `'static` introduced here
--> $DIR/local-outlives-static-via-hrtb.rs:19:30
|
LL | for<'a> &'a T: Reference<AssociatedType = &'a ()>,
| ^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 2 previous errors

View file

@ -13,16 +13,16 @@ LL | let b = |_| &a;
LL | }
| - `a` dropped here while still borrowed
|
note: requirement that `a` is borrowed for `'static` introduced here
--> $DIR/location-insensitive-scopes-issue-117146.rs:20:22
|
LL | fn bad<F: Fn(&()) -> &()>(_: F) {}
| ^^^
note: due to a current limitation of the type system, this implies a `'static` lifetime
--> $DIR/location-insensitive-scopes-issue-117146.rs:20:11
|
LL | fn bad<F: Fn(&()) -> &()>(_: F) {}
| ^^^^^^^^^^^^^^
note: requirement for `'static` introduced here
--> $DIR/location-insensitive-scopes-issue-117146.rs:20:22
|
LL | fn bad<F: Fn(&()) -> &()>(_: F) {}
| ^^^
error: implementation of `Fn` is not general enough
--> $DIR/location-insensitive-scopes-issue-117146.rs:13:5

View file

@ -13,16 +13,16 @@ LL | let b = |_| &a;
LL | }
| - `a` dropped here while still borrowed
|
note: requirement that `a` is borrowed for `'static` introduced here
--> $DIR/location-insensitive-scopes-issue-117146.rs:20:22
|
LL | fn bad<F: Fn(&()) -> &()>(_: F) {}
| ^^^
note: due to a current limitation of the type system, this implies a `'static` lifetime
--> $DIR/location-insensitive-scopes-issue-117146.rs:20:11
|
LL | fn bad<F: Fn(&()) -> &()>(_: F) {}
| ^^^^^^^^^^^^^^
note: requirement for `'static` introduced here
--> $DIR/location-insensitive-scopes-issue-117146.rs:20:22
|
LL | fn bad<F: Fn(&()) -> &()>(_: F) {}
| ^^^
error: implementation of `Fn` is not general enough
--> $DIR/location-insensitive-scopes-issue-117146.rs:13:5

View file

@ -12,7 +12,7 @@ LL | | });
LL | }
| - `x` dropped here while still borrowed
|
note: requirement that `x` is borrowed for `'static` introduced here
note: requirement for `'static` introduced here
--> $DIR/regions-infer-proc-static-upvar.rs:4:19
|
LL | fn foo<F:FnOnce()+'static>(_p: F) { }

View file

@ -11,7 +11,7 @@ LL | }
LL | }
| - `line` dropped here while still borrowed
|
note: requirement that `line` is borrowed for `'static` introduced here
note: requirement for `'static` introduced here
--> $DIR/regions-pattern-typing-issue-19552.rs:1:21
|
LL | fn assert_static<T: 'static>(_t: T) {}

View file

@ -11,7 +11,7 @@ LL | f(&x);
LL | }
| - `x` dropped here while still borrowed
|
note: requirement that `x` is borrowed for `'static` introduced here
note: requirement for `'static` introduced here
--> $DIR/static-lifetime-bound.rs:1:10
|
LL | fn f<'a: 'static>(_: &'a i32) {}

View file

@ -7,6 +7,12 @@ LL | f(x);
| ---- argument requires that borrow lasts for `'static`
LL | }
| - temporary value is freed at the end of this statement
|
note: requirement for `'static` introduced here
--> $DIR/static-region-bound.rs:3:8
|
LL | fn f<T:'static>(_: T) {}
| ^^^^^^^
error: aborting due to 1 previous error

View file

@ -6,6 +6,12 @@ LL | let s = foo(&String::from("blah blah blah"));
| | |
| | creates a temporary value which is freed while still in use
| argument requires that borrow lasts for `'static`
|
note: requirement for `'static` introduced here
--> $DIR/wf-in-where-clause-static.rs:12:17
|
LL | &'static S: Static,
| ^^^^^^
error: aborting due to 1 previous error

View file

@ -6,6 +6,12 @@ LL | let s = foo(&String::from("blah blah blah"));
| | |
| | creates a temporary value which is freed while still in use
| argument requires that borrow lasts for `'static`
|
note: requirement for `'static` introduced here
--> $DIR/wf-in-where-clause-static.rs:12:17
|
LL | &'static S: Static,
| ^^^^^^
error: aborting due to 1 previous error