Skip to content

Fix #3588: Do not emit switch when matching on Any #3589

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,8 @@ object PatternMatcher {
plan :: Nil
}

recur(plan)
if (isSwitchableType(scrutinee.tpe.widen)) recur(plan)
else Nil
}

/** Emit cases of a switch */
Expand Down
2 changes: 2 additions & 0 deletions compiler/test/dotty/tools/dotc/CompilationTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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"))
Expand Down
10 changes: 10 additions & 0 deletions tests/neg-custom-args/i3589-a.scala
Original file line number Diff line number Diff line change
@@ -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
}
}
7 changes: 7 additions & 0 deletions tests/pos-special/i3589-b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Test {
def test(x: 1) = (x: @annotation.switch) match {
case 1 => 1
case 2 => 2
case 3 => 3
}
}
51 changes: 51 additions & 0 deletions tests/pos/i3588.scala
Original file line number Diff line number Diff line change
@@ -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 _ =>
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add two more tests:

  • a pos test compiled with -Xfatal-warnings:
class Test {
  def test(x: 1) = (x: @annotation.switch) match {
    case 1 => 1
    case 2 => 2
    case 3 => 3
  }
}
  • a neg test compiled with -Xfatal-warnings:
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
  }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done