Skip to content

A variable narrowed before a loop and written in it is read again on each turn and after the loop (TSLANG 235) - #1127

Merged
ASDAlexander77 merged 2 commits into
mainfrom
fix-loop-narrowed-reread
Dec 4, 2026
Merged

ASDAlexander77 merged 2 commits into
mainfrom
fix-loop-narrowed-reread

Conversation

@ASDAlexander77

@ASDAlexander77 ASDAlexander77 commented Dec 4, 2026 •

Copy link
Copy Markdown
Owner

TSLANG 235: a narrowing (if (p !== null), or the body of an enclosing while (p !== null)) bound the name to the value the test read once. A loop that assigned the variable kept reading that value in its condition and later turns: p = p.next never moved on, earcut's linked-list merge sort pointed links at themselves and hung, and a nested while (n > 0 || p !== null) met a null in p! (exception 0x80000003). The same happened after a plain if narrowing (number number for number string).

Fix (MLIRGenStatements.cpp, unnarrowWrittenInLoop): before a while/do/for (for-of and for-in lower to these), each narrowed name the loop writes (=, compound, ++/--, destructuring, in nested loops/closures too) becomes the variable again, loaded at each use, in the loop and after it. Its type is the narrowed type plus the types of what the loop writes, as tsc types it at the loop's head: x = x + 1 keeps a number, p = p.next makes L an L | null; a write whose type can't be told leaves the declared type. Not in generators, whose narrowed locals are per-use views already.

Tests:

  • 00tslang_issue_235.ts (gc/rc/none): if / early return / enclosing loop condition narrowing; while, do, for (condition + incrementor), for-of; reads after the loop; the earcut merge sort. Not -mm=own: walking a linked list by its links is a borrow own refuses to store, independent of this fix.
  • 00tslang_issue_235_types.ts (gc/rc/none/own): types at the loop head, for-of over a generator, for-in.

All outputs checked against node. Full ctest: 10449/10449 passed (Windows, Release).

Also fixed (second commit): assigning to a variable narrowed inside another narrowing (if (x !== null) { if (typeof x === "number") x = "s"; }) or to an optional's narrowed value (x?: number | string) was refused with "can't assign to a value of type 'number | string'": only one level of narrowing was taken back to the variable. narrowedVariableLoad walks the casts (and an optional's value) down to the variable's read; the assignment, the re-narrowing after it (an optional of a union narrowed to the member given) and the loop re-read use it. Covered in 00tslang_issue_235_types.ts under all four models; full ctest 10449/10449 again.

🤖 Generated with Claude Code

ASDAlexander77 and others added 2 commits December 4, 2026 17:10
…each turn and after the loop (TSLANG 235)

A narrowing (`if (p !== null)`, the body of an enclosing `while (p !== null)`) bound the name to the value
the test read once; a loop that assigned the variable kept reading that value in its condition and later
turns (`p = p.next` never moved on; earcut's linked-list merge sort hung). Before a while/do/for, each
narrowed name the loop writes is the variable again, loaded at each use, in the loop and after it. Its type
is what it is narrowed to plus what the loop writes, as tsc takes it at the loop's head (`x = x + 1` keeps a
number); a write whose type can't be told leaves the declared type.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ed value, can be assigned

`if (x !== null) { if (typeof x === "number") x = "s"; }` and `x?: number | string` narrowed by `typeof`
refused the assignment ("can't assign to a value of type 'number | string'"): the name was a cast of a cast
(or of the optional's value), and only one level was taken back to the variable. narrowedVariableLoad walks
the narrowings down to the variable's read; the assignment, the narrowing after it (an optional of a union
narrowed to the member given) and the loop's re-read use it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit c564b06 into main Dec 4, 2026
8 checks passed
@ASDAlexander77
ASDAlexander77 deleted the fix-loop-narrowed-reread branch December 4, 2026 18:11
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