Auto merge of #54229 - davidtwco:issue-52534, r=pnkfelix

[nll] borrows that must be valid for a free lifetime should explain why

Fixes #52534.

r? @nikomatsakis
This commit is contained in:
bors 2018-09-23 15:00:53 +00:00
commit f49f6e73a8
21 changed files with 1340 additions and 433 deletions

View file

@ -1,17 +1,20 @@
error[E0597]: `x` does not live long enough
--> $DIR/issue-30438-c.rs:19:5
|
LL | fn silly<'y, 'z>(_s: &'y Test<'z>) -> &'y <Test<'z> as Trait>::Out where 'z: 'static {
| -- -- also has lifetime `'y`
| |
| has lifetime `'y`
LL | let x = Test { s: "this cannot last" };
LL | &x
| ^^ borrowed value does not live long enough
| ^^ `x` would have to be valid for `'y`...
LL | //~^ ERROR: `x` does not live long enough
LL | }
| - `x` dropped here while still borrowed
| - ...but `x` will be dropped here, when the function `silly` returns
|
note: borrowed value must be valid for the lifetime 'y as defined on the function body at 17:10...
--> $DIR/issue-30438-c.rs:17:10
|
LL | fn silly<'y, 'z>(_s: &'y Test<'z>) -> &'y <Test<'z> as Trait>::Out where 'z: 'static {
| ^^
= help: use data from the highlighted arguments which match the `'y` lifetime of the return type
= 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

@ -1,17 +1,20 @@
error[E0597]: `v` does not live long enough
--> $DIR/borrowed-universal-error-2.rs:16:5
|
LL | fn foo<'a>(x: &'a (u32,)) -> &'a u32 {
| -- -- also has lifetime `'a`
| |
| has lifetime `'a`
LL | let v = 22;
LL | &v
| ^^ borrowed value does not live long enough
| ^^ `v` would have to be valid for `'a`...
LL | //~^ ERROR `v` does not live long enough [E0597]
LL | }
| - `v` dropped here while still borrowed
| - ...but `v` will be dropped here, when the function `foo` returns
|
note: borrowed value must be valid for the lifetime 'a as defined on the function body at 14:8...
--> $DIR/borrowed-universal-error-2.rs:14:8
|
LL | fn foo<'a>(x: &'a (u32,)) -> &'a u32 {
| ^^
= help: use data from the highlighted arguments which match the `'a` lifetime of the return type
= 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

