-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
8 changed files
with
132 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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>) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
error[E0597]: `local` does not live long enough | ||
--> $DIR/trait-object-lifetime-default-note.rs:7:13 | ||
| | ||
LL | let local = 0; | ||
| ----- binding `local` declared here | ||
LL | let r = &local; | ||
| ^^^^^^ borrowed value does not live long enough | ||
... | ||
LL | require_box(Box::new(r)); | ||
| ----------- cast requires that `local` is borrowed for `'static` | ||
... | ||
LL | } | ||
| - `local` dropped here while still borrowed | ||
| | ||
= note: due to object lifetime defaults, `Box<dyn A>` actually means `Box<(dyn A + 'static)>` | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0597`. |