Skip to content

ICE when const evaluating UB #64506

Closed
@oli-obk

Description

@oli-obk

This ICEs on stable, probably since March 2018, but it requires the user to do UB in constants, so...

I think a possible fix would be to enable validation in const eval (cc @RalfJung ), though we should benchmark the perf considerations around that first. Alternatively we can just not ICE here and instead emit an error.

#[derive(Copy, Clone)]
pub struct ChildStdin {
    inner: AnonPipe,
}

#[derive(Copy, Clone)]
enum AnonPipe {}

const FOO: () = {
    union Foo {
        a: ChildStdin,
        b: (),
    }
    let x = unsafe { Foo { b: () }.a };
    let x = &x.inner;
};

fn main() {}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
warning: field is never used: `inner`
 --> src/main.rs:3:5
  |
3 |     inner: AnonPipe,
  |     ^^^^^^^^^^^^^^^
  |
  = note: #[warn(dead_code)] on by default

warning: constant item is never used: `FOO`
  --> src/main.rs:9:1
   |
9  | / const FOO: () = {
10 | |     union Foo {
11 | |         a: ChildStdin,
12 | |         b: (),
...  |
15 | |     let x = &x.inner;
16 | | };
   | |__^

thread 'rustc' panicked at 'Tried to access field 0 of union with 0 fields', src/librustc_mir/interpret/place.rs:360:17
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.

error: internal compiler error: unexpected panic

note: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#bug-reports

note: rustc 1.37.0 (eae3437df 2019-08-13) running on x86_64-unknown-linux-gnu

note: compiler flags: -C codegen-units=1 -C debuginfo=2 --crate-type bin

note: some of the compiler flags provided by cargo are hidden

error: Could not compile `playground`.

To learn more, run the command again with --verbose.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)C-bugCategory: This is a bug.I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions