diff --git a/tsc/internal/binder/binder.go b/tsc/internal/binder/binder.go index e1058bc33b043..1b6c633fecc3c 100644 --- a/tsc/internal/binder/binder.go +++ b/tsc/internal/binder/binder.go @@ -10,6 +10,7 @@ import ( "github.com/microsoft/TypeScript/tsc/internal/core" "github.com/microsoft/TypeScript/tsc/internal/debug" "github.com/microsoft/TypeScript/tsc/internal/diagnostics" + "github.com/microsoft/TypeScript/tsc/internal/jsnum" "github.com/microsoft/TypeScript/tsc/internal/scanner" ) @@ -2304,6 +2305,10 @@ func (b *Binder) bindLogicalLikeExpression(node *ast.Node, trueTarget *ast.FlowL } else { b.bindCondition(expr.Left, trueTarget, preRightLabel) } + parent := ast.WalkUpParenthesizedExpressions(node.Parent) + if (expr.OperatorToken.Kind == ast.KindQuestionQuestionToken || expr.OperatorToken.Kind == ast.KindQuestionQuestionEqualsToken) && canBeFalsyNonNullish(expr.Left) && !ast.IsLogicalExpression(parent) && !(ast.IsOptionalChain(parent) && ast.SkipParentheses(parent.Expression()) == node) { + b.addAntecedent(falseTarget, b.createFlowCondition(ast.FlowFlagsCondition, b.currentFlow, expr.Left)) + } b.currentFlow = b.finishFlowLabel(preRightLabel) b.bind(expr.OperatorToken) if ast.IsLogicalOrCoalescingAssignmentOperator(expr.OperatorToken.Kind) { @@ -2316,6 +2321,23 @@ func (b *Binder) bindLogicalLikeExpression(node *ast.Node, trueTarget *ast.FlowL } } +func canBeFalsyNonNullish(node *ast.Node) bool { + node = ast.SkipOuterExpressions(node, ast.OEKAll) + switch node.Kind { + case ast.KindFalseKeyword, ast.KindThisKeyword, ast.KindPropertyAccessExpression, ast.KindElementAccessExpression, ast.KindTaggedTemplateExpression, ast.KindAwaitExpression, ast.KindYieldExpression: + return true + case ast.KindCallExpression: + return !ast.IsImportCall(node) + case ast.KindIdentifier: + return node.Text() != "undefined" + case ast.KindNumericLiteral, ast.KindBigIntLiteral: + return jsnum.ParsePseudoBigInt(node.Text()) == "0" + case ast.KindStringLiteral, ast.KindNoSubstitutionTemplateLiteral: + return node.Text() == "" + } + return false +} + func (b *Binder) bindDeleteExpressionFlow(node *ast.Node) { expr := node.AsDeleteExpression() b.bindEachChild(node) diff --git a/tsc/internal/checker/flow.go b/tsc/internal/checker/flow.go index 05daef984819d..8ad08621eebaa 100644 --- a/tsc/internal/checker/flow.go +++ b/tsc/internal/checker/flow.go @@ -366,6 +366,10 @@ func (c *Checker) getTypeAtFlowCondition(f *FlowState, flow *ast.FlowNode) FlowT assumeTrue := flow.Flags&ast.FlowFlagsTrueCondition != 0 nonEvolvingType := c.finalizeEvolvingArrayType(flowType.t) narrowedType := c.narrowType(f, nonEvolvingType, flow.Node, assumeTrue) + if flow.Flags&ast.FlowFlagsCondition == ast.FlowFlagsCondition { + node := ast.SkipOuterExpressions(flow.Node, ast.OEKAll) + narrowedType = c.narrowTypeByTruthiness(f, c.narrowTypeByOptionality(f, nonEvolvingType, node, true), node, false) + } if narrowedType == nonEvolvingType { return flowType } diff --git a/tsc/testdata/baselines/reference/compiler/nullishCoalescingFalsyLeftOperandFlow.errors.txt b/tsc/testdata/baselines/reference/compiler/nullishCoalescingFalsyLeftOperandFlow.errors.txt new file mode 100644 index 0000000000000..a83876cc1b961 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/nullishCoalescingFalsyLeftOperandFlow.errors.txt @@ -0,0 +1,43 @@ +nullishCoalescingFalsyLeftOperandFlow.ts(4,24): error TS2869: Right operand of ?? is unreachable because the left operand is never nullish. +nullishCoalescingFalsyLeftOperandFlow.ts(4,50): error TS7027: Unreachable code detected. +nullishCoalescingFalsyLeftOperandFlow.ts(5,24): error TS2871: This expression is always nullish. +nullishCoalescingFalsyLeftOperandFlow.ts(5,50): error TS7027: Unreachable code detected. +nullishCoalescingFalsyLeftOperandFlow.ts(6,25): error TS2869: Right operand of ?? is unreachable because the left operand is never nullish. +nullishCoalescingFalsyLeftOperandFlow.ts(6,59): error TS7027: Unreachable code detected. +nullishCoalescingFalsyLeftOperandFlow.ts(7,60): error TS7027: Unreachable code detected. +nullishCoalescingFalsyLeftOperandFlow.ts(8,112): error TS2322: Type '"" | undefined' is not assignable to type 'undefined'. + Type '""' is not assignable to type 'undefined'. + + +==== nullishCoalescingFalsyLeftOperandFlow.ts (8 errors) ==== + declare function c(x?: undefined): boolean; + + function f1() { while (c() ?? (c() || true)) {} c(); } + function f2() { while (true ?? (c() || true)) {} c(); } + ~~~~ +!!! error TS2869: Right operand of ?? is unreachable because the left operand is never nullish. + ~~~~ +!!! error TS7027: Unreachable code detected. + function f3() { while (null ?? (c() || true)) {} c(); } + ~~~~ +!!! error TS2871: This expression is always nullish. + ~~~~ +!!! error TS7027: Unreachable code detected. + function f4() { while ((c() ? 1 : 2) ?? (c() || true)) {} c(); } + ~~~~~~~~~~~ +!!! error TS2869: Right operand of ?? is unreachable because the left operand is never nullish. + ~~~~ +!!! error TS7027: Unreachable code detected. + function f5() { while ((c() || null)! ?? (c() || true)) {} c(); } + ~~~~ +!!! error TS7027: Unreachable code detected. + function f6(a: "" | object | undefined, b: boolean) { if (a ?? b) {} else { const s: "" | undefined = a; const u: undefined = a; } ((a ?? b) || b) ?? c(a); } + ~ +!!! error TS2322: Type '"" | undefined' is not assignable to type 'undefined'. +!!! error TS2322: Type '""' is not assignable to type 'undefined'. + function f7(a: "" | boolean | undefined, b: boolean) { if (a ??= b) {} else { a === ""; } } + function f8(a: "" | undefined) { let b: 0 | 1 = 0; if ((a ?? (b = 1, ""))?.[b = 1]) {} else { const one: 1 = b; } } + function f9(a: ((x: 1) => boolean) | undefined, d: (x: 1) => boolean) { let b: 0 | 1 = 0; if ((a ?? (b = 1, d))?.(b = 1)) {} else { const one: 1 = b; } } + function f10(u: { tag: ""; empty: 1 } | { tag: "ok" }) { if ((u.tag) ?? (false || true)) {} else { u.empty; } } + function f11(u: { tag: ""; empty: 1 } | { tag: "ok" }) { if (u.tag! ?? (false || true)) {} else { u.empty; } } + \ No newline at end of file diff --git a/tsc/testdata/tests/cases/compiler/nullishCoalescingFalsyLeftOperandFlow.ts b/tsc/testdata/tests/cases/compiler/nullishCoalescingFalsyLeftOperandFlow.ts new file mode 100644 index 0000000000000..c23d32b978c15 --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/nullishCoalescingFalsyLeftOperandFlow.ts @@ -0,0 +1,18 @@ +// @strict: true +// @allowUnreachableCode: false +// @noEmit: true +// @noTypesAndSymbols: true + +declare function c(x?: undefined): boolean; + +function f1() { while (c() ?? (c() || true)) {} c(); } +function f2() { while (true ?? (c() || true)) {} c(); } +function f3() { while (null ?? (c() || true)) {} c(); } +function f4() { while ((c() ? 1 : 2) ?? (c() || true)) {} c(); } +function f5() { while ((c() || null)! ?? (c() || true)) {} c(); } +function f6(a: "" | object | undefined, b: boolean) { if (a ?? b) {} else { const s: "" | undefined = a; const u: undefined = a; } ((a ?? b) || b) ?? c(a); } +function f7(a: "" | boolean | undefined, b: boolean) { if (a ??= b) {} else { a === ""; } } +function f8(a: "" | undefined) { let b: 0 | 1 = 0; if ((a ?? (b = 1, ""))?.[b = 1]) {} else { const one: 1 = b; } } +function f9(a: ((x: 1) => boolean) | undefined, d: (x: 1) => boolean) { let b: 0 | 1 = 0; if ((a ?? (b = 1, d))?.(b = 1)) {} else { const one: 1 = b; } } +function f10(u: { tag: ""; empty: 1 } | { tag: "ok" }) { if ((u.tag) ?? (false || true)) {} else { u.empty; } } +function f11(u: { tag: ""; empty: 1 } | { tag: "ok" }) { if (u.tag! ?? (false || true)) {} else { u.empty; } }