Repository navigation
let-else: match-arm pattern grammar in let; else takes every §29.13 body form (#1373, #1382) - #1397
Merged
Merged
Conversation
ehartford
force-pushed
the
fix-1382-let-else-parse
branch
from
September 23, 2026 01:13
0a54b4b to
cbbe1bc
Compare
ehartford
force-pushed
the
fix-1354-var-destructure-mut
branch
from
September 23, 2026 01:54
06cb0b8 to
075b0dd
Compare
ehartford
force-pushed
the
fix-1382-let-else-parse
branch
from
September 23, 2026 01:59
cbbe1bc to
37a2441
Compare
ehartford
changed the base branch from
fix-1354-var-destructure-mut
to
main
September 23, 2026 01:59
This was referenced Sep 23, 2026
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
marked this pull request as ready for review
September 23, 2026 02:54
ehartford
marked this pull request as draft
September 23, 2026 02:55
ehartford
force-pushed
the
fix-1382-let-else-parse
branch
from
September 23, 2026 02:56
37a2441 to
c846339
Compare
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
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
force-pushed
the
fix-1382-let-else-parse
branch
from
September 23, 2026 04:04
c846339 to
afed5a6
Compare
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1373, Fixes #1382
Stacked on #1370 (
fix-1354-var-destructure-mut): it builds on that PR'sNK_LET_PATTERNhead (thevarbit and type annotation).Root cause
#1373.
Parser.parse_let_bindinghand-parsed three pattern heads:(/{/[,.Variant, and a capitalizedName(..). Everything else went to the name-binding path. Forlet Shape.Named(n) = s,expect_identtookShape,.Namedwas neither:nor=, and the parser emitted "let binding requires initializer".let_head_is_pattern()picks every head that a match arm accepts. The pattern is parsed byparse_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..Variant(..)andName(..)paths are deleted.=still binds a name unlesselsefollows (let N = 5binds;let Empty = s else: ...matches). That case now also takes a type annotation.#1382. Every
NK_LET_ELSEproducer ranskip_newlines(); parse_block_or_expr().parse_block_or_exprchooses 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.parse_let_else_body()leaves the newline in place.let(§29.13). Before this,let Some(v) = o else:followed byreturn vat the let's own column compiled and ran, withreturn vsilently taken as the else branch.Matrix (stage1 of the base branch vs this branch,
with run)let Shape.Named(n) = s else: return 03 0let Shape.Empty = s else:let Named(n) = s else:let Some(Shape.Named(n)) = o else:var Shape.Named(n) = s else:+ mutate15 0let Shape.Named(n): Shape = s else:.Named(n)/.Empty(Some(a), b)0,-1,"hi",true,'a',1..=5Some(1) | Some(2)match(#823)whole @ Some(v)Point { x: 0, y }match(#1388)&Vec[i32]match(#1389)else: returninlineelse:indented multi-statementelse { a; return }/ multi-line bracedelse return/else+ indented block (§9.7 examples)continue/break/panictailselse:then nothing / an unindented lineelseNew fixtures:
behav_let_else_pattern_forms.w,behav_let_else_block_body.w, anderr_let_else_{empty_else_block,else_block_not_indented,block_must_diverge,qualified_missing_else,literal_missing_else}.w.Sweep
checkwas 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:err_drop_destructure_let_named.w, whose caret now points at the pattern (R { repr }) instead oflet. The message is unchanged, the fixture matches on the message, and the span now matches the anonymouslet { repr }form.check src/main.wwith 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 ...andelse+ an indented block. §29.13 says the introducer afterelse"must be:or{". The compiler already acceptedelsewithout:, and existing fixtures use it. This PR keeps it (an indented block now works there too) and does not settle the question. Separately, §30.4LET_STMThas noelseclause at all.Filed while building the matrix:
Point { x: 0, y }matches any x (match and let-else) #1388: a literal field sub-pattern in a struct pattern is ignored, in match and let. Wrong code, no error.&Vecgives invalid MIR.parse_block_or_expr(if c:/while c:followed by an unindented line).