about summary refs log tree commit diff
path: root/tests/ui/traits/trait-object-lifetime-default-note.rs
diff options
context:
space:
mode:
authorNilstrieb <48135649+Nilstrieb@users.noreply.github.com>2023-11-12 13:48:47 +0100
committerNilstrieb <48135649+Nilstrieb@users.noreply.github.com>2023-11-12 13:51:16 +0100
commite5c330ac48b9524e42d872c5ca0e231622d42bee (patch)
treed6f8010f6d920fdd8462cb6afcc812fc32ca8a71 /tests/ui/traits/trait-object-lifetime-default-note.rs
parented086d86b8b224f7df2da09cf48ac2a654bf841e (diff)
downloadrust-e5c330ac48b9524e42d872c5ca0e231622d42bee.tar.gz
rust-e5c330ac48b9524e42d872c5ca0e231622d42bee.zip
Note about object lifetime defaults in does not live long enough error
This is a aspect of Rust that frequently trips up people who are not
aware of it yet. This diagnostic attempts to explain what's happening
and why the lifetime constraint, that was never mentioned in the source,
arose.
Diffstat (limited to 'tests/ui/traits/trait-object-lifetime-default-note.rs')
-rw-r--r--tests/ui/traits/trait-object-lifetime-default-note.rs16
1 files changed, 16 insertions, 0 deletions
diff --git a/tests/ui/traits/trait-object-lifetime-default-note.rs b/tests/ui/traits/trait-object-lifetime-default-note.rs
new file mode 100644
index 00000000000..31e3eb4ba41
--- /dev/null
+++ b/tests/ui/traits/trait-object-lifetime-default-note.rs
@@ -0,0 +1,16 @@
+trait A {}
+
+impl<T> A for T {}
+
+fn main() {
+    let local = 0; //~ NOTE binding `local` declared here
+    let r = &local; //~ ERROR `local` does not live long enough
+    //~| NOTE borrowed value does not live long enough
+    //~| NOTE due to object lifetime defaults, `Box<dyn A>` actually means `Box<(dyn A + 'static)>`
+    require_box(Box::new(r));
+    //~^ NOTE cast requires that `local` is borrowed for `'static`
+
+    let _ = 0;
+} //~ NOTE `local` dropped here while still borrowed
+
+fn require_box(_a: Box<dyn A>) {}