Skip to content

Fix missed errors in switch when using union of literal and non-literal types (#38686) #51373

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 2 commits into from
Dec 6, 2022
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
15 changes: 5 additions & 10 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40048,7 +40048,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
let hasDuplicateDefaultClause = false;

const expressionType = checkExpression(node.expression);
const expressionIsLiteral = isLiteralType(expressionType);

forEach(node.caseBlock.clauses, clause => {
// Grammar check for duplicate default clauses, skip if we already report duplicate default clause
if (clause.kind === SyntaxKind.DefaultClause && !hasDuplicateDefaultClause) {
Expand All @@ -40074,16 +40074,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
// TypeScript 1.0 spec (April 2014): 5.9
// In a 'switch' statement, each 'case' expression must be of a type that is comparable
// to or from the type of the 'switch' expression.
let caseType = checkExpression(clause.expression);
const caseIsLiteral = isLiteralType(caseType);
let comparedExpressionType = expressionType;
if (!caseIsLiteral || !expressionIsLiteral) {
caseType = caseIsLiteral ? getBaseTypeOfLiteralType(caseType) : caseType;
comparedExpressionType = getBaseTypeOfLiteralType(expressionType);
}
if (!isTypeEqualityComparableTo(comparedExpressionType, caseType)) {
const caseType = checkExpression(clause.expression);

if (!isTypeEqualityComparableTo(expressionType, caseType)) {
// expressionType is not comparable to caseType, try the reversed check and report errors if it fails
checkTypeComparableTo(caseType, comparedExpressionType, clause.expression, /*headMessage*/ undefined);
checkTypeComparableTo(caseType, expressionType, clause.expression, /*headMessage*/ undefined);
}
};
}
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/switchAssignmentCompat.errors.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
tests/cases/compiler/switchAssignmentCompat.ts(4,10): error TS2678: Type 'typeof Foo' is not comparable to type 'number'.
tests/cases/compiler/switchAssignmentCompat.ts(4,10): error TS2678: Type 'typeof Foo' is not comparable to type '0'.


==== tests/cases/compiler/switchAssignmentCompat.ts (1 errors) ====
Expand All @@ -7,6 +7,6 @@ tests/cases/compiler/switchAssignmentCompat.ts(4,10): error TS2678: Type 'typeof
switch (0) {
case Foo: break; // Error expected
~~~
!!! error TS2678: Type 'typeof Foo' is not comparable to type 'number'.
!!! error TS2678: Type 'typeof Foo' is not comparable to type '0'.
}

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
tests/cases/compiler/switchCaseCircularRefeference.ts(5,10): error TS2678: Type '{ a: "A"; b: any; } | { a: "C"; e: any; }' is not comparable to type 'string'.
Type '{ a: "C"; e: any; }' is not comparable to type 'string'.
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"'.


==== tests/cases/compiler/switchCaseCircularRefeference.ts (1 errors) ====
Expand All @@ -9,8 +9,8 @@ tests/cases/compiler/switchCaseCircularRefeference.ts(5,10): error TS2678: Type
switch (x.a) {
case x:
~
!!! error TS2678: Type '{ a: "A"; b: any; } | { a: "C"; e: any; }' is not comparable to type 'string'.
!!! error TS2678: Type '{ a: "C"; e: any; }' is not comparable to type 'string'.
!!! 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"'.
break;
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
tests/cases/compiler/switchCasesExpressionTypeMismatch.ts(4,10): error TS2678: Type 'typeof Foo' is not comparable to type 'number'.
tests/cases/compiler/switchCasesExpressionTypeMismatch.ts(4,10): error TS2678: Type 'typeof Foo' is not comparable to type '0'.
tests/cases/compiler/switchCasesExpressionTypeMismatch.ts(5,10): error TS2678: Type '"sss"' is not comparable to type '0'.
tests/cases/compiler/switchCasesExpressionTypeMismatch.ts(6,10): error TS2678: Type '123' is not comparable to type '0'.
tests/cases/compiler/switchCasesExpressionTypeMismatch.ts(7,10): error TS2678: Type 'true' is not comparable to type '0'.
tests/cases/compiler/switchCasesExpressionTypeMismatch.ts(16,10): error TS2678: Type 'true' is not comparable to type 'number | "hello"'.
tests/cases/compiler/switchCasesExpressionTypeMismatch.ts(18,10): error TS2678: Type '"world"' is not comparable to type 'number | "hello"'.


==== tests/cases/compiler/switchCasesExpressionTypeMismatch.ts (4 errors) ====
==== tests/cases/compiler/switchCasesExpressionTypeMismatch.ts (6 errors) ====
class Foo { }

switch (0) {
case Foo: break; // Error
~~~
!!! error TS2678: Type 'typeof Foo' is not comparable to type 'number'.
!!! error TS2678: Type 'typeof Foo' is not comparable to type '0'.
case "sss": break; // Error
~~~~~
!!! error TS2678: Type '"sss"' is not comparable to type '0'.
Expand All @@ -22,6 +24,21 @@ tests/cases/compiler/switchCasesExpressionTypeMismatch.ts(7,10): error TS2678: T
!!! error TS2678: Type 'true' is not comparable to type '0'.
}

declare var q: string
declare var r: number | "hello"

switch (r) {
case q: break
case 42: break
case true: break // Error
~~~~
!!! error TS2678: Type 'true' is not comparable to type 'number | "hello"'.
case "hello": break
case "world": break // Error
~~~~~~~
!!! error TS2678: Type '"world"' is not comparable to type 'number | "hello"'.
}

var s: any = 0;

// No error for all
Expand Down
18 changes: 18 additions & 0 deletions tests/baselines/reference/switchCasesExpressionTypeMismatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ switch (0) {
case true: break; // Error
}

declare var q: string
declare var r: number | "hello"

switch (r) {
case q: break
case 42: break
case true: break // Error
case "hello": break
case "world": break // Error
}

var s: any = 0;

// No error for all
Expand All @@ -31,6 +42,13 @@ switch (0) {
case 123: break; // Error
case true: break; // Error
}
switch (r) {
case q: break;
case 42: break;
case true: break; // Error
case "hello": break;
case "world": break; // Error
}
var s = 0;
// No error for all
switch (s) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,30 @@ switch (0) {
case true: break; // Error
}

declare var q: string
>q : Symbol(q, Decl(switchCasesExpressionTypeMismatch.ts, 9, 11))

declare var r: number | "hello"
>r : Symbol(r, Decl(switchCasesExpressionTypeMismatch.ts, 10, 11))

switch (r) {
>r : Symbol(r, Decl(switchCasesExpressionTypeMismatch.ts, 10, 11))

case q: break
>q : Symbol(q, Decl(switchCasesExpressionTypeMismatch.ts, 9, 11))

case 42: break
case true: break // Error
case "hello": break
case "world": break // Error
}

var s: any = 0;
>s : Symbol(s, Decl(switchCasesExpressionTypeMismatch.ts, 9, 3))
>s : Symbol(s, Decl(switchCasesExpressionTypeMismatch.ts, 20, 3))

// No error for all
switch (s) {
>s : Symbol(s, Decl(switchCasesExpressionTypeMismatch.ts, 9, 3))
>s : Symbol(s, Decl(switchCasesExpressionTypeMismatch.ts, 20, 3))

case Foo: break;
>Foo : Symbol(Foo, Decl(switchCasesExpressionTypeMismatch.ts, 0, 0))
Expand Down
25 changes: 25 additions & 0 deletions tests/baselines/reference/switchCasesExpressionTypeMismatch.types
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,31 @@ switch (0) {
>true : true
}

declare var q: string
>q : string

declare var r: number | "hello"
>r : number | "hello"

switch (r) {
>r : number | "hello"

case q: break
>q : string

case 42: break
>42 : 42

case true: break // Error
>true : true

case "hello": break
>"hello" : "hello"

case "world": break // Error
>"world" : "world"
}

var s: any = 0;
>s : any
>0 : 0
Expand Down
11 changes: 11 additions & 0 deletions tests/cases/compiler/switchCasesExpressionTypeMismatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ switch (0) {
case true: break; // Error
}

declare var q: string
declare var r: number | "hello"

switch (r) {
case q: break
case 42: break
case true: break // Error
case "hello": break
case "world": break // Error
}

var s: any = 0;

// No error for all
Expand Down