From a8a8826f69f8caad309fd702a2d237047bee3224 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Mon, 4 Jul 2016 20:27:59 -0700 Subject: [PATCH 1/2] Exclude FlowSwitchClause from flow graph for case expressions --- src/compiler/binder.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index b45af24d4d198..b7852a64d04de 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -551,6 +551,9 @@ namespace ts { case SyntaxKind.CaseBlock: bindCaseBlock(node); break; + case SyntaxKind.CaseClause: + bindCaseClause(node); + break; case SyntaxKind.LabeledStatement: bindLabeledStatement(node); break; @@ -989,6 +992,14 @@ namespace ts { } } + function bindCaseClause(node: CaseClause): void { + const saveCurrentFlow = currentFlow; + currentFlow = preSwitchCaseFlow; + bind(node.expression); + currentFlow = saveCurrentFlow; + forEach(node.statements, bind); + } + function pushActiveLabel(name: string, breakTarget: FlowLabel, continueTarget: FlowLabel): ActiveLabel { const activeLabel = { name, From 829c3bc2649a920ce68f87f240708b74abbe97c5 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Mon, 4 Jul 2016 20:38:00 -0700 Subject: [PATCH 2/2] Add regression test --- .../switchCaseCircularRefeference.errors.txt | 18 ++++++++++++++++++ .../reference/switchCaseCircularRefeference.js | 18 ++++++++++++++++++ .../compiler/switchCaseCircularRefeference.ts | 8 ++++++++ 3 files changed, 44 insertions(+) create mode 100644 tests/baselines/reference/switchCaseCircularRefeference.errors.txt create mode 100644 tests/baselines/reference/switchCaseCircularRefeference.js create mode 100644 tests/cases/compiler/switchCaseCircularRefeference.ts diff --git a/tests/baselines/reference/switchCaseCircularRefeference.errors.txt b/tests/baselines/reference/switchCaseCircularRefeference.errors.txt new file mode 100644 index 0000000000000..9ee571de468ce --- /dev/null +++ b/tests/baselines/reference/switchCaseCircularRefeference.errors.txt @@ -0,0 +1,18 @@ +tests/cases/compiler/switchCaseCircularRefeference.ts(5,10): error TS2678: Type '{ a: "A"; b: any; } | { a: "C"; e: any; }' is not comparable to type '"A" | "C"'. + Type '{ a: "C"; e: any; }' is not comparable to type '"A" | "C"'. + Type '{ a: "C"; e: any; }' is not comparable to type '"C"'. + + +==== tests/cases/compiler/switchCaseCircularRefeference.ts (1 errors) ==== + // Repro from #9507 + + function f(x: {a: "A", b} | {a: "C", e}) { + switch (x.a) { + case x: + ~ +!!! error TS2678: Type '{ a: "A"; b: any; } | { a: "C"; e: any; }' is not comparable to type '"A" | "C"'. +!!! error TS2678: Type '{ a: "C"; e: any; }' is not comparable to type '"A" | "C"'. +!!! error TS2678: Type '{ a: "C"; e: any; }' is not comparable to type '"C"'. + break; + } + } \ No newline at end of file diff --git a/tests/baselines/reference/switchCaseCircularRefeference.js b/tests/baselines/reference/switchCaseCircularRefeference.js new file mode 100644 index 0000000000000..7ef901511cf3e --- /dev/null +++ b/tests/baselines/reference/switchCaseCircularRefeference.js @@ -0,0 +1,18 @@ +//// [switchCaseCircularRefeference.ts] +// Repro from #9507 + +function f(x: {a: "A", b} | {a: "C", e}) { + switch (x.a) { + case x: + break; + } +} + +//// [switchCaseCircularRefeference.js] +// Repro from #9507 +function f(x) { + switch (x.a) { + case x: + break; + } +} diff --git a/tests/cases/compiler/switchCaseCircularRefeference.ts b/tests/cases/compiler/switchCaseCircularRefeference.ts new file mode 100644 index 0000000000000..060b90fa8a1cd --- /dev/null +++ b/tests/cases/compiler/switchCaseCircularRefeference.ts @@ -0,0 +1,8 @@ +// Repro from #9507 + +function f(x: {a: "A", b} | {a: "C", e}) { + switch (x.a) { + case x: + break; + } +} \ No newline at end of file