Skip to content

Commit 5f51ddb

Browse files
authored
Unrolled build for #146011
Rollup merge of #146011 - estebank:lifetime-obligation-span, r=lcnr Point at fn bound that introduced lifetime obligation The last note is new ``` error[E0597]: `c` does not live long enough --> $DIR/without-precise-captures-we-are-powerless.rs:19:20 | LL | fn simple<'a>(x: &'a i32) { | -- lifetime `'a` defined here ... LL | let c = async move || { println!("{}", *x); }; | - binding `c` declared here LL | outlives::<'a>(c()); | ---------------^--- | | | | | borrowed value does not live long enough | argument requires that `c` is borrowed for `'a` LL | outlives::<'a>(call_once(c)); LL | } | - `c` dropped here while still borrowed | note: requirement that `c` is borrowed for `'a` introduced here --> $DIR/without-precise-captures-we-are-powerless.rs:7:33 | LL | fn outlives<'a>(_: impl Sized + 'a) {} | ^^ ``` When encountering a `ConstraintCategory::Predicate` in a funtion call, point at the `Span` for that `Predicate` to explain where the lifetime obligation originates from. CC #55307.
2 parents a2db928 + c5313fe commit 5f51ddb

24 files changed

+258
-32
lines changed

compiler/rustc_borrowck/src/borrowck_errors.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ impl<'infcx, 'tcx> crate::MirBorrowckCtxt<'_, 'infcx, 'tcx> {
426426
}
427427

428428
pub(crate) fn path_does_not_live_long_enough(&self, span: Span, path: &str) -> Diag<'infcx> {
429-
struct_span_code_err!(self.dcx(), span, E0597, "{} does not live long enough", path,)
429+
struct_span_code_err!(self.dcx(), span, E0597, "{} does not live long enough", path)
430430
}
431431

432432
pub(crate) fn cannot_return_reference_to_local(
@@ -480,7 +480,7 @@ impl<'infcx, 'tcx> crate::MirBorrowckCtxt<'_, 'infcx, 'tcx> {
480480
}
481481

482482
pub(crate) fn temporary_value_borrowed_for_too_long(&self, span: Span) -> Diag<'infcx> {
483-
struct_span_code_err!(self.dcx(), span, E0716, "temporary value dropped while borrowed",)
483+
struct_span_code_err!(self.dcx(), span, E0716, "temporary value dropped while borrowed")
484484
}
485485
}
486486

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2992,6 +2992,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
29922992
self.buffer_error(err);
29932993
}
29942994

2995+
#[tracing::instrument(level = "debug", skip(self, explanation))]
29952996
fn report_local_value_does_not_live_long_enough(
29962997
&self,
29972998
location: Location,
@@ -3001,13 +3002,6 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
30013002
borrow_spans: UseSpans<'tcx>,
30023003
explanation: BorrowExplanation<'tcx>,
30033004
) -> Diag<'infcx> {
3004-
debug!(
3005-
"report_local_value_does_not_live_long_enough(\
3006-
{:?}, {:?}, {:?}, {:?}, {:?}\
3007-
)",
3008-
location, name, borrow, drop_span, borrow_spans
3009-
);
3010-
30113005
let borrow_span = borrow_spans.var_or_use_path_span();
30123006
if let BorrowExplanation::MustBeValidFor {
30133007
category,

compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,26 @@ impl<'tcx> BorrowExplanation<'tcx> {
416416
{
417417
self.add_object_lifetime_default_note(tcx, err, unsize_ty);
418418
}
419+
420+
let mut preds = path
421+
.iter()
422+
.filter_map(|constraint| match constraint.category {
423+
ConstraintCategory::Predicate(pred) if !pred.is_dummy() => Some(pred),
424+
_ => None,
425+
})
426+
.collect::<Vec<Span>>();
427+
preds.sort();
428+
preds.dedup();
429+
if !preds.is_empty() {
430+
let s = if preds.len() == 1 { "" } else { "s" };
431+
err.span_note(
432+
preds,
433+
format!(
434+
"requirement{s} that the value outlives `{region_name}` introduced here"
435+
),
436+
);
437+
}
438+
419439
self.add_lifetime_bound_suggestion_to_diagnostic(err, &category, span, region_name);
420440
}
421441
_ => {}

tests/ui/async-await/async-closures/without-precise-captures-we-are-powerless.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,18 @@ fn through_field_and_ref_move<'a>(x: &S<'a>) {
4444
outlives::<'a>(call_once(c)); //~ ERROR explicit lifetime required in the type of `x`
4545
}
4646

47+
struct T;
48+
impl T {
49+
fn outlives<'a>(&'a self, _: impl Sized + 'a) {}
50+
}
51+
fn through_method<'a>(x: &'a i32) {
52+
let c = async || { println!("{}", *x); }; //~ ERROR `x` does not live long enough
53+
T.outlives::<'a>(c());
54+
T.outlives::<'a>(call_once(c));
55+
56+
let c = async move || { println!("{}", *x); };
57+
T.outlives::<'a>(c()); //~ ERROR `c` does not live long enough
58+
T.outlives::<'a>(call_once(c));
59+
}
60+
4761
fn main() {}

tests/ui/async-await/async-closures/without-precise-captures-we-are-powerless.stderr

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ LL | outlives::<'a>(c());
2828
LL | outlives::<'a>(call_once(c));
2929
LL | }
3030
| - `c` dropped here while still borrowed
31+
|
32+
note: requirement that the value outlives `'a` introduced here
33+
--> $DIR/without-precise-captures-we-are-powerless.rs:7:33
34+
|
35+
LL | fn outlives<'a>(_: impl Sized + 'a) {}
36+
| ^^
3137

3238
error[E0597]: `x` does not live long enough
3339
--> $DIR/without-precise-captures-we-are-powerless.rs:26:13
@@ -73,6 +79,12 @@ LL | outlives::<'a>(c());
7379
LL | outlives::<'a>(call_once(c));
7480
LL | }
7581
| - `c` dropped here while still borrowed
82+
|
83+
note: requirement that the value outlives `'a` introduced here
84+
--> $DIR/without-precise-captures-we-are-powerless.rs:7:33
85+
|
86+
LL | fn outlives<'a>(_: impl Sized + 'a) {}
87+
| ^^
7688

7789
error[E0505]: cannot move out of `c` because it is borrowed
7890
--> $DIR/without-precise-captures-we-are-powerless.rs:32:30
@@ -89,6 +101,12 @@ LL | outlives::<'a>(c());
89101
| argument requires that `c` is borrowed for `'a`
90102
LL | outlives::<'a>(call_once(c));
91103
| ^ move out of `c` occurs here
104+
|
105+
note: requirement that the value outlives `'a` introduced here
106+
--> $DIR/without-precise-captures-we-are-powerless.rs:7:33
107+
|
108+
LL | fn outlives<'a>(_: impl Sized + 'a) {}
109+
| ^^
92110

93111
error[E0597]: `x` does not live long enough
94112
--> $DIR/without-precise-captures-we-are-powerless.rs:36:13
@@ -129,6 +147,12 @@ LL | outlives::<'a>(c());
129147
LL | outlives::<'a>(call_once(c));
130148
LL | }
131149
| - `c` dropped here while still borrowed
150+
|
151+
note: requirement that the value outlives `'a` introduced here
152+
--> $DIR/without-precise-captures-we-are-powerless.rs:7:33
153+
|
154+
LL | fn outlives<'a>(_: impl Sized + 'a) {}
155+
| ^^
132156

133157
error[E0621]: explicit lifetime required in the type of `x`
134158
--> $DIR/without-precise-captures-we-are-powerless.rs:44:5
@@ -141,7 +165,44 @@ help: add explicit lifetime `'a` to the type of `x`
141165
LL | fn through_field_and_ref_move<'a>(x: &'a S<'a>) {
142166
| ++
143167

144-
error: aborting due to 10 previous errors
168+
error[E0597]: `x` does not live long enough
169+
--> $DIR/without-precise-captures-we-are-powerless.rs:52:13
170+
|
171+
LL | fn through_method<'a>(x: &'a i32) {
172+
| -- lifetime `'a` defined here
173+
LL | let c = async || { println!("{}", *x); };
174+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ borrowed value does not live long enough
175+
LL | T.outlives::<'a>(c());
176+
LL | T.outlives::<'a>(call_once(c));
177+
| ------------------------------ argument requires that `x` is borrowed for `'a`
178+
...
179+
LL | }
180+
| - `x` dropped here while still borrowed
181+
182+
error[E0597]: `c` does not live long enough
183+
--> $DIR/without-precise-captures-we-are-powerless.rs:57:22
184+
|
185+
LL | fn through_method<'a>(x: &'a i32) {
186+
| -- lifetime `'a` defined here
187+
...
188+
LL | let c = async move || { println!("{}", *x); };
189+
| - binding `c` declared here
190+
LL | T.outlives::<'a>(c());
191+
| -----------------^---
192+
| | |
193+
| | borrowed value does not live long enough
194+
| argument requires that `c` is borrowed for `'a`
195+
LL | T.outlives::<'a>(call_once(c));
196+
LL | }
197+
| - `c` dropped here while still borrowed
198+
|
199+
note: requirement that the value outlives `'a` introduced here
200+
--> $DIR/without-precise-captures-we-are-powerless.rs:49:47
201+
|
202+
LL | fn outlives<'a>(&'a self, _: impl Sized + 'a) {}
203+
| ^^
204+
205+
error: aborting due to 12 previous errors
145206

