Skip to content

Fix switch-true case-true narrowing to behave equivalently to default case #59459

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
3 changes: 2 additions & 1 deletion src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1718,7 +1718,8 @@ function createBinder(): (file: SourceFile, options: CompilerOptions) => void {
preSwitchCaseFlow = currentFlow;
bind(node.caseBlock);
addAntecedent(postSwitchLabel, currentFlow);
const hasDefault = forEach(node.caseBlock.clauses, c => c.kind === SyntaxKind.DefaultClause);
const hasDefault = forEach(node.caseBlock.clauses, c => c.kind === SyntaxKind.DefaultClause) ||
(skipParentheses(node.expression).kind === SyntaxKind.TrueKeyword && node.caseBlock.clauses.some(clause => clause.kind === SyntaxKind.CaseClause && skipParentheses(clause.expression).kind === SyntaxKind.TrueKeyword));
// We mark a switch statement as possibly exhaustive if it has no default clause and if all
// case clauses have unreachable end points (e.g. they all return). Note, we no longer need
// this property in control flow analysis, it's there only for backwards compatibility.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//// [tests/cases/compiler/narrowByClauseExpressionInSwitchTrue11.ts] ////

=== narrowByClauseExpressionInSwitchTrue11.ts ===
function uhoh(input: string) : "A"|"B"|"C" {
>uhoh : Symbol(uhoh, Decl(narrowByClauseExpressionInSwitchTrue11.ts, 0, 0))
>input : Symbol(input, Decl(narrowByClauseExpressionInSwitchTrue11.ts, 0, 14))

switch (true) {
case /a/.test(input):
>/a/.test : Symbol(RegExp.test, Decl(lib.es5.d.ts, --, --))
>test : Symbol(RegExp.test, Decl(lib.es5.d.ts, --, --))
>input : Symbol(input, Decl(narrowByClauseExpressionInSwitchTrue11.ts, 0, 14))

return "A";
case /b/.test(input):
>/b/.test : Symbol(RegExp.test, Decl(lib.es5.d.ts, --, --))
>test : Symbol(RegExp.test, Decl(lib.es5.d.ts, --, --))
>input : Symbol(input, Decl(narrowByClauseExpressionInSwitchTrue11.ts, 0, 14))

return "B";
case true:
return "C";
}
}

function uhoh1(input: string) : "A"|"B"|"C" {
>uhoh1 : Symbol(uhoh1, Decl(narrowByClauseExpressionInSwitchTrue11.ts, 9, 1))
>input : Symbol(input, Decl(narrowByClauseExpressionInSwitchTrue11.ts, 11, 15))

switch ((true)) {
case /a/.test(input):
>/a/.test : Symbol(RegExp.test, Decl(lib.es5.d.ts, --, --))
>test : Symbol(RegExp.test, Decl(lib.es5.d.ts, --, --))
>input : Symbol(input, Decl(narrowByClauseExpressionInSwitchTrue11.ts, 11, 15))

return "A";
case /b/.test(input):
>/b/.test : Symbol(RegExp.test, Decl(lib.es5.d.ts, --, --))
>test : Symbol(RegExp.test, Decl(lib.es5.d.ts, --, --))
>input : Symbol(input, Decl(narrowByClauseExpressionInSwitchTrue11.ts, 11, 15))

return "B";
case (true):
return "C";
}
}
110 changes: 110 additions & 0 deletions tests/baselines/reference/narrowByClauseExpressionInSwitchTrue11.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
//// [tests/cases/compiler/narrowByClauseExpressionInSwitchTrue11.ts] ////

=== narrowByClauseExpressionInSwitchTrue11.ts ===
function uhoh(input: string) : "A"|"B"|"C" {
>uhoh : (input: string) => "A" | "B" | "C"
> : ^ ^^ ^^^^^
>input : string
> : ^^^^^^

switch (true) {
>true : true
> : ^^^^

case /a/.test(input):
>/a/.test(input) : boolean
> : ^^^^^^^
>/a/.test : (string: string) => boolean
> : ^ ^^ ^^^^^
>/a/ : RegExp
> : ^^^^^^
>test : (string: string) => boolean
> : ^ ^^ ^^^^^
>input : string
> : ^^^^^^

return "A";
>"A" : "A"
> : ^^^

case /b/.test(input):
>/b/.test(input) : boolean
> : ^^^^^^^
>/b/.test : (string: string) => boolean
> : ^ ^^ ^^^^^
>/b/ : RegExp
> : ^^^^^^
>test : (string: string) => boolean
> : ^ ^^ ^^^^^
>input : string
> : ^^^^^^

return "B";
>"B" : "B"
> : ^^^

case true:
>true : true
> : ^^^^

return "C";
>"C" : "C"
> : ^^^
}
}

function uhoh1(input: string) : "A"|"B"|"C" {
>uhoh1 : (input: string) => "A" | "B" | "C"
> : ^ ^^ ^^^^^
>input : string
> : ^^^^^^

switch ((true)) {
>(true) : true
> : ^^^^
>true : true
> : ^^^^

case /a/.test(input):
>/a/.test(input) : boolean
> : ^^^^^^^
>/a/.test : (string: string) => boolean
> : ^ ^^ ^^^^^
>/a/ : RegExp
> : ^^^^^^
>test : (string: string) => boolean
> : ^ ^^ ^^^^^
>input : string
> : ^^^^^^

return "A";
>"A" : "A"
> : ^^^

case /b/.test(input):
>/b/.test(input) : boolean
> : ^^^^^^^
>/b/.test : (string: string) => boolean
> : ^ ^^ ^^^^^
>/b/ : RegExp
> : ^^^^^^
>test : (string: string) => boolean
> : ^ ^^ ^^^^^
>input : string
> : ^^^^^^

return "B";
>"B" : "B"
> : ^^^

case (true):
>(true) : true
> : ^^^^
>true : true
> : ^^^^

return "C";
>"C" : "C"
> : ^^^
}
}
24 changes: 24 additions & 0 deletions tests/cases/compiler/narrowByClauseExpressionInSwitchTrue11.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// @strict: true
// @noEmit: true

function uhoh(input: string) : "A"|"B"|"C" {
switch (true) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awful, but, what about switch (false) + case false:?

case /a/.test(input):
return "A";
case /b/.test(input):
return "B";
case true:
return "C";
}
}

function uhoh1(input: string) : "A"|"B"|"C" {
switch ((true)) {
case /a/.test(input):
return "A";
case /b/.test(input):
return "B";
case (true):
return "C";
}
}