Skip to content
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

Reuse computed type of condition expressions #49881

Merged
merged 1 commit into from Aug 27, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/compiler/checker.ts
Expand Up @@ -34108,7 +34108,7 @@ namespace ts {
|| isBinaryExpression(parent) && (parent.operatorToken.kind === SyntaxKind.AmpersandAmpersandToken || parent.operatorToken.kind === SyntaxKind.BarBarToken)) {
parent = parent.parent;
}
checkTestingKnownTruthyCallableOrAwaitableType(node.left, isIfStatement(parent) ? parent.thenStatement : undefined);
checkTestingKnownTruthyCallableOrAwaitableType(node.left, leftType, isIfStatement(parent) ? parent.thenStatement : undefined);
}
checkTruthinessOfType(leftType, node.left);
}
Expand Down Expand Up @@ -34692,8 +34692,8 @@ namespace ts {
}

function checkConditionalExpression(node: ConditionalExpression, checkMode?: CheckMode): Type {
checkTruthinessExpression(node.condition);
checkTestingKnownTruthyCallableOrAwaitableType(node.condition, node.whenTrue);
const type = checkTruthinessExpression(node.condition);
checkTestingKnownTruthyCallableOrAwaitableType(node.condition, type, node.whenTrue);
const type1 = checkExpression(node.whenTrue, checkMode);
const type2 = checkExpression(node.whenFalse, checkMode);
return getUnionType([type1, type2], UnionReduction.Subtype);
Expand Down Expand Up @@ -38398,8 +38398,8 @@ namespace ts {
function checkIfStatement(node: IfStatement) {
// Grammar checking
checkGrammarStatementInAmbientContext(node);
checkTruthinessExpression(node.expression);
checkTestingKnownTruthyCallableOrAwaitableType(node.expression, node.thenStatement);
const type = checkTruthinessExpression(node.expression);
checkTestingKnownTruthyCallableOrAwaitableType(node.expression, type, node.thenStatement);
checkSourceElement(node.thenStatement);

if (node.thenStatement.kind === SyntaxKind.EmptyStatement) {
Expand All @@ -38409,7 +38409,7 @@ namespace ts {
checkSourceElement(node.elseStatement);
}

function checkTestingKnownTruthyCallableOrAwaitableType(condExpr: Expression, body?: Statement | Expression) {
function checkTestingKnownTruthyCallableOrAwaitableType(condExpr: Expression, condType: Type, body?: Statement | Expression) {
if (!strictNullChecks) return;

helper(condExpr, body);
Expand All @@ -38424,7 +38424,7 @@ namespace ts {
? condExpr.right
: condExpr;
if (isModuleExportsAccessExpression(location)) return;
const type = checkTruthinessExpression(location);
const type = location === condExpr ? condType : checkTruthinessExpression(location);
const isPropertyExpressionCast = isPropertyAccessExpression(location) && isTypeAssertion(location.expression);
if (!(getTypeFacts(type) & TypeFacts.Truthy) || isPropertyExpressionCast) return;

Expand Down
@@ -0,0 +1,8 @@
//// [uncalledFunctionChecksInConditionalPerf.ts]
declare const b: boolean;

((((((((((((((((((((((((((b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b);


//// [uncalledFunctionChecksInConditionalPerf.js]
((((((((((((((((((((((((((b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b);
@@ -0,0 +1,32 @@
=== tests/cases/compiler/uncalledFunctionChecksInConditionalPerf.ts ===
declare const b: boolean;
>b : Symbol(b, Decl(uncalledFunctionChecksInConditionalPerf.ts, 0, 13))

((((((((((((((((((((((((((b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b);
>b : Symbol(b, Decl(uncalledFunctionChecksInConditionalPerf.ts, 0, 13))
>b : Symbol(b, Decl(uncalledFunctionChecksInConditionalPerf.ts, 0, 13))
>b : Symbol(b, Decl(uncalledFunctionChecksInConditionalPerf.ts, 0, 13))
>b : Symbol(b, Decl(uncalledFunctionChecksInConditionalPerf.ts, 0, 13))
>b : Symbol(b, Decl(uncalledFunctionChecksInConditionalPerf.ts, 0, 13))
>b : Symbol(b, Decl(uncalledFunctionChecksInConditionalPerf.ts, 0, 13))
>b : Symbol(b, Decl(uncalledFunctionChecksInConditionalPerf.ts, 0, 13))
>b : Symbol(b, Decl(uncalledFunctionChecksInConditionalPerf.ts, 0, 13))
>b : Symbol(b, Decl(uncalledFunctionChecksInConditionalPerf.ts, 0, 13))
>b : Symbol(b, Decl(uncalledFunctionChecksInConditionalPerf.ts, 0, 13))
>b : Symbol(b, Decl(uncalledFunctionChecksInConditionalPerf.ts, 0, 13))
>b : Symbol(b, Decl(uncalledFunctionChecksInConditionalPerf.ts, 0, 13))
>b : Symbol(b, Decl(uncalledFunctionChecksInConditionalPerf.ts, 0, 13))
>b : Symbol(b, Decl(uncalledFunctionChecksInConditionalPerf.ts, 0, 13))
>b : Symbol(b, Decl(uncalledFunctionChecksInConditionalPerf.ts, 0, 13))
>b : Symbol(b, Decl(uncalledFunctionChecksInConditionalPerf.ts, 0, 13))
>b : Symbol(b, Decl(uncalledFunctionChecksInConditionalPerf.ts, 0, 13))
>b : Symbol(b, Decl(uncalledFunctionChecksInConditionalPerf.ts, 0, 13))
>b : Symbol(b, Decl(uncalledFunctionChecksInConditionalPerf.ts, 0, 13))
>b : Symbol(b, Decl(uncalledFunctionChecksInConditionalPerf.ts, 0, 13))
>b : Symbol(b, Decl(uncalledFunctionChecksInConditionalPerf.ts, 0, 13))
>b : Symbol(b, Decl(uncalledFunctionChecksInConditionalPerf.ts, 0, 13))
>b : Symbol(b, Decl(uncalledFunctionChecksInConditionalPerf.ts, 0, 13))
>b : Symbol(b, Decl(uncalledFunctionChecksInConditionalPerf.ts, 0, 13))
>b : Symbol(b, Decl(uncalledFunctionChecksInConditionalPerf.ts, 0, 13))
>b : Symbol(b, Decl(uncalledFunctionChecksInConditionalPerf.ts, 0, 13))

@@ -0,0 +1,83 @@
=== tests/cases/compiler/uncalledFunctionChecksInConditionalPerf.ts ===
declare const b: boolean;
>b : boolean

((((((((((((((((((((((((((b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b);
>((((((((((((((((((((((((((b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) : boolean
>(((((((((((((((((((((((((b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b : boolean
>(((((((((((((((((((((((((b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) : boolean
>((((((((((((((((((((((((b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b : boolean
>((((((((((((((((((((((((b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) : boolean
>(((((((((((((((((((((((b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b : boolean
>(((((((((((((((((((((((b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) : boolean
>((((((((((((((((((((((b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b : boolean
>((((((((((((((((((((((b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) : boolean
>(((((((((((((((((((((b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b : boolean
>(((((((((((((((((((((b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) : boolean
>((((((((((((((((((((b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b : boolean
>((((((((((((((((((((b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) : boolean
>(((((((((((((((((((b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b : boolean
>(((((((((((((((((((b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) : boolean
>((((((((((((((((((b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b : boolean
>((((((((((((((((((b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) : boolean
>(((((((((((((((((b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b : boolean
>(((((((((((((((((b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) : boolean
>((((((((((((((((b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b : boolean
>((((((((((((((((b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) : boolean
>(((((((((((((((b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b : boolean
>(((((((((((((((b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) : boolean
>((((((((((((((b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b : boolean
>((((((((((((((b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) : boolean
>(((((((((((((b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b : boolean
>(((((((((((((b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) : boolean
>((((((((((((b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b : boolean
>((((((((((((b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) : boolean
>(((((((((((b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b : boolean
>(((((((((((b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) : boolean
>((((((((((b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b : boolean
>((((((((((b) && b) && b) && b) && b) && b) && b) && b) && b) && b) : boolean
>(((((((((b) && b) && b) && b) && b) && b) && b) && b) && b) && b : boolean
>(((((((((b) && b) && b) && b) && b) && b) && b) && b) && b) : boolean
>((((((((b) && b) && b) && b) && b) && b) && b) && b) && b : boolean
>((((((((b) && b) && b) && b) && b) && b) && b) && b) : boolean
>(((((((b) && b) && b) && b) && b) && b) && b) && b : boolean
>(((((((b) && b) && b) && b) && b) && b) && b) : boolean
>((((((b) && b) && b) && b) && b) && b) && b : boolean
>((((((b) && b) && b) && b) && b) && b) : boolean
>(((((b) && b) && b) && b) && b) && b : boolean
>(((((b) && b) && b) && b) && b) : boolean
>((((b) && b) && b) && b) && b : boolean
>((((b) && b) && b) && b) : boolean
>(((b) && b) && b) && b : boolean
>(((b) && b) && b) : boolean
>((b) && b) && b : boolean
>((b) && b) : boolean
>(b) && b : boolean
>(b) : boolean
>b : boolean
>b : true
>b : true
>b : true
>b : true
>b : true
>b : true
>b : true
>b : true
>b : true
>b : true
>b : true
>b : true
>b : true
>b : true
>b : true
>b : true
>b : true
>b : true
>b : true
>b : true
>b : true
>b : true
>b : true
>b : true
>b : true

@@ -0,0 +1,5 @@
// @strictNullChecks: true
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we have a better way of capturing this kind of test, where the thing we're interested in is the execution time.

Copy link
Member

Choose a reason for hiding this comment

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

Historically we don't - but tests do have a timeout (around 50s), so if you make sure they're large enough to hit the timeout before your change, they're a decent test; they won't capture tiny pretty regressions, but will prevent big ones. We have other tests in this vein.

Copy link
Member

Choose a reason for hiding this comment

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

Ok then. @JoostK can you make this example much much bigger, so that the test would timeout without the change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will add 4 additional binary ops, which would have brought check times up by a factor of 16 (easily pushing it beyond the 50s threshold)


declare const b: boolean;

((((((((((((((((((((((((((b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b) && b);