Skip to content

Commit 53b287a

Browse files
committed
Allow more beta-reductions
1 parent aa755f5 commit 53b287a

File tree

2 files changed

+18
-21
lines changed

2 files changed

+18
-21
lines changed

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

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package dotc
33
package transform
44

55
import core._
6+
import Flags._
67
import MegaPhase._
78
import Symbols._, Contexts._, Types._, Decorators._
89
import StdNames.nme
@@ -46,38 +47,34 @@ class BetaReduce extends MiniPhase:
4647
fn match
4748
case Typed(expr, _) => betaReduce(tree, expr, args)
4849
case Block(Nil, expr) => betaReduce(tree, expr, args)
49-
case Block((anonFun: DefDef) :: Nil, closure: Closure) => BetaReduce(tree)(anonFun, args, true)
50+
case Block((anonFun: DefDef) :: Nil, closure: Closure) => BetaReduce(anonFun, args)
5051
case _ => tree
5152

5253
object BetaReduce:
5354
import ast.tpd._
5455

5556
/** Beta-reduces a call to `ddef` with arguments `argSyms` */
56-
def apply(tree: Tree)(ddef: DefDef, args: List[Tree], noBindings: Boolean)(using ctx: Context) =
57+
def apply(ddef: DefDef, args: List[Tree])(using ctx: Context) =
5758
val bindings = List.newBuilder[ValDef]
5859
val vparams = ddef.vparamss.iterator.flatten.toList
60+
assert(args.hasSameLengthAs(vparams))
5961
val argSyms =
6062
for (arg, param) <- args.zip(vparams) yield
6163
arg.tpe.dealias match
6264
case ref @ TermRef(NoPrefix, _) if isPurePath(arg) =>
6365
ref.symbol
6466
case _ =>
65-
if noBindings then // TODO always generate bindings
66-
NoSymbol
67-
else
68-
val binding = SyntheticValDef(param.name, arg)
69-
bindings += binding
70-
binding.symbol
67+
val flags = Synthetic | (param.symbol.flags & Erased)
68+
val binding = ValDef(ctx.newSymbol(ctx.owner, param.name, flags, arg.tpe.widen, coord = arg.span), arg)
69+
bindings += binding
70+
binding.symbol
7171

72-
if argSyms.forall(_.exists) && argSyms.hasSameLengthAs(vparams) then // TODO assert rather than fail silently
73-
seq(
74-
bindings.result(),
75-
TreeTypeMap(
76-
oldOwners = ddef.symbol :: Nil,
77-
newOwners = ctx.owner :: Nil,
78-
substFrom = vparams.map(_.symbol),
79-
substTo = argSyms
80-
).transform(ddef.rhs)
81-
)
82-
else
83-
tree
72+
val expansion = TreeTypeMap(
73+
oldOwners = ddef.symbol :: Nil,
74+
newOwners = ctx.owner :: Nil,
75+
substFrom = vparams.map(_.symbol),
76+
substTo = argSyms
77+
).transform(ddef.rhs)
78+
79+
seq(bindings.result(), expansion)
80+
end apply

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,6 @@ class InlinePatterns extends MiniPhase:
5656
fn match
5757
case Block(TypeDef(_, template: Template) :: Nil, Apply(Select(New(_),_), Nil)) if template.constr.rhs.isEmpty =>
5858
template.body match
59-
case List(ddef @ DefDef(`name`, _, _, _, _)) => BetaReduce(tree)(ddef, args, false)
59+
case List(ddef @ DefDef(`name`, _, _, _, _)) => BetaReduce(ddef, args)
6060
case _ => tree
6161
case _ => tree

0 commit comments

Comments
 (0)