Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -13701,8 +13701,15 @@ func (c *Checker) getNarrowedTypeOfSymbol(symbol *ast.Symbol, location *ast.Node
// as if it occurred in the specified location. We then recompute the narrowed binding element type by
// destructuring from the narrowed parent type.
case ast.IsBindingElement(declaration) && declaration.Initializer() == nil && !hasDotDotDotToken(declaration) && len(declaration.Parent.Elements()) >= 2:
Comment on lines +13722 to +13723

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#2542 pushed this to the latter line - and that's what broke the reported case

But now, thanks to the introduced rootInitializer check, we just never enter this entire branch. It seems to me that thanks to that we can push back this flag reset to an earlier spot (that is required to fix one of the tests I added as part of this PR)

rootDeclaration := ast.GetRootDeclaration(declaration)
rootInitializer := rootDeclaration.Initializer()
// Avoid declaration circularity without blocking binding defaults or nested callbacks.
if rootInitializer != nil &&
ast.IsNodeDescendantOf(location, rootInitializer) &&
c.getControlFlowContainer(declaration) == c.getControlFlowContainer(location) {
break
}
parent := declaration.Parent.Parent
rootDeclaration := ast.GetRootDeclaration(parent)
if ast.IsVariableDeclaration(rootDeclaration) && c.getCombinedNodeFlagsCached(rootDeclaration)&ast.NodeFlagsConstant != 0 || ast.IsParameterDeclaration(rootDeclaration) {
links := c.nodeLinks.Get(parent)
if links.flags&NodeCheckFlagsInCheckIdentifier == 0 {
Expand All @@ -13712,6 +13719,8 @@ func (c *Checker) getNarrowedTypeOfSymbol(symbol *ast.Symbol, location *ast.Node
if parentType != nil {
parentTypeConstraint = c.mapType(parentType, c.getBaseConstraintOrType)
}
// Guard parent-type resolution only; flow analysis should allow re-entrant narrowing
links.flags &^= NodeCheckFlagsInCheckIdentifier
if parentTypeConstraint != nil && parentTypeConstraint.flags&TypeFlagsUnion != 0 && !(ast.IsParameterDeclaration(rootDeclaration) && c.isSomeSymbolAssigned(rootDeclaration)) {
pattern := declaration.Parent
narrowedType := c.getFlowTypeOfReferenceEx(pattern, parentTypeConstraint, parentTypeConstraint, nil /*flowContainer*/, getFlowNodeOfNode(location))
Expand All @@ -13724,7 +13733,6 @@ func (c *Checker) getNarrowedTypeOfSymbol(symbol *ast.Symbol, location *ast.Node
t = c.getBindingElementTypeFromParentType(declaration, narrowedType, true /*noTupleBoundsCheck*/)
}
}
links.flags &^= NodeCheckFlagsInCheckIdentifier
}
}
// If we have a const-like parameter with no type annotation or initializer, and if the parameter is contextually
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//// [tests/cases/compiler/controlFlowDependentDestructuringOverloadedAssertion.ts] ////

=== controlFlowDependentDestructuringOverloadedAssertion.ts ===
// https://github.com/microsoft/typescript-go/issues/4572

type State =
>State : Symbol(State, Decl(controlFlowDependentDestructuringOverloadedAssertion.ts, 0, 0))

| { status: "ready"; payload: { value?: string } }
>status : Symbol(status, Decl(controlFlowDependentDestructuringOverloadedAssertion.ts, 3, 5))
>payload : Symbol(payload, Decl(controlFlowDependentDestructuringOverloadedAssertion.ts, 3, 22))
>value : Symbol(value, Decl(controlFlowDependentDestructuringOverloadedAssertion.ts, 3, 33))

| { status: "pending"; payload?: undefined };
>status : Symbol(status, Decl(controlFlowDependentDestructuringOverloadedAssertion.ts, 4, 5))
>payload : Symbol(payload, Decl(controlFlowDependentDestructuringOverloadedAssertion.ts, 4, 24))

declare function getState(): State;
>getState : Symbol(getState, Decl(controlFlowDependentDestructuringOverloadedAssertion.ts, 4, 47))
>State : Symbol(State, Decl(controlFlowDependentDestructuringOverloadedAssertion.ts, 0, 0))

declare function assert(condition: boolean): asserts condition;
>assert : Symbol(assert, Decl(controlFlowDependentDestructuringOverloadedAssertion.ts, 6, 35), Decl(controlFlowDependentDestructuringOverloadedAssertion.ts, 7, 63))
>condition : Symbol(condition, Decl(controlFlowDependentDestructuringOverloadedAssertion.ts, 7, 24))
>condition : Symbol(condition, Decl(controlFlowDependentDestructuringOverloadedAssertion.ts, 7, 24))

declare function assert(condition: boolean, message: string): asserts condition;
>assert : Symbol(assert, Decl(controlFlowDependentDestructuringOverloadedAssertion.ts, 6, 35), Decl(controlFlowDependentDestructuringOverloadedAssertion.ts, 7, 63))
>condition : Symbol(condition, Decl(controlFlowDependentDestructuringOverloadedAssertion.ts, 8, 24))
>message : Symbol(message, Decl(controlFlowDependentDestructuringOverloadedAssertion.ts, 8, 43))
>condition : Symbol(condition, Decl(controlFlowDependentDestructuringOverloadedAssertion.ts, 8, 24))

const readValue = () => {
>readValue : Symbol(readValue, Decl(controlFlowDependentDestructuringOverloadedAssertion.ts, 10, 5))

const { status, payload } = getState();
>status : Symbol(status, Decl(controlFlowDependentDestructuringOverloadedAssertion.ts, 11, 9))
>payload : Symbol(payload, Decl(controlFlowDependentDestructuringOverloadedAssertion.ts, 11, 17))
>getState : Symbol(getState, Decl(controlFlowDependentDestructuringOverloadedAssertion.ts, 4, 47))

if (status !== "ready") throw new Error("Value is not ready.");
>status : Symbol(status, Decl(controlFlowDependentDestructuringOverloadedAssertion.ts, 11, 9))
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2022.error.d.ts, --, --))

assert(payload.value !== undefined);
>assert : Symbol(assert, Decl(controlFlowDependentDestructuringOverloadedAssertion.ts, 6, 35), Decl(controlFlowDependentDestructuringOverloadedAssertion.ts, 7, 63))
>payload.value : Symbol(value, Decl(controlFlowDependentDestructuringOverloadedAssertion.ts, 3, 33))
>payload : Symbol(payload, Decl(controlFlowDependentDestructuringOverloadedAssertion.ts, 11, 17))
>value : Symbol(value, Decl(controlFlowDependentDestructuringOverloadedAssertion.ts, 3, 33))
>undefined : Symbol(undefined)

return payload.value;
>payload.value : Symbol(value, Decl(controlFlowDependentDestructuringOverloadedAssertion.ts, 3, 33))
>payload : Symbol(payload, Decl(controlFlowDependentDestructuringOverloadedAssertion.ts, 11, 17))
>value : Symbol(value, Decl(controlFlowDependentDestructuringOverloadedAssertion.ts, 3, 33))

};

Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//// [tests/cases/compiler/controlFlowDependentDestructuringOverloadedAssertion.ts] ////

