mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-12 00:24:03 +00:00
e5c330ac48
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.
17 lines
522 B
Rust
17 lines
522 B
Rust
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>) {}
|