diff --git a/src/librustc_mir/borrow_check/error_reporting.rs b/src/librustc_mir/borrow_check/error_reporting.rs index 6fe9087370b0..3c8a8e26335e 100644 --- a/src/librustc_mir/borrow_check/error_reporting.rs +++ b/src/librustc_mir/borrow_check/error_reporting.rs @@ -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 ", + ); } else { err.span_label( drop_span, diff --git a/src/test/ui/issues/issue-30438-c.nll.stderr b/src/test/ui/issues/issue-30438-c.nll.stderr index 11dbe5fcaca3..e7f2efcb01a5 100644 --- a/src/test/ui/issues/issue-30438-c.nll.stderr +++ b/src/test/ui/issues/issue-30438-c.nll.stderr @@ -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 error: aborting due to previous error diff --git a/src/test/ui/nll/borrowed-universal-error-2.stderr b/src/test/ui/nll/borrowed-universal-error-2.stderr index c8b473d2b2cc..cfa46881ae0c 100644 --- a/src/test/ui/nll/borrowed-universal-error-2.stderr +++ b/src/test/ui/nll/borrowed-universal-error-2.stderr @@ -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 error: aborting due to previous error diff --git a/src/test/ui/nll/issue-52534-1.stderr b/src/test/ui/nll/issue-52534-1.stderr index 233ed4716305..2400cb72a385 100644 --- a/src/test/ui/nll/issue-52534-1.stderr +++ b/src/test/ui/nll/issue-52534-1.stderr @@ -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 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 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 error[E0597]: borrowed value does not live long enough --> $DIR/issue-52534-1.rs:30:6 diff --git a/src/test/ui/nll/issue-52534.stderr b/src/test/ui/nll/issue-52534.stderr index 50b379755d35..fc7766885f5e 100644 --- a/src/test/ui/nll/issue-52534.stderr +++ b/src/test/ui/nll/issue-52534.stderr @@ -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 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 error: aborting due to 2 previous errors diff --git a/src/test/ui/regions/regions-nested-fns-2.nll.stderr b/src/test/ui/regions/regions-nested-fns-2.nll.stderr index f6b7d8aa3b74..62c4a6eae94e 100644 --- a/src/test/ui/regions/regions-nested-fns-2.nll.stderr +++ b/src/test/ui/regions/regions-nested-fns-2.nll.stderr @@ -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 error: aborting due to previous error diff --git a/src/test/ui/regions/regions-nested-fns.nll.stderr b/src/test/ui/regions/regions-nested-fns.nll.stderr index 94e559b52382..b53ea459d6c7 100644 --- a/src/test/ui/regions/regions-nested-fns.nll.stderr +++ b/src/test/ui/regions/regions-nested-fns.nll.stderr @@ -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 error: unsatisfied lifetime constraints --> $DIR/regions-nested-fns.rs:23:68