Fixed syntactic nullisness semantics for comma expressions (#60402)

This commit is contained in:
Mateusz Burzyński 2024-11-04 21:43:30 +01:00 committed by GitHub
parent 0ec4d30a6e
commit c09e2ab4ff
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 152 additions and 5 deletions

View File

@ -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:

View File

@ -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;
}
}
// 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.
}

View File

@ -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
}

View File

@ -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))
}

View File

@ -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
> : ^^^^
}

View File

@ -41,4 +41,13 @@ 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
}