From 90df4598c0fbbc42f7eefdd4a07588f8610231d6 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Wed, 29 Nov 2017 13:33:47 +0100 Subject: [PATCH] Fix #3588: Do not emit switch when matching on non switchable types --- .../tools/dotc/transform/PatternMatcher.scala | 3 +- .../dotty/tools/dotc/CompilationTests.scala | 2 + tests/neg-custom-args/i3589-a.scala | 10 ++++ tests/pos-special/i3589-b.scala | 7 +++ tests/pos/i3588.scala | 51 +++++++++++++++++++ 5 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 tests/neg-custom-args/i3589-a.scala create mode 100644 tests/pos-special/i3589-b.scala create mode 100644 tests/pos/i3588.scala diff --git a/compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala b/compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala index 8d217cf96e0d..a562410b262c 100644 --- a/compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala +++ b/compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala @@ -812,7 +812,8 @@ object PatternMatcher { plan :: Nil } - recur(plan) + if (isSwitchableType(scrutinee.tpe.widen)) recur(plan) + else Nil } /** Emit cases of a switch */ diff --git a/compiler/test/dotty/tools/dotc/CompilationTests.scala b/compiler/test/dotty/tools/dotc/CompilationTests.scala index e1057838ddbe..a73def1f66ed 100644 --- a/compiler/test/dotty/tools/dotc/CompilationTests.scala +++ b/compiler/test/dotty/tools/dotc/CompilationTests.scala @@ -55,6 +55,7 @@ class CompilationTests extends ParallelTesting { compileFile("../tests/pos-scala2/rewrites.scala", scala2Mode.and("-rewrite")).copyToTarget() + compileFile("../tests/pos-special/utf8encoded.scala", explicitUTF8) + compileFile("../tests/pos-special/utf16encoded.scala", explicitUTF16) + + compileFile("../tests/pos-special/i3589-b.scala", defaultOptions.and("-Xfatal-warnings")) + compileList( "compileMixed", List( @@ -181,6 +182,7 @@ class CompilationTests extends ParallelTesting { compileFile("../tests/neg-custom-args/overloadsOnAbstractTypes.scala", allowDoubleBindings) + compileFile("../tests/neg-custom-args/xfatalWarnings.scala", defaultOptions.and("-Xfatal-warnings")) + compileFile("../tests/neg-custom-args/pureStatement.scala", defaultOptions.and("-Xfatal-warnings")) + + compileFile("../tests/neg-custom-args/i3589-a.scala", defaultOptions.and("-Xfatal-warnings")) + compileFile("../tests/neg-custom-args/phantom-overload.scala", allowDoubleBindings) + compileFile("../tests/neg-custom-args/phantom-overload-2.scala", allowDoubleBindings) + compileFile("../tests/neg-custom-args/structural.scala", defaultOptions.and("-Xfatal-warnings")) diff --git a/tests/neg-custom-args/i3589-a.scala b/tests/neg-custom-args/i3589-a.scala new file mode 100644 index 000000000000..f6621b430ddf --- /dev/null +++ b/tests/neg-custom-args/i3589-a.scala @@ -0,0 +1,10 @@ +object Test { + case class IntAnyVal(x: Int) extends AnyVal + + def test(x: IntAnyVal) = (x: @annotation.switch) match { //error: warning: could not emit switch + case IntAnyVal(1) => 1 + case IntAnyVal(2) => 2 + case IntAnyVal(3) => 3 + case _ => 4 + } +} diff --git a/tests/pos-special/i3589-b.scala b/tests/pos-special/i3589-b.scala new file mode 100644 index 000000000000..e6fb462736e1 --- /dev/null +++ b/tests/pos-special/i3589-b.scala @@ -0,0 +1,7 @@ +class Test { + def test(x: 1) = (x: @annotation.switch) match { + case 1 => 1 + case 2 => 2 + case 3 => 3 + } +} diff --git a/tests/pos/i3588.scala b/tests/pos/i3588.scala new file mode 100644 index 000000000000..17aea68dc7b2 --- /dev/null +++ b/tests/pos/i3588.scala @@ -0,0 +1,51 @@ +class Foo { + val a: Any = 3 + a match { + case 1 => + case 2 => + case 3 => + case _ => + } +} + +class Bar[T] { + val a: T = ??? + a match { + case 1 => + case 2 => + case 3 => + case _ => + } +} + +class Baz { + val a: Double = 1.0 + a match { + case 1 => + case 2 => + case 3 => + case _ => + } +} + +class Foo2 { + val a: AnyVal = 3 + a match { + case 1 => + case 2 => + case 3 => + case _ => + } +} + + +case class A(i: Int) extends AnyVal +class Foo3 { + val a: A = new A(3) + a match { + case A(1) => + case A(2) => + case A(3) => + case _ => + } +}