Skip to content

Commit 2facf71

Browse files
committed
Discard results computed from a binding pattern's own elements
While the type implied by a binding pattern is computed as the contextual type of its initializer, references to the pattern's elements from the elements' defaults are any. The calls and types computed from them were cached anyway, so asking for the contextual type of the initializer first left `extra` in const { kind, payload, extra = kind === "a" ? ... : ... } = u; as any rather than unknown. TypeScript behaves the same way. Track the patterns as resolutions in progress rather than a separate stack, so that a reference returning any marks the computation circular and what was cached from it is discarded. (seen in check order fuzzing)
1 parent 912d417 commit 2facf71

2 files changed

Lines changed: 12 additions & 8 deletions

File tree

‎tsc/internal/checker/checker.go‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ const (
8080
TypeSystemPropertyNameCallResolution
8181
TypeSystemPropertyNameAssertionOperand
8282
TypeSystemPropertyNamePatternInitializer
83+
TypeSystemPropertyNameContextualBindingPattern
8384
)
8485

8586
type TypeResolution struct {
@@ -686,7 +687,6 @@ type Checker struct {
686687
reportUnmeasurableMapper *TypeMapper
687688
restrictiveMapper *TypeMapper
688689
permissiveMapper *TypeMapper
689-
contextualBindingPatterns []*ast.Node
690690
resolving resolvingTable // Index in resolutions of the outermost computation of each key in progress
691691
typeResolutionTop int // 1-based index in resolutions of the innermost resolution, or 0
692692
// Like typeResolutionTop, but skipping the type resolutions of current
@@ -11786,8 +11786,9 @@ func (c *Checker) checkIdentifier(node *ast.Node, checkMode CheckMode) *Type {
1178611786
// If the identifier is declared in a binding pattern for which we're currently computing the implied type and the
1178711787
// reference occurs with the same binding pattern, return the non-inferrable any type. This for example occurs in
1178811788
// 'const [a, b = a + 1] = [2]' when we're computing the contextual type for the array literal '[2]'.
11789-
if declaration != nil && declaration.Kind == ast.KindBindingElement && slices.Contains(c.contextualBindingPatterns, declaration.Parent) &&
11790-
ast.FindAncestor(node, func(parent *ast.Node) bool { return parent == declaration.Parent }) != nil {
11789+
if declaration != nil && declaration.Kind == ast.KindBindingElement &&
11790+
ast.FindAncestor(node, func(parent *ast.Node) bool { return parent == declaration.Parent }) != nil &&
11791+
c.isResolvingAnywhere(declaration.Parent, TypeSystemPropertyNameContextualBindingPattern) {
1179111792
return c.nonInferrableAnyType
1179211793
}
1179311794
t := c.getNarrowedTypeOfSymbol(localOrExportSymbol, node)
@@ -19376,7 +19377,10 @@ func (c *Checker) getParentElementAccess(node *ast.Node) *ast.Node {
1937619377
// the parameter.
1937719378
func (c *Checker) getTypeFromBindingPattern(pattern *ast.Node, includePatternInType bool, reportErrors bool) *Type {
1937819379
if includePatternInType {
19379-
c.contextualBindingPatterns = append(c.contextualBindingPatterns, pattern)
19380+
// While the implied type is computed, references to the pattern's own
19381+
// elements are any (see checkIdentifier), so nothing computed from
19382+
// them may be cached; see isResolvingAnywhere.
19383+
c.beginTypeResolution(pattern, TypeSystemPropertyNameContextualBindingPattern)
1938019384
}
1938119385
var result *Type
1938219386
if ast.IsObjectBindingPattern(pattern) {
@@ -19385,7 +19389,7 @@ func (c *Checker) getTypeFromBindingPattern(pattern *ast.Node, includePatternInT
1938519389
result = c.getTypeFromArrayBindingPattern(pattern, includePatternInType, reportErrors)
1938619390
}
1938719391
if includePatternInType {
19388-
c.contextualBindingPatterns = c.contextualBindingPatterns[:len(c.contextualBindingPatterns)-1]
19392+
c.popTypeResolution()
1938919393
}
1939019394
return result
1939119395
}
@@ -20937,7 +20941,7 @@ func (c *Checker) typeResolutionHasProperty(r *TypeResolution) bool {
2093720941
TypeSystemPropertyNameSimplifiedForReading, TypeSystemPropertyNameSimplifiedForWriting, TypeSystemPropertyNameDefaultType,
2093820942
TypeSystemPropertyNameAwaitedType, TypeSystemPropertyNameExplicitType,
2093920943
TypeSystemPropertyNameBindingElementParent, TypeSystemPropertyNameCallResolution,
20940-
TypeSystemPropertyNameAssertionOperand, TypeSystemPropertyNamePatternInitializer:
20944+
TypeSystemPropertyNameAssertionOperand, TypeSystemPropertyNamePatternInitializer, TypeSystemPropertyNameContextualBindingPattern:
2094120945
// Marked as checked or resolved on entry, so it is only on the stack
2094220946
// while in progress.
2094320947
return false

‎tsc/testdata/baselines/reference/compiler/dependentDestructuringBindingElementInitializer(checkorder=typesfirst).types‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ const {
2525
>payload : string | number
2626

2727
extra = kind === "a" ? payload.toFixed() : payload.toUpperCase(),
28-
>extra : any
29-
>kind === "a" ? payload.toFixed() : payload.toUpperCase() : any
28+
>extra : unknown
29+
>kind === "a" ? payload.toFixed() : payload.toUpperCase() : string
3030
>kind === "a" : boolean
3131
>kind : "a" | "b"
3232
>"a" : "a"

0 commit comments

Comments
 (0)