Skip to content

Commit 7a0319f

Browse files
committed
Point at lifetime requirement origin in more cases
1 parent c3e0b29 commit 7a0319f

21 files changed

+93
-51
lines changed

compiler/rustc_borrowck/src/borrowck_errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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 & 16 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,
@@ -3031,15 +3025,6 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
30313025

30323026
let mut err = self.path_does_not_live_long_enough(borrow_span, &name);
30333027

3034-
if let BorrowExplanation::MustBeValidFor { ref path, region_name, .. } = explanation {
3035-
for constraint in path {
3036-
if let ConstraintCategory::Predicate(pred) = constraint.category
3037-
&& !pred.is_dummy()
3038-
{
3039-
err.span_note(pred, format!("requirement that {name} is borrowed for `{region_name}` introduced here"));
3040-
}
3041-
}
3042-
}
30433028
if let Some(annotation) = self.annotate_argument_and_return_for_borrow(borrow) {
30443029
let region_name = annotation.emit(self, &mut err);
30453030

compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,19 @@ impl<'tcx> BorrowExplanation<'tcx> {
416416
{
417417
self.add_object_lifetime_default_note(tcx, err, unsize_ty);
418418
}
419+
420+
for constraint in path {
421+
if let ConstraintCategory::Predicate(pred) = constraint.category
422+
&& !pred.is_dummy()
423+
{
424+
err.span_note(
425+
pred,
426+
format!("requirement for `{region_name}` introduced here"),
427+
);
428+
break;
429+
}
430+
}
431+
419432
self.add_lifetime_bound_suggestion_to_diagnostic(err, &category, span, region_name);
420433
}
421434
_ => {}

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ LL | outlives::<'a>(call_once(c));
2929
LL | }
3030
| - `c` dropped here while still borrowed
3131
|
32-
note: requirement that `c` is borrowed for `'a` introduced here
32+
note: requirement for `'a` introduced here
3333
--> $DIR/without-precise-captures-we-are-powerless.rs:7:33
3434
|
3535
LL | fn outlives<'a>(_: impl Sized + 'a) {}
@@ -80,7 +80,7 @@ LL | outlives::<'a>(call_once(c));
8080
LL | }
8181
| - `c` dropped here while still borrowed
8282
|
83-
note: requirement that `c` is borrowed for `'a` introduced here
83+
note: requirement for `'a` introduced here
8484
--> $DIR/without-precise-captures-we-are-powerless.rs:7:33
8585
|
8686
LL | fn outlives<'a>(_: impl Sized + 'a) {}
@@ -101,6 +101,12 @@ LL | outlives::<'a>(c());
101101
| argument requires that `c` is borrowed for `'a`
102102
LL | outlives::<'a>(call_once(c));
103103
| ^ move out of `c` occurs here
104+
|
105+
note: requirement for `'a` introduced here
106+
--> $DIR/without-precise-captures-we-are-powerless.rs:7:33
107+
|
108+
LL | fn outlives<'a>(_: impl Sized + 'a) {}
109+
| ^^
104110

105111
error[E0597]: `x` does not live long enough
106112
--> $DIR/without-precise-captures-we-are-powerless.rs:36:13
@@ -142,7 +148,7 @@ LL | outlives::<'a>(call_once(c));
142148
LL | }
143149
| - `c` dropped here while still borrowed
144150
|
145-
note: requirement that `c` is borrowed for `'a` introduced here
151+
note: requirement for `'a` introduced here
146152
--> $DIR/without-precise-captures-we-are-powerless.rs:7:33
147153
|
148154
LL | fn outlives<'a>(_: impl Sized + 'a) {}
@@ -190,7 +196,7 @@ LL | T.outlives::<'a>(call_once(c));
190196
LL | }
191197
| - `c` dropped here while still borrowed
192198
|
193-
note: requirement that `c` is borrowed for `'a` introduced here
199+
note: requirement for `'a` introduced here
194200
--> $DIR/without-precise-captures-we-are-powerless.rs:49:47
195201
|
196202
LL | fn outlives<'a>(&'a self, _: impl Sized + 'a) {}

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 for `'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 for `'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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ LL | force_send(async_load(&not_static));
2323
LL | }
2424
| - `not_static` dropped here while still borrowed
2525
|
26-
note: requirement that `not_static` is borrowed for `'1` introduced here
26+
note: requirement for `'1` introduced here
2727
--> $DIR/implementation-not-general-enough-ice-133252.rs:16:18
2828
|
2929
LL | fn force_send<T: Send>(_: T) {}

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 for `'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 for `'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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +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 `x` is borrowed for `'static` introduced here
35+
//~^ NOTE requirement for `'static` introduced here
3636
needs_static(a);
3737
//~^ NOTE argument requires that `x` is borrowed for `'static`
3838
}
@@ -80,7 +80,7 @@ fn needs_static_mut() {
8080
//~| NOTE borrowed value does not live long enough
8181

8282
fn needs_static(_: impl Sized + 'static) {}
83-
//~^ NOTE requirement that `x` is borrowed for `'static` introduced here
83+
//~^ NOTE requirement for `'static` introduced here
8484
needs_static(a);
8585
//~^ NOTE argument requires that `x` is borrowed for `'static`
8686
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ LL |
5050
LL | }
5151
| - `x` dropped here while still borrowed
5252
|
53-
note: requirement that `x` is borrowed for `'static` introduced here
53+
note: requirement for `'static` introduced here
5454
--> $DIR/migration-note.rs:34:37
5555
|
5656
LL | fn needs_static(_: impl Sized + 'static) {}
@@ -131,7 +131,7 @@ LL |
131131
LL | }
132132
| - `x` dropped here while still borrowed
133133
|
134-
note: requirement that `x` is borrowed for `'static` introduced here
134+
note: requirement for `'static` introduced here
135135
--> $DIR/migration-note.rs:82:37
136136
|
137137
LL | fn needs_static(_: impl Sized + 'static) {}

0 commit comments

Comments
 (0)