Skip to content

fix(ts): read no scrutinee for a switch whose other variants can't exist - #293

Merged
kixelated merged 2 commits into
mainfrom
quest/m3/ts-uninhabited-switch
Oct 30, 2026
Merged

kixelated merged 2 commits into
mainfrom
quest/m3/ts-uninhabited-switch

Conversation

@kixelated

@kixelated kixelated commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

A pattern on part of a non-path place, such as let (Ok(a), Ok(b)) = (wide, wider); of two Result<_, Infallible>s, makes MIR read each part's discriminant and switch to unreachable otherwise. Lowering drops that one-way switch, but the read had already been spilled to a const scrutinee that nothing reads, which tsc refuses (TS6133). Reproduced on #290's widenings in the tuple form: ints.ts(5809,11): 'scrutinee' is declared but its value is never read, and on every new shape below.

Approach

  • Slot::Discr's assignment (ts/src/lower/mir/mod.rs) checks whether its switch tests anything (discr_tested, via switch_targets). If it doesn't, the read folds in what it holds back and runs only for its effects, so a call that the place folds in still runs, in order.
  • Second instance on the same path: Some(Ok(r)) of an Option<Result<_, Void>>. Some is the path itself, so the Option switch's narrowing of right let the Ok downcast (whose own switch is now dropped) skip its cast: right.value on Result<u16, never> (TS2339). switched is now keyed by path and the enum tested.
  • Tests: new shapes::uninhabited differential case (native wasm32 vs TS) covering tuples, arrays, nested tuples, struct fields, a nested Option<Result<_, Void>>, an enum with a Void variant, a one-arm match, if let, let else, and calls that fold into the read (counted so their order shows). feat(ts): type a try_from that can't fail by its Infallible #290's char widenings now use the tuple form.

Impact

  • Public API: the translator's output only. No other snapshot changed.

Alternatives

  • Suppressing the unused variable (void scrutinee, or naming it _) leaves a dead read in the output. Rejected per the quest.
  • Deferring the spill to the switch would also work, but nothing else is ordered between the read and the switch, and spilling at the read keeps the current ordering guarantees.

Follow-ups

None. Behind a reference (let &(Ok(x), ..) = &pair), rustc treats the variant as reachable and requires let else, so that shape can't reach this path.

Review

  • Reviewer: Codex, gpt-6.1-sol, medium effort (quest review, round 1)
  • Head: fb76607
  • Findings: none
  • Local checks: just ci passed in nix develop on this head.

(written by Claude Opus 5.5)

🤖 Generated with Claude Code

kixelated and others added 2 commits October 30, 2026 10:26
A pattern on a part of a non-path place, such as `let (Ok(a), Ok(b)) = (wide, wider);`
of two `Result<_, Infallible>`s, switches to `unreachable` for each part's other variant.
Lowering drops that one-way switch, but its discriminant read was already spilled to an
unused `const scrutinee`, which `tsc` refuses. Such a read now runs only for its effects.

A switch's narrowing is also keyed by the enum it tests, so `Some(Ok(r))` of an
`Option<Result<_, Void>>`, whose `Some` is the path itself, still casts to `Ok`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated kixelated changed the title A switch whose other variants are uninhabited fix(ts): read no scrutinee for a switch whose other variants can't exist Oct 30, 2026
@kixelated
kixelated marked this pull request as ready for review October 30, 2026 17:51
@coderabbitai

coderabbitai Bot commented Oct 30, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 7 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 33fa2315-f653-485d-a744-f60b406d732a
Commits

Reviewing files that changed from the base of the PR and between 683e224 and fb76607.

Files selected for processing: 13
  • quest/m3/README.md
  • quest/m3/ts-uninhabited-switch.md
  • ts/fixtures/lang/expected/core/bool.ts
  • ts/fixtures/lang/expected/harness.ts
  • ts/fixtures/lang/expected/ints.ts
  • ts/fixtures/lang/expected/shapes.Partial.ts
  • ts/fixtures/lang/expected/shapes.Parts.ts
  • ts/fixtures/lang/expected/shapes.ts
  • ts/fixtures/lang/src/harness.rs
  • ts/fixtures/lang/src/ints.rs
  • ts/fixtures/lang/src/shapes.rs
  • ts/src/lower/mir/mod.rs
  • ts/src/lower/mir/place.rs
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@kixelated
kixelated merged commit f0c6c9e into main Oct 30, 2026
@kixelated
kixelated deleted the quest/m3/ts-uninhabited-switch branch October 30, 2026 17:51
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