Skip to content

Commit 5210c8c

Browse files
committed
Eliminate polyDefDef calls
1 parent eef65e1 commit 5210c8c

File tree

5 files changed

+37
-35
lines changed

5 files changed

+37
-35
lines changed

compiler/src/dotty/tools/dotc/ast/tpd.scala

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
404404
def forwarder(fn: TermSymbol, name: TermName) = {
405405
val fwdMeth = fn.copy(cls, name, Synthetic | Method | Final).entered.asTerm
406406
if (fwdMeth.allOverriddenSymbols.exists(!_.is(Deferred))) fwdMeth.setFlag(Override)
407-
polyDefDef(fwdMeth, tprefs => prefss => ref(fn).appliedToTypeTrees(tprefs).appliedToArgss(prefss))
407+
DefDef(fwdMeth, ref(fn).appliedToArgss(_))
408408
}
409409
val forwarders = fns.lazyZip(methNames).map(forwarder)
410410
val cdef = ClassDef(cls, DefDef(constr), forwarders)
@@ -1285,12 +1285,14 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
12851285
Ident(defn.ScalaRuntimeModule.requiredMethod(name).termRef).appliedToTermArgs(args)
12861286

12871287
/** An extractor that pulls out type arguments */
1288-
object MaybePoly {
1289-
def unapply(tree: Tree): Option[(Tree, List[Tree])] = tree match {
1288+
object MaybePoly:
1289+
def unapply(tree: Tree): Option[(Tree, List[Tree])] = tree match
12901290
case TypeApply(tree, targs) => Some(tree, targs)
12911291
case _ => Some(tree, Nil)
1292-
}
1293-
}
1292+
1293+
object TypeArgs:
1294+
def unapply(ts: List[Tree]): Option[List[Tree]] =
1295+
if ts.nonEmpty && ts.head.isType then Some(ts) else None
12941296

