Repository navigation
Description
Summary
Finish let ... else as the spec's refutable-binding early-exit form. The syntax now parses and one simple .Some runtime case exists, but sema does not enforce diverging else and lowering still has pattern coverage gaps such as slice rest binding.
Spec
§9.7 says let ... else handles refutable patterns and the else branch must diverge with return, break, continue, or panic (docs/with-specification.md:3651-3661). It supports enum shorthand such as let .TString(key) = ... else return ... (docs/with-specification.md:3698-3711) and is used by other sections such as select-await branch handling.
Current state
The original body said let ... else was explicitly not implemented in test/behavior/behav_let_else.w:4; that file is now stale and check-only (test/behavior/behav_let_else.w:1-9). Parser support exists for tuple/struct destructuring, enum shorthand, and uppercase variant patterns through NK_LET_ELSE (src/Parser.w:5908-6028). Sema currently checks the value, binds the pattern, and checks the else body but does not verify the else body is Never (src/SemaCheck.w:7487-7496). MIR lowering dispatches NK_LET_ELSE as a statement (src/MirLower.w:3488-3496), and a simple behavior test for .Some(v) passes through test/behavior/behav_let_else_runtime.w:11-19.
The precise gap is not parsing; it is correctness. Non-diverging else branches can typecheck, and more complex pattern lowering still inherits MIR pattern gaps, notably #474 for [first, ..rest].
Implementation plan
- In
Sema.check_let_else, requireelse_body != 0for every refutable pattern and check it in a context that records its resulting type. Ifelse_bodyis absent or its resolved type kind is notTY_NEVER, emit a diagnostic such aslet ... else requires a diverging else branch. - Distinguish irrefutable destructuring from refutable
let ... else. Parser currently createsNK_LET_ELSEwithd2 = 0for tuple/struct destructuring without anelse(src/Parser.w:5920-5926); either lower this as ordinary destructuring or make sema treat it as irrefutable-only. Do not letd2 = 0silently become a valid refutable binding. - Audit
MirBuilder.lower_let_elseandlower_pattern_matchso it lowers as: evaluate subject once, test pattern, bind values on success, and branch to diverging else on failure. Keep success bindings scoped like normal let bindings. - Add explicit support for slice patterns only after Bug: slice ..rest binding fails MIR lowering (§9.7) #474's rest-binding lowering is fixed; until then, emit a loud compile error for unsupported
let [first, ..rest] = items else ...instead of generating unusable bindings. - Replace stale
test/behavior/behav_let_else.wcomments with real positive coverage and keepbehav_let_else_runtime.was the runtime smoke.
Test plan
Add or update test/behavior/behav_let_else.w:
let Some(v) = opt else return -1returns payload on success and early-exits on failure.let .Some(v) = opt else return -1covers enum shorthand.let Ok(v) = result else return Err(.Bad)covers Result.
Add test/compile_errors/err_let_else_else_must_diverge.w with //! expect-error: let ... else requires a diverging else branch.
Add test/compile_errors/err_let_else_missing_else_for_refutable.w if the parser can construct a refutable pattern without an else.
Add a deferred regression under #474 for let [first, ..rest] = items else return ... that uses rest.
Acceptance criteria
let ... elserejects non-diverging else branches.- Refutable
let ... elsenever lowers without an else branch. - Existing
.Someruntime behavior still passes. - Slice-rest cases either work via Bug: slice ..rest binding fails MIR lowering (§9.7) #474 or fail loudly with a clear diagnostic.
with build && with build :fixpoint && with build :testpass.
Dependencies
#474 for slice ..rest binding lowering. #518 for broader pattern-form tests.
Activity
ehartford commentedon Jun 14, 2026
Implemented in 904991f. Let-else now parses destructuring patterns with optional else branches, enforces that refutable patterns provide an else, requires the else branch to diverge, and fails loudly for slice rest bindings until #474. Added positive Option/Result behavior coverage and negative tests for non-diverging else, missing else, and slice-rest unsupported lowering. Verification passed: build, fixpoint, full test suite, and test-green.