rust/tests/ui/pattern/usefulness
Matthias Krüger a37fa37281
Rollup merge of #118803 - Nadrieril:min-exhaustive-patterns, r=compiler-errors
Add the `min_exhaustive_patterns` feature gate

## Motivation

Pattern-matching on empty types is tricky around unsafe code. For that reason, current stable rust conservatively requires arms for empty types in all but the simplest case. It has long been the intention to allow omitting empty arms when it's safe to do so. The [`exhaustive_patterns`](https://github.com/rust-lang/rust/issues/51085) feature allows the omission of all empty arms, but hasn't been stabilized because that was deemed dangerous around unsafe code.

## Proposal

This feature aims to stabilize an uncontroversial subset of exhaustive_patterns. Namely: when `min_exhaustive_patterns` is enabled and the data we're matching on is guaranteed to be valid by rust's operational semantics, then we allow empty arms to be omitted. E.g.:

```rust
let x: Result<T, !> = foo();
match x { // ok
    Ok(y) => ...,
}
let Ok(y) = x; // ok
```

If the place is not guaranteed to hold valid data (namely ptr dereferences, ref dereferences (conservatively) and union field accesses), then we keep stable behavior i.e. we (usually) require arms for the empty cases.

```rust
unsafe {
    let ptr: *const Result<u32, !> = ...;
    match *ptr {
        Ok(x) => { ... }
        Err(_) => { ... } // still required
    }
}
let foo: Result<u32, &!> = ...;
match foo {
    Ok(x) => { ... }
    Err(&_) => { ... } // still required because of the dereference
}
unsafe {
    let ptr: *const ! = ...;
    match *ptr {} // already allowed on stable
}
```

Note that we conservatively consider that a valid reference can point to invalid data, hence we don't allow arms of type `&!` and similar cases to be omitted. This could eventually change depending on [opsem decisions](https://github.com/rust-lang/unsafe-code-guidelines/issues/413). Whenever opsem is undecided on a case, we conservatively keep today's stable behavior.

