Skip to content

Commit ba41139

Browse files
committed
Fix #3561: Correctly handle switches followed by other tests in matches
1 parent f802abf commit ba41139

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -845,10 +845,10 @@ object PatternMatcher {
845845

846846
/** Emit cases of a switch */
847847
private def emitSwitchCases(cases: List[Plan]): List[CaseDef] = (cases: @unchecked) match {
848-
case TestPlan(EqualTest(tree), _, _, ons, _) :: cases1 =>
849-
CaseDef(tree, EmptyTree, emit(ons)) :: emitSwitchCases(cases1)
850848
case (default: Plan) :: Nil =>
851849
CaseDef(Underscore(defn.IntType), EmptyTree, emit(default)) :: Nil
850+
case TestPlan(EqualTest(tree), _, _, ons, _) :: cases1 =>
851+
CaseDef(tree, EmptyTree, emit(ons)) :: emitSwitchCases(cases1)
852852
}
853853

854854
/** If selfCheck is `true`, used to check whether a tree gets generated twice */

tests/pos/i3561.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Test {
2+
val Constant = 'Q' // OK if final
3+
def tokenMe(ch: Char) = (ch: @annotation.switch) match {
4+
case ' ' => 1
5+
case 'A' => 2
6+
case '5' | Constant => 3
7+
}
8+
9+
def test2(x: Any) = x match {
10+
case 1 => 1
11+
case 2 => 2
12+
case 3 => 3
13+
case x: String => 4
14+
}
15+
}

0 commit comments

Comments
 (0)