Bodiless match arms not being unconditionally syntactically legal is surprising and unprincipled
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
AFAICT the open RFC 3719 doesn't specify if bodiless match arms are unconditionally legal or not (A never pattern is accepted as an arm in a match expression, and that arm takes no body nor guards. doesn't tell us that esp. since that sentence talks about the semantics, not the syntax). I'm opening this discussion here instead of over there because this concerns rustc's current behavior I'm strongly disagreeing with and which I consider to be a blocker for never_patterns (I know that bodiless arms in general are contested & might not make it into the final version of the RFC).
Currently rustc only syntactically accepts a bodiless match arm (i.e., one that "doesn't end in => $expr") under feature never_patterns if
- the pattern "might contain a never pattern" (i.e., the pattern syntactically contains a never pattern or it's a macro call (!)) or
- it's "the last arm of the match expr" (i.e., if the pattern is followed by
}instead of e.g.,,) or - it has a guard (i.e., if the pattern is followed by
if).
I find this partly rule partly heuristic incredibly complex and unprincipled. I would just make all bodiless match syntactically legal. I know that we currently have a nice parser diagnostic + recovery for match $expr { $pat, $pat, … … } → match $expr { $pat | $pat | … … } (i.e., helping users who confused , with |). That would indeed no longer be possible. Still, we can provide a similar structured suggestion in a later, semantic analysis pass.
Concretely, it means we currently syntactically accept match(){ x }, reject match(){ x, }, accept match(){ x if(), }, reject match(){ X(), Y() => {} }, accept match(){ X!(), Y!() => {} }.
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 by reading RFC 3719 and reproducing the listed bodiless match-arm examples with rustc under never_patterns. Trace the parser behavior that distinguishes never-pattern-containing arms, final arms, and guarded arms. Done means reaching and documenting a settled syntax decision, with the associated compiler behavior aligned to that decision.
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
- 28/100