Fix #653/#654: variable reassignment flow narrowing + widen all enclosing guards on escape#744
Merged
Merged
Conversation
…ing guards on escape #653: after `x = v`, a variable read as its full declared type even when the RHS put it safely inside that type, so SharpTS rejected code tsc accepts (`x = "s"` on a `string | null` `x`, then `x.length`). CheckAssign now restores a tracked variable to the post-write flow-narrowed type (declared union filtered to the RHS-compatible members, via NarrowToDeclaredSlot) instead of unconditionally to the declared type — mirroring the property-write narrowing of #48. The narrowed binding lives in the current lexical scope and is discarded at its block's join, so it can't leak past a conditional. Two guards keep it sound: only function-local/parameter variables narrow (module vars aren't tracked in the declared-type stack — see #743), and a purely-nullish slot is not installed so a later access still raises "possibly null/undefined" (the bare-nullish access gap is #742). #654: WidenEnclosingNarrowing now widens EVERY enclosing scope that holds a narrowing of the reassigned variable, not just the nearest. When two guards narrowed the same variable and an escaping reassignment sat under the inner one, only the inner guard was widened, so a read at the outer level after the inner block kept the stale outer narrowing (a soundness hole — SharpTS accepted code tsc rejects). The walk stops at the variable's declaration (binding == declared type) so it never crosses into an outer, same-named shadowing variable whose own narrowing is still valid. Tests: new VariableAssignmentFlowNarrowingTests (#653, companion to the #48 property tests) and three #654 cases in NestedReassignmentNarrowingTests (verbatim repro, three-level nesting, and a shadowing non-regression guard).
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.
Fixes #653 and #654 — two related gaps in the type checker's control-flow narrowing of variables across assignments.
#653 — reassignment now narrows to the RHS type (was over-strict)
SharpTS narrowed a property slot to the RHS type after a write (
o.x = "s"→o.x.lengthok, #48) but did not do the equivalent for a variable, so it rejected codetscaccepts:CheckAssignpreviously restored the declared type after every variable write. It now restores the post-write flow-narrowed type — the declared union filtered to the members the RHS can be (NarrowToDeclaredSlot, the same primitive #48 uses for properties). The narrowed binding lives in the current lexical scope and is discarded at its block's join, so it can't leak a too-narrow type past a conditional.Two guards keep it sound and non-regressive:
null/undefined) is not installed; the variable keeps its declared union. Property access on a barenull/undefinedisn't flagged yet (only on a union —CheckGetOnUnion), so narrowingx = undefinedtoundefinedwould silently drop the "possibly undefined" error a laterx.lengthmust still raise. Filed as Property access on a barenull/undefinedtype is not flagged (only on a union) #742.#654 — escaping reassignment now widens every enclosing guard (was a soundness hole)
if-guard variable narrowing is applied by redefining the variable in a childTypeEnvironment.WidenEnclosingNarrowing(the #570 fix) widened only the nearest enclosing rebinding on an escaping reassignment, so with two guards on the same variable the outer guard's narrowing stayed stale. It now walks outward and widens every enclosing scope that narrows the variable, stopping at the declaration (binding == declared type) so it never crosses into an outer same-named shadowing variable.Tests
VariableAssignmentFlowNarrowingTests— Variable reassignment doesn't narrow to the RHS type (over-strict; tsc accepts) #653 companion to the Type checker: narrowing doesn't flow across assignments #48 property tests: basic reassign, guard-recovery path, literal-union member preservation, compatible-in-guard, cross-member reassign checks against declared, nullish soundness, RHS-spans-union no-op, and a conditional-escape soundness guard.NestedReassignmentNarrowingTestsfor #570 follow-up: nested guards on the SAME variable can leave a stale outer narrowing #654: verbatim repro, three-level nesting, and a shadowing non-regression guard.Verification
assignmentCompatibility,conditional): 31 passed, 0 failed — no baseline diff..jswith type-checking off, so its baseline can't move.Follow-ups filed
null/undefinedtype is not flagged (only on a union) #742 — flag property access on a barenull/undefinedtype (lets theIsPurelyNullishguard be removed for full property parity).