Skip to content

Instantiate anonymous types less #7138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 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
91 changes: 73 additions & 18 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ namespace ts {
const anySignature = createSignature(undefined, undefined, emptyArray, anyType, /*typePredicate*/ undefined, 0, /*hasRestParameter*/ false, /*hasStringLiterals*/ false);
const unknownSignature = createSignature(undefined, undefined, emptyArray, unknownType, /*typePredicate*/ undefined, 0, /*hasRestParameter*/ false, /*hasStringLiterals*/ false);


const identityMapper: TypeMapper = <TypeMapper>((t: TypeParameter) => <Type>t);
identityMapper.mapsType = sym => false;

const enumNumberIndexInfo = createIndexInfo(stringType, /*isReadonly*/ true);

const globals: SymbolTable = {};
Expand Down Expand Up @@ -4954,54 +4958,66 @@ namespace ts {
}

function createUnaryTypeMapper(source: Type, target: Type): TypeMapper {
return t => t === source ? target : t;
const mapper = <TypeMapper>(t => t === source ? target : t);
mapper.mapsType = sym => sym.name === source.symbol.name;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this like some sort of optimization?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the information that mappers previously did not expose -- which types they map. This is important because hasTypeParametersInScope will now tell its caller whether a type needs to be instantiated by a particular mapper.

This is only partly an optimization, because some of the types for which we now skip instantiation should never be instantiated, like the anonymous typeof ListWrapper types in the generic static functions in the test.

return mapper;
}

function createBinaryTypeMapper(source1: Type, target1: Type, source2: Type, target2: Type): TypeMapper {
return t => t === source1 ? target1 : t === source2 ? target2 : t;
const mapper = <TypeMapper>(t => t === source1 ? target1 : t === source2 ? target2 : t);
mapper.mapsType = sym => sym.name === source1.symbol.name || sym.name === source2.symbol.name;
return mapper;
}

function createTypeMapper(sources: Type[], targets: Type[]): TypeMapper {
switch (sources.length) {
case 1: return createUnaryTypeMapper(sources[0], targets[0]);
case 2: return createBinaryTypeMapper(sources[0], targets[0], sources[1], targets[1]);
}
return t => {
const mapper = <TypeMapper>(t => {
for (let i = 0; i < sources.length; i++) {
if (t === sources[i]) {
return targets[i];
}
}
return t;
};
});
mapper.mapsType = sym => forEach(sources, source => sym.name === source.symbol.name);
return mapper;
}

function createUnaryTypeEraser(source: Type): TypeMapper {
return t => t === source ? anyType : t;
const eraser = <TypeMapper>(t => t === source ? <Type>anyType : <Type>t);
eraser.mapsType = sym => sym.name === source.symbol.name;
return eraser;
}

function createBinaryTypeEraser(source1: Type, source2: Type): TypeMapper {
return t => t === source1 || t === source2 ? anyType : t;
const eraser = <TypeMapper>(t => t === source1 || t === source2 ? <Type>anyType : <Type>t);
eraser.mapsType = sym => sym.name === source1.symbol.name || sym.name === source2.symbol.name;
return eraser;
}

function createTypeEraser(sources: Type[]): TypeMapper {
switch (sources.length) {
case 1: return createUnaryTypeEraser(sources[0]);
case 2: return createBinaryTypeEraser(sources[0], sources[1]);
}
return t => {
const mapper = <TypeMapper>(t => {
for (const source of sources) {
if (t === source) {
return anyType;
return <Type>anyType;
}
}
return t;
};
return <Type>t;
});
mapper.mapsType = sym => forEach(sources, source => source.symbol.name === sym.name);
return mapper;
}

function getInferenceMapper(context: InferenceContext): TypeMapper {
if (!context.mapper) {
const mapper: TypeMapper = t => {
const mapper = <TypeMapper>(t => {
const typeParameters = context.typeParameters;
for (let i = 0; i < typeParameters.length; i++) {
if (t === typeParameters[i]) {
Expand All @@ -5010,19 +5026,18 @@ namespace ts {
}
}
return t;
};
});
mapper.context = context;
mapper.mapsType = sym => forEach(context.typeParameters, param => param.symbol.name === sym.name);
context.mapper = mapper;
}
return context.mapper;
}

function identityMapper(type: Type): Type {
return type;
}

function combineTypeMappers(mapper1: TypeMapper, mapper2: TypeMapper): TypeMapper {
return t => instantiateType(mapper1(t), mapper2);
const mapper = <TypeMapper>(t => instantiateType(mapper1(t), mapper2));
mapper.mapsType = sym => mapper1.mapsType(sym) || mapper2.mapsType(sym);
return mapper;
}

function cloneTypeParameter(typeParameter: TypeParameter): TypeParameter {
Expand Down Expand Up @@ -5123,7 +5138,9 @@ namespace ts {
return mapper(<TypeParameter>type);
}
if (type.flags & TypeFlags.Anonymous) {
return type.symbol && type.symbol.flags & (SymbolFlags.Function | SymbolFlags.Method | SymbolFlags.Class | SymbolFlags.TypeLiteral | SymbolFlags.ObjectLiteral) ?
return type.symbol &&
type.symbol.flags & (SymbolFlags.Function | SymbolFlags.Method | SymbolFlags.Class | SymbolFlags.TypeLiteral | SymbolFlags.ObjectLiteral) &&
hasTypeParametersInScope(type, mapper) ?
instantiateAnonymousType(<AnonymousType>type, mapper) : type;
}
if (type.flags & TypeFlags.Reference) {
Expand All @@ -5142,6 +5159,44 @@ namespace ts {
return type;
}

function hasTypeParametersInScope(type: Type, mapper: TypeMapper) {
for (const original of type.symbol.declarations) {
let node: Node = original;
while (node !== undefined) {
if (node.kind === SyntaxKind.FunctionType ||
node.kind === SyntaxKind.ConstructorType ||
node.kind === SyntaxKind.FunctionDeclaration ||
node.kind === SyntaxKind.MethodDeclaration ||
node.kind === SyntaxKind.MethodSignature ||
node.kind === SyntaxKind.Constructor ||
node.kind === SyntaxKind.CallSignature ||
node.kind === SyntaxKind.ConstructSignature ||
node.kind === SyntaxKind.IndexSignature ||
node.kind === SyntaxKind.GetAccessor ||
node.kind === SyntaxKind.SetAccessor ||
node.kind === SyntaxKind.FunctionExpression ||
node.kind === SyntaxKind.ArrowFunction ||
node.kind === SyntaxKind.JSDocFunctionType ||
node.kind === SyntaxKind.ClassDeclaration ||
node.kind === SyntaxKind.ClassExpression ||
node.kind === SyntaxKind.TypeAliasDeclaration ||
node.kind === SyntaxKind.InterfaceDeclaration) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turn this into a big switch over kind

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

const decl = <SignatureDeclaration | ClassLikeDeclaration | InterfaceDeclaration>node;
if (decl.typeParameters && decl.typeParameters.length && forEach(decl.typeParameters, p => mapper.mapsType(getSymbolOfNode(p)))) {
return true;
}
if ((node.kind === SyntaxKind.ClassDeclaration || node.kind === SyntaxKind.InterfaceDeclaration || node.kind === SyntaxKind.ClassExpression) &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isClassLike

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

mapper.mapsType(getSymbolOfNode(decl))) {
// mapper maps the this class type of a class
return true;
}
}
node = node.parent;
}
}
return false;
}

function instantiateIndexInfo(info: IndexInfo, mapper: TypeMapper): IndexInfo {
return info && createIndexInfo(instantiateType(info.type, mapper), info.isReadonly, info.declaration);
}
Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2286,6 +2286,7 @@ namespace ts {
context?: InferenceContext; // The inference context this mapper was created from.
// Only inference mappers have this set (in createInferenceMapper).
// The identity mapper and regular instantiation mappers do not need it.
mapsType(s: Symbol): boolean;
}

/* @internal */
Expand Down
Loading