Skip to content

Compiler allows for wrong struct initialization #27391

Closed
@ldanko

Description

@ldanko

By mistake I used recursively Default::default() inside Default::default() implementation.
Compilation suceeded, but program crashed on illegal instruction.

I tried this code:

struct A {
    x: i32,
}

impl Default for A {
    fn default() -> A {
        A {
            x: 1
        }
    }
}

struct B {
    x: i32,
    a: A
}

impl Default for B {
    fn default() -> B {
        B {
            x: 5,
            ..Default::default()
        }
    }
}

fn main() {
    let _b = B {..Default::default()};

}

I expected to see this happen: compiler error

Instead, this happened: compilation succeeded, program crashes (illegal instruction)

Meta

rustc --version --verbose:

rustc 1.1.0 (35ceea399 2015-06-19)
binary: rustc
commit-hash: 35ceea3997c79a3b7562e89b462ab76af5b86b22
commit-date: 2015-06-19
host: x86_64-unknown-linux-gnu
release: 1.1.0

Backtrace:

thread '<main>' has overflowed its stack

Program received signal SIGILL, Illegal instruction.
0x000055555555b97e in rust_stack_exhausted ()
(gdb) bt
#0  0x000055555555b97e in rust_stack_exhausted ()
#1  0x00005555555590c1 in __morestack ()
#2  0x000055555555900b in B.Default::default::h3de843a3f3bb0910zaa ()
#3  0x000055555555900b in B.Default::default::h3de843a3f3bb0910zaa ()
#4  0x000055555555900b in B.Default::default::h3de843a3f3bb0910zaa ()
#5  0x000055555555900b in B.Default::default::h3de843a3f3bb0910zaa ()
#6  0x000055555555900b in B.Default::default::h3de843a3f3bb0910zaa ()
#7  0x000055555555900b in B.Default::default::h3de843a3f3bb0910zaa ()
(...)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions