Skip to content

Fix switch statement exhaustiveness checking #34840

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 5, 2019
Merged
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
6 changes: 2 additions & 4 deletions src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -965,9 +965,6 @@ namespace ts {
}

function createFlowSwitchClause(antecedent: FlowNode, switchStatement: SwitchStatement, clauseStart: number, clauseEnd: number): FlowNode {
if (!isNarrowingExpression(switchStatement.expression)) {
return antecedent;
}
setFlowNodeReferenced(antecedent);
return flowNodeCreated({ flags: FlowFlags.SwitchClause, antecedent, switchStatement, clauseStart, clauseEnd });
}
Expand Down Expand Up @@ -1323,6 +1320,7 @@ namespace ts {
const savedSubtreeTransformFlags = subtreeTransformFlags;
subtreeTransformFlags = 0;
const clauses = node.clauses;
const isNarrowingSwitch = isNarrowingExpression(node.parent.expression);
let fallthroughFlow = unreachableFlow;
for (let i = 0; i < clauses.length; i++) {
const clauseStart = i;
Expand All @@ -1331,7 +1329,7 @@ namespace ts {
i++;
}
const preCaseLabel = createBranchLabel();
addAntecedent(preCaseLabel, createFlowSwitchClause(preSwitchCaseFlow!, node.parent, clauseStart, i + 1));
addAntecedent(preCaseLabel, isNarrowingSwitch ? createFlowSwitchClause(preSwitchCaseFlow!, node.parent, clauseStart, i + 1) : preSwitchCaseFlow!);
addAntecedent(preCaseLabel, fallthroughFlow);
currentFlow = finishFlowLabel(preCaseLabel);
const clause = clauses[i];
Expand Down
13 changes: 13 additions & 0 deletions tests/baselines/reference/exhaustiveSwitchStatements1.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,17 @@ tests/cases/conformance/controlFlow/exhaustiveSwitchStatements1.ts(7,9): error T
}
return x;
}

// Repro from #34661

enum Animal { DOG, CAT }

declare const zoo: { animal: Animal } | undefined;

function expression(): Animal {
switch (zoo?.animal ?? Animal.DOG) {
case Animal.DOG: return Animal.DOG
case Animal.CAT: return Animal.CAT
}
}

34 changes: 34 additions & 0 deletions tests/baselines/reference/exhaustiveSwitchStatements1.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,19 @@ function test4(value: 1 | 2) {
}
return x;
}

// Repro from #34661

enum Animal { DOG, CAT }

declare const zoo: { animal: Animal } | undefined;

function expression(): Animal {
switch (zoo?.animal ?? Animal.DOG) {
case Animal.DOG: return Animal.DOG
case Animal.CAT: return Animal.CAT
}
}


//// [exhaustiveSwitchStatements1.js]
Expand Down Expand Up @@ -379,6 +392,19 @@ function test4(value) {
}
return x;
}
// Repro from #34661
var Animal;
(function (Animal) {
Animal[Animal["DOG"] = 0] = "DOG";
Animal[Animal["CAT"] = 1] = "CAT";
})(Animal || (Animal = {}));
function expression() {
var _a, _b;
switch ((_b = (_a = zoo) === null || _a === void 0 ? void 0 : _a.animal, (_b !== null && _b !== void 0 ? _b : Animal.DOG))) {
case Animal.DOG: return Animal.DOG;
case Animal.CAT: return Animal.CAT;
}
}


//// [exhaustiveSwitchStatements1.d.ts]
Expand Down Expand Up @@ -435,3 +461,11 @@ declare type Shape2 = Square2 | Circle2;
declare function withDefault(s1: Shape2, s2: Shape2): string;
declare function withoutDefault(s1: Shape2, s2: Shape2): string;
declare function test4(value: 1 | 2): string;
declare enum Animal {
DOG = 0,
CAT = 1
}
declare const zoo: {
animal: Animal;
} | undefined;
declare function expression(): Animal;
42 changes: 42 additions & 0 deletions tests/baselines/reference/exhaustiveSwitchStatements1.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -501,3 +501,45 @@ function test4(value: 1 | 2) {
>x : Symbol(x, Decl(exhaustiveSwitchStatements1.ts, 188, 7))
}

// Repro from #34661

enum Animal { DOG, CAT }
>Animal : Symbol(Animal, Decl(exhaustiveSwitchStatements1.ts, 194, 1))
>DOG : Symbol(Animal.DOG, Decl(exhaustiveSwitchStatements1.ts, 198, 13))
>CAT : Symbol(Animal.CAT, Decl(exhaustiveSwitchStatements1.ts, 198, 18))

