Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiler allows for wrong struct initialization #27391

Closed
ldanko opened this issue Jul 29, 2015 · 4 comments
Closed

Compiler allows for wrong struct initialization #27391

ldanko opened this issue Jul 29, 2015 · 4 comments

Comments

@ldanko
Copy link

ldanko commented Jul 29, 2015

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 ()
(...)
@ldanko
Copy link
Author

ldanko commented Jul 29, 2015

This may be related to #24732

@eefriedman
Copy link
Contributor

See also #26783.

@eddyb
Copy link
Member

eddyb commented Jul 30, 2015

The SIGILL is an intentional abort triggered by the stack overflow handling code.
The important part is thread '<main>' has overflowed its stack.
Even with #26783, this would only be a warning, Rust does not statically prevent infinite recursion.

@huonw
Copy link
Member

huonw commented Jul 30, 2015

Dupe of #18785.

Thanks for filing!

@huonw huonw closed this as completed Jul 30, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants