Skip to content

Commit 43c6ba7

Browse files
committed
Refactor BetaReduce logic
1 parent a6de5e4 commit 43c6ba7

File tree

3 files changed

+14
-16
lines changed

3 files changed

+14
-16
lines changed

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

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -111,22 +111,11 @@ object BetaReduce:
111111
case _ =>
112112
tree
113113

114-
/** Beta-reduces a call to `ddef` with arguments `args` */
115-
def apply(ddef: DefDef, argss: List[List[Tree]])(using Context) =
116-
val bindings = new ListBuffer[DefTree]()
117-
val expansion1 = reduceApplication(ddef, argss, bindings)
118-
val bindings1 = bindings.result()
119-
seq(bindings1, expansion1)
120-
121114
/** Beta-reduces a call to `ddef` with arguments `args` and registers new bindings */
122115
def reduceApplication(ddef: DefDef, argss: List[List[Tree]], bindings: ListBuffer[DefTree])(using Context): Tree =
123-
assert(argss.size == 1 || argss.size == 2)
124-
val targs = if argss.size == 2 then argss.head else Nil
125-
val args = argss.last
116+
val (targs, args) = argss.flatten.partition(_.isType)
126117
val tparams = ddef.leadingTypeParams
127118
val vparams = ddef.termParamss.flatten
128-
assert(targs.hasSameLengthAs(tparams))
129-
assert(args.hasSameLengthAs(vparams))
130119

131120
val targSyms =
132121
for (targ, tparam) <- targs.zip(tparams) yield

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import Symbols._, Contexts._, Types._, Decorators._
88
import NameOps._
99
import Names._
1010

11+
import scala.collection.mutable.ListBuffer
12+
1113
/** Rewrite an application
1214
*
1315
* {new { def unapply(x0: X0)(x1: X1,..., xn: Xn) = b }}.unapply(y0)(y1, ..., yn)
@@ -38,7 +40,7 @@ class InlinePatterns extends MiniPhase:
3840
if app.symbol.name.isUnapplyName && !app.tpe.isInstanceOf[MethodicType] then
3941
app match
4042
case App(Select(fn, name), argss) =>
41-
val app1 = betaReduce(app, fn, name, argss.flatten)
43+
val app1 = betaReduce(app, fn, name, argss)
4244
if app1 ne app then report.log(i"beta reduce $app -> $app1")
4345
app1
4446
case _ =>
@@ -52,11 +54,15 @@ class InlinePatterns extends MiniPhase:
5254
case _ => (app, Nil)
5355

5456
// TODO merge with BetaReduce.scala
55-
private def betaReduce(tree: Apply, fn: Tree, name: Name, args: List[Tree])(using Context): Tree =
57+
private def betaReduce(tree: Apply, fn: Tree, name: Name, argss: List[List[Tree]])(using Context): Tree =
5658
fn match
5759
case Block(TypeDef(_, template: Template) :: Nil, Apply(Select(New(_),_), Nil)) if template.constr.rhs.isEmpty =>
5860
template.body match
59-
case List(ddef @ DefDef(`name`, _, _, _)) => BetaReduce(ddef, List(args))
61+
case List(ddef @ DefDef(`name`, _, _, _)) =>
62+
val bindings = new ListBuffer[DefTree]()
63+
val expansion1 = BetaReduce.reduceApplication(ddef, argss, bindings)
64+
val bindings1 = bindings.result()
65+
seq(bindings1, expansion1)
6066
case _ => tree
6167
case _ => tree
6268

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,10 @@ object PickleQuotes {
322322
}
323323
val Block(List(ddef: DefDef), _) = splice: @unchecked
324324
// TODO: beta reduce inner closure? Or wait until BetaReduce phase?
325-
BetaReduce(ddef, List(spliceArgs)).select(nme.apply).appliedTo(args(2).asInstance(quotesType))
325+
BetaReduce(
326+
splice
327+
.select(nme.apply).appliedToArgs(spliceArgs))
328+
.select(nme.apply).appliedTo(args(2).asInstance(quotesType))
326329
}
327330
CaseDef(Literal(Constant(idx)), EmptyTree, rhs)
328331
}

0 commit comments

Comments
 (0)