diff --git a/tsc/internal/checker/checker.go b/tsc/internal/checker/checker.go index 41e76dc37b730..36382bbb3bdc3 100644 --- a/tsc/internal/checker/checker.go +++ b/tsc/internal/checker/checker.go @@ -28308,7 +28308,29 @@ func (c *Checker) getBaseConstraintOfType(t *Type) *Type { return nil } -func (c *Checker) getResolvedBaseConstraint(t *Type, stack []RecursionId) *Type { +// baseConstraintFrame records a type's recursion identity and type ID on the +// getResolvedBaseConstraint stack. +type baseConstraintFrame struct { + identity RecursionId + id TypeId +} + +// repeatsInBaseConstraintWalk reports whether the candidate type shares a +// recursion identity with a frame on stack and has an equal or higher type ID. +// Type IDs are assigned in creation order: expanding instantiations +// (Foo -> Foo>) create types with higher IDs, while walking into +// component types with the same identity, such as the object type of an +// indexed access (T["a"]["a"] -> T["a"]), moves to lower IDs and terminates. +func repeatsInBaseConstraintWalk(identity RecursionId, id TypeId, stack []baseConstraintFrame) bool { + for _, f := range stack { + if f.identity == identity && id >= f.id { + return true + } + } + return false +} + +func (c *Checker) getResolvedBaseConstraint(t *Type, stack []baseConstraintFrame) *Type { constrained := t.AsConstrainedType() if constrained == nil { return t @@ -28327,8 +28349,8 @@ func (c *Checker) getResolvedBaseConstraint(t *Type, stack []RecursionId) *Type // yet triggered the deeply nested limiter. We have no test cases that actually get to 50 levels of // nesting, so it is effectively just a safety stop. identity := getRecursionIdentity(t) - if len(stack) < 10 || len(stack) < 50 && !slices.Contains(stack, identity) { - constraint = c.computeBaseConstraint(c.getSimplifiedType(t /*writing*/, false), append(stack, identity)) + if len(stack) < 10 || len(stack) < 50 && !repeatsInBaseConstraintWalk(identity, t.id, stack) { + constraint = c.computeBaseConstraint(c.getSimplifiedType(t /*writing*/, false), append(stack, baseConstraintFrame{identity, t.id})) } if !c.popTypeResolution() { if t.flags&TypeFlagsTypeParameter != 0 { @@ -28351,7 +28373,7 @@ func (c *Checker) getResolvedBaseConstraint(t *Type, stack []RecursionId) *Type return constraint } -func (c *Checker) computeBaseConstraint(t *Type, stack []RecursionId) *Type { +func (c *Checker) computeBaseConstraint(t *Type, stack []baseConstraintFrame) *Type { switch { case t.flags&TypeFlagsTypeParameter != 0: constraint := c.getConstraintFromTypeParameter(t) @@ -28454,7 +28476,7 @@ func (c *Checker) computeBaseConstraint(t *Type, stack []RecursionId) *Type { return t } -func (c *Checker) getNextBaseConstraint(t *Type, stack []RecursionId) *Type { +func (c *Checker) getNextBaseConstraint(t *Type, stack []baseConstraintFrame) *Type { if t == nil { return nil } diff --git a/tsc/testdata/baselines/reference/compiler/deepIndexedAccessChainConstraint.symbols b/tsc/testdata/baselines/reference/compiler/deepIndexedAccessChainConstraint.symbols new file mode 100644 index 0000000000000..faaa14d10c054 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/deepIndexedAccessChainConstraint.symbols @@ -0,0 +1,63 @@ +//// [tests/cases/compiler/deepIndexedAccessChainConstraint.ts] //// + +=== deepIndexedAccessChainConstraint.ts === +// Resolving the base constraint of a 10-level indexed access chain steps +// through nested object types that share the type parameter's recursion +// identity. Checking f resolves typeof victim while T's base constraint is +// uncached; in both victim and lone, base constraints resolve to Box (65508). + +interface Box { a: Box; b: string } +>Box : Symbol(Box, Decl(deepIndexedAccessChainConstraint.ts, 0, 0)) +>a : Symbol(Box.a, Decl(deepIndexedAccessChainConstraint.ts, 5, 15)) +>Box : Symbol(Box, Decl(deepIndexedAccessChainConstraint.ts, 0, 0)) +>b : Symbol(Box.b, Decl(deepIndexedAccessChainConstraint.ts, 5, 23)) + +const f: typeof victim = (x: U, y: { b: string }) => x.b; +>f : Symbol(f, Decl(deepIndexedAccessChainConstraint.ts, 7, 5)) +>victim : Symbol(victim, Decl(deepIndexedAccessChainConstraint.ts, 7, 72)) +>U : Symbol(U, Decl(deepIndexedAccessChainConstraint.ts, 7, 26)) +>Box : Symbol(Box, Decl(deepIndexedAccessChainConstraint.ts, 0, 0)) +>x : Symbol(x, Decl(deepIndexedAccessChainConstraint.ts, 7, 41)) +>U : Symbol(U, Decl(deepIndexedAccessChainConstraint.ts, 7, 26)) +>y : Symbol(y, Decl(deepIndexedAccessChainConstraint.ts, 7, 46)) +>b : Symbol(b, Decl(deepIndexedAccessChainConstraint.ts, 7, 51)) +>x.b : Symbol(Box.b, Decl(deepIndexedAccessChainConstraint.ts, 5, 23)) +>x : Symbol(x, Decl(deepIndexedAccessChainConstraint.ts, 7, 41)) +>b : Symbol(Box.b, Decl(deepIndexedAccessChainConstraint.ts, 5, 23)) + +function victim( +>victim : Symbol(victim, Decl(deepIndexedAccessChainConstraint.ts, 7, 72)) +>T : Symbol(T, Decl(deepIndexedAccessChainConstraint.ts, 9, 16)) +>Box : Symbol(Box, Decl(deepIndexedAccessChainConstraint.ts, 0, 0)) + + x: T, +>x : Symbol(x, Decl(deepIndexedAccessChainConstraint.ts, 9, 31)) +>T : Symbol(T, Decl(deepIndexedAccessChainConstraint.ts, 9, 16)) + + y: T["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"] +>y : Symbol(y, Decl(deepIndexedAccessChainConstraint.ts, 10, 9)) +>T : Symbol(T, Decl(deepIndexedAccessChainConstraint.ts, 9, 16)) + +): string { + return x.b + y.b; +>x.b : Symbol(Box.b, Decl(deepIndexedAccessChainConstraint.ts, 5, 23)) +>x : Symbol(x, Decl(deepIndexedAccessChainConstraint.ts, 9, 31)) +>b : Symbol(Box.b, Decl(deepIndexedAccessChainConstraint.ts, 5, 23)) +>y.b : Symbol(Box.b, Decl(deepIndexedAccessChainConstraint.ts, 5, 23)) +>y : Symbol(y, Decl(deepIndexedAccessChainConstraint.ts, 10, 9)) +>b : Symbol(Box.b, Decl(deepIndexedAccessChainConstraint.ts, 5, 23)) +} + +function lone(y: U["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]): string { +>lone : Symbol(lone, Decl(deepIndexedAccessChainConstraint.ts, 14, 1)) +>U : Symbol(U, Decl(deepIndexedAccessChainConstraint.ts, 16, 14)) +>Box : Symbol(Box, Decl(deepIndexedAccessChainConstraint.ts, 0, 0)) +>y : Symbol(y, Decl(deepIndexedAccessChainConstraint.ts, 16, 29)) +>U : Symbol(U, Decl(deepIndexedAccessChainConstraint.ts, 16, 14)) + + return y.b; +>y.b : Symbol(Box.b, Decl(deepIndexedAccessChainConstraint.ts, 5, 23)) +>y : Symbol(y, Decl(deepIndexedAccessChainConstraint.ts, 16, 29)) +>b : Symbol(Box.b, Decl(deepIndexedAccessChainConstraint.ts, 5, 23)) +} + diff --git a/tsc/testdata/baselines/reference/compiler/deepIndexedAccessChainConstraint.types b/tsc/testdata/baselines/reference/compiler/deepIndexedAccessChainConstraint.types new file mode 100644 index 0000000000000..1bc741135670b --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/deepIndexedAccessChainConstraint.types @@ -0,0 +1,53 @@ +//// [tests/cases/compiler/deepIndexedAccessChainConstraint.ts] //// + +=== deepIndexedAccessChainConstraint.ts === +// Resolving the base constraint of a 10-level indexed access chain steps +// through nested object types that share the type parameter's recursion +// identity. Checking f resolves typeof victim while T's base constraint is +// uncached; in both victim and lone, base constraints resolve to Box (65508). + +interface Box { a: Box; b: string } +>a : Box +>b : string + +const f: typeof victim = (x: U, y: { b: string }) => x.b; +>f : (x: T, y: T["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]) => string +>victim : (x: T, y: T["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]) => string +>(x: U, y: { b: string }) => x.b : (x: U, y: { b: string; }) => string +>x : U +>y : { b: string; } +>b : string +>x.b : string +>x : U +>b : string + +function victim( +>victim : (x: T, y: T["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]) => string + + x: T, +>x : T + + y: T["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"] +>y : T["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"] + +): string { + return x.b + y.b; +>x.b + y.b : string +>x.b : string +>x : T +>b : string +>y.b : string +>y : T["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"] +>b : string +} + +function lone(y: U["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]): string { +>lone : (y: U["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]) => string +>y : U["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"] + + return y.b; +>y.b : string +>y : U["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"] +>b : string +} + diff --git a/tsc/testdata/tests/cases/compiler/deepIndexedAccessChainConstraint.ts b/tsc/testdata/tests/cases/compiler/deepIndexedAccessChainConstraint.ts new file mode 100644 index 0000000000000..8a3c8d158ac2f --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/deepIndexedAccessChainConstraint.ts @@ -0,0 +1,22 @@ +// @strict: true +// @noEmit: true + +// Resolving the base constraint of a 10-level indexed access chain steps +// through nested object types that share the type parameter's recursion +// identity. Checking f resolves typeof victim while T's base constraint is +// uncached; in both victim and lone, base constraints resolve to Box (65508). + +interface Box { a: Box; b: string } + +const f: typeof victim = (x: U, y: { b: string }) => x.b; + +function victim( + x: T, + y: T["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"] +): string { + return x.b + y.b; +} + +function lone(y: U["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]): string { + return y.b; +}