Skip to content

let-else: match-arm pattern grammar in let; else takes every §29.13 body form (#1373, #1382) - #1397

Merged
ehartford merged 1 commit into
mainfrom
fix-1382-let-else-parse
Sep 23, 2026
Merged

ehartford merged 1 commit into
mainfrom
fix-1382-let-else-parse

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Fixes #1373, Fixes #1382

Stacked on #1370 (fix-1354-var-destructure-mut): it builds on that PR's NK_LET_PATTERN head (the var bit and type annotation).

Root cause

#1373. Parser.parse_let_binding hand-parsed three pattern heads: (/{/[, .Variant, and a capitalized Name(..). Everything else went to the name-binding path. For let Shape.Named(n) = s, expect_ident took Shape, .Named was neither : nor =, and the parser emitted "let binding requires initializer".

  • Fix: let_head_is_pattern() picks every head that a match arm accepts. The pattern is parsed by parse_or_pattern(), which the two match-arm parsers now also use (the or-pattern loop that was duplicated in both is gone). parse_let_pattern_rest() finishes the let.
  • The hand-rolled .Variant(..) and Name(..) paths are deleted.
  • A bare capitalized name before = still binds a name unless else follows (let N = 5 binds; let Empty = s else: ... matches). That case now also takes a type annotation.

#1382. Every NK_LET_ELSE producer ran skip_newlines(); parse_block_or_expr(). parse_block_or_expr chooses the indented-block form only when the current token is the newline. Skipping the newline first meant only the block's first statement became the else branch.

  • Fix: parse_let_else_body() leaves the newline in place.
  • It also requires the block to be indented deeper than the line holding the let (§29.13). Before this, let Some(v) = o else: followed by return v at the let's own column compiled and ran, with return v silently taken as the else branch.

Matrix (stage1 of the base branch vs this branch, with run)

case base this PR
let Shape.Named(n) = s else: return 0 parse error 3 0
let Shape.Empty = s else: parse error ok
bare let Named(n) = s else: ok ok
let Some(Shape.Named(n)) = o else: ok ok
var Shape.Named(n) = s else: + mutate parse error 15 0
let Shape.Named(n): Shape = s else: parse error ok
.Named(n) / .Empty ok ok
tuple (Some(a), b) ok ok
literals 0, -1, "hi", true, 'a', 1..=5 "expected identifier" ok
or-pattern Some(1) | Some(2) parse error ok
or-pattern with bindings parse error same "shadowing" error as match (#823)
at-binding whole @ Some(v) parse error ok
struct Point { x: 0, y } runs, wrong result runs, wrong result: same in match (#1388)
slice over &Vec[i32] invalid MIR invalid MIR: same in match (#1389)
else: return inline ok ok
else: indented multi-statement diverge error ok
else { a; return } / multi-line braced ok ok
else return / else + indented block (§9.7 examples) inline ok / block error ok / ok
nested if/let-else/for inside else, continue/break/panic tails diverge error ok
else: then nothing / an unindented line "expected expression" / silently accepted "expected an indented block after 'else'"
refutable qualified / literal / or without else parse errors "requires an else branch for refutable patterns"
multi-statement else with a non-diverging tail error on the wrong statement "requires a diverging else branch"

New fixtures: behav_let_else_pattern_forms.w, behav_let_else_block_body.w, and err_let_else_{empty_else_block,else_block_not_indented,block_must_diverge,qualified_missing_else,literal_missing_else}.w.

Sweep

check was run over test/behavior, test/compile_errors, examples and build/release_uat_fixtures (2115 files), with the base stage1 and this branch's stage1. The only differences are:

  • the new fixtures;
  • a random c_import temp path in one diagnostic;
  • err_drop_destructure_let_named.w, whose caret now points at the pattern (R { repr }) instead of let. The message is unchanged, the fixture matches on the message, and the span now matches the anonymous let { repr } form.

check src/main.w with the new stage1: ok. Only the iterate tier has been run; the battery has not.

For Eric

The spec disagrees with itself on the no-colon form. §9.7's let-else examples use else return ... and else + an indented block. §29.13 says the introducer after else "must be : or {". The compiler already accepted else without :, and existing fixtures use it. This PR keeps it (an indented block now works there too) and does not settle the question. Separately, §30.4 LET_STMT has no else clause at all.

Filed while building the matrix:

@ehartford

Copy link
Copy Markdown
Contributor Author

Battery (seed-driven) on main 00408eb + #1400 → #1370 → #1371 → #1397 as one chain (head 37a2441): build ok, :fixpoint ok, :test ok (no reds), :test-green ok, :last-green ok. Each branch holds exactly its tested commit on top of the previous PR's; all target main. Merge in order #1400, #1370, #1371, #1397.

@ehartford
ehartford marked this pull request as ready for review September 23, 2026 02:54
@ehartford
ehartford marked this pull request as draft September 23, 2026 02:55
@ehartford
ehartford force-pushed the fix-1382-let-else-parse branch from 37a2441 to c846339 Compare September 23, 2026 02:56
@ehartford

Copy link
Copy Markdown
Contributor Author

Battery (seed-driven, with :drop-audit/:move-audit) on main 43796ac + #1400 → #1370 → #1371 → #1397 (head c846339, including the #1385 lower_let_else merge resolution): build ok, :fixpoint ok, :drop-audit ok, :move-audit ok, :test ok (no reds), :test-green ok, :last-green ok. Merge in order #1370, #1371, #1397 (#1400 already merged).

@ehartford
ehartford marked this pull request as ready for review September 23, 2026 03:58
…anch takes every §29.13 body form (#1373, #1382)

#1373: parse_let_binding hand-rolled three pattern heads (`(`/`{`/`[`,
`.Variant`, capitalized `Name(..)`) and sent everything else to the name-binding
path, so `let Shape.Named(n) = s else:` reached expect_ident + "let binding
requires initializer". Every head a match arm accepts now goes through one
parser: let_head_is_pattern() selects it, parse_or_pattern() (shared with both
match-arm parsers) parses it, parse_let_pattern_rest() finishes it. Qualified
`Type.Variant(..)`/unit, bare variants, nested, `var`, annotated, literals,
ranges, or-patterns and at-bindings all parse; refutability without `else`
is Sema's existing diagnostic. A bare capitalized name before `=` still binds
unless `else` follows.

#1382: every NK_LET_ELSE producer did `skip_newlines(); parse_block_or_expr()`,
and parse_block_or_expr picks the indented-block form only when it sees the
newline — so only the block's first statement became the branch.
parse_let_else_body() keeps the newline and requires the block to sit deeper
than the let's line (§29.13); `else:` followed by an unindented line was
silently taken as the branch.
@ehartford
ehartford force-pushed the fix-1382-let-else-parse branch from c846339 to afed5a6 Compare September 23, 2026 04:04
@ehartford
ehartford merged commit 04e4934 into main Sep 23, 2026
@ehartford
ehartford deleted the fix-1382-let-else-parse branch September 23, 2026 04:06
ehartford pushed a commit that referenced this pull request Sep 23, 2026
… `else:` fix-it (§9.7, §29.13, §30.4)

D58 (blessed 2026-09-22): the `else` of `let PATTERN = EXPR else` takes a
§29.13 body (`else: stmt`, `else:` + indented block, `else { ... }`) or a
single diverging expression on the same line (`else return -1`). `else` +
newline + an indented block with no introducer is a parse error.

The compiler still accepted it: Parser.parse_let_else_body advanced over an
optional `:` and handed a NEWLINE straight to parse_block_or_expr, which
takes a newline as the indented-block form whether or not a colon opened it
(#1397 pinned the shape in behav_let_else_block_body's `bare_indented`).

Now a NEWLINE directly after `else` reports, at the `else`:

  error: expected ':' after 'else': a let-else branch on the next line is a body (§9.7)
    = help: write `else:` to introduce the indented block; only a single
      diverging expression may follow a bare `else`, on the same line

and parses on as if the colon were written, so the indented branch raises
nothing further (one error per site; a dedented next line still adds the
#1382 indented-block error).

Matrix (stage1 before -> after), let/var/unit-variant heads:
  else\n<block>            ok -> error + help   (let, var, `let A = s`, trailing comment)
  else\n<dedented>         indented-block error -> both errors
  else return -1           ok -> ok
  else: return -1          ok -> ok
  else:\n<block>           ok -> ok
  else { ... } / multi     ok -> ok
  else print("x")          must-diverge error -> unchanged

Tree rewrite: tools/migrate_let_else_colon.w (Lexer-selected: an `else`
token followed by a newline token whose logical line starts with let/var)
over src lib tools test examples build build.w found one site, the
behav_let_else_block_body cell that pinned the removed form; it now writes
`else:` (renamed indented_option). No release UAT fixture, example or blog
program used the form. `stage1 check src/main.w`: ok.

New fixture: test/compile_errors/err_let_else_bare_else_next_line.w.
ehartford added a commit that referenced this pull request Sep 24, 2026
… `else:` fix-it (§9.7, §29.13, §30.4) (#1571)

D58 (blessed 2026-09-22): the `else` of `let PATTERN = EXPR else` takes a
§29.13 body (`else: stmt`, `else:` + indented block, `else { ... }`) or a
single diverging expression on the same line (`else return -1`). `else` +
newline + an indented block with no introducer is a parse error.

The compiler still accepted it: Parser.parse_let_else_body advanced over an
optional `:` and handed a NEWLINE straight to parse_block_or_expr, which
takes a newline as the indented-block form whether or not a colon opened it
(#1397 pinned the shape in behav_let_else_block_body's `bare_indented`).

Now a NEWLINE directly after `else` reports, at the `else`:

  error: expected ':' after 'else': a let-else branch on the next line is a body (§9.7)
    = help: write `else:` to introduce the indented block; only a single
      diverging expression may follow a bare `else`, on the same line

and parses on as if the colon were written, so the indented branch raises
nothing further (one error per site; a dedented next line still adds the
#1382 indented-block error).

Matrix (stage1 before -> after), let/var/unit-variant heads:
  else\n<block>            ok -> error + help   (let, var, `let A = s`, trailing comment)
  else\n<dedented>         indented-block error -> both errors
  else return -1           ok -> ok
  else: return -1          ok -> ok
  else:\n<block>           ok -> ok
  else { ... } / multi     ok -> ok
  else print("x")          must-diverge error -> unchanged

Tree rewrite: tools/migrate_let_else_colon.w (Lexer-selected: an `else`
token followed by a newline token whose logical line starts with let/var)
over src lib tools test examples build build.w found one site, the
behav_let_else_block_body cell that pinned the removed form; it now writes
`else:` (renamed indented_option). No release UAT fixture, example or blog
program used the form. `stage1 check src/main.w`: ok.

New fixture: test/compile_errors/err_let_else_bare_else_next_line.w.

Co-authored-by: Eric Hartford <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant