Change:
<ins> - // MIR for `main` before ConstProp + // MIR for `main` after ConstProp fn main() -> () { let mut _0: ();                      // return place in scope 0 at $DIR/invalid_constant.rs:15:11: 15:11 let _1: std::option::Option<()>;     // in scope 0 at $DIR/invalid_constant.rs:16:5: 16:12 let mut _2: std::option::Option<std::option::Option<()>>; // in scope 0 at $DIR/invalid_constant.rs:16:7: 16:11 scope 1 (inlined f) {                // at $DIR/invalid_constant.rs:16:5: 16:12 debug x => _2;                   // in scope 1 at $DIR/invalid_constant.rs:16:5: 16:12 let mut _3: isize;               // in scope 1 at $DIR/invalid_constant.rs:16:5: 16:12 let _4: std::option::Option<()>; // in scope 1 at $DIR/invalid_constant.rs:16:5: 16:12 scope 2 { debug y => _4;               // in scope 2 at $DIR/invalid_constant.rs:16:5: 16:12 } } bb0: { discriminant(_2) = 0;            // scope 0 at $DIR/invalid_constant.rs:16:7: 16:11 -         _3 = discriminant(_2);           // scope 1 at $DIR/invalid_constant.rs:16:5: 16:12 -         switchInt(move _3) -> [0_isize: bb3, otherwise: bb2]; // scope 1 at $DIR/invalid_constant.rs:16:5: 16:12 +         _3 = const 0_isize;              // scope 1 at $DIR/invalid_constant.rs:16:5: 16:12 +         switchInt(const 0_isize) -> [0_isize: bb3, otherwise: bb2]; // scope 1 at $DIR/invalid_constant.rs:16:5: 16:12 } bb1: { nop;                             // scope 0 at $DIR/invalid_constant.rs:15:11: 17:2 return;                          // scope 0 at $DIR/invalid_constant.rs:17:2: 17:2 } bb2: { -         _4 = ((_2 as Some).0: std::option::Option<()>); // scope 1 at $DIR/invalid_constant.rs:16:5: 16:12 -         _1 = _4;                         // scope 2 at $DIR/invalid_constant.rs:16:5: 16:12 +         _4 = const Scalar(0x02): Option::<()>; // scope 1 at $DIR/invalid_constant.rs:16:5: 16:12 +                                          // ty::Const +                                          // + ty: std::option::Option<()> +                                          // + val: Value(Scalar(0x02)) +                                          // mir::Constant +                                          // + span: $DIR/invalid_constant.rs:16:5: 16:12 +                                          // + literal: Const { ty: std::option::Option<()>, val: Value(Scalar(0x02)) } +         _1 = const Scalar(0x02): Option::<()>; // scope 2 at $DIR/invalid_constant.rs:16:5: 16:12 +                                          // ty::Const +                                          // + ty: std::option::Option<()> +                                          // + val: Value(Scalar(0x02)) +                                          // mir::Constant +                                          // + span: $DIR/invalid_constant.rs:16:5: 16:12 +                                          // + literal: Const { ty: std::option::Option<()>, val: Value(Scalar(0x02)) } goto -> bb1;                     // scope 0 at $DIR/invalid_constant.rs:10:20: 10:21 } bb3: { discriminant(_1) = 0;            // scope 1 at $DIR/invalid_constant.rs:16:5: 16:12 goto -> bb1;                     // scope 0 at $DIR/invalid_constant.rs:9:17: 9:21 } } </ins>

Discussion:
<!-- Thank you for finding an Internal Compiler Error! If possible, try to provide a minimal verifiable example. You can read "Rust Bug Minimization Patterns" for how to create smaller examples. --<!-- If you're using the stable version of the compiler, you should also check if the bug also exists in the beta or nightly versions. --: <!-- Include a backtrace in the code block by setting in your environment. E.g. . --<details<summary<strongBacktrace</strong</summary<p</p</details
For the record, it is due to and doesn't require (but does require that flag, and only happens during ) I've tracked this down (possibly) to which is only enabled when opt-level = 3. I might take a look at this..
So actually this doesn't even depend on . I'm not sure which mir pass is causing this actually, but only happens when we match which is inlined in a match block (not in a separate statement). The variable binding for the case is also important, and the issue seems to be a discriminant value of being produced somewhere... minimized (ICEs with ):
Yeah so it just seems like we're creating invalid scalar values as intermediates in optimization passes (e.g. after const propagation), and then panicking when we pretty-print those invalid values when dumping MIR. I can give a more detailed explanation why we're creating an invalid scalar, but that scalar is being optimized out anyways in subsequent optimization passes, so I don't think this would ever affect codegen... So not sure how scary this ICE is in practice. I would claim this issue, but I don't know what the best fix for this would be. Perhaps making return a instead of a value...? Not sure who to ask -- or perhaps either of y'all have an opinion on this? <details<summary<breason why we're creating an invalid scalar</b</summaryvalue is const-evaluated into the scalar value . we generate the MIR branch for , we emit the MIR expression where is that scalar value above. const-propagation, we try to inline (that is, scalar value ) into all places that reference it, even those that are invalid, such as the unreachable branch. , we end up basically calculating . This ends up creating an with an invalid scalar value of (we only expect the values and ).

then try to pretty print that value by getting its discriminant and values, but we are unable to tell what the discriminant of an with an invalid scalar value is. So we ICE. </details
An optimization pass that introduces invalid scalars literally introduces UB into well-defined code (unless this is in dead code), so -- I would say this is rather scary, on the level of "I-unsound".
Fairly certain this only happens in unreachable branches (default branches of exhaustive switches), but i'll dig into it.
Sorry, yes, it only seems to happen (as far as I know) in dead branches. The issue here only seems to be printing MIR with these dead branches still in the code.
Okay that is less critical then. Arguably printing should work even for invalid code. It still seems interesting to figure out why this happens. I would understand if this was an uninhabited variant, but seeing an entirely non-existing discriminant is surprising.
I left an explanation above in an collapsible block as to why we're creating this invalid value. I believe the root cause is that we perform operations in const propagation infallibly. It would probably be desirable to prevent this by making const propagation fallible if the const is incompatible with the layout of the result of the operation being applied to it. Then when we apply something like when to access the field of the variant on a dead branch, we don't create an invalid value.
Okay so it looks like possibly we should adjust the "downcast" projection(s) to verify that the place has the right variant, and otherwise? Then evaluating would fail and ConstProp would not touch that code, I think.
yup that sounds correct. do you want me to make that change?
One thing I am wondering about is if this will work correctly for generators -- the have more complicated variants than enums do, and maybe there are situations there where projections to the "wrong" variant are actually okay? Cc
are you proposing to declare downcast to an inactive variant to be undefined behaviour? This is probably larger change, in a sense that it would be incompatible with how downcast is currently used: In generators each field is accessed through a single representative variant, regardless of the current variant. Aggregate enum assignments are lowered to a field-wise assignments (using downcast) with a discriminant set last.
Yes, basically.

But it seems like that plan won't work. Interesting. I should probably add a comment in the code explaining why this is not currently considered UB. Well then it seems the best we can do for this ICE is to make the printing code more robust. It is anyway a good idea to be able to print invalid values; otherwise, debugging invalid values can be quite frustrating.