Skip to content

Casting a reference to a trait object reference loses lifetime information #12781

Closed
@Aatch

Description

@Aatch

Best I can do, but it's self-contained:

trait FooTrait {
    fn do_thing(&self) -> ~str;
}
struct Container<'a> {
    things : ~[&'a FooTrait]
}

impl<'a> Container<'a> {
    fn new() -> Container {
        Container { things: ~[] }
    }
    fn add(&mut self, thing: &'a FooTrait) {
        self.things.push(thing);
    }
}

struct Foo;

impl FooTrait for Foo {
    fn do_thing(&self) -> ~str {
        ~"Test"
    }
}

fn make_foo() -> Foo {
    Foo
}

fn main() {
    let mut c = Container::new();

    for _ in range(0, 10) {
        let foo : Foo = make_foo();
        let foop : &Foo = &foo;
        let foo_obj = foop as &FooTrait;
        c.add(foo_obj);
    }

}

This compiles. It shouldn't, since foo doesn't live for long enough. If I don't do the cast (this also happens with coercion), it fails with the expected error message.

cc @nikomatsakis

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lifetimesArea: Lifetimes / regionsA-trait-systemArea: Trait systemI-crashIssue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions