Skip to content

Beta-reduce under blocks #10267

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 1 commit into from
Nov 11, 2020
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
21 changes: 15 additions & 6 deletions compiler/src/dotty/tools/dotc/transform/BetaReduce.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,22 @@ object BetaReduce:
import ast.tpd._

/** Beta-reduces a call to `fn` with arguments `argSyms` or returns `tree` */
def apply(tree: Apply, fn: Tree, args: List[Tree])(using Context): Tree =
def apply(original: Tree, fn: Tree, args: List[Tree])(using Context): Tree =
fn match
case Typed(expr, _) => BetaReduce(tree, expr, args)
case Block(Nil, expr) => BetaReduce(tree, expr, args)
case Inlined(_, Nil, expr) => BetaReduce(tree, expr, args)
case Block((anonFun: DefDef) :: Nil, closure: Closure) => BetaReduce(anonFun, args)
case _ => tree
case Typed(expr, _) =>
BetaReduce(original, expr, args)
case Block((anonFun: DefDef) :: Nil, closure: Closure) =>
BetaReduce(anonFun, args)
case Block(stats, expr) =>
val tree = BetaReduce(original, expr, args)
if tree eq original then original
else cpy.Block(fn)(stats, tree)
case Inlined(call, bindings, expr) =>
val tree = BetaReduce(original, expr, args)
if tree eq original then original
else cpy.Inlined(fn)(call, bindings, tree)
case _ =>
original
end apply

/** Beta-reduces a call to `ddef` with arguments `argSyms` */
Expand Down
47 changes: 39 additions & 8 deletions compiler/test/dotty/tools/backend/jvm/InlineBytecodeTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -368,14 +368,14 @@ class InlineBytecodeTests extends DottyBytecodeTest {
val instructions = instructionsFromMethod(fun)
val expected =
List(
IntOp(BIPUSH, 10)
, VarOp(ISTORE, 1)
, VarOp(ILOAD, 1)
, Op(ICONST_1)
, Op(IADD)
, VarOp(ISTORE, 1)
, VarOp(ILOAD, 1)
, Op(IRETURN)
IntOp(BIPUSH, 10),
VarOp(ISTORE, 1),
VarOp(ILOAD, 1),
Op(ICONST_1),
Op(IADD),
VarOp(ISTORE, 1),
VarOp(ILOAD, 1),
Op(IRETURN),
)
assert(instructions == expected,
"`f` was not properly inlined in `fun`\n" + diffInstructions(instructions, expected))
Expand Down Expand Up @@ -524,4 +524,35 @@ class InlineBytecodeTests extends DottyBytecodeTest {

}
}

@Test def beta_reduce_under_block = {
val source = """class Test:
| def test =
| {
| val a = 3
| (i: Int) => i + a
| }.apply(2)
""".stripMargin

checkBCode(source) { dir =>
val clsIn = dir.lookupName("Test.class", directory = false).input
val clsNode = loadClassNode(clsIn)

val fun = getMethod(clsNode, "test")
val instructions = instructionsFromMethod(fun)
val expected =
List(
Op(ICONST_3),
VarOp(ISTORE, 1),
Op(ICONST_2),
VarOp(ILOAD, 1),
Op(IADD),
Op(IRETURN),
)

assert(instructions == expected,
"`i was not properly beta-reduced in `test`\n" + diffInstructions(instructions, expected))

}
}
}
4 changes: 2 additions & 2 deletions tests/run-staging/i3876-b.check
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
6
{
def f(x: scala.Int): scala.Int = x.+(x)
((`x₂`: scala.Int) => f(`x₂`))
}.apply(3)
f(3)
}