-
-
Notifications
You must be signed in to change notification settings - Fork 14.6k
Description
This is a tracking issue for the experimental "never patterns" feature (as per the T-lang experimental feature process).
The feature gate for the issue is #![feature(never_patterns)].
RFC: rust-lang/rfcs#3719
This feature introduces a new syntax for patterns: !. In a pattern, ! is valid for any uninhabited type and simply acknowledges that the type is empty. Because a never pattern is statically known to be unreachable, it obeys slightly different rules:
enum Void {}
let result: *const Result<u32, Void> = ...;
unsafe {
match *result {
Ok(x) => { foo(x) }
Err(!), // no need for `=> <expr>`
}
let (Ok(x) | Err(!)) = *result; // valid
}
fn blah(!: Void) -> SomeType {} // the function can never be called so it doesn't need a bodyThis feature does not affect exhaustiveness or pattern reachability; see the exhaustive_patterns and min_exhaustive_patterns features for that.
About tracking issues
Tracking issues are used to record the overall progress of implementation.
They are also used as hubs connecting to other relevant issues, e.g., bugs or open design questions.
A tracking issue is however not meant for large scale discussion, questions, or bug reports about a feature.
Instead, open a dedicated issue for the specific matter and add the relevant feature gate label.
Steps
- Implement the feature
- Write an RFC and have it accepted
- Adjust documentation (see instructions on rustc-dev-guide)
- Formatting for new syntax has been added to the Style Guide (nightly-style-procedure)
- Stabilization PR (see instructions on rustc-dev-guide)
Unresolved Questions
Implementation plans/details
-
!is parsed as a pattern. - Parsing of
matchexpressions allowsmatch <expr> { <pat>, ... }i.e. a pattern without a body. We later check to only allow that if the pattern is a never pattern. - The lack of body is desugared into something equivalent to
unreachable_unchecked!(). - Never patterns skip the or-pattern bindings check, to allow
Ok(x) | Err(!) - Never patterns don't allow bindings, like in
(x, !) - Allow
fn foo(!: Void) -> SomeType {} -
!is typechecked like a wildcard; it adds no type constraint - In match checking we check that
!is only used on an uninhabited type (modulo match ergonomics). - The
!pattern is lowered to MIR. - A
!pattern asserts validiy of the matched place - Exhaustiveness understands
!. - Exhaustiveness recommends using never patterns where sensible.
- Rustfmt understands never patterns
- Check every bit of the compiler that handles or-patterns to see if it handles never patterns properly
- Write up the RFC
- never patterns:
!argument not detected as diverging on async fn #120240
Implementation history
- Add
never_patternsfeature gate #118157 - never_patterns: Parse match arms with no body #118527
- Correctly gate the parsing of match arms without body #118868
- never patterns: Check bindings wrt never patterns #119610
- never patterns: Document behavior of never patterns with macros-by-example #119622
- never_patterns: typecheck never patterns #120009
- Report unreachable subpatterns consistently #120097
- never_patterns: Count
!bindings as diverging #120104 - never patterns: It is correct to lower
!to_. #120517 - never_patterns: lower a never arm to an unreachable terminator #120758
- never patterns: Fix liveness analysis in the presence of never patterns #121391
- never patterns: suggest
!patterns on non-exhaustive matches #121823 - never patterns: lower never patterns to
Unreachablein MIR #123332
Metadata
Metadata
Assignees
Labels
Type
Projects
Status