Skip to content

Rust 0.12.0 doesn't detect dangling reference inside struct when upcasting #16142

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
Xirdus opened this issue Jul 31, 2014 · 1 comment
Closed
Labels
A-type-system Area: Type system

Comments

@Xirdus
Copy link

Xirdus commented Jul 31, 2014

Minimal example:

use std::string::String;

trait T {
    fn get(&self) -> &String;
}

struct S<'a> {
    data: &'a String
}

impl<'a> T for S<'a> {
    fn get(&self) -> &String {
        return self.data;
    }
}

fn get_s() -> Box<T> {
    let s = String::from_str("Hello world");
    return box S{data: &s} as Box<T>;
}

fn main() {
    println!("{}", get_s().get());
}

get_s() returns a struct S upcasted to T which contains a reference to local variable which gets destroyed. This compiles fine on nightly, 0.11.0 complains about conflicting lifetime requirements. Removing upcast makes nightly complain too (about missing lifetime specifier).

Possibly related: #12781, #14868 and others.

@alexcrichton
Copy link
Member

Closing as a dupe of #5723, the lifetime is getting lost in the cast to the trait object.

bors pushed a commit to rust-lang-ci/rust that referenced this issue Jan 3, 2024
bors added a commit to rust-lang-ci/rust that referenced this issue Jan 3, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-type-system Area: Type system
Projects
None yet
Development

No branches or pull requests

3 participants