Skip to content

lang: widen case branches; null when no match - #124

Merged
vito merged 2 commits into
mainfrom
case-widen-nullable
Jun 12, 2026
Merged

vito merged 2 commits into
mainfrom
case-widen-nullable

Conversation

@vito

@vito vito commented Jun 12, 2026

Copy link
Copy Markdown
Owner

Addresses docs feedback on /control-flow: case should be consistent with if — widen on branch mismatch instead of erroring, and go nullable instead of raising at runtime when nothing matches.

Language changes

  • Clause bodies widen instead of erroring. When clause bodies don't merge to a common type, the case now infers an inline union (String! | Int!), exactly like diverging if/else branches.
  • No match yields null instead of a runtime error. A case with no matching clause and no else evaluates to null, so its result type is nullable — you're forced to add an else (or cover every possibility) to avoid the nullable taint.
  • Exhaustiveness check for unions. Type patterns covering every member of a non-null union (named or inline) make the case exhaustive: no else needed, result stays non-null. An interface operand is exhaustive only via an interface catch-all pattern (its implementer set is open); a nullable operand is never exhaustive, since null matches no type pattern.

try/catch is untouched: an uncaught error re-raises rather than falling through, so there's no equivalent fallthrough to make nullable.

Tests

  • tests/errors/case_no_match.dang and case_type_mismatch.dang now describe valid programs and are removed, replaced by tests/test_case_widen_nullable.dang covering widening, null fallthrough, union/inline-union/interface exhaustiveness, and the nullable-operand rule.
  • New error test case_nonexhaustive_nonnull.dang pins the diagnostic when a non-exhaustive case flows into a non-null return type.

Docs

/control-flow is rewritten for the new semantics (the literate build bakes the new null and widened results), and the union-discrimination notes on /interfaces-unions plus the dang-language skill references are updated to match.

🤖 Generated with Claude Code

vito added 2 commits June 12, 2026 23:28
Bring case in line with if: clause bodies that fail to merge now widen
to a union instead of erroring, and a case with no matching clause and
no else evaluates to null instead of raising at runtime, making the
result type nullable. Type patterns covering every member of a non-null
union (or an interface catch-all pattern) are recognized as exhaustive,
so such cases keep a non-null result without an else.

The old no-match and clause-mismatch error tests now describe valid
programs; they are replaced by test_case_widen_nullable.dang plus an
error test pinning the nullable taint diagnostic when a non-exhaustive
case flows into a non-null return type.

Signed-off-by: Alex Suraci <[email protected]>
Update /control-flow for the new case semantics: diverging clause
bodies widen to a union like if branches, an unmatched case yields null
(nullable result type), and exhaustive type-pattern coverage of a
non-null union keeps the result non-null without an else. Adjust the
union-discrimination notes on /interfaces-unions and the dang-language
skill references to match.

Signed-off-by: Alex Suraci <[email protected]>
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying dang with  Cloudflare Pages  Cloudflare Pages

Latest commit: 103cd1b
Status: ✅  Deploy successful!
Preview URL: https://02bb3baf.dang-3kk.pages.dev
Branch Preview URL: https://case-widen-nullable.dang-3kk.pages.dev

View logs

@vito
vito merged commit 35d92c3 into main Jun 12, 2026
8 checks passed
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