=== controlFlowDependentDestructuringOverloadedAssertion.ts ===
// https://github.com/microsoft/typescript-go/issues/4572

type State =
>State : State

| { status: "ready"; payload: { value?: string } }
>status : "ready"
>payload : { value?: string; }
>value : string | undefined

| { status: "pending"; payload?: undefined };
>status : "pending"
>payload : undefined

declare function getState(): State;
>getState : () => State

declare function assert(condition: boolean): asserts condition;
>assert : { (condition: boolean): asserts condition; (condition: boolean, message: string): asserts condition; }
>condition : boolean

declare function assert(condition: boolean, message: string): asserts condition;
>assert : { (condition: boolean): asserts condition; (condition: boolean, message: string): asserts condition; }
>condition : boolean
>message : string

const readValue = () => {
>readValue : () => string
>() => { const { status, payload } = getState(); if (status !== "ready") throw new Error("Value is not ready."); assert(payload.value !== undefined); return payload.value;} : () => string

const { status, payload } = getState();
>status : "pending" | "ready"
>payload : { value?: string; } | undefined
>getState() : State
>getState : () => State

if (status !== "ready") throw new Error("Value is not ready.");
>status !== "ready" : boolean
>status : "pending" | "ready"
>"ready" : "ready"
>new Error("Value is not ready.") : Error
>Error : ErrorConstructor
>"Value is not ready." : "Value is not ready."

assert(payload.value !== undefined);
>assert(payload.value !== undefined) : void
>assert : { (condition: boolean): asserts condition; (condition: boolean, message: string): asserts condition; }
>payload.value !== undefined : boolean
>payload.value : string | undefined
>payload : { value?: string; }
>value : string | undefined
>undefined : undefined

return payload.value;
>payload.value : string
>payload : { value?: string; }
>value : string

};

Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//// [tests/cases/compiler/dependentDestructuringBindingElementInitializer.ts] ////

