Skip to content

ICE with match statement on a struct with const fn constructor #46114

Closed
@GabrielMajeri

Description

@GabrielMajeri

Consider a simple struct which can be created at compile time (used in consts):

#[derive(Eq, PartialEq)]
struct A { value: u32 }

const fn new(value: u32) -> A {
  A { value }
}

I then defined some named constants:

const A_1: A = new(1);
const A_2: A = new(2);

Later on, I tried to use match to convert a variable of type A to a string:

let a_str = match a {
   A_1 => "A 1",
   A_2 => "A 2",
   _ => "Unknown A",
};

This leads to the following compiler crash:

error: internal compiler error: /checkout/src/librustc_const_eval/_match.rs:261: bad constructor ConstantValue(Const { ty: A, val: Aggregate(Struct([(value(91), Const { ty: u32, val: Integral(U32(1)) })])) }) for adt A

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.23.0-nightly (5041b3bb3 2017-11-19) running on x86_64-unknown-linux-gnu

thread 'rustc' panicked at 'Box<Any>', /checkout/src/librustc_errors/lib.rs:471:8
note: Run with `RUST_BACKTRACE=1` for a backtrace.

Also uploaded the sample code to the Rust Playground.

As a side-note: it does not crash if I manually construct the structs, i.e.

const A_1: A = A { value: 1 };
const A_2: A = A { value: 2 };

Alternatively, it also works if I replace the match with if / else chains.

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.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions