Skip to content

Spec: in a match arm, => takes the role of : and the body takes the three forms (inline, indented, braces) #2744

Closed

Description

@ehartford

The rule

In a match arm, => takes the role : plays for every other construct: it introduces the arm's body. The body takes the same three forms as any other body:

match source.next():
    Some(task) => thing          // inline

    Some(task) =>                // newline, indented block
        thing

    Some(task) => {              // curly braces
        thing
    }

Some(task): is an error, and the compiler is right to refuse it ("expected '=>'").

What the spec says today

The rule is shown only by example, and two sentences, read literally, say the opposite:

  • §9.7 Pattern Matching defines match's two outer forms in prose (block match uses : after the subject; inline match uses {} around the arms) but never says an arm is pattern [if guard] => body. Arms appear only in examples. The multi-line arm body appears once, in the click @ MouseClick { … } => example, and the braced arm body never appears.
  • §29.13 Block Body Syntax: "After a construct's header, a body introducer is required. For all constructs, including if, else if, and else, the introducer must be : or {". With an arm's pattern as its header, this makes Some(x): required and => illegal. Its "Applies uniformly to all block-introducers" example shows match only in its outer forms, with single-expression arms.
  • §30.8 Block Syntax lists the three body forms (COLON_INLINE, COLON_INDENTED, BRACE_BODY, each opening with : or {) as "interchangeable for every block-introducing construct: … labeled blocks, and match arms."
  • §30.4 Statements (informative grammar) has MATCH_ARM := PATTERN [ 'if' EXPR ] '=>' EXPR. This is the only place => is defined, and it allows only an expression, not an indented or braced body.
  • §29.2 mentions in passing that "Newlines that start a block after : or => retain their normal significance", the one hint that => opens a block.

A reader who follows §29.13 or §30.8 literally writes Some(x): and gets "expected '=>'". This happened while writing std.task.with_concurrency (#981).

What to change

The wording is Eric's; these are the places that need it:

  • §9.7: state the arm rule. An arm is a pattern, an optional if guard, =>, and a body. The body is inline on the same line, an indented block on the lines below, or a braced block.
  • §29.13: the ": or {" introducer rule excepts match arms, whose introducer is => (§9.7); the uniform example shows the three arm bodies.
  • §30.8: match arms take the three body forms after =>, not after :.
  • §30.4: MATCH_ARM := PATTERN [ 'if' EXPR ] '=>' ARM_BODY, with ARM_BODY covering the inline expression, the indented block and BRACE_BODY.

The compiler already implements this rule (Parser.parse_match_arms and parse_inline_match_arms in src/Parser.w), so this is a spec-only change. A test of all three arm-body forms would pin it (Law 10).

Activity

  1. ehartford commented on Oct 27, 2026

    @ehartford
    ContributorAuthor

    Fixed on main in 0dc5a25 (#2750), delegated as D163: §9.7 now states the arm rule (pattern [if guard] => body, body inline, indented or braced after =>); §29.13 excepts match arms from the :/{ introducer rule; §30.4's MATCH_ARM takes an ARM_BODY; §30.8 drops match arms from the colon constructs. No compiler behavior changes — the words now state what the compiler enforces. Pinned by behav_2744_match_arm_three_body_forms and err_2744_match_arm_colon_body. The wording is delegated under D98 (90%: the rule content is the confirmed compiler behavior), so veto or reword freely.

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions