Skip to content

Narrowing by && and ||: no else narrowing for &&, both sides of || - #692

Merged
ASDAlexander77 merged 2 commits into
mainfrom
narrowing-and-or
Oct 26, 2026
Merged

ASDAlexander77 merged 2 commits into
mainfrom
narrowing-and-or

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

What

Narrowing (checkSafeCast) of a condition joined with && or ||.

Miscompile. The else branch of x === undefined && k read x as defined. checkSafeCast handed its one else slot to both sides of &&, so what x === undefined being false says was applied to the whole &&, which is also false when only k is. An undefined x was read as a number there:

function f(a: number | undefined, k: boolean) {
    if (a === undefined && k) { return "then"; } else { return "else " + (a === undefined); }
}
f(undefined, false);   // was "else false"

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 | undefined type:

  • k && x !== undefined: the test stopped at k, a left side that is no test.
  • || had no case. Nothing narrowed its else branch, the code after if (x === undefined || k) return;, or the right side of x === 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 by collectElseSafeCases in 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

  • New: 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, typeof unions, and a ternary.
  • The new test fails without the fix (assertion failed).
  • Windows Release: 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

ASDAlexander77 and others added 2 commits October 26, 2026 10:31
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>
@ASDAlexander77
ASDAlexander77 merged commit 834d1dc into main Oct 26, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the narrowing-and-or branch October 26, 2026 11:30
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