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

Compute more precise syntactic truthy semantics for comma expressions #60830

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44715,6 +44715,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return !!(node as StringLiteral | NoSubstitutionTemplateLiteral).text ? PredicateSemantics.Always : PredicateSemantics.Never;
case SyntaxKind.ConditionalExpression:
return getSyntacticTruthySemantics((node as ConditionalExpression).whenTrue) | getSyntacticTruthySemantics((node as ConditionalExpression).whenFalse);
case SyntaxKind.BinaryExpression:
if ((node as BinaryExpression).operatorToken.kind !== SyntaxKind.CommaToken) {
break;
}
return getSyntacticTruthySemantics((node as BinaryExpression).right);
case SyntaxKind.Identifier:
if (getResolvedSymbol(node as Identifier) === undefinedSymbol) {
return PredicateSemantics.Never;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ invalidLetInForOfAndForIn_ES5.ts(5,13): error TS1005: ',' expected.
invalidLetInForOfAndForIn_ES5.ts(5,14): error TS1181: Array element destructuring pattern expected.
invalidLetInForOfAndForIn_ES5.ts(5,14): error TS2695: Left side of comma operator is unused and has no side effects.
invalidLetInForOfAndForIn_ES5.ts(5,14): error TS2695: Left side of comma operator is unused and has no side effects.
invalidLetInForOfAndForIn_ES5.ts(5,14): error TS2872: This kind of expression is always truthy.
invalidLetInForOfAndForIn_ES5.ts(5,19): error TS1005: ';' expected.
invalidLetInForOfAndForIn_ES5.ts(5,20): error TS1128: Declaration or statement expected.


==== invalidLetInForOfAndForIn_ES5.ts (6 errors) ====
==== invalidLetInForOfAndForIn_ES5.ts (7 errors) ====
// This should be an error
// More details: http://www.ecma-international.org/ecma-262/6.0/#sec-iteration-statements

Expand All @@ -20,6 +21,8 @@ invalidLetInForOfAndForIn_ES5.ts(5,20): error TS1128: Declaration or statement e
!!! error TS2695: Left side of comma operator is unused and has no side effects.
~~~
!!! error TS2695: Left side of comma operator is unused and has no side effects.
~~~~~
!!! error TS2872: This kind of expression is always truthy.
~
!!! error TS1005: ';' expected.
~
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ invalidLetInForOfAndForIn_ES6.ts(5,13): error TS1005: ',' expected.
invalidLetInForOfAndForIn_ES6.ts(5,14): error TS1181: Array element destructuring pattern expected.
invalidLetInForOfAndForIn_ES6.ts(5,14): error TS2695: Left side of comma operator is unused and has no side effects.
invalidLetInForOfAndForIn_ES6.ts(5,14): error TS2695: Left side of comma operator is unused and has no side effects.
invalidLetInForOfAndForIn_ES6.ts(5,14): error TS2872: This kind of expression is always truthy.
invalidLetInForOfAndForIn_ES6.ts(5,19): error TS1005: ';' expected.
invalidLetInForOfAndForIn_ES6.ts(5,20): error TS1128: Declaration or statement expected.


==== invalidLetInForOfAndForIn_ES6.ts (6 errors) ====
==== invalidLetInForOfAndForIn_ES6.ts (7 errors) ====
// This should be an error
// More details: http://www.ecma-international.org/ecma-262/6.0/#sec-iteration-statements

Expand All @@ -20,6 +21,8 @@ invalidLetInForOfAndForIn_ES6.ts(5,20): error TS1128: Declaration or statement e
!!! error TS2695: Left side of comma operator is unused and has no side effects.
~~~
!!! error TS2695: Left side of comma operator is unused and has no side effects.
~~~~~
!!! error TS2872: This kind of expression is always truthy.
~
!!! error TS1005: ';' expected.
~
Expand Down
10 changes: 9 additions & 1 deletion tests/baselines/reference/predicateSemantics.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ predicateSemantics.ts(52,14): error TS2695: Left side of comma operator is unuse
predicateSemantics.ts(52,14): error TS2869: Right operand of ?? is unreachable because the left operand is never nullish.
predicateSemantics.ts(70,1): error TS2869: Right operand of ?? is unreachable because the left operand is never nullish.
predicateSemantics.ts(71,1): error TS2869: Right operand of ?? is unreachable because the left operand is never nullish.
predicateSemantics.ts(75,5): error TS2872: This kind of expression is always truthy.


==== predicateSemantics.ts (16 errors) ====
==== predicateSemantics.ts (17 errors) ====
declare let cond: any;

// OK: One or other operand is possibly nullish
Expand Down Expand Up @@ -120,4 +121,11 @@ predicateSemantics.ts(71,1): error TS2869: Right operand of ?? is unreachable be
`foo` ?? 32; // error
~~~~~
!!! error TS2869: Right operand of ?? is unreachable because the left operand is never nullish.

// https://github.com/microsoft/TypeScript/issues/60822
declare function test60822(arg: string, arg2?: string): string | null;
if (test60822("foo"), "bar") {} // error
~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2872: This kind of expression is always truthy.
if (test60822("foo"), test60822("bar")) {} // ok

7 changes: 7 additions & 0 deletions tests/baselines/reference/predicateSemantics.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ tag`foo${1}` ?? 32; // ok

`foo${1}` ?? 32; // error
`foo` ?? 32; // error

// https://github.com/microsoft/TypeScript/issues/60822
declare function test60822(arg: string, arg2?: string): string | null;
if (test60822("foo"), "bar") {} // error
if (test60822("foo"), test60822("bar")) {} // ok


//// [predicateSemantics.js]
Expand Down Expand Up @@ -140,3 +145,5 @@ var X = /** @class */ (function () {
(_k = tag(__makeTemplateObject(["foo", ""], ["foo", ""]), 1)) !== null && _k !== void 0 ? _k : 32; // ok
(_l = "foo".concat(1)) !== null && _l !== void 0 ? _l : 32; // error
"foo" !== null && "foo" !== void 0 ? "foo" : 32; // error
if (test60822("foo"), "bar") { } // error
if (test60822("foo"), test60822("bar")) { } // ok
13 changes: 13 additions & 0 deletions tests/baselines/reference/predicateSemantics.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,16 @@ tag`foo${1}` ?? 32; // ok
`foo${1}` ?? 32; // error
`foo` ?? 32; // error

// https://github.com/microsoft/TypeScript/issues/60822
declare function test60822(arg: string, arg2?: string): string | null;
>test60822 : Symbol(test60822, Decl(predicateSemantics.ts, 70, 12))
>arg : Symbol(arg, Decl(predicateSemantics.ts, 73, 27))
>arg2 : Symbol(arg2, Decl(predicateSemantics.ts, 73, 39))

if (test60822("foo"), "bar") {} // error
>test60822 : Symbol(test60822, Decl(predicateSemantics.ts, 70, 12))

if (test60822("foo"), test60822("bar")) {} // ok
>test60822 : Symbol(test60822, Decl(predicateSemantics.ts, 70, 12))
>test60822 : Symbol(test60822, Decl(predicateSemantics.ts, 70, 12))

37 changes: 37 additions & 0 deletions tests/baselines/reference/predicateSemantics.types
Original file line number Diff line number Diff line change
Expand Up @@ -375,3 +375,40 @@ tag`foo${1}` ?? 32; // ok
>32 : 32
> : ^^

// https://github.com/microsoft/TypeScript/issues/60822
declare function test60822(arg: string, arg2?: string): string | null;
>test60822 : (arg: string, arg2?: string) => string | null
> : ^ ^^ ^^ ^^^ ^^^^^
>arg : string
> : ^^^^^^
>arg2 : string
> : ^^^^^^

if (test60822("foo"), "bar") {} // error
>test60822("foo"), "bar" : "bar"
> : ^^^^^
>test60822("foo") : string
> : ^^^^^^
>test60822 : (arg: string, arg2?: string) => string | null
> : ^ ^^ ^^ ^^^ ^^^^^
>"foo" : "foo"
> : ^^^^^
>"bar" : "bar"
> : ^^^^^

if (test60822("foo"), test60822("bar")) {} // ok
>test60822("foo"), test60822("bar") : string
> : ^^^^^^
>test60822("foo") : string
> : ^^^^^^
>test60822 : (arg: string, arg2?: string) => string | null
> : ^ ^^ ^^ ^^^ ^^^^^
>"foo" : "foo"
> : ^^^^^
>test60822("bar") : string
> : ^^^^^^
>test60822 : (arg: string, arg2?: string) => string | null
> : ^ ^^ ^^ ^^^ ^^^^^
>"bar" : "bar"
> : ^^^^^

5 changes: 5 additions & 0 deletions tests/cases/compiler/predicateSemantics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,8 @@ tag`foo${1}` ?? 32; // ok

`foo${1}` ?? 32; // error
`foo` ?? 32; // error

// https://github.com/microsoft/TypeScript/issues/60822
declare function test60822(arg: string, arg2?: string): string | null;
if (test60822("foo"), "bar") {} // error
if (test60822("foo"), test60822("bar")) {} // ok
Loading