Skip to content
Open
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
32 changes: 27 additions & 5 deletions tsc/internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> -> Foo<Foo<T>>) 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
Expand All @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 = <U extends Box>(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<T extends Box>(
>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<U extends Box>(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))
}

Original file line number Diff line number Diff line change
@@ -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 = <U extends Box>(x: U, y: { b: string }) => x.b;
>f : <T extends Box>(x: T, y: T["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]) => string
>victim : <T extends Box>(x: T, y: T["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]) => string
><U extends Box>(x: U, y: { b: string }) => x.b : <U extends Box>(x: U, y: { b: string; }) => string
>x : U
>y : { b: string; }
>b : string
>x.b : string
>x : U
>b : string

function victim<T extends Box>(
>victim : <T extends Box>(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<U extends Box>(y: U["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]): string {
>lone : <U extends Box>(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
}

Original file line number Diff line number Diff line change
@@ -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 = <U extends Box>(x: U, y: { b: string }) => x.b;

function victim<T extends Box>(
x: T,
y: T["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]
): string {
return x.b + y.b;
}

function lone<U extends Box>(y: U["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]): string {
return y.b;
}
Loading