about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/nll/nested-bodies-in-dead-code.rs28
-rw-r--r--tests/ui/nll/nested-bodies-in-dead-code.stderr50
2 files changed, 78 insertions, 0 deletions
diff --git a/tests/ui/nll/nested-bodies-in-dead-code.rs b/tests/ui/nll/nested-bodies-in-dead-code.rs
new file mode 100644
index 00000000000..6ac68f5adbc
--- /dev/null
+++ b/tests/ui/nll/nested-bodies-in-dead-code.rs
@@ -0,0 +1,28 @@
+//@ edition: 2024
+
+// Regression test for #140583. We want to borrowck nested
+// bodies even if they are in dead code. While not necessary for
+// soundness, it is desirable to error in such cases.
+
+fn main() {
+    return;
+    |x: &str| -> &'static str { x };
+    //~^ ERROR lifetime may not live long enough
+    || {
+        || {
+            let temp = 1;
+            let p: &'static u32 = &temp;
+            //~^ ERROR `temp` does not live long enough
+        };
+    };
+    const {
+        let temp = 1;
+        let p: &'static u32 = &temp;
+        //~^ ERROR `temp` does not live long enough
+    };
+    async {
+        let temp = 1;
+        let p: &'static u32 = &temp;
+        //~^ ERROR `temp` does not live long enough
+    };
+}
diff --git a/tests/ui/nll/nested-bodies-in-dead-code.stderr b/tests/ui/nll/nested-bodies-in-dead-code.stderr
new file mode 100644
index 00000000000..da6ccff5777
--- /dev/null
+++ b/tests/ui/nll/nested-bodies-in-dead-code.stderr
@@ -0,0 +1,50 @@
+error: lifetime may not live long enough
+  --> $DIR/nested-bodies-in-dead-code.rs:9:33
+   |
+LL |     |x: &str| -> &'static str { x };
+   |         -                       ^ returning this value requires that `'1` must outlive `'static`
+   |         |
+   |         let's call the lifetime of this reference `'1`
+
+error[E0597]: `temp` does not live long enough
+  --> $DIR/nested-bodies-in-dead-code.rs:14:35
+   |
+LL |             let temp = 1;
+   |                 ---- binding `temp` declared here
+LL |             let p: &'static u32 = &temp;
+   |                    ------------   ^^^^^ borrowed value does not live long enough
+   |                    |
+   |                    type annotation requires that `temp` is borrowed for `'static`
+LL |
+LL |         };
+   |         - `temp` dropped here while still borrowed
+
+error[E0597]: `temp` does not live long enough
+  --> $DIR/nested-bodies-in-dead-code.rs:20:31
+   |
+LL |         let temp = 1;
+   |             ---- binding `temp` declared here
+LL |         let p: &'static u32 = &temp;
+   |                ------------   ^^^^^ borrowed value does not live long enough
+   |                |
+   |                type annotation requires that `temp` is borrowed for `'static`
+LL |
+LL |     };
+   |     - `temp` dropped here while still borrowed
+
+error[E0597]: `temp` does not live long enough
+  --> $DIR/nested-bodies-in-dead-code.rs:25:31
+   |
+LL |         let temp = 1;
+   |             ---- binding `temp` declared here
+LL |         let p: &'static u32 = &temp;
+   |                ------------   ^^^^^ borrowed value does not live long enough
+   |                |
+   |                type annotation requires that `temp` is borrowed for `'static`
+LL |
+LL |     };
+   |     - `temp` dropped here while still borrowed
+
+error: aborting due to 4 previous errors
+
+For more information about this error, try `rustc --explain E0597`.