Skip to content

Bodiless match arms not being unconditionally syntactically legal is surprising and unprincipled #153134

Open

Description

@fmease

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

  1. the pattern "might contain a never pattern" (i.e., the pattern syntactically contains a never pattern or it's a macro call (!)) or
  2. it's "the last arm of the match expr" (i.e., if the pattern is followed by } instead of e.g., ,) or
  3. 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!() => {} }.

Activity

  1. added
    A-grammar
    A-parser
    T-lang
    T-compiler
    A-patterns
    C-discussion
    F-never_patterns
    on Feb 26, 2026
  2. added
    needs-triage
    on Feb 26, 2026
  3. removed
    needs-triage
    on Feb 26, 2026
  4. petrochenkov commented on Feb 26, 2026

    @petrochenkov
    Contributor

    I'd rather make them unconditionally illegal and always require some kind of body even for never patterns.
    Never patterns are not some super commonly used feature, they don't need an especially terse syntax.

  5. fmease commented on Feb 26, 2026

    @fmease
    MemberAuthor

    I'm fine with that, too. I'm content as long as we don't keep this odd ruleset rustc currently uses.

    I didn't suggest that in the issue description because then the impl would diverge too much from what's proposed in the current version of the RFC. If the RFC (and subsequently rustc) was updated to drop bodiless arms (as others have already suggested) then my issue would be addressed, too.

  6. Nadrieril commented on Feb 26, 2026

    @Nadrieril
    Member

    Bodiless arms got sufficient pushback that I too am ok with requiring a body for every arm. That was my plan for the next RFC update.

  7. Apersoma commented on Mar 4, 2026

    @Apersoma
    Contributor

    seconding this but also adding that ! should be allowed as the type of these branch arms to allow for clarity when compared with the other branches i.e.

    let result: Result<u64, !> = foo();
    match result {
        Ok(num) => num,
        Err(!) => !,
    }

    should type check correctly and be considered an idiomatic way of writing that branch (though I believe the branch may also be omitted, in that case that would also be idiomatic)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-grammarA-parserA-patternsC-discussionF-never_patternsT-compilerT-lang

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions