8000 Fix #5112: Recognize asInstanceOf around jump to default label. · scala-js/scala-js@498e56b · GitHub
[go: up one dir, main page]

Skip to content

Commit 498e56b

Browse files
committed
Fix #5112: Recognize asInstanceOf around jump to default label.
That shape gets produced by Scala 2.13.16+ in some cases.
1 parent 7b5528e commit 498e56b

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

compiler/src/main/scala/org/scalajs/nscplugin/GenJSCode.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3903,7 +3903,7 @@ abstract class GenJSCode[G <: Global with Singleton](val global: G)
39033903
assert(guard == EmptyTree, s"found a case guard at ${caze.pos}")
39043904

39053905
def genBody(body: Tree): js.Tree = body match {
3906-
case app @ Apply(_, Nil) if app.symbol == defaultLabelSym =>
3906+
case MaybeAsInstanceOf(app @ Apply(_, Nil)) if app.symbol == defaultLabelSym =>
39073907
genJumpToElseClause
39083908
case Block(List(app @ Apply(_, Nil)), _) if app.symbol == defaultLabelSym =>
39093909
genJumpToElseClause

test-suite/shared/src/test/scala/org/scalajs/testsuite/compiler/RegressionTest.scala

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,20 @@ class RegressionTest {
427427
assertEquals(-1, bug.result)
428428
}
429429

430+
@Test def switchMatchWithGuardAndResultTypeOfBoxedUnitWithTailrec_Issue5112(): Unit = {
431+
val bug = new Bug5112
432+
bug.bug('d', true)
433+
assertEquals(44, bug.result)
434+
bug.bug('a', true)
435+
assertEquals(11, bug.result)
436+
bug.bug('E', true)
437+
assertEquals(22, bug.result)
438+
bug.bug('e', false)
439+
assertEquals(44, bug.result)
440+
bug.bug('G', false)
441+
assertEquals(33, bug.result)
442+
}
443+
430444
@Test def switchMatchWithGuardInStatementPosButWithNonUnitBranches_Issue4105(): Unit = {
431445
def encodeString(string: String, isKey: Boolean): String = {
432446
val buffer = new java.lang.StringBuilder()
@@ -1002,6 +1016,25 @@ object RegressionTest {
10021016
}
10031017
}
10041018

1019+
class Bug5112 {
1020+
var result: Int = 0
1021+
1022+
// The tail-recursive transformation is required to trigger the bug
1023+
@tailrec
1024+
final def bug(ch: Char, guard: Boolean): Unit = {
1025+
if (ch >= 'A' && ch <= 'Z') {
1026+
bug((ch + 'a' - 'A').toChar, guard)
1027+
} else {
1028+
(ch: @switch) match {
1029+
case 'a' => result = 11
1030+
case 'c' | 'e' if guard => result = 22
1031+
case 'g' => result = 33
1032+
case _ => result = 44
1033+
}
1034+
}
1035+
}
1036+
}
1037+
10051038
object Bug3013 {
10061039
trait A1 {
10071040
private val s = "A1"

0 commit comments

Comments
 (0)
0