Skip to content

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

Closed
@Xirdus

Description

@Xirdus

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions