Skip to content

Commit

Permalink
Allow beta-reduction on pure inlined blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasstucki committed Nov 22, 2022
1 parent d0a17ce commit 63c34cd
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
12 changes: 6 additions & 6 deletions compiler/src/dotty/tools/dotc/inlines/InlineReducer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,6 @@ class InlineReducer(inliner: Inliner)(using Context):
case Apply(Select(cl, nme.apply), args) if defn.isFunctionType(cl.tpe) =>
val bindingsBuf = new DefBuffer
def recur(cl: Tree): Option[Tree] = cl match
case Inlined(call, bindings, expr) if bindings.forall(isPureBinding) =>
recur(expr).map(cpy.Inlined(cl)(call, bindings, _))
case Block(Nil, expr) =>
recur(expr).map(cpy.Block(cl)(Nil, _))
case Typed(expr, tpt) =>
recur(expr).map(cpy.Typed(cl)(_, tpt))
case Block((ddef : DefDef) :: Nil, closure: Closure) if ddef.symbol == closure.meth.symbol =>
ddef.tpe.widen match
case mt: MethodType if ddef.paramss.head.length == args.length =>
Expand All @@ -190,6 +184,12 @@ class InlineReducer(inliner: Inliner)(using Context):
substTo = argSyms)
Some(expander.transform(ddef.rhs))
case _ => None
case Block(stats, expr) if stats.forall(isPureBinding) =>
recur(expr).map(cpy.Block(cl)(stats, _))
case Inlined(call, bindings, expr) if bindings.forall(isPureBinding) =>
recur(expr).map(cpy.Inlined(cl)(call, bindings, _))
case Typed(expr, tpt) =>
recur(expr)
case _ => None
recur(cl) match
case Some(reduced) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -600,10 +600,12 @@ class InlineBytecodeTests extends DottyBytecodeTest {
val instructions = instructionsFromMethod(fun)
val expected = // TODO room for constant folding
List(
Op(ICONST_1),
Op(ICONST_2),
VarOp(ISTORE, 1),
Op(ICONST_1),
VarOp(ISTORE, 2),
Op(ICONST_2),
VarOp(ILOAD, 1),
VarOp(ILOAD, 2),
Op(IADD),
Op(ICONST_3),
Op(IADD),
Expand Down
7 changes: 7 additions & 0 deletions tests/pos/i16374c.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def method(using String): String = ???

inline def inlineMethod(inline op: String => Unit)(using String): Unit =
println({ val a: Int = 1; op }.apply(method))

def test(using String) =
inlineMethod(c => print(c))
4 changes: 4 additions & 0 deletions tests/pos/i16374d.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
inline def inline1(inline f: Int => Int): Int => Int = i => f(1)
inline def inline2(inline f: Int => Int): Int = f(2) + 3
def test: Int = inline2(inline1(2.+))

0 comments on commit 63c34cd

Please sign in to comment.