Skip to content

Commit db2d3eb

Browse files
committed
Support beta reduction of functions with opaque types
1 parent 60c2d70 commit db2d3eb

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

compiler/src/dotty/tools/dotc/transform/BetaReduce.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ object BetaReduce:
8686
recur(expr, argss).map(cpy.Inlined(fn)(call, bindings, _))
8787
case Typed(expr, tpt) =>
8888
recur(expr, argss)
89+
case TypeApply(Select(expr, nme.asInstanceOfPM), List(tpt)) =>
90+
recur(expr, argss)
8991
case _ => None
9092
tree match
9193
case Apply(Select(fn, nme.apply), args) if defn.isFunctionType(fn.tpe) =>

compiler/test/dotty/tools/backend/jvm/InlineBytecodeTests.scala

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,37 @@ class InlineBytecodeTests extends DottyBytecodeTest {
608608
}
609609
}
610610

611+
@Test def beta_reduce_function_of_opaque_types = {
612+
val source = """object foo:
613+
| opaque type T = Int
614+
| inline def apply(inline op: T => T): T = op(2)
615+
|
616+
|class Test:
617+
| def test = foo { n => n }
618+
""".stripMargin
619+
620+
checkBCode(source) { dir =>
621+
val clsIn = dir.lookupName("Test.class", directory = false).input
622+
val clsNode = loadClassNode(clsIn)
623+
624+
val fun = getMethod(clsNode, "test")
625+
val instructions = instructionsFromMethod(fun)
626+
val expected =
627+
List(
628+
Field(GETSTATIC, "foo$", "MODULE$", "Lfoo$;"),
629+
VarOp(ASTORE, 1),
630+
VarOp(ALOAD, 1),
631+
VarOp(ASTORE, 2),
632+
Op(ICONST_2),
633+
Op(IRETURN),
634+
)
635+
636+
assert(instructions == expected,
637+
"`i was not properly beta-reduced in `test`\n" + diffInstructions(instructions, expected))
638+
639+
}
640+
}
641+
611642
@Test def i9456 = {
612643
val source = """class Foo {
613644
| def test: Int = inline2(inline1(2.+))

0 commit comments

Comments
 (0)