Skip to content

Reuse operandType in checkPrefixUnaryExpression #53682

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

Merged
merged 1 commit into from
Apr 6, 2023
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
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36120,7 +36120,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
return getUnaryResultType(operandType);
case SyntaxKind.ExclamationToken:
checkTruthinessExpression(node.operand);
checkTruthinessOfType(operandType, node.operand);
Copy link
Member Author

@jakebailey jakebailey Apr 6, 2023

Choose a reason for hiding this comment

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

This direct call to checkTruthinessOfType when the operand type is known mirrors our handling of binary expressions in onOperator.

I checked all other call sites, and only this one benefits from not reevaluating the type (which is good, unless we wanted to find more of these for even more perf!).

const facts = getTypeFacts(operandType) & (TypeFacts.Truthy | TypeFacts.Falsy);
return facts === TypeFacts.Truthy ? falseType :
facts === TypeFacts.Falsy ? trueType :
Expand Down
11 changes: 11 additions & 0 deletions tests/baselines/reference/nestedUnaryExpressionHang.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
tests/cases/compiler/nestedUnaryExpressionHang.ts(1,6): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
tests/cases/compiler/nestedUnaryExpressionHang.ts(1,34): error TS1109: Expression expected.


==== tests/cases/compiler/nestedUnaryExpressionHang.ts (2 errors) ====
3333%!!!!!!!!!!!!!!!!!!!!!!!!!!!!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
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 code doesn't parse properly, but, that's the test case that the fuzzer gave me so I feel like it's fine? But, I can try and come up with something that exhibits this problem without errors if desired.


!!! error TS1109: Expression expected.

6 changes: 6 additions & 0 deletions tests/baselines/reference/nestedUnaryExpressionHang.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//// [nestedUnaryExpressionHang.ts]
3333%!!!!!!!!!!!!!!!!!!!!!!!!!!!!


//// [nestedUnaryExpressionHang.js]
3333 % !!!!!!!!!!!!!!!!!!!!!!!!!!!!;
4 changes: 4 additions & 0 deletions tests/baselines/reference/nestedUnaryExpressionHang.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
=== tests/cases/compiler/nestedUnaryExpressionHang.ts ===

3333%!!!!!!!!!!!!!!!!!!!!!!!!!!!!

35 changes: 35 additions & 0 deletions tests/baselines/reference/nestedUnaryExpressionHang.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
=== tests/cases/compiler/nestedUnaryExpressionHang.ts ===
3333%!!!!!!!!!!!!!!!!!!!!!!!!!!!!
>3333%!!!!!!!!!!!!!!!!!!!!!!!!!!!! : number
>3333 : 3333
>!!!!!!!!!!!!!!!!!!!!!!!!!!!! : boolean
>!!!!!!!!!!!!!!!!!!!!!!!!!!! : boolean
>!!!!!!!!!!!!!!!!!!!!!!!!!! : boolean
>!!!!!!!!!!!!!!!!!!!!!!!!! : boolean
>!!!!!!!!!!!!!!!!!!!!!!!! : boolean
>!!!!!!!!!!!!!!!!!!!!!!! : boolean
>!!!!!!!!!!!!!!!!!!!!!! : boolean
>!!!!!!!!!!!!!!!!!!!!! : boolean
>!!!!!!!!!!!!!!!!!!!! : boolean
>!!!!!!!!!!!!!!!!!!! : boolean
>!!!!!!!!!!!!!!!!!! : boolean
>!!!!!!!!!!!!!!!!! : boolean
>!!!!!!!!!!!!!!!! : boolean
>!!!!!!!!!!!!!!! : boolean
>!!!!!!!!!!!!!! : boolean
>!!!!!!!!!!!!! : boolean
>!!!!!!!!!!!! : boolean
>!!!!!!!!!!! : boolean
>!!!!!!!!!! : boolean
>!!!!!!!!! : boolean
>!!!!!!!! : boolean
>!!!!!!! : boolean
>!!!!!! : boolean
>!!!!! : boolean
>!!!! : boolean
>!!! : boolean
>!! : boolean
>! : boolean

> : any

1 change: 1 addition & 0 deletions tests/cases/compiler/nestedUnaryExpressionHang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3333%!!!!!!!!!!!!!!!!!!!!!!!!!!!!