Skip to content

Commit 129685c

Browse files
Merge pull request #3994 from dotty-staging/fix-3857
Fix #3857: partial fix to generate more shallow tree in LabelDef
2 parents 6e3f188 + 060b239 commit 129685c

File tree

2 files changed

+80
-3
lines changed

2 files changed

+80
-3
lines changed

compiler/src/dotty/tools/backend/jvm/LabelDefs.scala

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import scala.collection.mutable
2929
* <label> def foo(i: Int) = dough(i)
3030
* <label> def dough(i: Int) = if (i == 0) bar else foo(i-1)
3131
* <label> def bar = 2
32-
* foo(100)
32+
* foo(100)
3333
* ```
3434
*
3535
* Proposed way to generate this pattern in backend is:
@@ -49,7 +49,7 @@ import scala.collection.mutable
4949
* Unreachable jumps will be eliminated by local dead code analysis.
5050
* After JVM is smart enough to remove next-line jumps
5151
*
52-
* Note that his phase Ychecking this phase required softening scoping rules
52+
* Note that Ychecking this phase requires softening scoping rules
5353
* as it intentionally allowed to break scoping rules inside methods for labels.
5454
* This is modified by setting `labelsReordered` flag in Phases.
5555
*
@@ -76,7 +76,25 @@ class LabelDefs extends MiniPhase {
7676
case t: DefDef =>
7777
assert(t.symbol is Label)
7878
EmptyTree
79-
case _ => if (!labelDefs.isEmpty) super.transform(tree) else tree
79+
case t: If =>
80+
// Trees generated by pattern matcher usually looks like the
81+
// following:
82+
//
83+
// ...
84+
// }
85+
// else case58(x704)
86+
// }
87+
// else case58(x704)
88+
// }
89+
// else case58(x704)
90+
//
91+
// First transform on the else branch will make labeldef
92+
// inserted on a shallow node.
93+
val elsep2 = transform(t.elsep)
94+
val thenp2 = transform(t.thenp)
95+
cpy.If(tree)(transform(t.cond), thenp2, elsep2)
96+
case _ =>
97+
if (!labelDefs.isEmpty) super.transform(tree) else tree
8098
}
8199
}
82100
}

tests/pos/i3857.scala

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
object foo {
2+
abstract sealed class num
3+
final case class One() extends num
4+
final case class Bit0(a: num) extends num
5+
final case class Bit1(a: num) extends num
6+
7+
abstract sealed class abschar
8+
final case class zero_char() extends abschar
9+
final case class Char(a: num) extends abschar
10+
11+
def integer_of_char(x0: abschar): BigInt = {
12+
def f(x: Int): Int = x
13+
x0 match {
14+
case Char(Bit0(Bit1(Bit0(Bit1(Bit0(One())))))) => BigInt(42)
15+
case Char(Bit1(Bit0(Bit0(Bit1(Bit0(One())))))) => BigInt(41)
16+
case Char(Bit0(Bit0(Bit0(Bit1(Bit0(One())))))) => BigInt(40)
17+
case Char(Bit1(Bit1(Bit1(Bit0(Bit0(One())))))) => BigInt(39)
18+
case Char(Bit0(Bit1(Bit1(Bit0(Bit0(One())))))) => BigInt(38)
19+
case Char(Bit1(Bit0(Bit1(Bit0(Bit0(One())))))) => BigInt(37)
20+
case Char(Bit0(Bit0(Bit1(Bit0(Bit0(One())))))) => BigInt(36)
21+
case Char(Bit1(Bit1(Bit0(Bit0(Bit0(One())))))) => BigInt(35)
22+
case Char(Bit0(Bit1(Bit0(Bit0(Bit0(One())))))) => BigInt(34)
23+
case Char(Bit1(Bit0(Bit0(Bit0(Bit0(One())))))) => BigInt(33)
24+
case Char(Bit0(Bit0(Bit0(Bit0(Bit0(One())))))) => BigInt(32)
25+
case Char(Bit1(Bit1(Bit1(Bit1(One()))))) => BigInt(31)
26+
case Char(Bit0(Bit1(Bit1(Bit1(One()))))) => BigInt(30)
27+
case Char(Bit1(Bit0(Bit1(Bit1(One()))))) => BigInt(29)
28+
case Char(Bit0(Bit0(Bit1(Bit1(One()))))) => BigInt(28)
29+
case Char(Bit1(Bit1(Bit0(Bit1(One()))))) => BigInt(27)
30+
case Char(Bit0(Bit1(Bit0(Bit1(One()))))) => BigInt(26)
31+
case Char(Bit1(Bit0(Bit0(Bit1(One()))))) => BigInt(25)
32+
case Char(Bit0(Bit0(Bit0(Bit1(One()))))) => BigInt(24)
33+
case Char(Bit1(Bit1(Bit1(Bit0(One()))))) => BigInt(23)
34+
case Char(Bit0(Bit1(Bit1(Bit0(One()))))) => BigInt(22)
35+
case Char(Bit1(Bit0(Bit1(Bit0(One()))))) => BigInt(21)
36+
case Char(Bit0(Bit0(Bit1(Bit0(One()))))) => BigInt(20)
37+
case Char(Bit1(Bit1(Bit0(Bit0(One()))))) => BigInt(19)
38+
case Char(Bit0(Bit1(Bit0(Bit0(One()))))) => BigInt(18)
39+
case Char(Bit1(Bit0(Bit0(Bit0(One()))))) => BigInt(17)
40+
case Char(Bit0(Bit0(Bit0(Bit0(One()))))) => BigInt(16)
41+
case Char(Bit1(Bit1(Bit1(One())))) => BigInt(15)
42+
case Char(Bit0(Bit1(Bit1(One())))) => BigInt(14)
43+
case Char(Bit1(Bit0(Bit1(One())))) => BigInt(13)
44+
case Char(Bit0(Bit0(Bit1(One())))) => BigInt(12)
45+
case Char(Bit1(Bit1(Bit0(One())))) => BigInt(11)
46+
case Char(Bit0(Bit1(Bit0(One())))) => BigInt(10)
47+
case Char(Bit1(Bit0(Bit0(One())))) => BigInt(9)
48+
case Char(Bit0(Bit0(Bit0(One())))) => BigInt(8)
49+
case Char(Bit1(Bit1(One()))) => BigInt(7)
50+
case Char(Bit0(Bit1(One()))) => BigInt(6)
51+
case Char(Bit1(Bit0(One()))) => BigInt(5)
52+
case Char(Bit0(Bit0(One()))) => BigInt(4)
53+
case Char(Bit1(One())) => BigInt(3)
54+
case Char(Bit0(One())) => BigInt(2)
55+
case Char(One()) => BigInt(1)
56+
case zero_char() => BigInt(0)
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)