Repository navigation
Narrowing by && and ||: no else narrowing for &&, both sides of || - #692
Merged
Merged
Conversation
checkSafeCast handed its one else slot to both sides of `&&`, so the else branch of `x === undefined && k` (false also when only `k` is) read x as defined: an undefined x became a number, and `x === undefined` was false there. It also stopped at a left side that is no test (`k && x !== undefined` narrowed nothing) and had no `||` case. Now `&&` narrows its then branch by both sides and its else branch by neither; `||` narrows its else branch, and what follows when its then branch exits, by both sides (a list of else cases, gathered in a temporary module so the sides' then narrowings emit nothing). The right side of `||` is narrowed by the left being false (`x === undefined || x > 1`). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
What
Narrowing (
checkSafeCast) of a condition joined with&&or||.Miscompile. The else branch of
x === undefined && kreadxas defined.checkSafeCasthanded its one else slot to both sides of&&, so whatx === undefinedbeing false says was applied to the whole&&, which is also false when onlykis. An undefinedxwas read as anumberthere:The early-return form,
if (a === undefined && k) return;, narrowed the rest of the function in the same way.Missed narrowings. These didn't affect the result, since an optional is cast to its value where one is needed, but the variable kept its
T | undefinedtype:k && x !== undefined: the test stopped atk, a left side that is no test.||had no case. Nothing narrowed its else branch, the code afterif (x === undefined || k) return;, or the right side ofx === undefined || x > 1.How
&&: both sides narrow the then branch, whichever fails. Neither narrows the else branch.||: neither side narrows the then branch. The else branch (and what follows an exiting then branch) gets what each side being false gives. That is now a list,ElseSafeCases, gathered bycollectElseSafeCasesin a temporary module, so the sides' then-narrowings emit nothing.mlirGenAndOrLogic: the right side of||is narrowed by the left side being false. Before, the else case was gathered only for&&, where it isn't used.The
if, ternary and&&/||callers now take the list (addElseSafeCastStatements).Tests
tslang/test/tester/tests/00narrowing_and_or.ts, run compiled and through the JIT (the rc/none corpus runs pick it up too). It covers the else branch of&&(both branches and early return),k && x !== undefined, two narrowings in one&&, the||else branch, the early return and right side, the||then branch (stays optional),| null,typeofunions, and a ternary.assertion failed).ctest -C Release: 100% passed, 5798/5798 (3 disabled as before).Independent of #691, which fixes enum-to-enum comparison. Comparing a narrowed enum needs both.
🤖 Generated with Claude Code