Skip to content

Commit 0bc379d

Browse files
committed
Add tests for specializing unobscured Func1 implementation
1 parent d172f81 commit 0bc379d

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

compiler/test/dotty/tools/dotc/transform/SpecializeFunction1Tests.scala

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,21 @@ class SpecializeFunction1Tests extends DottyBytecodeTest {
1212
import dotty.tools.backend.jvm.ASMConverters._
1313
import dotty.tools.backend.jvm.AsmNode._
1414

15+
protected def checkForBoxing(ins: List[Instruction], source: String): Unit = ins.foreach {
16+
case Invoke(op, owner, name, desc, itf) =>
17+
def error =
18+
s"""|----------------------------------
19+
|${ins.mkString("\n")}
20+
|----------------------------------
21+
|From code:
22+
|$source
23+
|----------------------------------""".stripMargin
24+
25+
assert(!owner.toLowerCase.contains("box"), s"Boxing instruction discovered in:\n$error")
26+
assert(!name.toLowerCase.contains("box"), s"Boxing instruction discovered in:\n$error")
27+
case _ => ()
28+
}
29+
1530
@Test def specializeParentIntToInt = {
1631
val source = """
1732
|class Foo extends Function1[Int, Int] {
@@ -50,4 +65,30 @@ class SpecializeFunction1Tests extends DottyBytecodeTest {
5065
)
5166
}
5267
}
68+
69+
@Test def checkBoxingIntToInt = {
70+
val source =
71+
"""|object Test {
72+
| class Func1 extends Function1[Int, Int] {
73+
| def apply(i: Int) = i + 1
74+
| }
75+
|
76+
| (new Func1)(1)
77+
|}""".stripMargin
78+
79+
checkBCode(source) { dir =>
80+
import scala.collection.JavaConverters._
81+
val clsIn = dir.lookupName("Test$.class", directory = false).input
82+
val clsNode = loadClassNode(clsIn)
83+
assert(clsNode.name == "Test$", s"inspecting wrong class: ${clsNode.name}")
84+
85+
clsNode.methods.asScala
86+
.find(_.name == "<init>")
87+
.map { m =>
88+
checkForBoxing(instructionsFromMethod(m), source)
89+
}
90+
.getOrElse(assert(false, "Could not find constructor for object `Test`"))
91+
}
92+
93+
}
5394
}

0 commit comments

Comments
 (0)