Skip to content

Mark type arguments as used even if used in an invalid way#15557

Merged
3 commits merged into
masterfrom
unused-type-parameter
May 4, 2017
Merged

Mark type arguments as used even if used in an invalid way#15557
3 commits merged into
masterfrom
unused-type-parameter

Conversation

@ghost
Copy link
Copy Markdown

@ghost ghost commented May 3, 2017

Fixes #14953
Previously we would only check type arguments if there were matching type parameters. Now we always check type arguments. This gives additional diagnostics and makes sure they are marked as used.

Copy link
Copy Markdown
Member

@sandersn sandersn left a comment

Choose a reason for hiding this comment

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

Just one question about performance.

Comment thread src/compiler/checker.ts Outdated
}

function callLikeExpressionMayHaveTypeArguments(node: CallLikeExpression): node is CallExpression | NewExpression {
return node.kind === SyntaxKind.CallExpression || node.kind === SyntaxKind.NewExpression;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

there no predicate like this utilities or wherever they live? I'm kind of surprised there's not a isCallLikeLikeExpression or something.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I couldn't find one, but I've added one now. Still want to keep isCallOrNewExpression separate from callLikeExpressionMayHaveTypeArgumets since those may not always be the same thing (#11947).

Comment thread src/compiler/checker.ts
}

function getTypeReferenceType(node: TypeReferenceNode | ExpressionWithTypeArguments | JSDocTypeReference, symbol: Symbol) {
const typeArguments = typeArgumentsFromTypeReferenceNode(node); // Do unconditionally so we mark type arguments as referenced.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Are you sure it's not cheap enough to just call typeArgumentsFromTypeReferenceNode multiple times and avoid the argument-threading churn below?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Depending on what's in the arguments, this could presumably do an arbitrary amount of work.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

getTypeFromTypeNode dispatches to a bunch of functions that are all cached, so the only danger is an arbitrarily long list of type arguments. Either way is fine with me, but I'd prefer to have some measurement that says the duplicate way is measurably slower.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Found some duplicate code, so we call typeArgumentsFromTypeReferenceNode in fewer places now.
(The two parts of duplicate code were even added in the same commit: e223b2e)

As for performance, here are my measurements on a sample script:

{
    { type T = X<A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P>; }
    { type T = X<A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P>; }
    ... 1000 times ...
}

Tested with node ..\..\TypeScript\built\local\tsc.js --noEmit --diagnostics --noLib, and shimmed lib with e.g. interface Array {} so it would compile.

This PR:

Memory Used: 30702K 30379K 30134K 30140K 30114K (average: 30293.8K)
Check Time: 0.11s 0.11s 0.10s 0.10s 0.10s (average: 0.104s)

This PR with 1000 extra calls to typeArgumentsFromTypeReferenceNode:

Memory Used: 37329K 37266K 37830K 37825K 37247K (average: 37499.4K)
Check Time: 0.85s 0.83s 0.84s 0.84s 0.83s (average: 0.838s)

The memory difference is 7205K, a 0.02% difference per iteration.
The time difference is 0.734s, a 0.7% difference per iteration.

@ghost ghost merged commit 7fe1aba into master May 4, 2017
@ghost ghost deleted the unused-type-parameter branch May 4, 2017 21:15
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants