Skip to content

Discard a bare assignment if/match arm (#1319's missed case) — fixes red behav_distinct_compat - #1332

Closed
ehartford wants to merge 1 commit into
mainfrom
fix-1319-assignment-arm
Closed

ehartford wants to merge 1 commit into
mainfrom
fix-1319-assignment-arm

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

#1319 made Sema discard an assignment in tail position (§9.1: "in statement or tail position its value is discarded") for a block tail, a single-statement fn body and a closure body. It missed a bare if/match arm, and main is red on test/behavior/behav_distinct_compat.w:

fn check(name: str, ok: bool):
    if ok: pass_count = pass_count + 1
    else: { eprint(...); fail_count = fail_count + 1 }

failed with cannot infer return type: if arms have types i32 and Unit. The else block's tail was discarded (Unit), but the then arm kept its place type (i32).

Fix

  • check_if (both arms) and the match arm loop type an assignment arm as Unit, using the same predicate as the other tail sites (Sema.expr_is_assignment).
  • lower_if and lower_match lower a value-position arm through lower_tail_expr, so MIR discards it at the same site.

Test changes

Evidence (bootstrap stage1, :dev on this commit)

Draft until the seed-driven battery is green and posted (D56).

…arded (#1319's gap; fixes behav_distinct_compat)

#1319 discarded an assignment in tail position (§9.1) for a block tail, a
single-statement fn body and a closure body, but not for a bare `if`/`match`
arm. `fn check(ok): if ok: n = n + 1 else: { eprint(..); m = m + 1 }` joined
the then arm's place type i32 with the else block's Unit and was rejected
("if arms have types i32 and Unit"), turning main red on
test/behavior/behav_distinct_compat.w.

check_if and the match arm loop now type an assignment arm Unit through the
same predicate (Sema.expr_is_assignment), and lower_if / lower_match lower a
value-position arm through lower_tail_expr so MIR agrees at the same site.

err_d43_cannot_infer_match used `true => seen = 1` beside `assert(p)` as its
mixed-arm shape; under §9.1 that arm is Unit, so the program is valid (it now
runs, as `true => { seen = 1 }` already did). The test keeps its D43 purpose
with a real value arm (`true => 1`); the assignment-arm shape moved to
behav_1319_assignment_arm_discarded.w, which covers then-bare/else-block, the
reverse, both match orders, both-bare, #1180's assert shape and an annotated
`-> i32` body.
@ehartford

Copy link
Copy Markdown
Contributor Author

More evidence: check test/behavior/behav_distinct_compat.w --validate-all on stage1 prints validate-all: ok. That stage1 was built before the final commit's only change, removing the inferable -> TypeId from arm_type_discarding_assignment, which has no effect on behavior. --validate-all on the new test was killed after it hung for more than 2 minutes at 7 GB, which is the stage1 hang in #1315/#1282.

@ehartford

Copy link
Copy Markdown
Contributor Author

Held for a ruling: this PR rewrites err_d43_cannot_infer_match.w, which D43's own commit (c1e3292) pinned — an assignment as a written arm of a tail match keeps its value type, so true => seen = 1 / false => assert(p) is "cannot infer". #1319 (merged) and this PR read §9.1's "in statement or tail position its value is discarded" as covering arms; D43's test reads it as the function/closure tail only. Which reading holds is Eric's call; if it is the arms reading, D43's entry and test are amended together, not by this PR.

@ehartford

Copy link
Copy Markdown
Contributor Author

Closed per Eric's ruling (2026-09-22): the spec, D43 as blessed including its pinned test, decides — an assignment is discarded only as the function/closure's own tail or a statement; as a written arm of a tail if/match it keeps its value type. This PR bent D43's test to keep another test green, which is the wrong motivation. The fix instead narrows #1319's arm-block discard back (new PR).

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.

1 participant