Skip to content

Commit a0a0a67

Browse files
committed
Add tests and only try switches for Char, Int, Short and Byte
1 parent 470c650 commit a0a0a67

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,12 @@ object PatternMatcher {
791791
* plan and following onSuccess plans.
792792
*/
793793
private def collectSwitchCases(plan: TestPlan): List[Plan] = {
794+
def isSwitchableType(tpe: Type): Boolean =
795+
(tpe isRef defn.IntClass) ||
796+
(tpe isRef defn.ByteClass) ||
797+
(tpe isRef defn.ShortClass) ||
798+
(tpe isRef defn.CharClass)
799+
794800
val scrutinee = plan.scrutinee
795801

796802
def isIntConst(tree: Tree) = tree match {
@@ -805,9 +811,9 @@ object PatternMatcher {
805811
case _ =>
806812
plan :: Nil
807813
}
808-
val cls = scrutinee.tpe.widen.classSymbol
809-
if (cls == defn.AnyClass || cls == defn.AnyValClass) Nil
810-
else recur(plan)
814+
815+
if (isSwitchableType(scrutinee.tpe.widen)) recur(plan)
816+
else Nil
811817
}
812818

813819
/** Emit cases of a switch */

compiler/test/dotty/tools/dotc/CompilationTests.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class CompilationTests extends ParallelTesting {
5555
compileFile("../tests/pos-scala2/rewrites.scala", scala2Mode.and("-rewrite")).copyToTarget() +
5656
compileFile("../tests/pos-special/utf8encoded.scala", explicitUTF8) +
5757
compileFile("../tests/pos-special/utf16encoded.scala", explicitUTF16) +
58+
compileFile("../tests/pos-special/i3589-b.scala", defaultOptions.and("-Xfatal-warnings")) +
5859
compileList(
5960
"compileMixed",
6061
List(
@@ -179,6 +180,7 @@ class CompilationTests extends ParallelTesting {
179180
compileFile("../tests/neg/customArgs/overloadsOnAbstractTypes.scala", allowDoubleBindings) +
180181
compileFile("../tests/neg/customArgs/xfatalWarnings.scala", defaultOptions.and("-Xfatal-warnings")) +
181182
compileFile("../tests/neg/customArgs/pureStatement.scala", defaultOptions.and("-Xfatal-warnings")) +
183+
compileFile("../tests/neg/customArgs/i3589-a.scala", defaultOptions.and("-Xfatal-warnings")) +
182184
compileFile("../tests/neg/customArgs/phantom-overload.scala", allowDoubleBindings) +
183185
compileFile("../tests/neg/customArgs/phantom-overload-2.scala", allowDoubleBindings) +
184186
compileFile("../tests/neg/tailcall/t1672b.scala", defaultOptions) +

tests/neg/customArgs/i3589-a.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
object Test {
2+
case class IntAnyVal(x: Int) extends AnyVal
3+
4+
def test(x: IntAnyVal) = (x: @annotation.switch) match { //error: warning: could not emit switch
5+
case IntAnyVal(1) => 1
6+
case IntAnyVal(2) => 2
7+
case IntAnyVal(3) => 3
8+
case _ => 4
9+
}
10+
}

tests/pos-special/i3589-b.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Test {
2+
def test(x: 1) = (x: @annotation.switch) match {
3+
case 1 => 1
4+
case 2 => 2
5+
case 3 => 3
6+
}
7+
}

0 commit comments

Comments
 (0)