declare const zoo: { animal: Animal } | undefined;
>zoo : Symbol(zoo, Decl(exhaustiveSwitchStatements1.ts, 200, 13))
>animal : Symbol(animal, Decl(exhaustiveSwitchStatements1.ts, 200, 20))
>Animal : Symbol(Animal, Decl(exhaustiveSwitchStatements1.ts, 194, 1))

function expression(): Animal {
>expression : Symbol(expression, Decl(exhaustiveSwitchStatements1.ts, 200, 50))
>Animal : Symbol(Animal, Decl(exhaustiveSwitchStatements1.ts, 194, 1))

switch (zoo?.animal ?? Animal.DOG) {
>zoo?.animal : Symbol(animal, Decl(exhaustiveSwitchStatements1.ts, 200, 20))
>zoo : Symbol(zoo, Decl(exhaustiveSwitchStatements1.ts, 200, 13))
>animal : Symbol(animal, Decl(exhaustiveSwitchStatements1.ts, 200, 20))
>Animal.DOG : Symbol(Animal.DOG, Decl(exhaustiveSwitchStatements1.ts, 198, 13))
>Animal : Symbol(Animal, Decl(exhaustiveSwitchStatements1.ts, 194, 1))
>DOG : Symbol(Animal.DOG, Decl(exhaustiveSwitchStatements1.ts, 198, 13))

case Animal.DOG: return Animal.DOG
>Animal.DOG : Symbol(Animal.DOG, Decl(exhaustiveSwitchStatements1.ts, 198, 13))
>Animal : Symbol(Animal, Decl(exhaustiveSwitchStatements1.ts, 194, 1))
>DOG : Symbol(Animal.DOG, Decl(exhaustiveSwitchStatements1.ts, 198, 13))
>Animal.DOG : Symbol(Animal.DOG, Decl(exhaustiveSwitchStatements1.ts, 198, 13))
>Animal : Symbol(Animal, Decl(exhaustiveSwitchStatements1.ts, 194, 1))
>DOG : Symbol(Animal.DOG, Decl(exhaustiveSwitchStatements1.ts, 198, 13))

case Animal.CAT: return Animal.CAT
>Animal.CAT : Symbol(Animal.CAT, Decl(exhaustiveSwitchStatements1.ts, 198, 18))
>Animal : Symbol(Animal, Decl(exhaustiveSwitchStatements1.ts, 194, 1))
>CAT : Symbol(Animal.CAT, Decl(exhaustiveSwitchStatements1.ts, 198, 18))
>Animal.CAT : Symbol(Animal.CAT, Decl(exhaustiveSwitchStatements1.ts, 198, 18))
>Animal : Symbol(Animal, Decl(exhaustiveSwitchStatements1.ts, 194, 1))
>CAT : Symbol(Animal.CAT, Decl(exhaustiveSwitchStatements1.ts, 198, 18))
}
}

41 changes: 41 additions & 0 deletions tests/baselines/reference/exhaustiveSwitchStatements1.types
Original file line number Diff line number Diff line change
Expand Up @@ -593,3 +593,44 @@ function test4(value: 1 | 2) {
>x : string
}

// Repro from #34661

enum Animal { DOG, CAT }
>Animal : Animal
>DOG : Animal.DOG
>CAT : Animal.CAT

declare const zoo: { animal: Animal } | undefined;
>zoo : { animal: Animal; } | undefined
>animal : Animal

function expression(): Animal {
>expression : () => Animal

switch (zoo?.animal ?? Animal.DOG) {
>zoo?.animal ?? Animal.DOG : Animal
>zoo?.animal : Animal | undefined
>zoo : { animal: Animal; } | undefined
>animal : Animal | undefined
>Animal.DOG : Animal.DOG
>Animal : typeof Animal
>DOG : Animal.DOG

case Animal.DOG: return Animal.DOG
>Animal.DOG : Animal.DOG
>Animal : typeof Animal
>DOG : Animal.DOG
>Animal.DOG : Animal.DOG
>Animal : typeof Animal
>DOG : Animal.DOG

case Animal.CAT: return Animal.CAT
>Animal.CAT : Animal.CAT
>Animal : typeof Animal
>CAT : Animal.CAT
>Animal.CAT : Animal.CAT
>Animal : typeof Animal
>CAT : Animal.CAT
}
}

13 changes: 13 additions & 0 deletions tests/cases/conformance/controlFlow/exhaustiveSwitchStatements1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,16 @@ function test4(value: 1 | 2) {
}
return x;
}

// Repro from #34661

enum Animal { DOG, CAT }

declare const zoo: { animal: Animal } | undefined;

function expression(): Animal {
switch (zoo?.animal ?? Animal.DOG) {
case Animal.DOG: return Animal.DOG
case Animal.CAT: return Animal.CAT
}
}