Repository navigation
fix(ts): read no scrutinee for a switch whose other variants can't exist - #293
Merged
Merged
Conversation
Quest-Claim: 3b5c14ab7924d823
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
marked this pull request as ready for review
October 30, 2026 17:51
|
Warning Review limit reachedYou'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. View limit details
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. Comment |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
A pattern on part of a non-path place, such as
let (Ok(a), Ok(b)) = (wide, wider);of twoResult<_, Infallible>s, makes MIR read each part's discriminant and switch tounreachableotherwise. Lowering drops that one-way switch, but the read had already been spilled to aconst scrutineethat nothing reads, whichtscrefuses (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, viaswitch_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.Some(Ok(r))of anOption<Result<_, Void>>.Someis the path itself, so theOptionswitch's narrowing ofrightlet theOkdowncast (whose own switch is now dropped) skip its cast:right.valueonResult<u16, never>(TS2339).switchedis now keyed by path and the enum tested.shapes::uninhabiteddifferential case (native wasm32 vs TS) covering tuples, arrays, nested tuples, struct fields, a nestedOption<Result<_, Void>>, an enum with aVoidvariant, a one-armmatch,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'scharwidenings now use the tuple form.Impact
Alternatives
void scrutinee, or naming it_) leaves a dead read in the output. Rejected per the quest.Follow-ups
None. Behind a reference (
let &(Ok(x), ..) = &pair), rustc treats the variant as reachable and requireslet else, so that shape can't reach this path.Review
quest review, round 1)just cipassed innix developon this head.(written by Claude Opus 5.5)
🤖 Generated with Claude Code