Skip to content

Sema: destructuring let/for/with check their subject as a value (#1349) - #1355

Merged
ehartford merged 1 commit into
mainfrom
fix-1349-tuple-pattern-if-subject
Sep 23, 2026
Merged

ehartford merged 1 commit into
mainfrom
fix-1349-tuple-pattern-if-subject

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Fixes #1349. Stacked on #1351 (fix-stage-debug-info).

Root cause. Sema.check_let_else (SemaCheck.w:15258) checked the subject with check_expr, so it inherited the enclosing statement's context. lldb on a stage2 built from #1351 confirmed it: check_if_expr took its statement arm (line 9791) instead of the value arm (9787), skipped the join, and returned ty_void. check_pattern then reported the error at 14334. A plain let (check_let_binding:9603) already used check_expr_value_context. Every binding construct's subject now does: let-pattern, the dead check_tuple_destructure, the for iterable, and the source of all three with forms.

Matrix, seed vs. fixed stage2:

subject before after
let (x, y) = if call / literal, literal / call, call / call, literal / literal tuple-pattern error ok
multi-line if, else if chain, (x, _), nested ((p, q), r), nest(b).0 arm error ok
in a non-void function error ok
let .Some(v) = if ... else: return error ok
match / block tail / call / let t = if; let (x, y) = t ok ok
for (x, y) in Vec of tuples ok ok
for (x, y) in if c: v else: w error ok
with if ... as (x, y): "requires a tuple expression" ok
with if ... as t: / with t(if ...): MIR lowering failed ok
var (x, y) = ...; x = ... error immutable binding: separate bug, #1354
let (x, y): (i32, i32) = ... parse error parse error: the spec shows no annotated destructuring let

Evidence. Fixture test/behavior/behav_destructure_value_subject.w (23 cases, expected stdout). I checked all 1130 test/behavior and 850 test/compile_errors fixtures with the baseline stage2 and with the fixed stage2. Only the new fixture changed; one c_import message differs only in its temp path. On the fixture, analyze audit:all and --validate-all pass, and --debug-alloc reports a leak count of 0.

Codegen workaround stays (seed-gated). The pinned seed builds stage1, and the seed still has this bug, so restoring let (line, col) = if ... in Codegen.debug_push_lexical_block fails :dev. The fixed stage2 checks src/main.w fine with the natural spelling. A comment marks the spelling to restore once seed.lock points at a release that contains this fix.

Iterate tier only: :dev and :stage2. The battery has not run yet.

…value — `let (a, b) = if c: f() else: (1, 0)` binds (#1349)

`let (x, y) = if b > 0: lc(b) else: (1, 0)` was rejected with "tuple
pattern requires tuple subject" in any body where the `let` sat in
statement position (every void function, and any statement with
stmt_pos_depth > 0). §9.7: all pattern forms are available in let/var;
an if-expression is an ordinary expression.

Exact line (stage2 of fix-stage-debug-info, lldb file:line breakpoints
on c04.w `let (x, y) = if b > 0: (7, 8) else: (1, 0)` in `fn main`):
Sema.check_let_else at SemaCheck.w:15258 `self.check_expr(value)`
entered Sema.check_if_expr, which stopped at 9791 (`self.check_expr(then_body)`,
the statement arm) instead of 9787 (value arm); the join at 9825 and the
typed_expr_types insert at 9864 never ran, result_type stayed ty_void,
and check_pattern reported at 14334. check_if_expr's classification
(9749-9756) saw current_value_expr_root == 0 and current_return_type ==
ty_void / stmt_pos_depth > 0: the subject inherited the enclosing
statement's context. A plain `let x = if ...` goes through
check_let_binding:9603, which checks its value with
check_expr_value_context; the pattern path did not.

Fix: every binding construct's subject is a value —
check_let_else, check_tuple_destructure, check_for's iterable,
check_with_expr's, check_with_tuple's and check_with_implicit's source
all use check_expr_value_context. The same statement-context typing made
`with if c: t else: u as x:` and `with t(if ...):` fail in MIR lowering
("AST codegen was removed"), `with if ... as (a, b):` report "requires a
tuple expression", and `for (x, y) in if c: v else: w` report the tuple
pattern error.

Matrix (seed / fixed stage2):
  let (x, y) = if: call | literal         error / 4 5
  let (x, y) = if: literal | call         error / 4 5
  let (x, y) = if: call | call            error / 4 5
  let (x, y) = if: literal | literal      error / 7 8
  multi-line if, else-if chain, `_`       error / ok
  nested ((p, q), r) = if ...             error / 3 4 5
  (p, q) = if: nest(b).0 | literal        error / 3 4
  in a non-void fn                        error / ok
  let .Some(v) = if ... else: return      error / ok
  let (x, y) = match / block tail / call  ok / ok
  let t = if ...; let (x, y) = t          ok / ok
  for (x, y) in Vec of tuples             ok / ok
  for (x, y) in if c: v else: w           error / ok
  with if ... as (x, y):                  error / ok
  with if ... as t: / with t(if ...):     MIR-lowering error / ok
  var (x, y) = if ...; x = ...            error / "cannot assign to immutable" (#1354, separate)
  let (x, y): (i32, i32) = if ...         parse error both (spec shows no annotated pattern let)

Check comparison of the baseline stage2 against the fixed stage2 over
all 1130 test/behavior and 850 test/compile_errors fixtures: the only
difference is the new fixture (plus a c_import temp-path in one
message). analyze audit:all and --validate-all ok on the fixture;
--debug-alloc leak count 0.

The Codegen.debug_push_lexical_block workaround from
fix-stage-debug-info stays: stage1 is built by the pinned seed, which
has this bug, so the natural spelling fails the :dev build ("tuple
pattern requires tuple subject" at Codegen.w). A comment records the
spelling to restore once seed.lock names a release with this fix;
the fixed stage2 checks src/main.w with the natural spelling ok.

Fixture: test/behavior/behav_destructure_value_subject.w.
(cherry picked from commit 7e2fff1)
(cherry picked from commit de529dc0fa040df0ea9f4357d218ef39689c1854)
@ehartford

Copy link
Copy Markdown
Contributor Author

Batch battery (seed-driven) on main dd786a4 + #1355 → #1357 → #1370 → #1371 as one linear chain (head 45f8aa4): build ok, :fixpoint ok, :test ok (no reds), :test-green ok, :last-green ok. The four PRs are now stacked exactly as tested (each branch = its commit on top of the previous PR); merge in order #1355, #1357, #1370, #1371, retargeting each to main as its parent merges.

@ehartford
ehartford marked this pull request as ready for review September 23, 2026 01:03
@ehartford
ehartford merged commit 1f4636d into main Sep 23, 2026
@ehartford ehartford mentioned this pull request Oct 2, 2026
ehartford pushed a commit that referenced this pull request Oct 2, 2026
…ical_block (#1356)

The #1351 workaround for #1349 (`let (a, b) = if c: f() else: (1, 0)`
rejected by the then-pinned seed) is retired: seed.lock has been bumped
past 1f4636d (#1355), and the pinned seed builds stage1 with the natural
spelling (`src/main build :dev` rc=0).
ehartford added a commit that referenced this pull request Oct 2, 2026
…ical_block (#1356) (#1961)

The #1351 workaround for #1349 (`let (a, b) = if c: f() else: (1, 0)`
rejected by the then-pinned seed) is retired: seed.lock has been bumped
past 1f4636d (#1355), and the pinned seed builds stage1 with the natural
spelling (`src/main build :dev` rc=0).

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

Development

Successfully merging this pull request may close these issues.

Sema: let (a, b) = if c: f() else: (1, 0) rejected — "tuple pattern requires tuple subject"

1 participant