Skip to content

Fix #3857: partial fix to generate more shallow tree in LabelDef #3994

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 2 commits into from
Feb 20, 2018
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
24 changes: 21 additions & 3 deletions compiler/src/dotty/tools/backend/jvm/LabelDefs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import scala.collection.mutable
* <label> def foo(i: Int) = dough(i)
* <label> def dough(i: Int) = if (i == 0) bar else foo(i-1)
* <label> def bar = 2
* foo(100)
* foo(100)
* ```
*
* Proposed way to generate this pattern in backend is:
Expand All @@ -49,7 +49,7 @@ import scala.collection.mutable
* Unreachable jumps will be eliminated by local dead code analysis.
* After JVM is smart enough to remove next-line jumps
*
* Note that his phase Ychecking this phase required softening scoping rules
* Note that Ychecking this phase requires softening scoping rules
* as it intentionally allowed to break scoping rules inside methods for labels.
* This is modified by setting `labelsReordered` flag in Phases.
*
Expand All @@ -76,7 +76,25 @@ class LabelDefs extends MiniPhase {
case t: DefDef =>
assert(t.symbol is Label)
EmptyTree
case _ => if (!labelDefs.isEmpty) super.transform(tree) else tree
case t: If =>
// Trees generated by pattern matcher usually looks like the
// following:
//
// ...
// }
// else case58(x704)
// }
// else case58(x704)
// }
// else case58(x704)
//
// First transform on the else branch will make labeldef
// inserted on a shallow node.
val elsep2 = transform(t.elsep)
val thenp2 = transform(t.thenp)
cpy.If(tree)(transform(t.cond), thenp2, elsep2)
case _ =>
if (!labelDefs.isEmpty) super.transform(tree) else tree
}
}
}
Expand Down
59 changes: 59 additions & 0 deletions tests/pos/i3857.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
object foo {
abstract sealed class num
final case class One() extends num
final case class Bit0(a: num) extends num
final case class Bit1(a: num) extends num

abstract sealed class abschar
final case class zero_char() extends abschar
final case class Char(a: num) extends abschar

def integer_of_char(x0: abschar): BigInt = {
def f(x: Int): Int = x
x0 match {
case Char(Bit0(Bit1(Bit0(Bit1(Bit0(One())))))) => BigInt(42)
case Char(Bit1(Bit0(Bit0(Bit1(Bit0(One())))))) => BigInt(41)
case Char(Bit0(Bit0(Bit0(Bit1(Bit0(One())))))) => BigInt(40)
case Char(Bit1(Bit1(Bit1(Bit0(Bit0(One())))))) => BigInt(39)
case Char(Bit0(Bit1(Bit1(Bit0(Bit0(One())))))) => BigInt(38)
case Char(Bit1(Bit0(Bit1(Bit0(Bit0(One())))))) => BigInt(37)
case Char(Bit0(Bit0(Bit1(Bit0(Bit0(One())))))) => BigInt(36)
case Char(Bit1(Bit1(Bit0(Bit0(Bit0(One())))))) => BigInt(35)
case Char(Bit0(Bit1(Bit0(Bit0(Bit0(One())))))) => BigInt(34)
case Char(Bit1(Bit0(Bit0(Bit0(Bit0(One())))))) => BigInt(33)
case Char(Bit0(Bit0(Bit0(Bit0(Bit0(One())))))) => BigInt(32)
case Char(Bit1(Bit1(Bit1(Bit1(One()))))) => BigInt(31)
case Char(Bit0(Bit1(Bit1(Bit1(One()))))) => BigInt(30)
case Char(Bit1(Bit0(Bit1(Bit1(One()))))) => BigInt(29)
case Char(Bit0(Bit0(Bit1(Bit1(One()))))) => BigInt(28)
case Char(Bit1(Bit1(Bit0(Bit1(One()))))) => BigInt(27)
case Char(Bit0(Bit1(Bit0(Bit1(One()))))) => BigInt(26)
case Char(Bit1(Bit0(Bit0(Bit1(One()))))) => BigInt(25)
case Char(Bit0(Bit0(Bit0(Bit1(One()))))) => BigInt(24)
case Char(Bit1(Bit1(Bit1(Bit0(One()))))) => BigInt(23)
case Char(Bit0(Bit1(Bit1(Bit0(One()))))) => BigInt(22)
case Char(Bit1(Bit0(Bit1(Bit0(One()))))) => BigInt(21)
case Char(Bit0(Bit0(Bit1(Bit0(One()))))) => BigInt(20)
case Char(Bit1(Bit1(Bit0(Bit0(One()))))) => BigInt(19)
case Char(Bit0(Bit1(Bit0(Bit0(One()))))) => BigInt(18)
case Char(Bit1(Bit0(Bit0(Bit0(One()))))) => BigInt(17)
case Char(Bit0(Bit0(Bit0(Bit0(One()))))) => BigInt(16)
case Char(Bit1(Bit1(Bit1(One())))) => BigInt(15)
case Char(Bit0(Bit1(Bit1(One())))) => BigInt(14)
case Char(Bit1(Bit0(Bit1(One())))) => BigInt(13)
case Char(Bit0(Bit0(Bit1(One())))) => BigInt(12)
case Char(Bit1(Bit1(Bit0(One())))) => BigInt(11)
case Char(Bit0(Bit1(Bit0(One())))) => BigInt(10)
case Char(Bit1(Bit0(Bit0(One())))) => BigInt(9)
case Char(Bit0(Bit0(Bit0(One())))) => BigInt(8)
case Char(Bit1(Bit1(One()))) => BigInt(7)
case Char(Bit0(Bit1(One()))) => BigInt(6)
case Char(Bit1(Bit0(One()))) => BigInt(5)
case Char(Bit0(Bit0(One()))) => BigInt(4)
case Char(Bit1(One())) => BigInt(3)
case Char(Bit0(One())) => BigInt(2)
case Char(One()) => BigInt(1)
case zero_char() => BigInt(0)
}
}
}