=== dependentDestructuringBindingElementInitializer.ts ===
type U =
>U : Symbol(U, Decl(dependentDestructuringBindingElementInitializer.ts, 0, 0))

| { kind: "a"; payload: number; extra?: unknown }
>kind : Symbol(kind, Decl(dependentDestructuringBindingElementInitializer.ts, 1, 7))
>payload : Symbol(payload, Decl(dependentDestructuringBindingElementInitializer.ts, 1, 18))
>extra : Symbol(extra, Decl(dependentDestructuringBindingElementInitializer.ts, 1, 35))

| { kind: "b"; payload: string; extra?: unknown };
>kind : Symbol(kind, Decl(dependentDestructuringBindingElementInitializer.ts, 2, 7))
>payload : Symbol(payload, Decl(dependentDestructuringBindingElementInitializer.ts, 2, 18))
>extra : Symbol(extra, Decl(dependentDestructuringBindingElementInitializer.ts, 2, 35))

declare const u: U;
>u : Symbol(u, Decl(dependentDestructuringBindingElementInitializer.ts, 4, 13))
>U : Symbol(U, Decl(dependentDestructuringBindingElementInitializer.ts, 0, 0))

const {
kind,
>kind : Symbol(kind, Decl(dependentDestructuringBindingElementInitializer.ts, 6, 7))

payload,
>payload : Symbol(payload, Decl(dependentDestructuringBindingElementInitializer.ts, 7, 9))

extra = kind === "a" ? payload.toFixed() : payload.toUpperCase(),
>extra : Symbol(extra, Decl(dependentDestructuringBindingElementInitializer.ts, 8, 12))
>kind : Symbol(kind, Decl(dependentDestructuringBindingElementInitializer.ts, 6, 7))
>payload.toFixed : Symbol(Number.toFixed, Decl(lib.es5.d.ts, --, --))
>payload : Symbol(payload, Decl(dependentDestructuringBindingElementInitializer.ts, 7, 9))
>toFixed : Symbol(Number.toFixed, Decl(lib.es5.d.ts, --, --))
>payload.toUpperCase : Symbol(String.toUpperCase, Decl(lib.es5.d.ts, --, --))
>payload : Symbol(payload, Decl(dependentDestructuringBindingElementInitializer.ts, 7, 9))
>toUpperCase : Symbol(String.toUpperCase, Decl(lib.es5.d.ts, --, --))

} = u;
>u : Symbol(u, Decl(dependentDestructuringBindingElementInitializer.ts, 4, 13))

