Skip to content

Commit 876774b

Browse files
committed
Improve 'dropped here' note.
Start mentioning function name that the variable is valid within in notes to provide context.
1 parent 9eb8d11 commit 876774b

File tree

7 files changed

+25
-9
lines changed

7 files changed

+25
-9
lines changed

src/librustc_mir/borrow_check/error_reporting.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,23 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
519519
borrow_span,
520520
format!("`{}` would have to be valid for `{}`", name, region_name)
521521
);
522-
err.span_label(drop_span, format!("but `{}` dropped here while still borrowed", name));
522+
523+
if let Some(fn_node_id) = self.infcx.tcx.hir.as_local_node_id(self.mir_def_id) {
524+
err.span_label(
525+
drop_span,
526+
format!(
527+
"...but `{}` is only valid for the duration of the `{}` function, so it \
528+
is dropped here while still borrowed",
529+
name,
530+
self.infcx.tcx.hir.name(fn_node_id),
531+
)
532+
);
533+
} else {
534+
err.span_label(
535+
drop_span,
536+
format!("...but `{}` dropped here while still borrowed", name)
537+
);
538+
}
523539

524540
if let BorrowExplanation::MustBeValidFor(..) = explanation { } else {
525541
explanation.emit(self.infcx.tcx, &mut err);

src/test/ui/issues/issue-30438-c.nll.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ LL | &x
1010
| ^^ `x` would have to be valid for `'y`
1111
LL | //~^ ERROR: `x` does not live long enough
1212
LL | }
13-
| - but `x` dropped here while still borrowed
13+
| - ...but `x` is only valid for the duration of the `silly` function, so it is dropped here while still borrowed
1414

1515
error: aborting due to previous error
1616

src/test/ui/nll/borrowed-universal-error-2.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ LL | &v
1010
| ^^ `v` would have to be valid for `'a`
1111
LL | //~^ ERROR `v` does not live long enough [E0597]
1212
LL | }
13-
| - but `v` dropped here while still borrowed
13+
| - ...but `v` is only valid for the duration of the `foo` function, so it is dropped here while still borrowed
1414

1515
error: aborting due to previous error
1616

src/test/ui/nll/issue-52534-1.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ LL | let x = 22;
99
LL | &x
1010
| ^^ `x` would have to be valid for `'0`
1111
LL | }
12-
| - but `x` dropped here while still borrowed
12+
| - ...but `x` is only valid for the duration of the `bar` function, so it is dropped here while still borrowed
1313

1414
error[E0597]: `x` does not live long enough
1515
--> $DIR/issue-52534-1.rs:25:5
@@ -22,7 +22,7 @@ LL | let x = 22;
2222
LL | &x
2323
| ^^ `x` would have to be valid for `'0`
2424
LL | }
25-
| - but `x` dropped here while still borrowed
25+
| - ...but `x` is only valid for the duration of the `foo` function, so it is dropped here while still borrowed
2626

2727
error[E0597]: `x` does not live long enough
2828
--> $DIR/issue-52534-1.rs:30:6
@@ -35,7 +35,7 @@ LL | let x = 22;
3535
LL | &&x
3636
| ^^ `x` would have to be valid for `'0`
3737
LL | }
38-
| - but `x` dropped here while still borrowed
38+
| - ...but `x` is only valid for the duration of the `baz` function, so it is dropped here while still borrowed
3939

4040
error[E0597]: borrowed value does not live long enough
4141
--> $DIR/issue-52534-1.rs:30:6

src/test/ui/nll/issue-52534.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | foo(|a| &x)
66
| |
77
| has type `&'0 u32`
88
LL | }
9-
| - but `x` dropped here while still borrowed
9+
| - ...but `x` is only valid for the duration of the `bar` function, so it is dropped here while still borrowed
1010

1111
error: aborting due to previous error
1212

src/test/ui/regions/regions-nested-fns-2.nll.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | if false { &y } else { z }
88
| ^ `y` would have to be valid for `'0`
99
LL | });
1010
LL | }
11-
| - but `y` dropped here while still borrowed
11+
| - ...but `y` is only valid for the duration of the `nested` function, so it is dropped here while still borrowed
1212

1313
error: aborting due to previous error
1414

src/test/ui/regions/regions-nested-fns.nll.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ LL | ay = &y;
3131
| ^ `y` would have to be valid for `'0`
3232
...
3333
LL | }
34-
| - but `y` dropped here while still borrowed
34+
| - ...but `y` is only valid for the duration of the `nested` function, so it is dropped here while still borrowed
3535

3636
error: unsatisfied lifetime constraints
3737
--> $DIR/regions-nested-fns.rs:23:68

0 commit comments

Comments
 (0)