Skip to content

Commit 4fded25

Browse files
committed
Allow beta-reduction on pure inlined blocks
1 parent 229fdaa commit 4fded25

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
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)
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) =>

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)