rust/compiler/rustc_pattern_analysis/src
Matthias Krüger e7504ac704
Rollup merge of #128934 - Nadrieril:fix-empty-non-exhaustive, r=compiler-errors
Non-exhaustive structs may be empty

This is a follow-up to a discrepancy noticed in https://github.com/rust-lang/rust/pull/122792: today, the following struct is considered inhabited (non-empty) outside its defining crate:
```rust
#[non_exhaustive]
pub struct UninhabitedStruct {
    pub never: !,
    // other fields
}
```

`#[non_exhaustive]` on a struct should mean that adding fields to it isn't a breaking change. There is no way that adding fields to this struct could make it non-empty since the `never` field must stay and is inconstructible. I suspect this was implemented this way due to confusion with `#[non_exhaustive]` enums, which indeed should be considered non-empty outside their defining crate.

I propose that we consider such a struct uninhabited (empty), just like it would be without the `#[non_exhaustive]` annotation.

Code that doesn't pass today and will pass after this:
```rust
// In a different crate
fn empty_match_on_empty_struct<T>(x: UninhabitedStruct) -> T {
    match x {}
}
```

This is not a breaking change.

r? ``@compiler-errors``
2024-09-03 19:13:24 +02:00
..
rustc Remove print::Pat entirely, replacing it with String 2024-08-11 20:25:03 +10:00
constructor.rs chore: Fix typos in 'compiler' (batch 2) 2024-09-02 07:50:22 +02:00
errors.rs Encapsulate the printing of WitnessPat 2024-07-29 14:56:50 +10:00
lib.rs Add warn(unreachable_pub) to rustc_pattern_analysis. 2024-08-29 20:18:44 +10:00
lints.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
pat_column.rs Rename TypeCx -> PatCx 2024-03-13 13:56:38 +01:00
pat.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
rustc.rs Non-exhaustive structs may be empty 2024-09-02 21:16:37 +02:00
usefulness.rs Stabilize min_exhaustive_patterns 2024-08-10 12:07:17 +02:00