rust/tests/mir-opt/building
bors f8463896a9 Auto merge of #150681 - meithecatte:always-discriminate, r=JonathanBrouwer,Nadrieril
Make operational semantics of pattern matching independent of crate and module

The question of "when does matching an enum against a pattern of one of its variants read its discriminant" is currently an underspecified part of the language, causing weird behavior around borrowck, drop order, and UB.

Of course, in the common cases, the discriminant must be read to distinguish the variant of the enum, but currently the following exceptions are implemented:

1. If the enum has only one variant, we currently skip the discriminant read.
     - This has the advantage that single-variant enums behave the same way as structs in this regard.
     - However, it means that if the discriminant exists in the layout, we can't say that this discriminant being invalid is UB. This makes me particularly uneasy in its interactions with niches – consider the following example ([playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=5904a6155cbdd39af4a2e7b1d32a9b1a)), where miri currently doesn't detect any UB (because the semantics don't specify any):

        <details><summary>Example 1</summary>

        ```rust
        #![allow(dead_code)]
        use core::mem::{size_of, transmute};
        
        #[repr(u8)]
        enum Inner {
            X(u8),
        }
        
        enum Outer {
            A(Inner),
            B(u8),
        }
        
        fn f(x: &Inner) {
            match x {
                Inner::X(v) => {
                    println!("{v}");
                }
            }
        }
        
        fn main() {
            assert_eq!(size_of::<Inner>(), 2);
            assert_eq!(size_of::<Outer>(), 2);
            let x = Outer::B(42);
            let y = &x;
            f(unsafe { transmute(y) });
        }
        ```

      </details>

2. For the purpose of the above, enums with marked with `#[non_exhaustive]` are always considered to have multiple variants when observed from foreign crates, but the actual number of variants is considered in the current crate.
    - This means that whether code has UB can depend on which crate it is in: https://github.com/rust-lang/rust/issues/147722
    - In another case of `#[non_exhaustive]` affecting the runtime semantics, its presence or absence can change what gets captured by a closure, and by extension, the drop order: https://github.com/rust-lang/rust/issues/147722#issuecomment-3674554872
    - Also at the above link, there is an example where removing `#[non_exhaustive]` can cause borrowck to suddenly start failing in another crate.
3. Moreover, we currently make a more specific check: we only read the discriminant if there is more than one *inhabited* variant in the enum.
    - This means that the semantics can differ between `foo<!>`, and a copy of `foo` where `T` was manually replaced with `!`: rust-lang/rust#146803
    - Moreover, due to the privacy rules for inhabitedness, it means that the semantics of code can depend on the *module* in which it is located.
    - Additionally, this inhabitedness rule is even uglier due to the fact that closure capture analysis needs to happen before we can determine whether types are uninhabited, which means that whether the discriminant read happens has a different answer specifically for capture analysis.
    - For the two above points, see the following example ([playground](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=a07d8a3ec0b31953942e96e2130476d9)):

        <details><summary>Example 2</summary>

        ```rust
        #![allow(unused)]
        
        mod foo {
            enum Never {}
            struct PrivatelyUninhabited(Never);
            pub enum A {
                V(String, String),
                Y(PrivatelyUninhabited),
            }
            
            fn works(mut x: A) {
                let a = match x {
                    A::V(ref mut a, _) => a,
                    _ => unreachable!(),
                };
                
                let b = match x {
                    A::V(_, ref mut b) => b,
                    _ => unreachable!(),
                };
            
                a.len(); b.len();
            }
            
            fn fails(mut x: A) {
                let mut f = || match x {
                    A::V(ref mut a, _) => (),
                    _ => unreachable!(),
                };
                
                let mut g = || match x {
                    A::V(_, ref mut b) => (),
                    _ => unreachable!(),
                };
            
                f(); g();
            }
        }
        
        use foo::A;
        
        fn fails(mut x: A) {
            let a = match x {
                A::V(ref mut a, _) => a,
                _ => unreachable!(),
            };
            
            let b = match x {
                A::V(_, ref mut b) => b,
                _ => unreachable!(),
            };
        
            a.len(); b.len();
        }
        
        
        fn fails2(mut x: A) {
            let mut f = || match x {
                A::V(ref mut a, _) => (),
                _ => unreachable!(),
            };
            
            let mut g = || match x {
                A::V(_, ref mut b) => (),
                _ => unreachable!(),
            };
        
            f(); g();
        }
        ```

        </details>

In light of the above, and following the discussion at rust-lang/rust#138961 and rust-lang/rust#147722, this PR ~~makes it so that, operationally, matching on an enum *always* reads its discriminant.~~ introduces the following changes to this behavior:

 - matching on a `#[non_exhaustive]` enum will always introduce a discriminant read, regardless of whether the enum is from an external crate
 - uninhabited variants now count just like normal ones, and don't get skipped in the checks

As per the discussion below, the resolution for point (1) above is that it should land as part of a separate PR, so that the subtler decision can be more carefully considered.

Note that this is a breaking change, due to the aforementioned changes in borrow checking behavior, new UB (or at least UB newly detected by miri), as well as drop order around closure captures. However, it seems to me that the combination of this PR with rust-lang/rust#138961 should have smaller real-world impact than rust-lang/rust#138961 by itself.

Fixes rust-lang/rust#142394 
Fixes rust-lang/rust#146590
Fixes rust-lang/rust#146803 (though already marked as duplicate)
Fixes parts of rust-lang/rust#147722
Fixes rust-lang/miri#4778

r? @Nadrieril @RalfJung 

@rustbot label +A-closures +A-patterns +T-opsem +T-lang
2026-02-14 12:53:09 +00:00
..
custom Auto merge of #145513 - beepster4096:erasedereftemps, r=saethlin,cjgillot 2025-10-12 02:34:20 +00:00
match Auto merge of #150681 - meithecatte:always-discriminate, r=JonathanBrouwer,Nadrieril 2026-02-14 12:53:09 +00:00
async_await.a-{closure#0}.coroutine_resume.0.mir Only load pin field once. 2025-10-24 02:41:50 +00:00
async_await.b-{closure#0}.coroutine_resume.0.mir Only load pin field once. 2025-10-24 02:41:50 +00:00
async_await.rs Use -Zmir-opt-level=0 in tests for MIR building 2025-08-26 23:42:38 -04:00
coroutine.main-{closure#0}.StateTransform.after.mir Only load pin field once. 2025-10-24 02:41:50 +00:00
coroutine.main-{closure#1}.StateTransform.after.mir Only load pin field once. 2025-10-24 02:41:50 +00:00
coroutine.rs Do not renumber resume local. 2025-09-16 22:50:32 +00:00
dump_mir_cycle.rs Use -Zmir-opt-level=0 in tests for MIR building 2025-08-26 23:42:38 -04:00
enum_cast.bar.built.after.mir MIR-build: No longer emit assumes in enum-as casting 2025-07-24 10:18:37 -07:00
enum_cast.boo.built.after.mir MIR-build: No longer emit assumes in enum-as casting 2025-07-24 10:18:37 -07:00
enum_cast.far.built.after.mir MIR-build: No longer emit assumes in enum-as casting 2025-07-24 10:18:37 -07:00
enum_cast.foo.built.after.mir Remove comments from mir-opt MIR dumps 2023-06-15 15:19:11 -04:00
enum_cast.offsetty.built.after.mir MIR-build: No longer emit assumes in enum-as casting 2025-07-24 10:18:37 -07:00
enum_cast.rs Use -Zmir-opt-level=0 in tests for MIR building 2025-08-26 23:42:38 -04:00
enum_cast.signy.built.after.mir MIR-build: No longer emit assumes in enum-as casting 2025-07-24 10:18:37 -07:00
enum_cast.unsigny.built.after.mir Remove comments from mir-opt MIR dumps 2023-06-15 15:19:11 -04:00
eq_never_type._f.built.after.mir Bless *all* the mir-opt tests 2024-08-18 16:07:33 -07:00
eq_never_type.rs Use -Zmir-opt-level=0 in tests for MIR building 2025-08-26 23:42:38 -04:00
index_array_and_slice.index_array.built.after.mir Reapply "Auto merge of #133734 - scottmcm:lower-indexing-to-ptrmetadata, r=davidtwco,RalfJung" 2025-01-27 23:42:47 +00:00
index_array_and_slice.index_const_generic_array.built.after.mir Reapply "Auto merge of #133734 - scottmcm:lower-indexing-to-ptrmetadata, r=davidtwco,RalfJung" 2025-01-27 23:42:47 +00:00
index_array_and_slice.index_custom.built.after.mir Represent the raw pointer for a array length check as a new kind of fake borrow 2025-01-28 00:00:33 +00:00
index_array_and_slice.index_mut_slice.built.after.mir Represent the raw pointer for a array length check as a new kind of fake borrow 2025-01-28 00:00:33 +00:00
index_array_and_slice.index_slice.built.after.mir Reapply "Auto merge of #133734 - scottmcm:lower-indexing-to-ptrmetadata, r=davidtwco,RalfJung" 2025-01-27 23:42:47 +00:00
index_array_and_slice.rs Use -Zmir-opt-level=0 in tests for MIR building 2025-08-26 23:42:38 -04:00
issue_49232.main.built.after.mir Bless *all* the mir-opt tests 2024-08-18 16:07:33 -07:00
issue_49232.rs Use -Zmir-opt-level=0 in tests for MIR building 2025-08-26 23:42:38 -04:00
issue_101867.main.built.after.mir Ignore #[doc(hidden)] items when computing trimmed paths 2026-01-19 12:27:27 +11:00
issue_101867.rs Use -Zmir-opt-level=0 in tests for MIR building 2025-08-26 23:42:38 -04:00
issue_110508.rs Use -Zmir-opt-level=0 in tests for MIR building 2025-08-26 23:42:38 -04:00
issue_110508.{impl#0}-BAR.built.after.mir Use -Zmir-opt-level=0 in tests for MIR building 2025-08-26 23:42:38 -04:00
issue_110508.{impl#0}-SELF_BAR.built.after.mir Use -Zmir-opt-level=0 in tests for MIR building 2025-08-26 23:42:38 -04:00
logical_or_in_conditional.rs Use -Zmir-opt-level=0 in tests for MIR building 2025-08-26 23:42:38 -04:00
logical_or_in_conditional.test_complex.built.after.mir Remove let_chains feature gate from even more tests 2025-04-18 15:57:29 +02:00
logical_or_in_conditional.test_or.built.after.mir Bless *all* the mir-opt tests 2024-08-18 16:07:33 -07:00
loop_match_diverges.break_to_block_unit.built.after.mir add place mention for #[loop_match] scrutinee 2025-08-10 14:25:49 +02:00
loop_match_diverges.infinite_a.built.after.mir add place mention for #[loop_match] scrutinee 2025-08-10 14:25:49 +02:00
loop_match_diverges.rs add place mention for #[loop_match] scrutinee 2025-08-10 14:25:49 +02:00
loop_match_diverges.simple.built.after.mir add place mention for #[loop_match] scrutinee 2025-08-10 14:25:49 +02:00
offset_of.rs Replace OffsetOf by an actual sum. 2025-11-18 00:10:03 +00:00
receiver_ptr_mutability.main.built.after.mir Use the name var_kinds more. 2026-01-08 13:37:34 +11:00
receiver_ptr_mutability.rs Use -Zmir-opt-level=0 in tests for MIR building 2025-08-26 23:42:38 -04:00
shifts.rs Use -Zmir-opt-level=0 in tests for MIR building 2025-08-26 23:42:38 -04:00
shifts.shift_signed.built.after.mir Bless *all* the mir-opt tests 2024-08-18 16:07:33 -07:00
shifts.shift_unsigned.built.after.mir Bless *all* the mir-opt tests 2024-08-18 16:07:33 -07:00
storage_live_dead_in_statics.rs Use -Zmir-opt-level=0 in tests for MIR building 2025-08-26 23:42:38 -04:00
storage_live_dead_in_statics.XXX.built.after.mir Give an AllocId to ConstValue::Slice. 2025-07-23 23:54:37 +00:00
uniform_array_move_out.move_out_by_subslice.built.after.mir Replace NullOp::SizeOf and NullOp::AlignOf by lang items. 2025-10-23 00:38:28 +00:00
uniform_array_move_out.move_out_from_end.built.after.mir Replace NullOp::SizeOf and NullOp::AlignOf by lang items. 2025-10-23 00:38:28 +00:00
uniform_array_move_out.rs Use -Zmir-opt-level=0 in tests for MIR building 2025-08-26 23:42:38 -04:00
user_type_annotations.let_else.built.after.mir Use the name var_kinds more. 2026-01-08 13:37:34 +11:00
user_type_annotations.let_else_bindless.built.after.mir Use the name var_kinds more. 2026-01-08 13:37:34 +11:00
user_type_annotations.let_init.built.after.mir Use the name var_kinds more. 2026-01-08 13:37:34 +11:00
user_type_annotations.let_init_bindless.built.after.mir Use the name var_kinds more. 2026-01-08 13:37:34 +11:00
user_type_annotations.let_uninit.built.after.mir Use the name var_kinds more. 2026-01-08 13:37:34 +11:00
user_type_annotations.let_uninit_bindless.built.after.mir Use the name var_kinds more. 2026-01-08 13:37:34 +11:00
user_type_annotations.match_assoc_const.built.after.mir Use the name var_kinds more. 2026-01-08 13:37:34 +11:00
user_type_annotations.match_assoc_const_range.built.after.mir Use the name var_kinds more. 2026-01-08 13:37:34 +11:00
user_type_annotations.rs Use -Zmir-opt-level=0 in tests for MIR building 2025-08-26 23:42:38 -04:00
while_storage.rs Update mir-opt filechecks 2024-08-18 15:52:23 -07:00
while_storage.while_loop.PreCodegen.after.panic-abort.mir Bless *all* the mir-opt tests 2024-08-18 16:07:33 -07:00
while_storage.while_loop.PreCodegen.after.panic-unwind.mir Bless *all* the mir-opt tests 2024-08-18 16:07:33 -07:00