Skip to content

Creating a struct that includes a self-referential type causes an internal compiler error #4548

Closed
@martica

Description

@martica

Creating a struct that includes a self-referential type causes an internal compiler error. It clearly needs to be boxed, but hopefully there could be an error message instead. This appears to be similar to #742, the closed ticket for a similar issues with enums (nee tags).

Starting with a naïve implementation such as:

struct RecursiveType {
    self_reference: RecursiveType
}

You get:

crash.rs:3:0: 5:1 error: this type cannot be instantiated without an instance of itself; consider using `option<RecursiveType>`
crash.rs:3 struct RecursiveType {
crash.rs:4     self_reference: RecursiveType
crash.rs:5 }

Switching to option doesn't work, as it should be Option:

crash.rs:4:20: 4:41 error: found type name used as a variable
crash.rs:4     self_reference: option<RecursiveType>
                               ^~~~~~~~~~~~~~~~~~~~~

Switching to Option triggers the error:

struct RecursiveType {
    self_reference: Option<RecursiveType>
}
$ RUST_LOG=rustc=1,::rt::backtrace rustc crash.rs
rust: task 7fa823d01170 ran out of stack
error: internal compiler error: unexpected failure
note: the compiler hit an unexpected failure path. this is a bug
note: try running with RUST_LOG=rustc=1,::rt::backtrace to get further details and report the results to github.com/mozilla/rust/issues
rust: task failed at 'explicit failure', /private/tmp/rust-3z40/rust-0.5/src/librustc/rustc.rc:425
rust: domain main @0x7fa824800010 root task failed
rust: task failed at 'killed', /private/tmp/rust-3z40/rust-0.5/src/libcore/task/mod.rs:570

I've tested this in the released 0.5 and the current master branch version of 0.6.

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