Skip to content

Commit 350ed42

Browse files
committed
Added note about dangling references.
This error can only occur within a function when a borrow of data owned within the function is returned; and when there are arguments that could have been returned instead. Therefore, it is always applicable to add a specific note that links to the relevant rust documentation about dangling references.
1 parent 3becbbc commit 350ed42

7 files changed

+36
-0
lines changed

src/librustc_mir/borrow_check/error_reporting.rs

+9
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,15 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
530530
self.infcx.tcx.hir.name(fn_node_id),
531531
)
532532
);
533+
534+
err.note(
535+
"functions cannot return a borrow to data owned within the function's scope, \
536+
functions can only return borrows to data passed as arguments",
537+
);
538+
err.note(
539+
"to learn more, visit <https://doc.rust-lang.org/book/second-edition/ch04-02-\
540+
references-and-borrowing.html#dangling-references>",
541+
);
533542
} else {
534543
err.span_label(
535544
drop_span,

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

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ LL | &x
1111
LL | //~^ ERROR: `x` does not live long enough
1212
LL | }
1313
| - ...but `x` is only valid for the duration of the `silly` function, so it is dropped here while still borrowed
14+
|
15+
= note: functions cannot return a borrow to data owned within the function's scope, functions can only return borrows to data passed as arguments
16+
= note: to learn more, visit <https://doc.rust-lang.org/book/second-edition/ch04-02-references-and-borrowing.html#dangling-references>
1417

1518
error: aborting due to previous error
1619

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

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ LL | &v
1111
LL | //~^ ERROR `v` does not live long enough [E0597]
1212
LL | }
1313
| - ...but `v` is only valid for the duration of the `foo` function, so it is dropped here while still borrowed
14+
|
15+
= note: functions cannot return a borrow to data owned within the function's scope, functions can only return borrows to data passed as arguments
16+
= note: to learn more, visit <https://doc.rust-lang.org/book/second-edition/ch04-02-references-and-borrowing.html#dangling-references>
1417

1518
error: aborting due to previous error
1619

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

+9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ LL | &x
1010
| ^^ `x` would have to be valid for `'0`
1111
LL | }
1212
| - ...but `x` is only valid for the duration of the `bar` function, so it is dropped here while still borrowed
13+
|
14+
= note: functions cannot return a borrow to data owned within the function's scope, functions can only return borrows to data passed as arguments
15+
= note: to learn more, visit <https://doc.rust-lang.org/book/second-edition/ch04-02-references-and-borrowing.html#dangling-references>
1316

1417
error[E0597]: `x` does not live long enough
1518
--> $DIR/issue-52534-1.rs:25:5
@@ -23,6 +26,9 @@ LL | &x
2326
| ^^ `x` would have to be valid for `'0`
2427
LL | }
2528
| - ...but `x` is only valid for the duration of the `foo` function, so it is dropped here while still borrowed
29+
|
30+
= note: functions cannot return a borrow to data owned within the function's scope, functions can only return borrows to data passed as arguments
31+
= note: to learn more, visit <https://doc.rust-lang.org/book/second-edition/ch04-02-references-and-borrowing.html#dangling-references>
2632

2733
error[E0597]: `x` does not live long enough
2834
--> $DIR/issue-52534-1.rs:30:6
@@ -36,6 +42,9 @@ LL | &&x
3642
| ^^ `x` would have to be valid for `'0`
3743
LL | }
3844
| - ...but `x` is only valid for the duration of the `baz` function, so it is dropped here while still borrowed
45+
|
46+
= note: functions cannot return a borrow to data owned within the function's scope, functions can only return borrows to data passed as arguments
47+
= note: to learn more, visit <https://doc.rust-lang.org/book/second-edition/ch04-02-references-and-borrowing.html#dangling-references>
3948

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

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

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ LL | foo(|a| &x)
77
| has type `&'0 u32`
88
LL | }
99
| - ...but `x` is only valid for the duration of the `bar` function, so it is dropped here while still borrowed
10+
|
11+
= note: functions cannot return a borrow to data owned within the function's scope, functions can only return borrows to data passed as arguments
12+
= note: to learn more, visit <https://doc.rust-lang.org/book/second-edition/ch04-02-references-and-borrowing.html#dangling-references>
1013

1114
error[E0597]: `y` does not live long enough
1215
--> $DIR/issue-52534.rs:27:26
@@ -17,6 +20,9 @@ LL | baz(|first, second| &y)
1720
| has type `&'0 u32`
1821
LL | }
1922
| - ...but `y` is only valid for the duration of the `foobar` function, so it is dropped here while still borrowed
23+
|
24+
= note: functions cannot return a borrow to data owned within the function's scope, functions can only return borrows to data passed as arguments
25+
= note: to learn more, visit <https://doc.rust-lang.org/book/second-edition/ch04-02-references-and-borrowing.html#dangling-references>
2026

2127
error: aborting due to 2 previous errors
2228

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

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ LL | if false { &y } else { z }
99
LL | });
1010
LL | }
1111
| - ...but `y` is only valid for the duration of the `nested` function, so it is dropped here while still borrowed
12+
|
13+
= note: functions cannot return a borrow to data owned within the function's scope, functions can only return borrows to data passed as arguments
14+
= note: to learn more, visit <https://doc.rust-lang.org/book/second-edition/ch04-02-references-and-borrowing.html#dangling-references>
1215

1316
error: aborting due to previous error
1417

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

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ LL | ay = &y;
3232
...
3333
LL | }
3434
| - ...but `y` is only valid for the duration of the `nested` function, so it is dropped here while still borrowed
35+
|
36+
= note: functions cannot return a borrow to data owned within the function's scope, functions can only return borrows to data passed as arguments
37+
= note: to learn more, visit <https://doc.rust-lang.org/book/second-edition/ch04-02-references-and-borrowing.html#dangling-references>
3538

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

0 commit comments

Comments
 (0)