Skip to content

cast to &Trait causes code to segfault on method call #5008

Closed
@mitsuhiko

Description

@mitsuhiko
trait Debuggable {
    fn debug_name(&self) -> ~str;
}

struct Thing {
    name: ~str,
}

impl Thing {
    static pure fn new() -> Thing { Thing { name: ~"dummy" } }
}

impl Debuggable for Thing {
    fn debug_name(&self) -> ~str { copy self.name }
}

fn print_name(x: &Debuggable)
{
    io::println(fmt!("debug_name = %s", x.debug_name()));
}

fn main() {
    let thing = Thing::new();
    print_name(thing as &Debuggable);
}

This code segfaults with this traceback:

#0  0x0000000100000e4b in glue_take_1991 ()
#1  0x0000000100414680 in ?? ()
#2  0x0000000100000e03 in __extensions__::meth_1987::debug_name::_8b22bed097777cf7::_00 ()
#3  0x000000010000113c in print_name::_e4762c9c8d3caa0::_00 ()
#4  0x0000000100001a49 in __morestack ()
#5  0x000000010000106c in print_name::_e4762c9c8d3caa0::_00 ()
#6  0x00000001000016ee in _rust_main ()
#7  0x00000001002e4805 in task_start_wrapper (a=0x10081c628) at rust_task.cpp:162

It fixes itself if the &Trait is changed to @Trait.

Metadata

Metadata

Assignees

No one assigned

    Labels

    E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions