diff --git a/compiler/src/dotty/tools/dotc/inlines/InlineReducer.scala b/compiler/src/dotty/tools/dotc/inlines/InlineReducer.scala index 5c2893d3bbe3..b85454b8ba35 100644 --- a/compiler/src/dotty/tools/dotc/inlines/InlineReducer.scala +++ b/compiler/src/dotty/tools/dotc/inlines/InlineReducer.scala @@ -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 => @@ -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) => diff --git a/compiler/test/dotty/tools/backend/jvm/InlineBytecodeTests.scala b/compiler/test/dotty/tools/backend/jvm/InlineBytecodeTests.scala index ea9009de1d9e..a492e8785afc 100644 --- a/compiler/test/dotty/tools/backend/jvm/InlineBytecodeTests.scala +++ b/compiler/test/dotty/tools/backend/jvm/InlineBytecodeTests.scala @@ -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), diff --git a/tests/pos/i16374c.scala b/tests/pos/i16374c.scala new file mode 100644 index 000000000000..33e11f324bfa --- /dev/null +++ b/tests/pos/i16374c.scala @@ -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)) diff --git a/tests/pos/i16374d.scala b/tests/pos/i16374d.scala new file mode 100644 index 000000000000..5f0c8d715496 --- /dev/null +++ b/tests/pos/i16374d.scala @@ -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.+)) +