about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMara Bos <m-ou.se@m-ou.se>2025-05-03 08:42:41 +0200
committerMara Bos <m-ou.se@m-ou.se>2025-06-13 09:20:55 +0200
commit578fa8e945fa7af10dddf4861da718fbaf4c34f3 (patch)
treef2a01d7b078876d7411f0dc0465ce08709f98465
parente71a93b5c2a7d0d4377ce7a9692ed75dec4aa6b5 (diff)
downloadrust-578fa8e945fa7af10dddf4861da718fbaf4c34f3.tar.gz
rust-578fa8e945fa7af10dddf4861da718fbaf4c34f3.zip
Add tests.
-rw-r--r--tests/ui/lifetimes/temporary-lifetime-extension-tuple-ctor.rs25
-rw-r--r--tests/ui/lifetimes/temporary-lifetime-extension-tuple-ctor.stderr19
2 files changed, 44 insertions, 0 deletions
diff --git a/tests/ui/lifetimes/temporary-lifetime-extension-tuple-ctor.rs b/tests/ui/lifetimes/temporary-lifetime-extension-tuple-ctor.rs
new file mode 100644
index 00000000000..cf60991e519
--- /dev/null
+++ b/tests/ui/lifetimes/temporary-lifetime-extension-tuple-ctor.rs
@@ -0,0 +1,25 @@
+//@ edition:2024
+
+fn temp() -> String {
+    String::from("Hello")
+}
+
+#[derive(Debug)]
+struct X<'a>(&'a String);
+
+fn main() {
+    let a = &temp();
+    let b = Some(&temp());
+    let c = Option::Some::<&String>(&temp());
+    use Option::Some as S;
+    let d = S(&temp());
+    let e = X(&temp());
+    let f = Some(Ok::<_, ()>(std::borrow::Cow::Borrowed(if true {
+        &temp()
+    } else {
+        panic!()
+    })));
+    let some = Some; // Turn the ctor into a regular function.
+    let g = some(&temp()); //~ERROR temporary value dropped while borrowe
+    println!("{a:?} {b:?} {c:?} {d:?} {e:?} {f:?} {g:?}");
+}
diff --git a/tests/ui/lifetimes/temporary-lifetime-extension-tuple-ctor.stderr b/tests/ui/lifetimes/temporary-lifetime-extension-tuple-ctor.stderr
new file mode 100644
index 00000000000..8a4b7e8ae43
--- /dev/null
+++ b/tests/ui/lifetimes/temporary-lifetime-extension-tuple-ctor.stderr
@@ -0,0 +1,19 @@
+error[E0716]: temporary value dropped while borrowed
+  --> $DIR/temporary-lifetime-extension-tuple-ctor.rs:23:19
+   |
+LL |     let g = some(&temp());
+   |                   ^^^^^^ - temporary value is freed at the end of this statement
+   |                   |
+   |                   creates a temporary value which is freed while still in use
+LL |     println!("{a:?} {b:?} {c:?} {d:?} {e:?} {f:?} {g:?}");
+   |                                                   ----- borrow later used here
+   |
+help: consider using a `let` binding to create a longer lived value
+   |
+LL ~     let binding = temp();
+LL ~     let g = some(&binding);
+   |
+
+error: aborting due to 1 previous error
+
+For more information about this error, try `rustc --explain E0716`.