146207
Some errors have detailed explanations: E0505, E0597, E0621.
147208
For more information about an error, try `rustc --explain E0505`.

tests/ui/borrowck/fn-item-check-type-params.stderr

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ LL | want(&String::new(), extend_lt);
2727
| | |
2828
| | creates a temporary value which is freed while still in use
2929
| argument requires that borrow lasts for `'static`
30+
|
31+
note: requirement that the value outlives `'static` introduced here
32+
--> $DIR/fn-item-check-type-params.rs:47:33
33+
|
34+
LL | fn want<I, O>(_: I, _: impl Fn(I) -> O) {}
35+
| ^^^^^^^^^^
3036

3137
error[E0716]: temporary value dropped while borrowed
3238
--> $DIR/fn-item-check-type-params.rs:54:26
@@ -36,6 +42,12 @@ LL | let val = extend_lt(&String::from("blah blah blah"));
3642
| | |
3743
| | creates a temporary value which is freed while still in use
3844
| argument requires that borrow lasts for `'static`
45+
|
46+
note: requirement that the value outlives `'static` introduced here
47+
--> $DIR/fn-item-check-type-params.rs:22:21
48+
|
49+
LL | (T, Option<U>): Displayable,
50+
| ^^^^^^^^^^^
3951

4052
error: aborting due to 4 previous errors
4153

tests/ui/borrowck/implementation-not-general-enough-ice-133252.stderr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ LL | force_send(async_load(&not_static));
2222
...
2323
LL | }
2424
| - `not_static` dropped here while still borrowed
25+
|
26+
note: requirement that the value outlives `'1` introduced here
27+
--> $DIR/implementation-not-general-enough-ice-133252.rs:16:18
28+
|
29+
LL | fn force_send<T: Send>(_: T) {}
30+
| ^^^^
2531

2632
error: aborting due to 2 previous errors
2733

tests/ui/borrowck/issue-17545.stderr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ LL | | ));
1010
| | -- temporary value is freed at the end of this statement
1111
| |______|
1212
| argument requires that borrow lasts for `'a`
13+
|
14+
note: requirement that the value outlives `'a` introduced here
15+
--> $SRC_DIR/core/src/ops/function.rs:LL:COL
1316

1417
error: aborting due to 1 previous error
1518

tests/ui/generic-associated-types/bugs/hrtb-implied-1.stderr

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ note: due to a current limitation of the type system, this implies a `'static` l
1414
|
1515
LL | for<'a> I::Item<'a>: Debug,
1616
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
17+
note: requirement that the value outlives `'static` introduced here
18+
--> $DIR/hrtb-implied-1.rs:26:26
19+
|
20+
LL | for<'a> I::Item<'a>: Debug,
21+
| ^^^^^
1722

1823
error: aborting due to 1 previous error
1924

tests/ui/impl-trait/precise-capturing/migration-note.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ fn needs_static() {
3232
//~| NOTE borrowed value does not live long enoug
3333

3434
fn needs_static(_: impl Sized + 'static) {}
35+
//~^ NOTE requirement that the value outlives `'static` introduced here
3536
needs_static(a);
3637
//~^ NOTE argument requires that `x` is borrowed for `'static`
3738
}
@@ -79,6 +80,7 @@ fn needs_static_mut() {
7980
//~| NOTE borrowed value does not live long enough
8081

8182
fn needs_static(_: impl Sized + 'static) {}
83+
//~^ NOTE requirement that the value outlives `'static` introduced here
8284
needs_static(a);
8385
//~^ NOTE argument requires that `x` is borrowed for `'static`
8486
}

0 commit comments

Comments
 (0)