12951297
/** A key to be used in a context property that tracks enclosing inlined calls */
12961298
private val InlinedCalls = Property.Key[List[Tree]]()

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,17 +117,14 @@ class FirstTransform extends MiniPhase with InfoTransformer { thisPhase =>
117117
override def transformTemplate(impl: Template)(using Context): Tree =
118118
cpy.Template(impl)(self = EmptyValDef)
119119

120-
override def transformDefDef(ddef: DefDef)(using Context): Tree = {
120+
override def transformDefDef(ddef: DefDef)(using Context): Tree =
121121
val meth = ddef.symbol.asTerm
122-
if (meth.hasAnnotation(defn.NativeAnnot)) {
122+
if meth.hasAnnotation(defn.NativeAnnot) then
123123
meth.resetFlag(Deferred)
124-
polyDefDef(meth,
125-
_ => _ => ref(defn.Sys_error.termRef).withSpan(ddef.span)
124+
DefDef(meth, _ =>
125+
ref(defn.Sys_error.termRef).withSpan(ddef.span)
126126
.appliedTo(Literal(Constant(s"native method stub"))))
127-
}
128-
129127
else ddef
130-
}
131128

132129
override def transformStats(trees: List[Tree])(using Context): List[Tree] =
133130
ast.Trees.flatten(atPhase(thisPhase.next)(reorderAndComplete(trees)))

compiler/src/dotty/tools/dotc/typer/Deriving.scala

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -271,16 +271,19 @@ trait Deriving {
271271
import tpd._
272272

273273
/** The type class instance definition with symbol `sym` */
274-
def typeclassInstance(sym: Symbol)(using Context): List[tpd.Tree] => (List[List[tpd.Tree]] => tpd.Tree) = {
275-
(tparamRefs: List[tpd.Tree]) => (paramRefss: List[List[tpd.Tree]]) =>
274+
def typeclassInstance(sym: Symbol)(using Context): List[List[tpd.Tree]] => tpd.Tree =
275+
(paramRefss: List[List[tpd.Tree]]) =>
276+
val (tparamRefs, vparamRefss) = paramRefss match
277+
case TypeArgs(args) :: rest => (args, rest)
278+
case _ => (Nil, paramRefss)
276279
val tparamTypes = tparamRefs.tpes
277280
val tparams = tparamTypes.map(_.typeSymbol.asType)
278-
val params = if (paramRefss.isEmpty) Nil else paramRefss.head.map(_.symbol.asTerm)
281+
val vparams = if (vparamRefss.isEmpty) Nil else vparamRefss.head.map(_.symbol.asTerm)
279282
tparams.foreach(ctx.enter(_))
280-
params.foreach(ctx.enter(_))
283+
vparams.foreach(ctx.enter(_))
281284
def instantiated(info: Type): Type = info match {
282285
case info: PolyType => instantiated(info.instantiate(tparamTypes))
283-
case info: MethodType => info.instantiate(params.map(_.termRef))
286+
case info: MethodType => info.instantiate(vparams.map(_.termRef))
284287
case info => info.widenExpr
285288
}
286289
def companionRef(tp: Type): TermRef = tp match {
@@ -293,11 +296,11 @@ trait Deriving {
293296
val module = untpd.ref(companionRef(resultType)).withSpan(sym.span)
294297
val rhs = untpd.Select(module, nme.derived)
295298
typed(rhs, resultType)
296-
}
299+
end typeclassInstance
297300

298301
def syntheticDef(sym: Symbol): Tree = inContext(ctx.fresh.setOwner(sym).setNewScope) {
299-
if sym.is(Method) then tpd.polyDefDef(sym.asTerm, typeclassInstance(sym))
300-
else tpd.ValDef(sym.asTerm, typeclassInstance(sym)(Nil)(Nil))
302+
if sym.is(Method) then tpd.DefDef(sym.asTerm, typeclassInstance(sym))
303+
else tpd.ValDef(sym.asTerm, typeclassInstance(sym)(Nil))
301304
}
302305

303306
synthetics.map(syntheticDef).toList

compiler/src/dotty/tools/dotc/typer/Inliner.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,9 @@ object Inliner {
200200
flags = meth.flags &~ (Inline | Macro | Override) | Private,
201201
coord = mdef.rhs.span.startPos).asTerm
202202
retainer.deriveTargetNameAnnotation(meth, name => BodyRetainerName(name.asTermName))
203-
polyDefDef(retainer, targs => prefss =>
203+
DefDef(retainer, prefss =>
204204
inlineCall(
205-
ref(meth).appliedToTypeTrees(targs).appliedToArgss(prefss)
206-
.withSpan(mdef.rhs.span.startPos))(
205+
ref(meth).appliedToArgss(prefss).withSpan(mdef.rhs.span.startPos))(
207206
using ctx.withOwner(retainer)))
208207
.showing(i"retainer for $meth: $result", inlining)
209208

compiler/src/dotty/tools/dotc/typer/Namer.scala

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -988,21 +988,23 @@ class Namer { typer: Typer =>
988988
* provided `mbr` is accessible and of the right implicit/non-implicit kind.
989989
*/
990990
def addForwarder(alias: TermName, mbr: SingleDenotation, span: Span): Unit =
991+
991992
def adaptForwarderParams(acc: List[List[tpd.Tree]], tp: Type, prefss: List[List[tpd.Tree]])
992993
: List[List[tpd.Tree]] = tp match
993-
case mt: MethodType =>
994+
case mt: MethodType
995+
if mt.paramInfos.nonEmpty && mt.paramInfos.last.isRepeatedParam =>
994996
// Note: in this branch we use the assumptions
995997
// that `prefss.head` corresponds to `mt.paramInfos` and
996998
// that `prefss.tail` corresponds to `mt.resType`
997-
if mt.paramInfos.nonEmpty && mt.paramInfos.last.isRepeatedParam then
998-
val init :+ vararg = prefss.head
999-
val prefs = init :+ ctx.typeAssigner.seqToRepeated(vararg)
1000-
adaptForwarderParams(prefs :: acc, mt.resType, prefss.tail)
1001-
else
1002-
adaptForwarderParams(prefss.head :: acc, mt.resType, prefss.tail)
999+
val init :+ vararg = prefss.head
1000+
val prefs = init :+ ctx.typeAssigner.seqToRepeated(vararg)
1001+
adaptForwarderParams(prefs :: acc, mt.resType, prefss.tail)
1002+
case mt: MethodOrPoly =>
1003+
adaptForwarderParams(prefss.head :: acc, mt.resultType, prefss.tail)
10031004
case _ =>
10041005
acc.reverse ::: prefss
1005-
if (whyNoForwarder(mbr) == "") {
1006+
1007+
if whyNoForwarder(mbr) == "" then
10061008
val sym = mbr.symbol
10071009
val forwarder =
10081010
if mbr.isType then
@@ -1037,17 +1039,16 @@ class Namer { typer: Typer =>
10371039
else {
10381040
import tpd._
10391041
val ref = path.select(sym.asTerm)
1040-
val ddef = tpd.polyDefDef(forwarder.asTerm, targs => prefss =>
1041-
ref.appliedToTypeTrees(targs)
1042-
.appliedToArgss(adaptForwarderParams(Nil, sym.info.stripPoly, prefss))
1042+
val ddef = tpd.DefDef(forwarder.asTerm, prefss =>
1043+
ref.appliedToArgss(adaptForwarderParams(Nil, sym.info, prefss))
10431044
)
10441045
if forwarder.isInlineMethod then
10451046
PrepareInlineable.registerInlineInfo(forwarder, ddef.rhs)
10461047
ddef
10471048
}
10481049

10491050
buf += forwarderDef.withSpan(span)
1050-
}
1051+
end addForwarder
10511052

10521053
def addForwardersNamed(name: TermName, alias: TermName, span: Span): Unit = {
10531054
val size = buf.size

0 commit comments

Comments
 (0)