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
58 changes: 33 additions & 25 deletions tsc/internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -25539,14 +25539,13 @@ func isUnaryTupleTypeNode(node *ast.Node) bool {
return ast.IsTupleTypeNode(node) && len(node.Elements()) == 1
}

func (c *Checker) newType(flags TypeFlags, objectFlags ObjectFlags, data TypeData) *Type {
func (c *Checker) newType(flags TypeFlags, objectFlags ObjectFlags, t *Type, kind typeKind) *Type {
c.TypeCount++
t := data.AsType()
t.flags = flags
t.objectFlags = objectFlags &^ (ObjectFlagsCouldContainTypeVariablesComputed | ObjectFlagsCouldContainTypeVariables | ObjectFlagsMembersResolved)
t.id = TypeId(c.TypeCount)
t.kind = kind
t.checker = c
t.data = data
if c.tracer != nil {
c.tracer.RecordType(t)
}
Expand All @@ -25560,7 +25559,7 @@ func (c *Checker) newIntrinsicType(flags TypeFlags, intrinsicName string) *Type
func (c *Checker) newIntrinsicTypeEx(flags TypeFlags, intrinsicName string, objectFlags ObjectFlags) *Type {
data := &IntrinsicType{}
data.intrinsicName = intrinsicName
return c.newType(flags, objectFlags, data)
return c.newType(flags, objectFlags, data.AsType(), typeKindIntrinsic)
}

func (c *Checker) createWideningType(nonWideningType *Type) *Type {
Expand All @@ -25582,7 +25581,7 @@ func (c *Checker) createUnknownUnionType() *Type {
func (c *Checker) newLiteralType(flags TypeFlags, value any, regularType *Type) *Type {
data := &LiteralType{}
data.value = value
t := c.newType(flags, ObjectFlagsNone, data)
t := c.newType(flags, ObjectFlagsNone, data.AsType(), typeKindLiteral)
if regularType != nil {
data.regularType = regularType
} else {
Expand All @@ -25594,34 +25593,43 @@ func (c *Checker) newLiteralType(flags TypeFlags, value any, regularType *Type)
func (c *Checker) newUniqueESSymbolType(symbol *ast.Symbol, name string) *Type {
data := &UniqueESSymbolType{}
data.name = name
t := c.newType(TypeFlagsUniqueESSymbol, ObjectFlagsNone, data)
t := c.newType(TypeFlagsUniqueESSymbol, ObjectFlagsNone, data.AsType(), typeKindUniqueESSymbol)
t.symbol = symbol
return t
}

func (c *Checker) newObjectType(objectFlags ObjectFlags, symbol *ast.Symbol) *Type {
var data TypeData
var t *Type
var kind typeKind
switch {
case objectFlags&ObjectFlagsClassOrInterface != 0:
data = &InterfaceType{}
t = (&InterfaceType{}).AsType()
kind = typeKindInterface
case objectFlags&ObjectFlagsTuple != 0:
data = &TupleType{}
t = (&TupleType{}).AsType()
kind = typeKindTuple
case objectFlags&ObjectFlagsReference != 0:
data = &TypeReference{}
t = (&TypeReference{}).AsType()
kind = typeKindTypeReference
case objectFlags&ObjectFlagsMapped != 0:
data = &MappedType{}
t = (&MappedType{}).AsType()
kind = typeKindMapped
case objectFlags&ObjectFlagsReverseMapped != 0:
data = &ReverseMappedType{}
t = (&ReverseMappedType{}).AsType()
kind = typeKindReverseMapped
case objectFlags&ObjectFlagsEvolvingArray != 0:
data = &EvolvingArrayType{}
t = (&EvolvingArrayType{}).AsType()
kind = typeKindEvolvingArray
case objectFlags&ObjectFlagsInstantiationExpressionType != 0:
data = &InstantiationExpressionType{}
t = (&InstantiationExpressionType{}).AsType()
kind = typeKindInstantiationExpression
case objectFlags&ObjectFlagsAnonymous != 0:
data = &ObjectType{}
t = (&ObjectType{}).AsType()
kind = typeKindObject
default:
panic("Unhandled case in newObjectType")
}
t := c.newType(TypeFlagsObject, objectFlags, data)
c.newType(TypeFlagsObject, objectFlags, t, kind)
t.symbol = symbol
return t
}
Expand Down Expand Up @@ -25705,7 +25713,7 @@ func (c *Checker) setStructuredTypeMembers(t *Type, members ast.SymbolTable, cal
}

func (c *Checker) newTypeParameter(symbol *ast.Symbol) *Type {
t := c.newType(TypeFlagsTypeParameter, ObjectFlagsNone, &TypeParameter{})
t := c.newType(TypeFlagsTypeParameter, ObjectFlagsNone, (&TypeParameter{}).AsType(), typeKindTypeParameter)
t.symbol = symbol
return t
}
Expand All @@ -25727,41 +25735,41 @@ func (c *Checker) getPropagatingFlagsOfTypes(types []*Type, excludeKinds TypeFla
func (c *Checker) newUnionType(objectFlags ObjectFlags, types []*Type) *Type {
data := &UnionType{}
data.types = types
return c.newType(TypeFlagsUnion, objectFlags, data)
return c.newType(TypeFlagsUnion, objectFlags, data.AsType(), typeKindUnion)
}

func (c *Checker) newIntersectionType(objectFlags ObjectFlags, types []*Type) *Type {
data := &IntersectionType{}
data.types = types
return c.newType(TypeFlagsIntersection, objectFlags, data)
return c.newType(TypeFlagsIntersection, objectFlags, data.AsType(), typeKindIntersection)
}

func (c *Checker) newIndexedAccessType(objectType *Type, indexType *Type, accessFlags AccessFlags) *Type {
data := &IndexedAccessType{}
data.objectType = objectType
data.indexType = indexType
data.accessFlags = accessFlags
return c.newType(TypeFlagsIndexedAccess, ObjectFlagsNone, data)
return c.newType(TypeFlagsIndexedAccess, ObjectFlagsNone, data.AsType(), typeKindIndexedAccess)
}

func (c *Checker) newIndexType(target *Type, indexFlags IndexFlags) *Type {
data := &IndexType{}
data.target = target
data.indexFlags = indexFlags
return c.newType(TypeFlagsIndex, ObjectFlagsNone, data)
return c.newType(TypeFlagsIndex, ObjectFlagsNone, data.AsType(), typeKindIndex)
}

func (c *Checker) newTemplateLiteralType(texts []string, types []*Type) *Type {
data := &TemplateLiteralType{}
data.texts = texts
data.types = types
return c.newType(TypeFlagsTemplateLiteral, ObjectFlagsNone, data)
return c.newType(TypeFlagsTemplateLiteral, ObjectFlagsNone, data.AsType(), typeKindTemplateLiteral)
}

func (c *Checker) newStringMappingType(symbol *ast.Symbol, target *Type) *Type {
data := &StringMappingType{}
data.target = target
t := c.newType(TypeFlagsStringMapping, ObjectFlagsNone, data)
t := c.newType(TypeFlagsStringMapping, ObjectFlagsNone, data.AsType(), typeKindStringMapping)
t.symbol = symbol
return t
}
Expand All @@ -25773,14 +25781,14 @@ func (c *Checker) newConditionalType(root *ConditionalRoot, mapper *TypeMapper,
data.extendsType = c.instantiateType(root.extendsType, mapper)
data.mapper = mapper
data.combinedMapper = combinedMapper
return c.newType(TypeFlagsConditional, ObjectFlagsNone, data)
return c.newType(TypeFlagsConditional, ObjectFlagsNone, data.AsType(), typeKindConditional)
}

func (c *Checker) newSubstitutionType(baseType *Type, constraint *Type) *Type {
data := &SubstitutionType{}
data.baseType = baseType
data.constraint = constraint
return c.newType(TypeFlagsSubstitution, ObjectFlagsNone, data)
return c.newType(TypeFlagsSubstitution, ObjectFlagsNone, data.AsType(), typeKindSubstitution)
}

func (c *Checker) newSignature(flags SignatureFlags, declaration *ast.Node, typeParameters []*Type, thisParameter *ast.Symbol, parameters []*ast.Symbol, resolvedReturnType *Type, resolvedTypePredicate *TypePredicate, minArgumentCount int) *Signature {
Expand Down
57 changes: 57 additions & 0 deletions tsc/internal/checker/checker_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package checker_test

import (
"fmt"
"path/filepath"
"strings"
"testing"

"github.com/microsoft/TypeScript/tsc/internal/ast"
Expand Down Expand Up @@ -131,3 +133,58 @@ func BenchmarkNewChecker(b *testing.B) {
checker.NewChecker(program, nil)
}
}

func BenchmarkCheck(b *testing.B) {
fs := bundled.WrapFS(osvfs.FS())
rootPath := tspath.RootedDirectoryPathFromAbsolute(filepath.Join(repo.TestDataPath(), "fixtures/compiler"))
host := compiler.NewCompilerHost(fs, bundled.LibPath(), nil, nil, nil)
parsed, errors := tsoptions.GetParsedCommandLineOfConfigFile(rootPath.ResolveFile("tsconfig.json"), &core.CompilerOptions{}, nil, fs, nil)
assert.Equal(b, len(errors), 0, "Expected no errors in parsed command line")
program := compiler.NewProgram(compiler.ProgramOptions{
Config: parsed,
Host: host,
})
program.BindSourceFiles()
ctx := b.Context()

b.ReportAllocs()
for b.Loop() {
c, _ := checker.NewChecker(program, nil)
for _, file := range program.GetSourceFiles() {
c.GetDiagnostics(ctx, file)
}
c.GetGlobalDiagnostics()
}
}

func TestTypeAllocationAcrossCheckerGrowth(t *testing.T) {
t.Parallel()
var content strings.Builder
content.WriteString(`interface Box<T> { value: T }
export type Initial = Box<"first">;
`)
for i := range 1024 {
fmt.Fprintf(&content, "export type T%d = Box<\"value%d\">;\n", i, i)
}
fs := bundled.WrapFS(vfstest.FromMap(map[string]string{
"/foo.ts": content.String(),
}, tspath.CaseSensitive))
opts := &core.CompilerOptions{NoLib: core.TSTrue}
program := compiler.NewProgram(compiler.ProgramOptions{
Config: tsoptions.NewParsedCommandLine(opts, []tspath.RootedFilePath{"/foo.ts"}, nil, "/", fs.CaseSensitivity()),
Host: compiler.NewCompilerHost(fs, bundled.LibPath(), nil, nil, nil),
})
program.BindSourceFiles()
c, done := program.GetTypeChecker(t.Context())
defer done()
file := program.GetSourceFile("/foo.ts")
initial := c.GetTypeAtLocation(file.Statements.Nodes[1].AsTypeAliasDeclaration().Type)
for i := range 1024 {
node := file.Statements.Nodes[i+2].AsTypeAliasDeclaration().Type
typ := c.GetTypeAtLocation(node)
assert.Equal(t, c.TypeToString(typ), fmt.Sprintf("T%d", i))
}
assert.Equal(t, c.GetTypeAtLocation(file.Statements.Nodes[1].AsTypeAliasDeclaration().Type), initial)
assert.Equal(t, c.TypeToString(initial), "Initial")
assert.Equal(t, len(c.GetDiagnostics(t.Context(), file)), 0)
}
5 changes: 2 additions & 3 deletions tsc/internal/checker/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,10 @@ func (a *tracedTypeAdapter) IntrinsicName() string {
if a.t.flags&TypeFlagsIntrinsic == 0 {
return ""
}
data, ok := a.t.data.(*IntrinsicType)
if !ok {
if a.t.kind != typeKindIntrinsic {
return ""
}
return data.intrinsicName
return a.t.AsIntrinsicType().intrinsicName
}

func (a *tracedTypeAdapter) UnionTypes() []tracing.TracedType {
Expand Down
Loading
Loading