Skip to content

Commit ba339ff

Browse files
committed
Fix #3588: Do not emit switch when matching on Any
1 parent 068f42d commit ba339ff

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,9 @@ object PatternMatcher {
834834
}
835835
plan match {
836836
case plan: TestPlan =>
837-
val switchCases = collectSwitchCases(plan)
837+
val switchCases =
838+
if (plan.scrutinee.tpe.widen.classSymbol == defn.AnyClass) Nil
839+
else collectSwitchCases(plan)
838840
if (switchCases.lengthCompare(4) >= 0) // at least 3 cases + default
839841
Match(plan.scrutinee, emitSwitchCases(switchCases))
840842
else

tests/pos/i3588.scala

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Foo {
2+
val a: Any = 3
3+
a match {
4+
case 1 =>
5+
case 2 =>
6+
case 3 =>
7+
case _ =>
8+
}
9+
}
10+
11+
class Bar[T] {
12+
val a: T = ???
13+
a match {
14+
case 1 =>
15+
case 2 =>
16+
case 3 =>
17+
case _ =>
18+
}
19+
}

0 commit comments

Comments
 (0)