function f({
>f : Symbol(f, Decl(dependentDestructuringBindingElementInitializer.ts, 10, 6))

kind,
>kind : Symbol(kind, Decl(dependentDestructuringBindingElementInitializer.ts, 12, 12))

payload,
>payload : Symbol(payload, Decl(dependentDestructuringBindingElementInitializer.ts, 13, 9))

extra = kind === "a" ? payload.toFixed() : payload.toUpperCase(),
>extra : Symbol(extra, Decl(dependentDestructuringBindingElementInitializer.ts, 14, 12))
>kind : Symbol(kind, Decl(dependentDestructuringBindingElementInitializer.ts, 12, 12))
>payload.toFixed : Symbol(Number.toFixed, Decl(lib.es5.d.ts, --, --))
>payload : Symbol(payload, Decl(dependentDestructuringBindingElementInitializer.ts, 13, 9))
>toFixed : Symbol(Number.toFixed, Decl(lib.es5.d.ts, --, --))
>payload.toUpperCase : Symbol(String.toUpperCase, Decl(lib.es5.d.ts, --, --))
>payload : Symbol(payload, Decl(dependentDestructuringBindingElementInitializer.ts, 13, 9))
>toUpperCase : Symbol(String.toUpperCase, Decl(lib.es5.d.ts, --, --))

}: U) {}
>U : Symbol(U, Decl(dependentDestructuringBindingElementInitializer.ts, 0, 0))

Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//// [tests/cases/compiler/dependentDestructuringBindingElementInitializer.ts] ////

=== dependentDestructuringBindingElementInitializer.ts ===
type U =
>U : U

| { kind: "a"; payload: number; extra?: unknown }
>kind : "a"
>payload : number
>extra : unknown

| { kind: "b"; payload: string; extra?: unknown };
>kind : "b"
>payload : string
>extra : unknown

declare const u: U;
>u : U

const {
kind,
>kind : "a" | "b"

payload,
>payload : string | number

extra = kind === "a" ? payload.toFixed() : payload.toUpperCase(),
>extra : unknown
>kind === "a" ? payload.toFixed() : payload.toUpperCase() : string
>kind === "a" : boolean
>kind : "a" | "b"
>"a" : "a"
>payload.toFixed() : string
>payload.toFixed : (fractionDigits?: number) => string
>payload : number
>toFixed : (fractionDigits?: number) => string
>payload.toUpperCase() : string
>payload.toUpperCase : () => string
>payload : string
>toUpperCase : () => string

} = u;
>u : U

function f({
>f : ({ kind, payload, extra, }: U) => void

kind,
>kind : "a" | "b"

payload,
>payload : string | number

extra = kind === "a" ? payload.toFixed() : payload.toUpperCase(),
>extra : unknown
>kind === "a" ? payload.toFixed() : payload.toUpperCase() : string
>kind === "a" : boolean
>kind : "a" | "b"
>"a" : "a"
>payload.toFixed() : string
>payload.toFixed : (fractionDigits?: number) => string
>payload : number
>toFixed : (fractionDigits?: number) => string
>payload.toUpperCase() : string
>payload.toUpperCase : () => string
>payload : string
>toUpperCase : () => string

}: U) {}

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//// [tests/cases/compiler/dependentDestructuringCrossFilePosition.ts] ////

=== types.d.ts ===
type U =
>U : Symbol(U, Decl(types.d.ts, 0, 0))

| { kind: "a"; payload: number }
>kind : Symbol(kind, Decl(types.d.ts, 1, 7))
>payload : Symbol(payload, Decl(types.d.ts, 1, 18))

| { kind: "b"; payload: string };
>kind : Symbol(kind, Decl(types.d.ts, 2, 7))
>payload : Symbol(payload, Decl(types.d.ts, 2, 18))

declare function make(): U;
>make : Symbol(make, Decl(types.d.ts, 2, 37))
>U : Symbol(U, Decl(types.d.ts, 0, 0))

