Skip to content

Segfault when returning a boxed struct as a trait object from a match #20097

Closed
@shepmaster

Description

@shepmaster
trait Thing {
    fn hello(&self) -> uint;
}

struct Thing1 { name: String }
impl Thing for Thing1 {
    fn hello(&self) -> uint { 1 }
}

struct Thing2 { name: String }
impl Thing for Thing2 {
    fn hello(&self) -> uint { 2 }
}

fn main() {
    let t: Box<Thing> = match true {
        true => box Thing1 { name: "hello".to_string() },
        _    => box Thing2 { name: "hello".to_string() },
    };
    println!("{}", t.hello());
}

Running this code yields various failures (SIGSEGV, "Illegal instruction: 4"):

$ rustc s.rs
# Warnings snipped
$ ./s
Illegal instruction: 4

LLDB has this to say:

(lldb) bt
* thread #1: tid = 0x821935, 0x0000000100003358 s`Box$LT$Thing$GT$::glue_drop.1635::ha5cee14c6e595b4f + 72, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
  * frame #0: 0x0000000100003358 s`Box$LT$Thing$GT$::glue_drop.1635::ha5cee14c6e595b4f + 72
    frame #1: 0x00000001000011f8 s`main::hb72fec937d964a29Zaa + 680
    frame #2: 0x000000010000f2d9 s`rust_try_inner + 9
    frame #3: 0x000000010000f2c6 s`rust_try + 6
    frame #4: 0x000000010000e1df s`rt::lang_start::h270935babb5b337fRCz + 831
    frame #5: 0x00000001000013bf s`main + 79
    frame #6: 0x00007fff92d9b5c9 libdyld.dylib`start + 1

and I'm reasonably up-to-date:

$ rustc --version=verbose
rustc 0.13.0-dev (1c2df5cc3 2014-12-20 01:12:19 +0000)
binary: rustc
commit-hash: 1c2df5cc3cfc0c9e80adf9fa6504d55056741c5a
commit-date: 2014-12-20 01:12:19 +0000
host: x86_64-apple-darwin
release: 0.13.0-dev

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-codegenArea: Code generationI-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