@ -0,0 +1,53 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(nll)]
#![allow(warnings)]
struct Test;
impl Test {
fn bar(&self, x: &u32) -> &u32 {
let x = 22;
&x
}
}
fn foo(x: &u32) -> &u32 {
let x = 22;
&x
}
fn baz(x: &u32) -> &&u32 {
let x = 22;
&&x
}
fn foobazbar<'a>(x: u32, y: &'a u32) -> &'a u32 {
let x = 22;
&x
}
fn foobar<'a>(x: &'a u32) -> &'a u32 {
let x = 22;
&x
}
fn foobaz<'a, 'b>(x: &'a u32, y: &'b u32) -> &'a u32 {
let x = 22;
&x
}
fn foobarbaz<'a, 'b>(x: &'a u32, y: &'b u32, z: &'a u32) -> &'a u32 {
let x = 22;
&x
}
fn main() { }

View file

@ -0,0 +1,140 @@
error[E0597]: `x` does not live long enough
--> $DIR/issue-52534-1.rs:19:9
|
LL | fn bar(&self, x: &u32) -> &u32 {
| ----- ---- has type `&'0 u32`
| |
| has type `&'0 Test`
LL | let x = 22;
LL | &x
| ^^ `x` would have to be valid for `'0`...
LL | }
| - ...but `x` will be dropped here, when the function `bar` returns
|
= note: argument and return type have the same lifetime due to lifetime elision rules
= note: to learn more, visit <https://doc.rust-lang.org/book/second-edition/ch10-03-lifetime-syntax.html#lifetime-elision>
= 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
|
LL | fn foo(x: &u32) -> &u32 {
| ---- ---- also has type `&'0 u32`
| |
| has type `&'0 u32`
LL | let x = 22;
LL | &x
| ^^ `x` would have to be valid for `'0`...
LL | }
| - ...but `x` will be dropped here, when the function `foo` returns
|
= note: argument and return type have the same lifetime due to lifetime elision rules
= note: to learn more, visit <https://doc.rust-lang.org/book/second-edition/ch10-03-lifetime-syntax.html#lifetime-elision>
= 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
|
LL | fn baz(x: &u32) -> &&u32 {
| ---- ----- has type `&'0 &'0 u32`
| |
| has type `&'0 u32`
LL | let x = 22;
LL | &&x
| ^^ `x` would have to be valid for `'0`...
LL | }
| - ...but `x` will be dropped here, when the function `baz` returns
|
= note: argument and return type have the same lifetime due to lifetime elision rules
= note: to learn more, visit <https://doc.rust-lang.org/book/second-edition/ch10-03-lifetime-syntax.html#lifetime-elision>
= 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
|
LL | &&x
| ^^ temporary value does not live long enough
LL | }
| - temporary value only lives until here
|
note: borrowed value must be valid for the anonymous lifetime #1 defined on the function body at 28:1...
--> $DIR/issue-52534-1.rs:28:1
|
LL | / fn baz(x: &u32) -> &&u32 {
LL | | let x = 22;
LL | | &&x
LL | | }
| |_^
error[E0597]: `x` does not live long enough
--> $DIR/issue-52534-1.rs:35:5
|
LL | fn foobazbar<'a>(x: u32, y: &'a u32) -> &'a u32 {
| -- -- also has lifetime `'a`
| |
| has lifetime `'a`
LL | let x = 22;
LL | &x
| ^^ `x` would have to be valid for `'a`...
LL | }
| - ...but `x` will be dropped here, when the function `foobazbar` returns
|
= help: use data from the highlighted arguments which match the `'a` lifetime of the return type
= 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:40:5
|
LL | fn foobar<'a>(x: &'a u32) -> &'a u32 {
| -- -- also has lifetime `'a`
| |
| has lifetime `'a`
LL | let x = 22;
LL | &x
| ^^ `x` would have to be valid for `'a`...
LL | }
| - ...but `x` will be dropped here, when the function `foobar` returns
|
= help: use data from the highlighted arguments which match the `'a` lifetime of the return type
= 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:45:5
|
LL | fn foobaz<'a, 'b>(x: &'a u32, y: &'b u32) -> &'a u32 {
| -- has lifetime `'a` -- also has lifetime `'a`
LL | let x = 22;
LL | &x
| ^^ `x` would have to be valid for `'a`...
LL | }
| - ...but `x` will be dropped here, when the function `foobaz` returns
|
= help: use data from the highlighted arguments which match the `'a` lifetime of the return type
= 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:50:5
|
LL | fn foobarbaz<'a, 'b>(x: &'a u32, y: &'b u32, z: &'a u32) -> &'a u32 {
| -- -- -- also has lifetime `'a`
| | |
| has lifetime `'a` has lifetime `'a`
LL | let x = 22;
LL | &x
| ^^ `x` would have to be valid for `'a`...
LL | }
| - ...but `x` will be dropped here, when the function `foobarbaz` returns
|
= help: use data from the highlighted arguments which match the `'a` lifetime of the return type
= 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 8 previous errors
For more information about this error, try `rustc --explain E0597`.

View file

@ -0,0 +1,26 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(nll)]
#![allow(warnings)]
fn foo(x: &u32) -> &u32 {
let y;
{
let x = 32;
y = &x
}
println!("{}", y);
x
}
fn main() { }

View file

@ -0,0 +1,14 @@
error[E0597]: `x` does not live long enough
--> $DIR/issue-52534-2.rs:19:9
|
LL | y = &x
| ^^^^^^ borrowed value does not live long enough
LL | }
| - `x` dropped here while still borrowed
LL |
LL | println!("{}", y);
| - borrow later used here
error: aborting due to previous error
For more information about this error, try `rustc --explain E0597`.

View file

@ -0,0 +1,30 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(nll)]
#![allow(warnings)]
fn foo(_: impl FnOnce(&u32) -> &u32) {
}
fn baz(_: impl FnOnce(&u32, u32) -> &u32) {
}
fn bar() {
let x = 22;
foo(|a| &x)
}
fn foobar() {
let y = 22;
baz(|first, second| &y)
}
fn main() { }

View file

@ -0,0 +1,29 @@
error[E0597]: `x` does not live long enough
--> $DIR/issue-52534.rs:22:14
|
LL | foo(|a| &x)
| - ^ `x` would have to be valid for `'0`...
| |
| has type `&'0 u32`
LL | }
| - ...but `x` will be dropped here, when the function `bar` returns
|
= 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
|
LL | baz(|first, second| &y)
| ----- ^ `y` would have to be valid for `'0`...
| |
| has type `&'0 u32`
LL | }
| - ...but `y` will be dropped here, when the function `foobar` returns
|
= 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
For more information about this error, try `rustc --explain E0597`.