Skip to content

Commit 614c6a7

Browse files
committed
Refine switch warning
The test code counted the number of different types in all patterns, but it needs to account for alternative patterns as well.
1 parent 387f39b commit 614c6a7

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -967,11 +967,15 @@ object PatternMatcher {
967967
case Block(_, Match(_, cases)) => cases
968968
case _ => Nil
969969
}
970-
def numConsts(cdefs: List[CaseDef]): Int = {
971-
val tpes = cdefs.map(_.pat.tpe)
970+
def numTypes(cdefs: List[CaseDef]): Int = {
971+
def patTypes(pat: Tree): List[Type] = pat match {
972+
case Alternative(pats) => pats.flatMap(patTypes)
973+
case _ => pat.tpe :: Nil
974+
}
975+
val tpes = cdefs.flatMap(patTypes)
972976
tpes.toSet.size: Int // without the type ascription, testPickling fails because of #2840.
973977
}
974-
if (numConsts(resultCases) < numConsts(original.cases))
978+
if (numTypes(resultCases) < numTypes(original.cases))
975979
ctx.warning(UnableToEmitSwitch(), original.pos)
976980
case _ =>
977981
}

tests/neg-custom-args/i3561.scala

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

0 commit comments

Comments
 (0)