I proposed this behavior in the [`never_patterns`](https://github.com/rust-lang/rust/issues/118155) feature gate but it makes sense on its own and could be stabilized more quickly. The two proposals nicely complement each other.

## Unresolved Questions

Part of the question is whether this requires an RFC. I'd argue this doesn't need one since there is no design question beyond the intent to omit unreachable patterns, but I'm aware the problem can be framed in ways that require design (I'm thinking of the [original never patterns proposal](https://smallcultfollowing.com/babysteps/blog/2018/08/13/never-patterns-exhaustive-matching-and-uninhabited-types-oh-my/), which would frame this behavior as "auto-nevering" happening).

EDIT: I initially proposed a future-compatibility lint as part of this feature, I don't anymore.
2024-01-26 06:36:36 +01:00
..
auxiliary Add note when matching on nested non-exhaustive enums 2023-08-28 14:50:32 +08:00
integer-ranges Only lint ranges that really overlap 2024-01-11 14:04:11 +01:00
always-inhabited-union-ref.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
always-inhabited-union-ref.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
conflicting_bindings.rs Always do all the pattern checks 2023-11-02 03:19:19 +01:00
conflicting_bindings.stderr Always do all the pattern checks 2023-11-02 03:19:19 +01:00
const-partial_eq-fallback-ice.rs remove StructuralEq trait 2024-01-24 07:56:23 +01:00
const-partial_eq-fallback-ice.stderr remove StructuralEq trait 2024-01-24 07:56:23 +01:00
const-pat-ice.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
const-private-fields.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
consts-opaque.rs remove StructuralEq trait 2024-01-24 07:56:23 +01:00
consts-opaque.stderr remove StructuralEq trait 2024-01-24 07:56:23 +01:00
deny-irrefutable-let-patterns.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deny-irrefutable-let-patterns.stderr Remove hir::Guard 2024-01-05 10:56:59 +00:00
doc-hidden-fields.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
doc-hidden-fields.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
doc-hidden-non-exhaustive.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
doc-hidden-non-exhaustive.stderr Tweak spans for "adt defined here" note 2023-11-03 18:26:16 +01:00
empty-match-check-notes.exhaustive_patterns.stderr Test empty types better 2023-12-09 00:39:59 +01:00
empty-match-check-notes.normal.stderr Test empty types better 2023-12-09 00:39:59 +01:00
empty-match-check-notes.rs Test empty types better 2023-12-09 00:39:59 +01:00
empty-match.exhaustive_patterns.stderr Test empty types better 2023-12-09 00:39:59 +01:00
empty-match.normal.stderr Test empty types better 2023-12-09 00:39:59 +01:00
empty-match.rs Test empty types better 2023-12-09 00:39:59 +01:00
empty-types.exhaustive_patterns.stderr Implement feature gate logic 2024-01-25 00:12:32 +01:00
empty-types.min_exh_pats.stderr Implement feature gate logic 2024-01-25 00:12:32 +01:00
empty-types.normal.stderr Implement feature gate logic 2024-01-25 00:12:32 +01:00
empty-types.rs Implement feature gate logic 2024-01-25 00:12:32 +01:00
floats.rs Evaluate float consts eagerly 2023-10-01 00:00:37 +02:00
floats.stderr Evaluate float consts eagerly 2023-10-01 00:00:37 +02:00
guards.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
guards.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
impl-trait.rs Reveal empty opaques in depth 2023-12-23 14:59:12 +01:00
impl-trait.stderr Reveal empty opaques in depth 2023-12-23 14:59:12 +01:00
irrefutable-let-patterns.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
irrefutable-unit.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-2111.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-2111.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
issue-3096-1.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-3096-1.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
issue-3096-2.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-3096-2.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
issue-3601.rs Keep rows with guards in the matrix 2023-11-22 03:25:15 +01:00
issue-3601.stderr Auto merge of #117611 - Nadrieril:linear-pass-take-4, r=cjgillot 2023-11-26 00:14:14 +00:00
issue-4321.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-4321.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
issue-12116.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-12116.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
issue-12369.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-12369.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
issue-13727.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-13727.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
issue-15129.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-15129.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
issue-30240-b.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-30240-b.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
issue-30240-rpass.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-30240.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-30240.stderr Improve clarity of diagnostic message on non-exhaustive matches 2023-09-03 19:55:11 +08:00
issue-31221.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-31221.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-31561.rs Migrate pattern matching 2023-01-11 14:40:02 -08:00
issue-31561.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
issue-35609.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-35609.stderr Tweak diagnostic for consistency 2023-11-02 03:19:19 +01:00
issue-39362.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-39362.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
issue-40221.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-40221.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
issue-50900.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-50900.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
issue-53820-slice-pattern-large-array.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-56379.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-56379.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
issue-57472.rs Stabilize THIR unsafeck 2024-01-05 10:00:59 +00:00
issue-57472.stderr Stabilize THIR unsafeck 2024-01-05 10:00:59 +00:00
issue-65413-constants-and-slices-exhaustiveness.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-66501.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-71930-type-of-match-scrutinee.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-72377.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-72377.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
issue-72476-and-89393-associated-type.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-78123-non-exhaustive-reference.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-78123-non-exhaustive-reference.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
issue-78549-ref-pat-and-str.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-80501-or-pat-and-macro.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-82772-match-box-as-struct.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-82772-match-box-as-struct.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
issue-85222-types-containing-non-exhaustive-types.rs Match usize/isize exhaustively 2023-10-27 19:56:12 +02:00
issue-85222-types-containing-non-exhaustive-types.stderr Remove the precise_pointer_size_matching feature gate 2023-12-04 11:56:21 +01:00
issue-88747.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-105479-str-non-exhaustiveness.rs Improve clarity of diagnostic message on non-exhaustive matches 2023-09-03 19:55:11 +08:00
issue-105479-str-non-exhaustiveness.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
issue-118437-exponential-time-on-diagonal-match.rs Improve performance on wide matches 2023-12-23 13:11:38 +01:00
issue-119493-type-error-ice.rs Abort analysis on type error 2024-01-07 22:13:08 +01:00
issue-119493-type-error-ice.stderr Bless tests 2024-01-13 12:46:58 -05:00
issue-119778-type-error-ice.rs Add test case for #119778 2024-01-10 14:50:48 +01:00
issue-119778-type-error-ice.stderr Bless tests 2024-01-13 12:46:58 -05:00
match-arm-statics-2.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
match-arm-statics-2.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
match-arm-statics.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
match-arm-statics.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
match-byte-array-patterns-2.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
match-byte-array-patterns-2.stderr Perform match checking on THIR. 2023-04-03 15:59:21 +00:00
match-byte-array-patterns.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
match-byte-array-patterns.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
match-non-exhaustive.rs add note for non-exhaustive matches with guards 2023-06-28 01:51:53 -04:00
match-non-exhaustive.stderr Keep rows with guards in the matrix 2023-11-22 03:25:15 +01:00
match-privately-empty.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
match-privately-empty.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
match-ref-ice.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
match-ref-ice.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
match-slice-patterns.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
match-slice-patterns.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
match-vec-fixed.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
match-vec-fixed.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
match-vec-unreachable.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
match-vec-unreachable.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
nested-exhaustive-match.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
nested-non-exhaustive-enums.rs Add note when matching on nested non-exhaustive enums 2023-08-28 14:50:32 +08:00
nested-non-exhaustive-enums.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
non-exhaustive-defined-here.rs Tweak spans for "adt defined here" note 2023-11-03 18:26:16 +01:00
non-exhaustive-defined-here.stderr Tweak spans for "adt defined here" note 2023-11-03 18:26:16 +01:00
non-exhaustive-match-nested.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
non-exhaustive-match-nested.stderr Tweak spans for "adt defined here" note 2023-11-03 18:26:16 +01:00
non-exhaustive-match.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
non-exhaustive-match.stderr Tweak spans for "adt defined here" note 2023-11-03 18:26:16 +01:00
non-exhaustive-pattern-witness.rs Match usize/isize exhaustively 2023-10-27 19:56:12 +02:00
non-exhaustive-pattern-witness.stderr Tweak spans for "adt defined here" note 2023-11-03 18:26:16 +01:00
refutable-pattern-errors.rs Match usize/isize exhaustively 2023-10-27 19:56:12 +02:00
refutable-pattern-errors.stderr Match usize/isize exhaustively 2023-10-27 19:56:12 +02:00
refutable-pattern-in-fn-arg.rs Match usize/isize exhaustively 2023-10-27 19:56:12 +02:00
refutable-pattern-in-fn-arg.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
slice-pattern-const-2.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
slice-pattern-const-2.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
slice-pattern-const-3.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
slice-pattern-const-3.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
slice-pattern-const.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
slice-pattern-const.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
slice-patterns-exhaustiveness.rs Keep rows with guards in the matrix 2023-11-22 03:25:15 +01:00
slice-patterns-exhaustiveness.stderr Keep rows with guards in the matrix 2023-11-22 03:25:15 +01:00
slice-patterns-irrefutable.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
slice-patterns-reachability.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
slice-patterns-reachability.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
slice_of_empty.rs Don't warn an empty pattern unreachable if we're not sure the data is valid 2023-12-09 00:44:49 +01:00
slice_of_empty.stderr Don't warn an empty pattern unreachable if we're not sure the data is valid 2023-12-09 00:44:49 +01:00
stable-gated-fields.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
stable-gated-fields.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
stable-gated-patterns.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
stable-gated-patterns.stderr Tweak spans for "adt defined here" note 2023-11-03 18:26:16 +01:00
struct-like-enum-nonexhaustive.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
struct-like-enum-nonexhaustive.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
struct-pattern-match-useless.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
struct-pattern-match-useless.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
top-level-alternation.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
top-level-alternation.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
tuple-struct-nonexhaustive.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
tuple-struct-nonexhaustive.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
type_polymorphic_byte_str_literals.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
type_polymorphic_byte_str_literals.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
uninhabited.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
unstable-gated-fields.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
unstable-gated-fields.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
unstable-gated-patterns.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
unstable-gated-patterns.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00