=== a.ts ===
const { kind, payload }: U = make();
>kind : Symbol(kind, Decl(a.ts, 0, 7))
>payload : Symbol(payload, Decl(a.ts, 0, 13))
>U : Symbol(U, Decl(types.d.ts, 0, 0))
>make : Symbol(make, Decl(types.d.ts, 2, 37))

=== b.ts ===
if (kind === "a") payload.toFixed();
>kind : Symbol(kind, Decl(a.ts, 0, 7))
>payload.toFixed : Symbol(Number.toFixed, Decl(lib.es5.d.ts, --, --))
>payload : Symbol(payload, Decl(a.ts, 0, 13))
>toFixed : Symbol(Number.toFixed, Decl(lib.es5.d.ts, --, --))

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//// [tests/cases/compiler/dependentDestructuringCrossFilePosition.ts] ////

=== types.d.ts ===
type U =
>U : U

| { kind: "a"; payload: number }
>kind : "a"
>payload : number

| { kind: "b"; payload: string };
>kind : "b"
>payload : string

declare function make(): U;
>make : () => U

=== a.ts ===
const { kind, payload }: U = make();
>kind : "a" | "b"
>payload : string | number
>make() : U
>make : () => U

=== b.ts ===
if (kind === "a") payload.toFixed();
>kind === "a" : boolean
>kind : "a" | "b"
>"a" : "a"
>payload.toFixed() : string
>payload.toFixed : (fractionDigits?: number) => string
>payload : number
>toFixed : (fractionDigits?: number) => string

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//// [tests/cases/compiler/dependentDestructuringDeferredCallback.ts] ////

=== dependentDestructuringDeferredCallback.ts ===
type U =
>U : Symbol(U, Decl(dependentDestructuringDeferredCallback.ts, 0, 0))

| { kind: "a"; payload: number; callback: () => void }
>kind : Symbol(kind, Decl(dependentDestructuringDeferredCallback.ts, 1, 7))
>payload : Symbol(payload, Decl(dependentDestructuringDeferredCallback.ts, 1, 18))
>callback : Symbol(callback, Decl(dependentDestructuringDeferredCallback.ts, 1, 35))

| { kind: "b"; payload: string; callback: () => void };
>kind : Symbol(kind, Decl(dependentDestructuringDeferredCallback.ts, 2, 7))
>payload : Symbol(payload, Decl(dependentDestructuringDeferredCallback.ts, 2, 18))
>callback : Symbol(callback, Decl(dependentDestructuringDeferredCallback.ts, 2, 35))

const { kind, payload, callback }: U = {
>kind : Symbol(kind, Decl(dependentDestructuringDeferredCallback.ts, 4, 7))
>payload : Symbol(payload, Decl(dependentDestructuringDeferredCallback.ts, 4, 13))
>callback : Symbol(callback, Decl(dependentDestructuringDeferredCallback.ts, 4, 22))
>U : Symbol(U, Decl(dependentDestructuringDeferredCallback.ts, 0, 0))

kind: "a",
>kind : Symbol(kind, Decl(dependentDestructuringDeferredCallback.ts, 4, 40))

payload: 1,
>payload : Symbol(payload, Decl(dependentDestructuringDeferredCallback.ts, 5, 14))

callback: () => {
>callback : Symbol(callback, Decl(dependentDestructuringDeferredCallback.ts, 6, 15))

if (kind === "a") payload.toFixed();
>kind : Symbol(kind, Decl(dependentDestructuringDeferredCallback.ts, 4, 7))
>payload.toFixed : Symbol(Number.toFixed, Decl(lib.es5.d.ts, --, --))
>payload : Symbol(payload, Decl(dependentDestructuringDeferredCallback.ts, 4, 13))
>toFixed : Symbol(Number.toFixed, Decl(lib.es5.d.ts, --, --))

},
};

Loading