Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Fix isExhaustiveSwitchStatement to better handle circularities (#51095)
* Fix isExhaustiveSwitchStatement to better handle circularities * Add regression test
- Loading branch information
1 parent
503604c
commit 4f54e7e
Showing
7 changed files
with
174 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
tests/baselines/reference/exhaustiveSwitchCheckCircularity.errors.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
tests/cases/compiler/exhaustiveSwitchCheckCircularity.ts(14,26): error TS2345: Argument of type 'string' is not assignable to parameter of type 'never'. | ||
|
||
|
||
==== tests/cases/compiler/exhaustiveSwitchCheckCircularity.ts (1 errors) ==== | ||
// Repro from #47539 | ||
|
||
declare function isNever(n: never): boolean; | ||
|
||
function f() { | ||
let foo: "aaa" | "bbb" = "aaa"; | ||
while (true) { | ||
switch (foo) { | ||
case "aaa": | ||
} | ||
if (foo === "aaa") { | ||
foo = "bbb"; | ||
} | ||
else if (isNever(foo)) { // Error expected | ||
~~~ | ||
!!! error TS2345: Argument of type 'string' is not assignable to parameter of type 'never'. | ||
break; | ||
} | ||
} | ||
} | ||
|
38 changes: 38 additions & 0 deletions
38
tests/baselines/reference/exhaustiveSwitchCheckCircularity.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
//// [exhaustiveSwitchCheckCircularity.ts] | ||
// Repro from #47539 | ||
|
||
declare function isNever(n: never): boolean; | ||
|
||
function f() { | ||
let foo: "aaa" | "bbb" = "aaa"; | ||
while (true) { | ||
switch (foo) { | ||
case "aaa": | ||
} | ||
if (foo === "aaa") { | ||
foo = "bbb"; | ||
} | ||
else if (isNever(foo)) { // Error expected | ||
break; | ||
} | ||
} | ||
} | ||
|
||
|
||
//// [exhaustiveSwitchCheckCircularity.js] | ||
"use strict"; | ||
// Repro from #47539 | ||
function f() { | ||
var foo = "aaa"; | ||
while (true) { | ||
switch (foo) { | ||
case "aaa": | ||
} | ||
if (foo === "aaa") { | ||
foo = "bbb"; | ||
} | ||
else if (isNever(foo)) { // Error expected | ||
break; | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
tests/baselines/reference/exhaustiveSwitchCheckCircularity.symbols
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
=== tests/cases/compiler/exhaustiveSwitchCheckCircularity.ts === | ||
// Repro from #47539 | ||
|
||
declare function isNever(n: never): boolean; | ||
>isNever : Symbol(isNever, Decl(exhaustiveSwitchCheckCircularity.ts, 0, 0)) | ||
>n : Symbol(n, Decl(exhaustiveSwitchCheckCircularity.ts, 2, 25)) | ||
|
||
function f() { | ||
>f : Symbol(f, Decl(exhaustiveSwitchCheckCircularity.ts, 2, 44)) | ||
|
||
let foo: "aaa" | "bbb" = "aaa"; | ||
>foo : Symbol(foo, Decl(exhaustiveSwitchCheckCircularity.ts, 5, 7)) | ||
|
||
while (true) { | ||
switch (foo) { | ||
>foo : Symbol(foo, Decl(exhaustiveSwitchCheckCircularity.ts, 5, 7)) | ||
|
||
case "aaa": | ||
} | ||
if (foo === "aaa") { | ||
>foo : Symbol(foo, Decl(exhaustiveSwitchCheckCircularity.ts, 5, 7)) | ||
|
||
foo = "bbb"; | ||
>foo : Symbol(foo, Decl(exhaustiveSwitchCheckCircularity.ts, 5, 7)) | ||
} | ||
else if (isNever(foo)) { // Error expected | ||
>isNever : Symbol(isNever, Decl(exhaustiveSwitchCheckCircularity.ts, 0, 0)) | ||
>foo : Symbol(foo, Decl(exhaustiveSwitchCheckCircularity.ts, 5, 7)) | ||
|
||
break; | ||
} | ||
} | ||
} | ||
|
43 changes: 43 additions & 0 deletions
43
tests/baselines/reference/exhaustiveSwitchCheckCircularity.types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
=== tests/cases/compiler/exhaustiveSwitchCheckCircularity.ts === | ||
// Repro from #47539 | ||
|
||
declare function isNever(n: never): boolean; | ||
>isNever : (n: never) => boolean | ||
>n : never | ||
|
||
function f() { | ||
>f : () => void | ||
|
||
let foo: "aaa" | "bbb" = "aaa"; | ||
>foo : "aaa" | "bbb" | ||
>"aaa" : "aaa" | ||
|
||
while (true) { | ||
>true : true | ||
|
||
switch (foo) { | ||
>foo : "aaa" | "bbb" | ||
|
||
case "aaa": | ||
>"aaa" : "aaa" | ||
} | ||
if (foo === "aaa") { | ||
>foo === "aaa" : boolean | ||
>foo : "aaa" | "bbb" | ||
>"aaa" : "aaa" | ||
|
||
foo = "bbb"; | ||
>foo = "bbb" : "bbb" | ||
>foo : "aaa" | "bbb" | ||
>"bbb" : "bbb" | ||
} | ||
else if (isNever(foo)) { // Error expected | ||
>isNever(foo) : boolean | ||
>isNever : (n: never) => boolean | ||
>foo : "bbb" | ||
|
||
break; | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// @strict: true | ||
|
||
// Repro from #47539 | ||
|
||
declare function isNever(n: never): boolean; | ||
|
||
function f() { | ||
let foo: "aaa" | "bbb" = "aaa"; | ||
while (true) { | ||
switch (foo) { | ||
case "aaa": | ||
} | ||
if (foo === "aaa") { | ||
foo = "bbb"; | ||
} | ||
else if (isNever(foo)) { // Error expected | ||
break; | ||
} | ||
} | ||
} |