From cbda51a5969ddf5d76ad381987d928028dc76d9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Mon, 4 Nov 2024 21:43:30 +0100 Subject: [PATCH 1/3] Fixed syntactic nullisness semantics for comma expressions (#60402) --- src/compiler/checker.ts | 2 + .../reference/predicateSemantics.errors.txt | 23 +++++- .../baselines/reference/predicateSemantics.js | 22 +++++- .../reference/predicateSemantics.symbols | 25 +++++++ .../reference/predicateSemantics.types | 74 +++++++++++++++++++ tests/cases/compiler/predicateSemantics.ts | 11 ++- 6 files changed, 152 insertions(+), 5 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index f4cdc626c270f..7607f540a80be 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -39801,6 +39801,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..1c9de704f1628 100644 --- a/tests/baselines/reference/predicateSemantics.errors.txt +++ b/tests/baselines/reference/predicateSemantics.errors.txt @@ -9,9 +9,12 @@ 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(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 (11 errors) ==== +==== predicateSemantics.ts (14 errors) ==== declare let cond: any; // OK: One or other operand is possibly nullish @@ -77,4 +80,20 @@ 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. + 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 eb0b66516b62c..3641305b71805 100644 --- a/tests/baselines/reference/predicateSemantics.js +++ b/tests/baselines/reference/predicateSemantics.js @@ -44,10 +44,20 @@ 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 + const f = (maybe, i++) ?? true; // error +} + //// [predicateSemantics.js] -var _a, _b, _c, _d, _e, _f; +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 @@ -88,3 +98,11 @@ 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 + 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 790e965f988cc..c91201e3fcda2 100644 --- a/tests/baselines/reference/predicateSemantics.symbols +++ b/tests/baselines/reference/predicateSemantics.symbols @@ -79,3 +79,28 @@ 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)) + + 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 3d3eba6683e25..ee88a95cd16ea 100644 --- a/tests/baselines/reference/predicateSemantics.types +++ b/tests/baselines/reference/predicateSemantics.types @@ -234,3 +234,77 @@ 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 +> : ^^^^ + + 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 +} From ee0eca7689857cf031d9e26bdebc95f83c5d933b Mon Sep 17 00:00:00 2001 From: Ryan Cavanaugh Date: Wed, 6 Nov 2024 13:06:12 -0800 Subject: [PATCH 2/3] MetaProperty is possibly nullish (#60440) --- src/compiler/checker.ts | 1 + .../reference/predicateSemantics.errors.txt | 7 +++++++ .../baselines/reference/predicateSemantics.js | 16 +++++++++++++++ .../reference/predicateSemantics.symbols | 12 +++++++++++ .../reference/predicateSemantics.types | 20 +++++++++++++++++++ tests/cases/compiler/predicateSemantics.ts | 7 +++++++ 6 files changed, 63 insertions(+) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 7607f540a80be..5ace597dbbc4f 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -39784,6 +39784,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { case SyntaxKind.AwaitExpression: case SyntaxKind.CallExpression: case SyntaxKind.ElementAccessExpression: + case SyntaxKind.MetaProperty: case SyntaxKind.NewExpression: case SyntaxKind.PropertyAccessExpression: case SyntaxKind.YieldExpression: diff --git a/tests/baselines/reference/predicateSemantics.errors.txt b/tests/baselines/reference/predicateSemantics.errors.txt index 1c9de704f1628..0dc25d43dbf2b 100644 --- a/tests/baselines/reference/predicateSemantics.errors.txt +++ b/tests/baselines/reference/predicateSemantics.errors.txt @@ -96,4 +96,11 @@ predicateSemantics.ts(52,14): error TS2869: Right operand of ?? is unreachable b ~~~~~~~~~~ !!! error TS2869: Right operand of ?? is unreachable because the left operand is never nullish. } + + // https://github.com/microsoft/TypeScript/issues/60439 + class X { + constructor() { + const p = new.target ?? 32; + } + } \ No newline at end of file diff --git a/tests/baselines/reference/predicateSemantics.js b/tests/baselines/reference/predicateSemantics.js index 3641305b71805..f4edc198ffecd 100644 --- a/tests/baselines/reference/predicateSemantics.js +++ b/tests/baselines/reference/predicateSemantics.js @@ -54,6 +54,13 @@ function foo(this: Object | undefined) { const e = (i++, i++) ?? true; // error const f = (maybe, i++) ?? true; // error } + +// https://github.com/microsoft/TypeScript/issues/60439 +class X { + constructor() { + const p = new.target ?? 32; + } +} //// [predicateSemantics.js] @@ -106,3 +113,12 @@ function foo() { var e = (_h = (i++, i++)) !== null && _h !== void 0 ? _h : true; // error var f = (_j = (maybe, i++)) !== null && _j !== void 0 ? _j : true; // error } +// https://github.com/microsoft/TypeScript/issues/60439 +var X = /** @class */ (function () { + function X() { + var _newTarget = this.constructor; + var _a; + var p = (_a = _newTarget) !== null && _a !== void 0 ? _a : 32; + } + return X; +}()); diff --git a/tests/baselines/reference/predicateSemantics.symbols b/tests/baselines/reference/predicateSemantics.symbols index c91201e3fcda2..e5c831515a0bc 100644 --- a/tests/baselines/reference/predicateSemantics.symbols +++ b/tests/baselines/reference/predicateSemantics.symbols @@ -104,3 +104,15 @@ function foo(this: Object | undefined) { >i : Symbol(i, Decl(predicateSemantics.ts, 48, 5)) } +// https://github.com/microsoft/TypeScript/issues/60439 +class X { +>X : Symbol(X, Decl(predicateSemantics.ts, 52, 1)) + + constructor() { + const p = new.target ?? 32; +>p : Symbol(p, Decl(predicateSemantics.ts, 57, 9)) +>new.target : Symbol(X, Decl(predicateSemantics.ts, 52, 1)) +>target : Symbol(X, Decl(predicateSemantics.ts, 52, 1)) + } +} + diff --git a/tests/baselines/reference/predicateSemantics.types b/tests/baselines/reference/predicateSemantics.types index ee88a95cd16ea..de0c9daf76eb0 100644 --- a/tests/baselines/reference/predicateSemantics.types +++ b/tests/baselines/reference/predicateSemantics.types @@ -308,3 +308,23 @@ function foo(this: Object | undefined) { > : ^^^^ } +// https://github.com/microsoft/TypeScript/issues/60439 +class X { +>X : X +> : ^ + + constructor() { + const p = new.target ?? 32; +>p : 32 | typeof X +> : ^^^^^^^^^^^^^ +>new.target ?? 32 : 32 | typeof X +> : ^^^^^^^^^^^^^ +>new.target : typeof X +> : ^^^^^^^^ +>target : typeof X +> : ^^^^^^^^ +>32 : 32 +> : ^^ + } +} + diff --git a/tests/cases/compiler/predicateSemantics.ts b/tests/cases/compiler/predicateSemantics.ts index 88374b9a2ff6b..e937b1d6182a8 100644 --- a/tests/cases/compiler/predicateSemantics.ts +++ b/tests/cases/compiler/predicateSemantics.ts @@ -51,3 +51,10 @@ function foo(this: Object | undefined) { const e = (i++, i++) ?? true; // error const f = (maybe, i++) ?? true; // error } + +// https://github.com/microsoft/TypeScript/issues/60439 +class X { + constructor() { + const p = new.target ?? 32; + } +} From 75372aa4d620191627d5431e76830ce2b05ae44f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Wed, 27 Nov 2024 00:17:29 +0100 Subject: [PATCH 3/3] Fixed syntactic nullisness semantics for tagged template expressions (#60616) --- src/compiler/checker.ts | 1 + .../reference/predicateSemantics.errors.txt | 19 +++++++- .../baselines/reference/predicateSemantics.js | 20 +++++++- .../reference/predicateSemantics.symbols | 21 +++++++++ .../reference/predicateSemantics.types | 47 +++++++++++++++++++ tests/cases/compiler/predicateSemantics.ts | 11 +++++ 6 files changed, 117 insertions(+), 2 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 5ace597dbbc4f..9aa454547e3d9 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -39783,6 +39783,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { switch (node.kind) { case SyntaxKind.AwaitExpression: case SyntaxKind.CallExpression: + case SyntaxKind.TaggedTemplateExpression: case SyntaxKind.ElementAccessExpression: case SyntaxKind.MetaProperty: case SyntaxKind.NewExpression: diff --git a/tests/baselines/reference/predicateSemantics.errors.txt b/tests/baselines/reference/predicateSemantics.errors.txt index 0dc25d43dbf2b..c80dd47d19357 100644 --- a/tests/baselines/reference/predicateSemantics.errors.txt +++ b/tests/baselines/reference/predicateSemantics.errors.txt @@ -12,9 +12,11 @@ predicateSemantics.ts(36,8): error TS2872: This kind of expression is always tru 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(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 (14 errors) ==== +==== predicateSemantics.ts (16 errors) ==== declare let cond: any; // OK: One or other operand is possibly nullish @@ -103,4 +105,19 @@ predicateSemantics.ts(52,14): error TS2869: Right operand of ?? is unreachable b const p = new.target ?? 32; } } + + // https://github.com/microsoft/TypeScript/issues/60614 + declare function tag( + strings: TemplateStringsArray, + ...values: number[] + ): T | null; + + tag`foo${1}` ?? 32; // ok + + `foo${1}` ?? 32; // error + ~~~~~~~~~ +!!! error TS2869: Right operand of ?? is unreachable because the left operand is never nullish. + `foo` ?? 32; // 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 f4edc198ffecd..30ada0d514d02 100644 --- a/tests/baselines/reference/predicateSemantics.js +++ b/tests/baselines/reference/predicateSemantics.js @@ -61,10 +61,25 @@ class X { const p = new.target ?? 32; } } + +// https://github.com/microsoft/TypeScript/issues/60614 +declare function tag( + strings: TemplateStringsArray, + ...values: number[] +): T | null; + +tag`foo${1}` ?? 32; // ok + +`foo${1}` ?? 32; // error +`foo` ?? 32; // error //// [predicateSemantics.js] -var _a, _b, _c, _d, _e, _f, _g, _h, _j; +var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) { + if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; } + return cooked; +}; +var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l; // 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 @@ -122,3 +137,6 @@ var X = /** @class */ (function () { } return X; }()); +(_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 diff --git a/tests/baselines/reference/predicateSemantics.symbols b/tests/baselines/reference/predicateSemantics.symbols index e5c831515a0bc..f8ddf0fe5db87 100644 --- a/tests/baselines/reference/predicateSemantics.symbols +++ b/tests/baselines/reference/predicateSemantics.symbols @@ -116,3 +116,24 @@ class X { } } +// https://github.com/microsoft/TypeScript/issues/60614 +declare function tag( +>tag : Symbol(tag, Decl(predicateSemantics.ts, 59, 1)) +>T : Symbol(T, Decl(predicateSemantics.ts, 62, 21)) + + strings: TemplateStringsArray, +>strings : Symbol(strings, Decl(predicateSemantics.ts, 62, 24)) +>TemplateStringsArray : Symbol(TemplateStringsArray, Decl(lib.es5.d.ts, --, --)) + + ...values: number[] +>values : Symbol(values, Decl(predicateSemantics.ts, 63, 32)) + +): T | null; +>T : Symbol(T, Decl(predicateSemantics.ts, 62, 21)) + +tag`foo${1}` ?? 32; // ok +>tag : Symbol(tag, Decl(predicateSemantics.ts, 59, 1)) + +`foo${1}` ?? 32; // error +`foo` ?? 32; // error + diff --git a/tests/baselines/reference/predicateSemantics.types b/tests/baselines/reference/predicateSemantics.types index de0c9daf76eb0..b054c66ad43e7 100644 --- a/tests/baselines/reference/predicateSemantics.types +++ b/tests/baselines/reference/predicateSemantics.types @@ -328,3 +328,50 @@ class X { } } +// https://github.com/microsoft/TypeScript/issues/60614 +declare function tag( +>tag : (strings: TemplateStringsArray, ...values: number[]) => T | null +> : ^ ^^ ^^ ^^^^^ ^^ ^^^^^ + + strings: TemplateStringsArray, +>strings : TemplateStringsArray +> : ^^^^^^^^^^^^^^^^^^^^ + + ...values: number[] +>values : number[] +> : ^^^^^^^^ + +): T | null; + +tag`foo${1}` ?? 32; // ok +>tag`foo${1}` ?? 32 : unknown +> : ^^^^^^^ +>tag`foo${1}` : unknown +> : ^^^^^^^ +>tag : (strings: TemplateStringsArray, ...values: number[]) => T | null +> : ^ ^^ ^^ ^^^^^ ^^ ^^^^^ +>`foo${1}` : string +> : ^^^^^^ +>1 : 1 +> : ^ +>32 : 32 +> : ^^ + +`foo${1}` ?? 32; // error +>`foo${1}` ?? 32 : 32 | "foo1" +> : ^^^^^^^^^^^ +>`foo${1}` : "foo1" +> : ^^^^^^ +>1 : 1 +> : ^ +>32 : 32 +> : ^^ + +`foo` ?? 32; // error +>`foo` ?? 32 : 32 | "foo" +> : ^^^^^^^^^^ +>`foo` : "foo" +> : ^^^^^ +>32 : 32 +> : ^^ + diff --git a/tests/cases/compiler/predicateSemantics.ts b/tests/cases/compiler/predicateSemantics.ts index e937b1d6182a8..a22b34341e7d5 100644 --- a/tests/cases/compiler/predicateSemantics.ts +++ b/tests/cases/compiler/predicateSemantics.ts @@ -58,3 +58,14 @@ class X { const p = new.target ?? 32; } } + +// https://github.com/microsoft/TypeScript/issues/60614 +declare function tag( + strings: TemplateStringsArray, + ...values: number[] +): T | null; + +tag`foo${1}` ?? 32; // ok + +`foo${1}` ?? 32; // error +`foo` ?? 32; // error