Skip to content

Emit switch bytecode when matching unions of a switchable type #20411

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 3 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 4 additions & 3 deletions compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,7 @@ object PatternMatcher {
(Nil, plan) :: Nil
}

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

Expand All @@ -889,8 +889,9 @@ object PatternMatcher {
*/

val (primScrutinee, scrutineeTpe) =
if (scrutinee.tpe.widen.isRef(defn.IntClass)) (scrutinee, defn.IntType)
else if (scrutinee.tpe.widen.isRef(defn.StringClass)) (scrutinee, defn.StringType)
val tpe = scrutinee.tpe.widen.widenSingletons()
if (tpe.isRef(defn.IntClass)) (scrutinee, defn.IntType)
else if (tpe.isRef(defn.StringClass)) (scrutinee, defn.StringType)
else (scrutinee.select(nme.toInt), defn.IntType)

def primLiteral(lit: Tree): Tree =
Expand Down
98 changes: 98 additions & 0 deletions compiler/test/dotty/tools/backend/jvm/DottyBytecodeTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,104 @@ class DottyBytecodeTests extends DottyBytecodeTest {
}
}

@Test def switchOnUnionOfInts = {
val source =
"""
|object Foo {
| def foo(x: 1 | 2 | 3 | 4 | 5) = x match {
| case 1 => println(3)
| case 2 | 3 => println(2)
| case 4 => println(1)
| case 5 => println(0)
| }
|}
""".stripMargin

checkBCode(source) { dir =>
val moduleIn = dir.lookupName("Foo$.class", directory = false)
val moduleNode = loadClassNode(moduleIn.input)
val methodNode = getMethod(moduleNode, "foo")
assert(verifySwitch(methodNode))
}
}

@Test def switchOnUnionOfStrings = {
val source =
"""
|object Foo {
| def foo(s: "one" | "two" | "three" | "four" | "five") = s match {
| case "one" => println(3)
| case "two" | "three" => println(2)
| case "four" | "five" => println(1)
| case _ => println(0)
| }
|}
""".stripMargin

checkBCode(source) { dir =>
val moduleIn = dir.lookupName("Foo$.class", directory = false)
val moduleNode = loadClassNode(moduleIn.input)
val methodNode = getMethod(moduleNode, "foo")
assert(verifySwitch(methodNode))
}
}

@Test def switchOnUnionOfIntSingletons = {
val source =
"""
|object Foo {
| final val One = 1
| final val Two = 2
| final val Three = 3
| final val Four = 4
| final val Five = 5
| type Values = One.type | Two.type | Three.type | Four.type | Five.type
|
| def foo(s: Values) = s match {
| case One => println(3)
| case Two | Three => println(2)
| case Four => println(1)
| case Five => println(0)
| }
|}
""".stripMargin

checkBCode(source) { dir =>
val moduleIn = dir.lookupName("Foo$.class", directory = false)
val moduleNode = loadClassNode(moduleIn.input)
val methodNode = getMethod(moduleNode, "foo")
assert(verifySwitch(methodNode))
}
}

@Test def switchOnUnionOfStringSingletons = {
val source =
"""
|object Foo {
| final val One = "one"
| final val Two = "two"
| final val Three = "three"
| final val Four = "four"
| final val Five = "five"
| type Values = One.type | Two.type | Three.type | Four.type | Five.type
|
| def foo(s: Values) = s match {
| case One => println(3)
| case Two | Three => println(2)
| case Four => println(1)
| case Five => println(0)
| }
|}
""".stripMargin

checkBCode(source) { dir =>
val moduleIn = dir.lookupName("Foo$.class", directory = false)
val moduleNode = loadClassNode(moduleIn.input)
val methodNode = getMethod(moduleNode, "foo")
assert(verifySwitch(methodNode))
}
}

@Test def matchWithDefaultNoThrowMatchError = {
val source =
"""class Test {
Expand Down
Loading