Skip to content

Commit a6de5e4

Browse files
committed
Reuse beta-reduction logic
1 parent d37902d commit a6de5e4

File tree

2 files changed

+8
-45
lines changed

2 files changed

+8
-45
lines changed

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

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,10 @@ class BetaReduce extends MiniPhase:
3838

3939
override def description: String = BetaReduce.description
4040

41-
override def transformApply(app: Apply)(using Context): Tree = app.fun match
42-
case Select(fn, nme.apply) if defn.isFunctionType(fn.tpe) =>
43-
val app1 = BetaReduce(app, fn, List(app.args))
44-
if app1 ne app then report.log(i"beta reduce $app -> $app1")
45-
app1
46-
case TypeApply(Select(fn, nme.apply), targs) if fn.tpe.typeSymbol eq defn.PolyFunctionClass =>
47-
val app1 = BetaReduce(app, fn, List(targs, app.args))
48-
if app1 ne app then report.log(i"beta reduce $app -> $app1")
49-
app1
50-
case _ =>
51-
app
41+
override def transformApply(app: Apply)(using Context): Tree =
42+
val app1 = BetaReduce(app)
43+
if app1 ne app then report.log(i"beta reduce $app -> $app1")
44+
app1
5245

5346
object BetaReduce:
5447
import ast.tpd._
@@ -118,31 +111,6 @@ object BetaReduce:
118111
case _ =>
119112
tree
120113

121-
/** Beta-reduces a call to `fn` with arguments `argSyms` or returns `tree` */
122-
def apply(original: Tree, fn: Tree, argss: List[List[Tree]])(using Context): Tree =
123-
fn match
124-
case Typed(expr, _) =>
125-
BetaReduce(original, expr, argss)
126-
case Block((anonFun: DefDef) :: Nil, closure: Closure) =>
127-
BetaReduce(anonFun, argss)
128-
case Block((TypeDef(_, template: Template)) :: Nil, Typed(Apply(Select(New(_), _), _), _)) if template.constr.rhs.isEmpty =>
129-
template.body match
130-
case (anonFun: DefDef) :: Nil =>
131-
BetaReduce(anonFun, argss)
132-
case _ =>
133-
original
134-
case Block(stats, expr) =>
135-
val tree = BetaReduce(original, expr, argss)
136-
if tree eq original then original
137-
else cpy.Block(fn)(stats, tree)
138-
case Inlined(call, bindings, expr) =>
139-
val tree = BetaReduce(original, expr, argss)
140-
if tree eq original then original
141-
else cpy.Inlined(fn)(call, bindings, tree)
142-
case _ =>
143-
original
144-
end apply
145-
146114
/** Beta-reduces a call to `ddef` with arguments `args` */
147115
def apply(ddef: DefDef, argss: List[List[Tree]])(using Context) =
148116
val bindings = new ListBuffer[DefTree]()

compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -362,20 +362,15 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
362362
object Term extends TermModule:
363363
def betaReduce(tree: Term): Option[Term] =
364364
tree match
365-
case app @ tpd.Apply(tpd.Select(fn, nme.apply), args) if dotc.core.Symbols.defn.isFunctionType(fn.tpe) =>
366-
val app1 = dotc.transform.BetaReduce(app, fn, List(args))
367-
if app1 eq app then None
368-
else Some(app1.withSpan(tree.span))
369-
case app @ tpd.Apply(tpd.TypeApply(tpd.Select(fn, nme.apply), targs), args) if fn.tpe.typeSymbol eq dotc.core.Symbols.defn.PolyFunctionClass =>
370-
val app1 = dotc.transform.BetaReduce(app, fn, List(targs, app.args))
371-
if app1 eq app then None
372-
else Some(app1.withSpan(tree.span))
373365
case tpd.Block(Nil, expr) =>
374366
for e <- betaReduce(expr) yield tpd.cpy.Block(tree)(Nil, e)
375367
case tpd.Inlined(_, Nil, expr) =>
376368
betaReduce(expr)
377369
case _ =>
378-
None
370+
val tree1 = dotc.transform.BetaReduce(tree)
371+
if tree1 eq tree then None
372+
else Some(tree1.withSpan(tree.span))
373+
379374
end Term
380375

381376
given TermMethods: TermMethods with

0 commit comments

Comments
 (0)