Skip to content

Commit 49f210f

Browse files
authored
Use one cached nodebuilder for potentially circular internal ops, just like strada (#3833)
1 parent ff5823b commit 49f210f

7 files changed

Lines changed: 45 additions & 2 deletions

File tree

internal/checker/checker.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,7 @@ type Checker struct {
884884
reportedUnreachableNodes collections.Set[*ast.Node]
885885
nonExistentProperties collections.Set[NonExistentPropertyKey]
886886
deferredDiagnosticCallbacks []func()
887+
typeToStringNodebuilder *NodeBuilder
887888

888889
mu sync.Mutex
889890
tracer *Tracer // Optional tracer for trace events and type recording (for --generateTrace)

internal/checker/nodebuilder.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,11 @@ func NewNodeBuilderEx(ch *Checker, e *printer.EmitContext, idToSymbol map[*ast.I
286286
}
287287

288288
func (c *Checker) getNodeBuilder() *NodeBuilder {
289-
return c.getNodeBuilderEx(nil /*idToSymbol*/)
289+
if c.typeToStringNodebuilder != nil {
290+
return c.typeToStringNodebuilder
291+
}
292+
c.typeToStringNodebuilder = c.getNodeBuilderEx(nil /*idToSymbol*/)
293+
return c.typeToStringNodebuilder
290294
}
291295

292296
func (c *Checker) getNodeBuilderEx(idToSymbol map[*ast.IdentifierNode]*ast.Symbol) *NodeBuilder {

internal/checker/nodebuilderimpl.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2990,7 +2990,7 @@ func (b *NodeBuilderImpl) visitAndTransformType(t *Type, transform func(b *NodeB
29902990
// of types allows us to catch circular references to instantiations of the same anonymous type
29912991

29922992
key := CompositeTypeCacheIdentity{typeId, b.ctx.flags, b.ctx.internalFlags}
2993-
if b.ctx.enclosingDeclaration != nil && b.links.Has(b.ctx.enclosingDeclaration) {
2993+
if b.ctx.maxExpansionDepth <= 0 && b.ctx.enclosingDeclaration != nil && b.links.Has(b.ctx.enclosingDeclaration) {
29942994
links := b.links.Get(b.ctx.enclosingDeclaration)
29952995
cachedResult, ok := links.serializedTypes[key]
29962996
if ok {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
noTypeToStringStackOverflow.ts(3,7): error TS7023: 'f' implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions.
2+
noTypeToStringStackOverflow.ts(3,20): error TS1360: Type 'number' does not satisfy the expected type '() => any'.
3+
4+
5+
==== noTypeToStringStackOverflow.ts (2 errors) ====
6+
// https://github.com/microsoft/typescript-go/issues/3805
7+
8+
const f = () => 42 satisfies typeof f;
9+
~
10+
!!! error TS7023: 'f' implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions.
11+
~~~~~~~~~
12+
!!! error TS1360: Type 'number' does not satisfy the expected type '() => any'.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//// [tests/cases/compiler/noTypeToStringStackOverflow.ts] ////
2+
3+
=== noTypeToStringStackOverflow.ts ===
4+
// https://github.com/microsoft/typescript-go/issues/3805
5+
6+
const f = () => 42 satisfies typeof f;
7+
>f : Symbol(f, Decl(noTypeToStringStackOverflow.ts, 2, 5))
8+
>f : Symbol(f, Decl(noTypeToStringStackOverflow.ts, 2, 5))
9+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//// [tests/cases/compiler/noTypeToStringStackOverflow.ts] ////
2+
3+
=== noTypeToStringStackOverflow.ts ===
4+
// https://github.com/microsoft/typescript-go/issues/3805
5+
6+
const f = () => 42 satisfies typeof f;
7+
>f : () => any
8+
>() => 42 satisfies typeof f : () => any
9+
>42 satisfies typeof f : 42
10+
>42 : 42
11+
>f : () => any
12+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// @noEmit: true
2+
3+
// https://github.com/microsoft/typescript-go/issues/3805
4+
5+
const f = () => 42 satisfies typeof f;

0 commit comments

Comments
 (0)