Skip to content

Commit f693e22

Browse files
committed
Reuse beta-reduction logic from BetaReduce
1 parent 4a26b78 commit f693e22

File tree

3 files changed

+19
-33
lines changed

3 files changed

+19
-33
lines changed

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

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import NameKinds.{InlineAccessorName, InlineBinderName, InlineScrutineeName}
1212
import config.Printers.inlining
1313
import util.SimpleIdentityMap
1414

15+
import dotty.tools.dotc.transform.BetaReduce
16+
1517
import collection.mutable
1618

1719
/** A utility class offering methods for rewriting inlined code */
@@ -163,26 +165,12 @@ class InlineReducer(inliner: Inliner)(using Context):
163165
*/
164166
def betaReduce(tree: Tree)(using Context): Tree = tree match {
165167
case Apply(Select(cl, nme.apply), args) if defn.isFunctionType(cl.tpe) =>
166-
val bindingsBuf = new DefBuffer
168+
val bindingsBuf = new mutable.ListBuffer[ValDef]
167169
def recur(cl: Tree): Option[Tree] = cl match
168170
case Block((ddef : DefDef) :: Nil, closure: Closure) if ddef.symbol == closure.meth.symbol =>
169171
ddef.tpe.widen match
170172
case mt: MethodType if ddef.paramss.head.length == args.length =>
171-
val argSyms = mt.paramNames.lazyZip(mt.paramInfos).lazyZip(args).map { (name, paramtp, arg) =>
172-
arg.tpe.dealias match {
173-
case ref @ TermRef(NoPrefix, _) => ref.symbol
174-
case _ =>
175-
paramBindingDef(name, paramtp, arg, bindingsBuf)(
176-
using ctx.withSource(cl.source)
177-
).symbol
178-
}
179-
}
180-
val expander = new TreeTypeMap(
181-
oldOwners = ddef.symbol :: Nil,
182-
newOwners = ctx.owner :: Nil,
183-
substFrom = ddef.paramss.head.map(_.symbol),
184-
substTo = argSyms)
185-
Some(expander.transform(ddef.rhs))
173+
Some(BetaReduce.reduceApplication(ddef, args, bindingsBuf))
186174
case _ => None
187175
case Block(stats, expr) if stats.forall(isPureBinding) =>
188176
recur(expr).map(cpy.Block(cl)(stats, _))
@@ -193,7 +181,7 @@ class InlineReducer(inliner: Inliner)(using Context):
193181
case _ => None
194182
recur(cl) match
195183
case Some(reduced) =>
196-
Block(bindingsBuf.toList, reduced).withSpan(tree.span)
184+
seq(bindingsBuf.result(), reduced).withSpan(tree.span)
197185
case None =>
198186
tree
199187
case _ =>

compiler/src/dotty/tools/dotc/transform/BetaReduce.scala

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import Symbols._, Contexts._, Types._, Decorators._
99
import StdNames.nme
1010
import ast.TreeTypeMap
1111

12+
import scala.collection.mutable.ListBuffer
13+
1214
/** Rewrite an application
1315
*
1416
* (((x1, ..., xn) => b): T)(y1, ..., yn)
@@ -70,9 +72,15 @@ object BetaReduce:
7072
original
7173
end apply
7274

73-
/** Beta-reduces a call to `ddef` with arguments `argSyms` */
75+
/** Beta-reduces a call to `ddef` with arguments `args` */
7476
def apply(ddef: DefDef, args: List[Tree])(using Context) =
75-
val bindings = List.newBuilder[ValDef]
77+
val bindings = new ListBuffer[ValDef]()
78+
val expansion1 = reduceApplication(ddef, args, bindings)
79+
val bindings1 = bindings.result()
80+
seq(bindings1, expansion1)
81+
82+
/** Beta-reduces a call to `ddef` with arguments `args` and registers new bindings */
83+
def reduceApplication(ddef: DefDef, args: List[Tree], bindings: ListBuffer[ValDef])(using Context): Tree =
7684
val vparams = ddef.termParamss.iterator.flatten.toList
7785
assert(args.hasSameLengthAs(vparams))
7886
val argSyms =
@@ -84,7 +92,8 @@ object BetaReduce:
8492
val flags = Synthetic | (param.symbol.flags & Erased)
8593
val tpe = if arg.tpe.dealias.isInstanceOf[ConstantType] then arg.tpe.dealias else arg.tpe.widen
8694
val binding = ValDef(newSymbol(ctx.owner, param.name, flags, tpe, coord = arg.span), arg).withSpan(arg.span)
87-
bindings += binding
95+
if !(tpe.isInstanceOf[ConstantType] && isPureExpr(arg)) then
96+
bindings += binding
8897
binding.symbol
8998

9099
val expansion = TreeTypeMap(
@@ -99,8 +108,5 @@ object BetaReduce:
99108
case ConstantType(const) if isPureExpr(tree) => cpy.Literal(tree)(const)
100109
case _ => super.transform(tree)
101110
}.transform(expansion)
102-
val bindings1 =
103-
bindings.result().filterNot(vdef => vdef.tpt.tpe.isInstanceOf[ConstantType] && isPureExpr(vdef.rhs))
104111

105-
seq(bindings1, expansion1)
106-
end apply
112+
expansion1

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -600,15 +600,7 @@ class InlineBytecodeTests extends DottyBytecodeTest {
600600
val instructions = instructionsFromMethod(fun)
601601
val expected = // TODO room for constant folding
602602
List(
603-
Op(ICONST_2),
604-
VarOp(ISTORE, 1),
605-
Op(ICONST_1),
606-
VarOp(ISTORE, 2),
607-
Op(ICONST_2),
608-
VarOp(ILOAD, 2),
609-
Op(IADD),
610-
Op(ICONST_3),
611-
Op(IADD),
603+
IntOp(BIPUSH, 6),
612604
Op(IRETURN),
613605
)
614606
assert(instructions == expected,

0 commit comments

Comments
 (0)