Skip to content

Commit 29ac7e4

Browse files
authored
Merge pull request microsoft#9510 from Microsoft/fixSwitchCase
Fix switch case circular reference stack overflow
2 parents d56a50b + 829c3bc commit 29ac7e4

4 files changed

Lines changed: 55 additions & 0 deletions

File tree

src/compiler/binder.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,9 @@ namespace ts {
551551
case SyntaxKind.CaseBlock:
552552
bindCaseBlock(<CaseBlock>node);
553553
break;
554+
case SyntaxKind.CaseClause:
555+
bindCaseClause(<CaseClause>node);
556+
break;
554557
case SyntaxKind.LabeledStatement:
555558
bindLabeledStatement(<LabeledStatement>node);
556559
break;
@@ -989,6 +992,14 @@ namespace ts {
989992
}
990993
}
991994

995+
function bindCaseClause(node: CaseClause): void {
996+
const saveCurrentFlow = currentFlow;
997+
currentFlow = preSwitchCaseFlow;
998+
bind(node.expression);
999+
currentFlow = saveCurrentFlow;
1000+
forEach(node.statements, bind);
1001+
}
1002+
9921003
function pushActiveLabel(name: string, breakTarget: FlowLabel, continueTarget: FlowLabel): ActiveLabel {
9931004
const activeLabel = {
9941005
name,
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
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"'.
2+
Type '{ a: "C"; e: any; }' is not comparable to type '"A" | "C"'.
3+
Type '{ a: "C"; e: any; }' is not comparable to type '"C"'.
4+
5+
6+
==== tests/cases/compiler/switchCaseCircularRefeference.ts (1 errors) ====
7+
// Repro from #9507
8+
9+
function f(x: {a: "A", b} | {a: "C", e}) {
10+
switch (x.a) {
11+
case x:
12+
~
13+
!!! error TS2678: Type '{ a: "A"; b: any; } | { a: "C"; e: any; }' is not comparable to type '"A" | "C"'.
14+
!!! error TS2678: Type '{ a: "C"; e: any; }' is not comparable to type '"A" | "C"'.
15+
!!! error TS2678: Type '{ a: "C"; e: any; }' is not comparable to type '"C"'.
16+
break;
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//// [switchCaseCircularRefeference.ts]
2+
// Repro from #9507
3+
4+
function f(x: {a: "A", b} | {a: "C", e}) {
5+
switch (x.a) {
6+
case x:
7+
break;
8+
}
9+
}
10+
11+
//// [switchCaseCircularRefeference.js]
12+
// Repro from #9507
13+
function f(x) {
14+
switch (x.a) {
15+
case x:
16+
break;
17+
}
18+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Repro from #9507
2+
3+
function f(x: {a: "A", b} | {a: "C", e}) {
4+
switch (x.a) {
5+
case x:
6+
break;
7+
}
8+
}

0 commit comments

Comments
 (0)