Skip to content

Implement let ... else (§9.7) #399

Closed

Description

@ehartford

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

  1. In Sema.check_let_else, require else_body != 0 for every refutable pattern and check it in a context that records its resulting type. If else_body is absent or its resolved type kind is not TY_NEVER, emit a diagnostic such as let ... else requires a diverging else branch.
  2. Distinguish irrefutable destructuring from refutable let ... else. Parser currently creates NK_LET_ELSE with d2 = 0 for tuple/struct destructuring without an else (src/Parser.w:5920-5926); either lower this as ordinary destructuring or make sema treat it as irrefutable-only. Do not let d2 = 0 silently become a valid refutable binding.
  3. Audit MirBuilder.lower_let_else and lower_pattern_match so 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.
  4. 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.
  5. Replace stale test/behavior/behav_let_else.w comments with real positive coverage and keep behav_let_else_runtime.w as the runtime smoke.

Test plan

Add or update test/behavior/behav_let_else.w:

  • let Some(v) = opt else return -1 returns payload on success and early-exits on failure.
  • let .Some(v) = opt else return -1 covers 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 ... else rejects non-diverging else branches.
  • Refutable let ... else never lowers without an else branch.
  • Existing .Some runtime 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 :test pass.

Dependencies

#474 for slice ..rest binding lowering. #518 for broader pattern-form tests.

Activity

  1. ehartford commented on Jun 14, 2026

    @ehartford
    ContributorAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions