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.
This commit is contained in:
David Wood 2018-09-17 20:49:21 +02:00
parent 3becbbc129
commit 350ed4200c
No known key found for this signature in database
GPG Key ID: 01760B4F9F53F154
7 changed files with 36 additions and 0 deletions

View File

@ -530,6 +530,15 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
self.infcx.tcx.hir.name(fn_node_id),
)
);
err.note(
"functions cannot return a borrow to data owned within the function's scope, \
functions can only return borrows to data passed as arguments",
);
err.note(
"to learn more, visit <https://doc.rust-lang.org/book/second-edition/ch04-02-\
references-and-borrowing.html#dangling-references>",
);
} else {
err.span_label(
drop_span,

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -32,6 +32,9 @@ LL | ay = &y;
...
LL | }
| - ...but `y` is only valid for the duration of the `nested` function, so it is dropped here while still borrowed
|
= note: functions cannot return a borrow to data owned within the function's scope, functions can only return borrows to data passed as arguments
= note: to learn more, visit <https://doc.rust-lang.org/book/second-edition/ch04-02-references-and-borrowing.html#dangling-references>
error: unsatisfied lifetime constraints
--> $DIR/regions-nested-fns.rs:23:68