Skip to content
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
1 change: 1 addition & 0 deletions tsc/internal/ast/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
FlowFlagsReduceLabel FlowFlags = 1 << 10 // Temporarily reduce antecedents of label
FlowFlagsReferenced FlowFlags = 1 << 11 // Referenced as antecedent once
FlowFlagsShared FlowFlags = 1 << 12 // Referenced as antecedent more than once
FlowFlagsNonNullish FlowFlags = 1 << 13 // With FalseCondition, falsy but not nullish left operand of ?? or ??=
FlowFlagsLabel = FlowFlagsBranchLabel | FlowFlagsLoopLabel
FlowFlagsCondition = FlowFlagsTrueCondition | FlowFlagsFalseCondition
)
Expand Down
38 changes: 38 additions & 0 deletions tsc/internal/binder/binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ type Binder struct {
currentReturnTarget *ast.FlowLabel
currentTrueTarget *ast.FlowLabel
currentFalseTarget *ast.FlowLabel
currentNullishTarget *ast.FlowLabel // Target of a nullish left operand of the ?? or ??= whose left operand is being bound
currentFalsyTarget *ast.FlowLabel // Target of a falsy, non-nullish left operand of that ?? or ??=
currentExceptionTarget *ast.FlowLabel
preSwitchCaseFlow *ast.FlowNode
activeLabelList *ActiveLabel
Expand Down Expand Up @@ -488,6 +490,12 @@ func (b *Binder) createFlowCondition(flags ast.FlowFlags, antecedent *ast.FlowNo
}
return b.unreachableFlow
}
if flags&ast.FlowFlagsNonNullish != 0 {
kind := ast.SkipParentheses(expression).Kind
if kind == ast.KindTrueKeyword || kind == ast.KindNullKeyword {
return b.unreachableFlow
}
}
if (expression.Kind == ast.KindTrueKeyword && flags&ast.FlowFlagsFalseCondition != 0 || expression.Kind == ast.KindFalseKeyword && flags&ast.FlowFlagsTrueCondition != 0) && !ast.IsExpressionOfOptionalChainRoot(expression) && !ast.IsNullishCoalesce(expression.Parent) {
return b.unreachableFlow
}
Expand Down Expand Up @@ -2301,6 +2309,8 @@ func (b *Binder) bindLogicalLikeExpression(node *ast.Node, trueTarget *ast.FlowL
preRightLabel := b.createBranchLabel()
if expr.OperatorToken.Kind == ast.KindAmpersandAmpersandToken || expr.OperatorToken.Kind == ast.KindAmpersandAmpersandEqualsToken {
b.bindCondition(expr.Left, preRightLabel, falseTarget)
} else if expr.OperatorToken.Kind == ast.KindQuestionQuestionToken || expr.OperatorToken.Kind == ast.KindQuestionQuestionEqualsToken {
b.bindNullishCoalescingLeft(node, trueTarget, falseTarget, preRightLabel)
} else {
b.bindCondition(expr.Left, trueTarget, preRightLabel)
}
Expand All @@ -2316,6 +2326,34 @@ func (b *Binder) bindLogicalLikeExpression(node *ast.Node, trueTarget *ast.FlowL
}
}

// A left operand of ?? or ??= that is falsy but not nullish makes the whole expression falsy without
// evaluating the right operand, so we add a path from such a left operand to the false target.
func (b *Binder) bindNullishCoalescingLeft(node *ast.Node, trueTarget *ast.FlowLabel, falseTarget *ast.FlowLabel, preRightLabel *ast.FlowLabel) {
left := node.AsBinaryExpression().Left
falsyTarget := falseTarget
if parent := ast.WalkUpParenthesizedExpressions(node.Parent); ast.IsOptionalChainRoot(parent) && ast.SkipParentheses(parent.Expression()) == node {
// In (a ?? b)?.c, a falsy, non-nullish a continues the chain
falsyTarget = trueTarget
} else if falseTarget == b.currentNullishTarget {
// In (a ?? b) ?? c, a falsy, non-nullish a makes the left operand of the outer ?? falsy and non-nullish
falsyTarget = b.currentFalsyTarget
}
saveNullishTarget := b.currentNullishTarget
saveFalsyTarget := b.currentFalsyTarget
b.currentNullishTarget = preRightLabel
b.currentFalsyTarget = falsyTarget
b.bindCondition(left, trueTarget, preRightLabel)
b.currentNullishTarget = saveNullishTarget
b.currentFalsyTarget = saveFalsyTarget
if isLogicalAssignmentExpression(left) || ast.IsLogicalExpression(left) {
// Conservatively assume that any falsy outcome of a logical left operand may be non-nullish
b.addAntecedent(falsyTarget, b.finishFlowLabel(preRightLabel))
} else if falsyTarget != trueTarget {
// When the targets are the same, the true condition of a non-nullish left operand already applies
b.addAntecedent(falsyTarget, b.createFlowCondition(ast.FlowFlagsFalseCondition|ast.FlowFlagsNonNullish, b.currentFlow, left))
}
}

