Tracking Issue for Never Patterns (`never_patterns`)
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
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: https://github.com/rust-lang/rfcs/pull/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 body
This 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
- https://github.com/rust-lang/rust/issues/120240
Implementation history
- https://github.com/rust-lang/rust/pull/118157
- #118527
- https://github.com/rust-lang/rust/pull/118868
- https://github.com/rust-lang/rust/pull/119610
- https://github.com/rust-lang/rust/pull/119622
- https://github.com/rust-lang/rust/pull/120009
- https://github.com/rust-lang/rust/pull/120097
- https://github.com/rust-lang/rust/pull/120104
- https://github.com/rust-lang/rust/pull/120517
- https://github.com/rust-lang/rust/pull/120758
- https://github.com/rust-lang/rust/pull/121391
- https://github.com/rust-lang/rust/pull/121823
- https://github.com/rust-lang/rust/pull/123332
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the RFC and the unchecked implementation items, especially MIR lowering, validity checking, rustfmt support, and handling of or-patterns. Review the referenced implementation history and the rustc-dev-guide stabilization and documentation guidance. Done means the remaining checklist items are addressed and the unresolved questions have been resolved.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100