Repository navigation
A variable narrowed before a loop and written in it is read again on each turn and after the loop (TSLANG 235) - #1127
Merged
Merged
Conversation
…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>
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.
TSLANG 235: a narrowing (
if (p !== null), or the body of an enclosingwhile (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.nextnever moved on, earcut's linked-list merge sort pointed links at themselves and hung, and a nestedwhile (n > 0 || p !== null)met a null inp!(exception 0x80000003). The same happened after a plainifnarrowing (number numberfornumber string).Fix (
MLIRGenStatements.cpp,unnarrowWrittenInLoop): before awhile/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 + 1keeps anumber,p = p.nextmakesLanL | 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.narrowedVariableLoadwalks 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 in00tslang_issue_235_types.tsunder all four models; full ctest 10449/10449 again.🤖 Generated with Claude Code