Skip to content

Commit 682dfb5

Browse files
committed
Also handle switches with only alternatives.
1 parent e8df01d commit 682dfb5

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -901,14 +901,18 @@ object PatternMatcher {
901901
Return(Literal(Constant(())), ref(label))
902902
case plan: SeqPlan =>
903903
def default = seq(emit(plan.head) :: Nil, emit(plan.tail))
904+
def maybeEmitSwitch(scrutinee: Tree): Tree = {
905+
val switchCases = collectSwitchCases(scrutinee, plan)
906+
if (hasEnoughSwitchCases(switchCases, MinSwitchCases)) // at least 3 cases + default
907+
Match(scrutinee, emitSwitchCases(switchCases))
908+
else
909+
default
910+
}
904911
plan.head match {
905912
case testPlan: TestPlan =>
906-
val scrutinee = testPlan.scrutinee
907-
val switchCases = collectSwitchCases(scrutinee, plan)
908-
if (hasEnoughSwitchCases(switchCases, MinSwitchCases)) // at least 3 cases + default
909-
Match(scrutinee, emitSwitchCases(switchCases))
910-
else
911-
default
913+
maybeEmitSwitch(testPlan.scrutinee)
914+
case LabeledPlan(_, SeqPlan(LabeledPlan(_, SeqPlan(testPlan: TestPlan, _)), _)) =>
915+
maybeEmitSwitch(testPlan.scrutinee)
912916
case _ =>
913917
default
914918
}
Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
1+
import scala.annotation.switch
2+
13
class Test {
2-
def test1(x: Int): Int = (x: @annotation.switch) match {
4+
import Test._
5+
6+
def test1(x: Int): Int = (x: @switch) match {
37
case 1 => 1
48
case 2 | 3 | 4 => 2
59
case 65 => 3
610
case 72 => 4
711
}
12+
13+
def test2(c: Char): Boolean = (c: @switch) match {
14+
case LF | CR | FF | SU => true
15+
case _ => false
16+
}
17+
}
18+
19+
object Test {
20+
final val LF = '\u000A'
21+
final val CR = '\u000D'
22+
final val FF = '\u000C'
23+
final val SU = '\u001A'
824
}

0 commit comments

Comments
 (0)