From 10129a452a893f9b6ab166912ccb7529058e9947 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Mon, 4 Nov 2024 00:07:30 +0100 Subject: [PATCH 1/2] Fixed syntactic nullisness semantics for comma expressions --- src/compiler/checker.ts | 2 + .../reference/predicateSemantics.errors.txt | 16 +++++- .../baselines/reference/predicateSemantics.js | 20 ++++++- .../reference/predicateSemantics.symbols | 20 +++++++ .../reference/predicateSemantics.types | 56 +++++++++++++++++++ 5 files changed, 110 insertions(+), 4 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index ce0291367b510..d1d09f32dd8cd 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -39772,6 +39772,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { case SyntaxKind.AmpersandAmpersandToken: case SyntaxKind.AmpersandAmpersandEqualsToken: return PredicateSemantics.Sometimes; + case SyntaxKind.CommaToken: + return getSyntacticNullishnessSemantics((node as BinaryExpression).right); } return PredicateSemantics.Never; case SyntaxKind.ConditionalExpression: diff --git a/tests/baselines/reference/predicateSemantics.errors.txt b/tests/baselines/reference/predicateSemantics.errors.txt index 14251121358c4..cd37d45749b4c 100644 --- a/tests/baselines/reference/predicateSemantics.errors.txt +++ b/tests/baselines/reference/predicateSemantics.errors.txt @@ -9,9 +9,10 @@ predicateSemantics.ts(33,8): error TS2872: This kind of expression is always tru predicateSemantics.ts(34,11): error TS2872: This kind of expression is always truthy. predicateSemantics.ts(35,8): error TS2872: This kind of expression is always truthy. predicateSemantics.ts(36,8): error TS2872: This kind of expression is always truthy. +predicateSemantics.ts(51,14): error TS2869: Right operand of ?? is unreachable because the left operand is never nullish. -==== predicateSemantics.ts (11 errors) ==== +==== predicateSemantics.ts (12 errors) ==== declare let cond: any; // OK: One or other operand is possibly nullish @@ -77,4 +78,15 @@ predicateSemantics.ts(36,8): error TS2872: This kind of expression is always tru function foo(this: Object | undefined) { // Should be OK return this ?? 0; - } \ No newline at end of file + } + + // https://github.com/microsoft/TypeScript/issues/60401 + { + const maybe = null as true | null; + let i = 0; + const d = (i++, maybe) ?? true; // ok + const e = (i++, i++) ?? true; // error + ~~~~~~~~ +!!! error TS2869: Right operand of ?? is unreachable because the left operand is never nullish. + } + \ No newline at end of file diff --git a/tests/baselines/reference/predicateSemantics.js b/tests/baselines/reference/predicateSemantics.js index eb0b66516b62c..a668d76b9015f 100644 --- a/tests/baselines/reference/predicateSemantics.js +++ b/tests/baselines/reference/predicateSemantics.js @@ -44,10 +44,19 @@ console.log((cond || undefined) && 1 / cond); function foo(this: Object | undefined) { // Should be OK return this ?? 0; -} +} + +// https://github.com/microsoft/TypeScript/issues/60401 +{ + const maybe = null as true | null; + let i = 0; + const d = (i++, maybe) ?? true; // ok + const e = (i++, i++) ?? true; // error +} + //// [predicateSemantics.js] -var _a, _b, _c, _d, _e, _f; +var _a, _b, _c, _d, _e, _f, _g, _h; // OK: One or other operand is possibly nullish var test1 = (_a = (cond ? undefined : 32)) !== null && _a !== void 0 ? _a : "possibly reached"; // Not OK: Both operands nullish @@ -88,3 +97,10 @@ function foo() { // Should be OK return this !== null && this !== void 0 ? this : 0; } +// https://github.com/microsoft/TypeScript/issues/60401 +{ + var maybe = null; + var i = 0; + var d = (_g = (i++, maybe)) !== null && _g !== void 0 ? _g : true; // ok + var e = (_h = (i++, i++)) !== null && _h !== void 0 ? _h : true; // error +} diff --git a/tests/baselines/reference/predicateSemantics.symbols b/tests/baselines/reference/predicateSemantics.symbols index 790e965f988cc..5c0f4dae0ef1c 100644 --- a/tests/baselines/reference/predicateSemantics.symbols +++ b/tests/baselines/reference/predicateSemantics.symbols @@ -79,3 +79,23 @@ function foo(this: Object | undefined) { return this ?? 0; >this : Symbol(this, Decl(predicateSemantics.ts, 40, 13)) } + +// https://github.com/microsoft/TypeScript/issues/60401 +{ + const maybe = null as true | null; +>maybe : Symbol(maybe, Decl(predicateSemantics.ts, 47, 7)) + + let i = 0; +>i : Symbol(i, Decl(predicateSemantics.ts, 48, 5)) + + const d = (i++, maybe) ?? true; // ok +>d : Symbol(d, Decl(predicateSemantics.ts, 49, 7)) +>i : Symbol(i, Decl(predicateSemantics.ts, 48, 5)) +>maybe : Symbol(maybe, Decl(predicateSemantics.ts, 47, 7)) + + const e = (i++, i++) ?? true; // error +>e : Symbol(e, Decl(predicateSemantics.ts, 50, 7)) +>i : Symbol(i, Decl(predicateSemantics.ts, 48, 5)) +>i : Symbol(i, Decl(predicateSemantics.ts, 48, 5)) +} + diff --git a/tests/baselines/reference/predicateSemantics.types b/tests/baselines/reference/predicateSemantics.types index 3d3eba6683e25..ff7a44aebfe91 100644 --- a/tests/baselines/reference/predicateSemantics.types +++ b/tests/baselines/reference/predicateSemantics.types @@ -234,3 +234,59 @@ function foo(this: Object | undefined) { >0 : 0 > : ^ } + +// https://github.com/microsoft/TypeScript/issues/60401 +{ + const maybe = null as true | null; +>maybe : true +> : ^^^^ +>null as true | null : true +> : ^^^^ +>true : true +> : ^^^^ + + let i = 0; +>i : number +> : ^^^^^^ +>0 : 0 +> : ^ + + const d = (i++, maybe) ?? true; // ok +>d : true +> : ^^^^ +>(i++, maybe) ?? true : true +> : ^^^^ +>(i++, maybe) : true +> : ^^^^ +>i++, maybe : true +> : ^^^^ +>i++ : number +> : ^^^^^^ +>i : number +> : ^^^^^^ +>maybe : true +> : ^^^^ +>true : true +> : ^^^^ + + const e = (i++, i++) ?? true; // error +>e : number | true +> : ^^^^^^^^^^^^^ +>(i++, i++) ?? true : number | true +> : ^^^^^^^^^^^^^ +>(i++, i++) : number +> : ^^^^^^ +>i++, i++ : number +> : ^^^^^^ +>i++ : number +> : ^^^^^^ +>i : number +> : ^^^^^^ +>i++ : number +> : ^^^^^^ +>i : number +> : ^^^^^^ +>true : true +> : ^^^^ +} + From 6d61888818a96bb272733ad0cab40df8d7309fc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Mon, 4 Nov 2024 00:09:28 +0100 Subject: [PATCH 2/2] add extra test case --- .../reference/predicateSemantics.errors.txt | 9 ++++++++- .../baselines/reference/predicateSemantics.js | 4 +++- .../reference/predicateSemantics.symbols | 5 +++++ .../reference/predicateSemantics.types | 18 ++++++++++++++++++ tests/cases/compiler/predicateSemantics.ts | 11 ++++++++++- 5 files changed, 44 insertions(+), 3 deletions(-) diff --git a/tests/baselines/reference/predicateSemantics.errors.txt b/tests/baselines/reference/predicateSemantics.errors.txt index cd37d45749b4c..1c9de704f1628 100644 --- a/tests/baselines/reference/predicateSemantics.errors.txt +++ b/tests/baselines/reference/predicateSemantics.errors.txt @@ -10,9 +10,11 @@ predicateSemantics.ts(34,11): error TS2872: This kind of expression is always tr predicateSemantics.ts(35,8): error TS2872: This kind of expression is always truthy. predicateSemantics.ts(36,8): error TS2872: This kind of expression is always truthy. predicateSemantics.ts(51,14): error TS2869: Right operand of ?? is unreachable because the left operand is never nullish. +predicateSemantics.ts(52,14): error TS2695: Left side of comma operator is unused and has no side effects. +predicateSemantics.ts(52,14): error TS2869: Right operand of ?? is unreachable because the left operand is never nullish. -==== predicateSemantics.ts (12 errors) ==== +==== predicateSemantics.ts (14 errors) ==== declare let cond: any; // OK: One or other operand is possibly nullish @@ -87,6 +89,11 @@ predicateSemantics.ts(51,14): error TS2869: Right operand of ?? is unreachable b const d = (i++, maybe) ?? true; // ok const e = (i++, i++) ?? true; // error ~~~~~~~~ +!!! error TS2869: Right operand of ?? is unreachable because the left operand is never nullish. + const f = (maybe, i++) ?? true; // error + ~~~~~ +!!! error TS2695: Left side of comma operator is unused and has no side effects. + ~~~~~~~~~~ !!! error TS2869: Right operand of ?? is unreachable because the left operand is never nullish. } \ No newline at end of file diff --git a/tests/baselines/reference/predicateSemantics.js b/tests/baselines/reference/predicateSemantics.js index a668d76b9015f..3641305b71805 100644 --- a/tests/baselines/reference/predicateSemantics.js +++ b/tests/baselines/reference/predicateSemantics.js @@ -52,11 +52,12 @@ function foo(this: Object | undefined) { let i = 0; const d = (i++, maybe) ?? true; // ok const e = (i++, i++) ?? true; // error + const f = (maybe, i++) ?? true; // error } //// [predicateSemantics.js] -var _a, _b, _c, _d, _e, _f, _g, _h; +var _a, _b, _c, _d, _e, _f, _g, _h, _j; // OK: One or other operand is possibly nullish var test1 = (_a = (cond ? undefined : 32)) !== null && _a !== void 0 ? _a : "possibly reached"; // Not OK: Both operands nullish @@ -103,4 +104,5 @@ function foo() { var i = 0; var d = (_g = (i++, maybe)) !== null && _g !== void 0 ? _g : true; // ok var e = (_h = (i++, i++)) !== null && _h !== void 0 ? _h : true; // error + var f = (_j = (maybe, i++)) !== null && _j !== void 0 ? _j : true; // error } diff --git a/tests/baselines/reference/predicateSemantics.symbols b/tests/baselines/reference/predicateSemantics.symbols index 5c0f4dae0ef1c..c91201e3fcda2 100644 --- a/tests/baselines/reference/predicateSemantics.symbols +++ b/tests/baselines/reference/predicateSemantics.symbols @@ -96,6 +96,11 @@ function foo(this: Object | undefined) { const e = (i++, i++) ?? true; // error >e : Symbol(e, Decl(predicateSemantics.ts, 50, 7)) >i : Symbol(i, Decl(predicateSemantics.ts, 48, 5)) +>i : Symbol(i, Decl(predicateSemantics.ts, 48, 5)) + + const f = (maybe, i++) ?? true; // error +>f : Symbol(f, Decl(predicateSemantics.ts, 51, 7)) +>maybe : Symbol(maybe, Decl(predicateSemantics.ts, 47, 7)) >i : Symbol(i, Decl(predicateSemantics.ts, 48, 5)) } diff --git a/tests/baselines/reference/predicateSemantics.types b/tests/baselines/reference/predicateSemantics.types index ff7a44aebfe91..ee88a95cd16ea 100644 --- a/tests/baselines/reference/predicateSemantics.types +++ b/tests/baselines/reference/predicateSemantics.types @@ -287,6 +287,24 @@ function foo(this: Object | undefined) { >i : number > : ^^^^^^ >true : true +> : ^^^^ + + const f = (maybe, i++) ?? true; // error +>f : number | true +> : ^^^^^^^^^^^^^ +>(maybe, i++) ?? true : number | true +> : ^^^^^^^^^^^^^ +>(maybe, i++) : number +> : ^^^^^^ +>maybe, i++ : number +> : ^^^^^^ +>maybe : true +> : ^^^^ +>i++ : number +> : ^^^^^^ +>i : number +> : ^^^^^^ +>true : true > : ^^^^ } diff --git a/tests/cases/compiler/predicateSemantics.ts b/tests/cases/compiler/predicateSemantics.ts index d6e12b297b25b..88374b9a2ff6b 100644 --- a/tests/cases/compiler/predicateSemantics.ts +++ b/tests/cases/compiler/predicateSemantics.ts @@ -41,4 +41,13 @@ console.log((cond || undefined) && 1 / cond); function foo(this: Object | undefined) { // Should be OK return this ?? 0; -} \ No newline at end of file +} + +// https://github.com/microsoft/TypeScript/issues/60401 +{ + const maybe = null as true | null; + let i = 0; + const d = (i++, maybe) ?? true; // ok + const e = (i++, i++) ?? true; // error + const f = (maybe, i++) ?? true; // error +}