func (b *Binder) bindDeleteExpressionFlow(node *ast.Node) {
expr := node.AsDeleteExpression()
b.bindEachChild(node)
Expand Down
8 changes: 7 additions & 1 deletion tsc/internal/checker/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,13 @@ func (c *Checker) getTypeAtFlowCondition(f *FlowState, flow *ast.FlowNode) FlowT
// *only* place a silent never type is ever generated.
assumeTrue := flow.Flags&ast.FlowFlagsTrueCondition != 0
nonEvolvingType := c.finalizeEvolvingArrayType(flowType.t)
narrowedType := c.narrowType(f, nonEvolvingType, flow.Node, assumeTrue)
var narrowedType *Type
if flow.Flags&ast.FlowFlagsNonNullish != 0 {
node := ast.SkipOuterExpressions(flow.Node, ast.OEKAll)
narrowedType = c.narrowTypeByTruthiness(f, c.narrowTypeByOptionality(f, nonEvolvingType, node, true /*assumePresent*/), node, false /*assumeTrue*/)
} else {
narrowedType = c.narrowType(f, nonEvolvingType, flow.Node, assumeTrue)
}
if narrowedType == nonEvolvingType {
return flowType
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
controlFlowNullishCoalescingFalsyLeft.ts(14,15): error TS2322: Type '"" | undefined' is not assignable to type 'undefined'.
Type '""' is not assignable to type 'undefined'.
controlFlowNullishCoalescingFalsyLeft.ts(42,15): error TS2322: Type 'boolean' is not assignable to type 'false'.
controlFlowNullishCoalescingFalsyLeft.ts(65,12): error TS2869: Right operand of ?? is unreachable because the left operand is never nullish.
controlFlowNullishCoalescingFalsyLeft.ts(66,5): error TS7027: Unreachable code detected.
controlFlowNullishCoalescingFalsyLeft.ts(70,12): error TS2869: Right operand of ?? is unreachable because the left operand is never nullish.
controlFlowNullishCoalescingFalsyLeft.ts(75,12): error TS2871: This expression is always nullish.
controlFlowNullishCoalescingFalsyLeft.ts(76,5): error TS7027: Unreachable code detected.
controlFlowNullishCoalescingFalsyLeft.ts(134,14): error TS2869: Right operand of ?? is unreachable because the left operand is never nullish.
controlFlowNullishCoalescingFalsyLeft.ts(135,5): error TS7027: Unreachable code detected.
controlFlowNullishCoalescingFalsyLeft.ts(139,14): error TS2871: This expression is always nullish.
controlFlowNullishCoalescingFalsyLeft.ts(140,5): error TS7027: Unreachable code detected.
controlFlowNullishCoalescingFalsyLeft.ts(144,14): error TS2869: Right operand of ?? is unreachable because the left operand is never nullish.


==== controlFlowNullishCoalescingFalsyLeft.ts (12 errors) ====
declare function c(): boolean;
declare function d(): boolean | undefined;
declare function u(x: undefined): boolean;

// https://github.com/microsoft/TypeScript/issues/65402

function f1() {
while (c() ?? (c() || true)) {}
c(); // Reachable
}

function f2(a: "" | undefined, b: boolean) {
if (a ?? b) {} else {
const x: undefined = a; // Error
~
!!! error TS2322: Type '"" | undefined' is not assignable to type 'undefined'.
!!! error TS2322: Type '""' is not assignable to type 'undefined'.
}
}

// Narrowing of a falsy, non-nullish left operand

function f3(a: boolean | undefined, b: boolean) {
if (a ?? b) {
a; // boolean | undefined
}
else {
a; // false | undefined
}
}

function f4(a: string | number | undefined) {
if (a ?? (c() || true)) {
a; // string | number | undefined
}
else {
a; // string | number
}
}

function f5(a: object | undefined, b: boolean) {
if (a ?? b) {}
else {
a; // undefined
const r: false = b; // Error, as with `if (a && b)` when `a: object`
~
!!! error TS2322: Type 'boolean' is not assignable to type 'false'.
}
}

function f6(o: { p?: number }, b: boolean) {
if (o.p ?? b) {}
else {
o.p; // number | undefined
}
}

function f7(a: "" | boolean | undefined, b: boolean) {
if (a ??= b) {
a; // "" | boolean
}
else {
a; // "" | false
}
}

// Literal left operands

function f8() {
while (true ?? (c() || true)) {}
~~~~
!!! error TS2869: Right operand of ?? is unreachable because the left operand is never nullish.
c(); // Unreachable
~~~~
!!! error TS7027: Unreachable code detected.
}

function f9() {
while (false ?? (c() || true)) {}
~~~~~
!!! error TS2869: Right operand of ?? is unreachable because the left operand is never nullish.
c(); // Reachable
}

function f10() {
while (null ?? (c() || true)) {}
~~~~
!!! error TS2871: This expression is always nullish.
c(); // Unreachable
~~~~
!!! error TS7027: Unreachable code detected.
}

// Logical left operands

function f11() {
while ((c() || 0) ?? (c() || true)) {}
c(); // Reachable
}

function f12() {
while (d() ?? d() ?? (c() || true)) {}
c(); // Reachable
}

function f13(a: "" | undefined, b: "" | undefined) {
if (a ?? b ?? u(a)) {
a; // "" | undefined
}
else {
a; // "" | undefined
b; // "" | undefined
}
const r = a ?? b ?? u(a);
}

function f14(x: boolean, z: 0 | undefined) {
let y: 0 | 1 | undefined;
(x || (y = z)) ?? (y = 1);
y; // 0 | 1 | undefined
}

// Nested in logical expressions

function f15(a: "" | undefined, b: true, d: boolean) {
if ((a ?? b) || d) {}
else {
a; // "" | undefined
}
}

// Expressions of optional chains only test for nullishness

function f16(a: "" | undefined) {
let b: 0 | 1 = 0;
if ((a ?? (b = 1, ""))?.[b = 1]) {}
else {
const one: 1 = b;
}
}

function f17(a: "" | undefined, b: "" | undefined) {
(a ?? b ?? u(a))?.valueOf();
}

// Parenthesized literal left operands

function f18() {
while (((true)) ?? (c() || true)) {}
~~~~
!!! error TS2869: Right operand of ?? is unreachable because the left operand is never nullish.
c(); // Unreachable
~~~~
!!! error TS7027: Unreachable code detected.
}

function f19() {
while (((null)) ?? (c() || true)) {}
~~~~
!!! error TS2871: This expression is always nullish.
c(); // Unreachable
~~~~
!!! error TS7027: Unreachable code detected.
}

function f20() {
while (((false)) ?? (c() || true)) {}
~~~~~
!!! error TS2869: Right operand of ?? is unreachable because the left operand is never nullish.
c(); // Reachable
}

// Optional-chain side effects still occur when nested in another ??

function f21(a: "" | undefined) {
let b: 0 | 1 = 0;
(a ?? (b = 1, ""))?.[b = 1] ?? false;
const one: 1 = b;
}

function f22(a: "" | undefined) {
let b: 0 | 1 = 0;
(a ?? (b = 1, ""))?.charAt(b = 1) ?? false;
const one: 1 = b;
}

function f23(u: { tag: ""; empty: 1 } | { tag: "ok" }) {
if ((u.tag) ?? (false || true)) {} else {
u.empty;
}
}

function f24(u: { tag: ""; empty: 1 } | { tag: "ok" }) {
if (u.tag! ?? (false || true)) {} else {
u.empty;
}
}

Loading
Loading