about summary refs log tree commit diff
path: root/src/test
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2018-09-23 15:00:53 +0000
committerbors <bors@rust-lang.org>2018-09-23 15:00:53 +0000
commitf49f6e73a8a2dd6ec9f86df9922ccfa2210d9eda (patch)
treec690cdcb18ef4d4ab0260686d22c509a1b2a7cdf /src/test
parentbe91c35f3466843713b9b6c5a7238ba83aef3602 (diff)
parentb342f0017931180097f17905da8640f674165255 (diff)
downloadrust-f49f6e73a8a2dd6ec9f86df9922ccfa2210d9eda.tar.gz
rust-f49f6e73a8a2dd6ec9f86df9922ccfa2210d9eda.zip
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
Diffstat (limited to 'src/test')
-rw-r--r--src/test/ui/issues/issue-30438-c.nll.stderr17
-rw-r--r--src/test/ui/nll/borrowed-universal-error-2.stderr17
-rw-r--r--src/test/ui/nll/issue-52534-1.rs53
-rw-r--r--src/test/ui/nll/issue-52534-1.stderr140
-rw-r--r--src/test/ui/nll/issue-52534-2.rs26
-rw-r--r--src/test/ui/nll/issue-52534-2.stderr14
-rw-r--r--src/test/ui/nll/issue-52534.rs30
-rw-r--r--src/test/ui/nll/issue-52534.stderr29
8 files changed, 312 insertions, 14 deletions
diff --git a/src/test/ui/issues/issue-30438-c.nll.stderr b/src/test/ui/issues/issue-30438-c.nll.stderr
index 28e63b2d36b..3d2c95013ab 100644
--- a/src/test/ui/issues/issue-30438-c.nll.stderr
+++ b/src/test/ui/issues/issue-30438-c.nll.stderr
@@ -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
-   |
-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
+   | - ...but `x` will be dropped here, when the function `silly` returns
    |
-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
 
diff --git a/src/test/ui/nll/borrowed-universal-error-2.stderr b/src/test/ui/nll/borrowed-universal-error-2.stderr
index 867e473af2c..c35a14ca704 100644
--- a/src/test/ui/nll/borrowed-universal-error-2.stderr
+++ b/src/test/ui/nll/borrowed-universal-error-2.stderr
@@ -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
-   |
-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
+   | - ...but `v` will be dropped here, when the function `foo` returns
    |
-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
 
diff --git a/src/test/ui/nll/issue-52534-1.rs b/src/test/ui/nll/issue-52534-1.rs
new file mode 100644
index 00000000000..cd6c10335cc
--- /dev/null
+++ b/src/test/ui/nll/issue-52534-1.rs
@@ -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() { }
diff --git a/src/test/ui/nll/issue-52534-1.stderr b/src/test/ui/nll/issue-52534-1.stderr
new file mode 100644
index 00000000000..e2f8134e3be
--- /dev/null
+++ b/src/test/ui/nll/issue-52534-1.stderr
@@ -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`.
diff --git a/src/test/ui/nll/issue-52534-2.rs b/src/test/ui/nll/issue-52534-2.rs
new file mode 100644
index 00000000000..4eac6feac0d
--- /dev/null
+++ b/src/test/ui/nll/issue-52534-2.rs
@@ -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() { }
diff --git a/src/test/ui/nll/issue-52534-2.stderr b/src/test/ui/nll/issue-52534-2.stderr
new file mode 100644
index 00000000000..51cd7c7bf3b
--- /dev/null
+++ b/src/test/ui/nll/issue-52534-2.stderr
@@ -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`.
diff --git a/src/test/ui/nll/issue-52534.rs b/src/test/ui/nll/issue-52534.rs
new file mode 100644
index 00000000000..273c9b3c802
--- /dev/null
+++ b/src/test/ui/nll/issue-52534.rs
@@ -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() { }
diff --git a/src/test/ui/nll/issue-52534.stderr b/src/test/ui/nll/issue-52534.stderr
new file mode 100644
index 00000000000..00d72546ebc
--- /dev/null
+++ b/src/test/ui/nll/issue-52534.stderr
@@ -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`.