Skip to content

Commit 63c34cd

Browse files
committed
Allow beta-reduction on pure inlined blocks
1 parent d0a17ce commit 63c34cd

File tree

4 files changed

+21
-8
lines changed

4 files changed

+21
-8
lines changed

compiler/src/dotty/tools/dotc/inlines/InlineReducer.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,6 @@ class InlineReducer(inliner: Inliner)(using Context):
165165
case Apply(Select(cl, nme.apply), args) if defn.isFunctionType(cl.tpe) =>
166166
val bindingsBuf = new DefBuffer
167167
def recur(cl: Tree): Option[Tree] = cl match
168-
case Inlined(call, bindings, expr) if bindings.forall(isPureBinding) =>
169-
recur(expr).map(cpy.Inlined(cl)(call, bindings, _))
170-
case Block(Nil, expr) =>
171-
recur(expr).map(cpy.Block(cl)(Nil, _))
172-
case Typed(expr, tpt) =>
173-
recur(expr).map(cpy.Typed(cl)(_, tpt))
174168
case Block((ddef : DefDef) :: Nil, closure: Closure) if ddef.symbol == closure.meth.symbol =>
175169
ddef.tpe.widen match
176170
case mt: MethodType if ddef.paramss.head.length == args.length =>
@@ -190,6 +184,12 @@ class InlineReducer(inliner: Inliner)(using Context):
190184
substTo = argSyms)
191185
Some(expander.transform(ddef.rhs))
192186
case _ => None
187+
case Block(stats, expr) if stats.forall(isPureBinding) =>
188+
recur(expr).map(cpy.Block(cl)(stats, _))
189+
case Inlined(call, bindings, expr) if bindings.forall(isPureBinding) =>
190+
recur(expr).map(cpy.Inlined(cl)(call, bindings, _))
191+
case Typed(expr, tpt) =>
192+
recur(expr)
193193
case _ => None
194194
recur(cl) match
195195
case Some(reduced) =>

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -600,10 +600,12 @@ class InlineBytecodeTests extends DottyBytecodeTest {
600600
val instructions = instructionsFromMethod(fun)
601601
val expected = // TODO room for constant folding
602602
List(
603-
Op(ICONST_1),
603+
Op(ICONST_2),
604604
VarOp(ISTORE, 1),
605+
Op(ICONST_1),
606+
VarOp(ISTORE, 2),
605607
Op(ICONST_2),
606-
VarOp(ILOAD, 1),
608+
VarOp(ILOAD, 2),
607609
Op(IADD),
608610
Op(ICONST_3),
609611
Op(IADD),

tests/pos/i16374c.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def method(using String): String = ???
2+
3+
inline def inlineMethod(inline op: String => Unit)(using String): Unit =
4+
println({ val a: Int = 1; op }.apply(method))
5+
6+
def test(using String) =
7+
inlineMethod(c => print(c))

tests/pos/i16374d.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
inline def inline1(inline f: Int => Int): Int => Int = i => f(1)
2+
inline def inline2(inline f: Int => Int): Int = f(2) + 3
3+
def test: Int = inline2(inline1(2.+))
4+

0 commit comments

Comments
 (0)