Skip to content

Commit 127f498

Browse files
committed
Merge type params and term params in DefDef nodes
1 parent deec4c1 commit 127f498

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+557
-443
lines changed

compiler/src/dotty/tools/backend/jvm/BCodeSkelBuilder.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ trait BCodeSkelBuilder extends BCodeHelpers {
667667
val enclosingClass = origSym.owner.asClass
668668
new TreeTypeMap(
669669
typeMap = _.substThis(enclosingClass, selfParamRef.symbol.termRef)
670-
.subst(dd.vparamss.head.map(_.symbol), regularParamRefs.map(_.symbol.termRef)),
670+
.subst(dd.termParamss.head.map(_.symbol), regularParamRefs.map(_.symbol.termRef)),
671671
treeMap = {
672672
case tree: This if tree.symbol == enclosingClass => selfParamRef
673673
case tree => tree
@@ -714,7 +714,7 @@ trait BCodeSkelBuilder extends BCodeHelpers {
714714

715715
def genDefDef(dd: DefDef): Unit = {
716716
val rhs = dd.rhs
717-
val vparamss = dd.vparamss
717+
val vparamss = dd.termParamss
718718
// the only method whose implementation is not emitted: getClass()
719719
if (dd.symbol eq defn.Any_getClass) { return }
720720
assert(mnode == null, "GenBCode detected nested method.")

compiler/src/dotty/tools/backend/sjs/JSCodeGen.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1035,7 +1035,7 @@ class JSCodeGen()(using genCtx: Context) {
10351035
private def genMethodWithCurrentLocalNameScope(dd: DefDef): Option[js.MethodDef] = {
10361036
implicit val pos = dd.span
10371037
val sym = dd.symbol
1038-
val vparamss = dd.vparamss
1038+
val vparamss = dd.termParamss
10391039
val rhs = dd.rhs
10401040

10411041
withScopedVars(

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

Lines changed: 140 additions & 97 deletions
Large diffs are not rendered by default.

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ object DesugarEnums {
130130
.withFlags(Private | Synthetic)
131131

132132
val valuesDef =
133-
DefDef(nme.values, Nil, Nil, defn.ArrayType.ofRawEnum, valuesDot(nme.clone_))
133+
DefDef(nme.values, Nil, defn.ArrayType.ofRawEnum, valuesDot(nme.clone_))
134134
.withFlags(Synthetic)
135135

136136
val valuesOfBody: Tree =
@@ -142,7 +142,7 @@ object DesugarEnums {
142142
CaseDef(Literal(Constant(enumValue.name.toString)), EmptyTree, enumValue)
143143
) ::: defaultCase :: Nil
144144
Match(Ident(nme.nameDollar), stringCases)
145-
val valueOfDef = DefDef(nme.valueOf, Nil, List(param(nme.nameDollar, defn.StringType) :: Nil),
145+
val valueOfDef = DefDef(nme.valueOf, List(param(nme.nameDollar, defn.StringType) :: Nil),
146146
TypeTree(), valuesOfBody)
147147
.withFlags(Synthetic)
148148

@@ -195,7 +195,7 @@ object DesugarEnums {
195195
self = EmptyValDef,
196196
body = fieldMethods
197197
).withAttachment(ExtendsSingletonMirror, ()))
198-
DefDef(nme.DOLLAR_NEW, Nil,
198+
DefDef(nme.DOLLAR_NEW,
199199
List(List(param(nme.ordinalDollar_, defn.IntType), param(nme.nameDollar, defn.StringType))),
200200
TypeTree(), creator).withFlags(Private | Synthetic)
201201
}
@@ -281,13 +281,13 @@ object DesugarEnums {
281281
private def isJavaEnum(using Context): Boolean = enumClass.derivesFrom(defn.JavaEnumClass)
282282

283283
def ordinalMeth(body: Tree)(using Context): DefDef =
284-
DefDef(nme.ordinal, Nil, Nil, TypeTree(defn.IntType), body).withAddedFlags(Synthetic)
284+
DefDef(nme.ordinal, Nil, TypeTree(defn.IntType), body).withAddedFlags(Synthetic)
285285

286286
def ordinalMethLit(ord: Int)(using Context): DefDef =
287287
ordinalMeth(Literal(Constant(ord)))
288288

289289
def fromOrdinalMeth(body: Tree => Tree)(using Context): DefDef =
290-
DefDef(nme.fromOrdinal, Nil, (param(nme.ordinal, defn.IntType) :: Nil) :: Nil,
290+
DefDef(nme.fromOrdinal, (param(nme.ordinal, defn.IntType) :: Nil) :: Nil,
291291
rawRef(enumClass.typeRef), body(Ident(nme.ordinal))).withFlags(Synthetic)
292292

293293
/** Expand a module definition representing a parameterless enum case */

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ object MainProxies {
102102
val body = Try(call, handler :: Nil, EmptyTree)
103103
val mainArg = ValDef(nme.args, TypeTree(defn.ArrayType.appliedTo(defn.StringType)), EmptyTree)
104104
.withFlags(Param)
105-
val mainMeth = DefDef(nme.main, Nil, (mainArg :: Nil) :: Nil, TypeTree(defn.UnitType), body)
105+
val mainMeth = DefDef(nme.main, (mainArg :: Nil) :: Nil, TypeTree(defn.UnitType), body)
106106
.withFlags(JavaStatic)
107107
val mainTempl = Template(emptyConstructor, Nil, Nil, EmptyValDef, mainMeth :: Nil)
108108
val mainCls = TypeDef(mainFun.name.toTypeName, mainTempl)

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -206,20 +206,15 @@ abstract class Positioned(implicit @constructorOnly src: SourceFile) extends Src
206206
this match {
207207
case tree: DefDef if tree.name == nme.CONSTRUCTOR && tree.mods.is(JavaDefined) =>
208208
// Special treatment for constructors coming from Java:
209-
// Leave out tparams, they are copied with wrong positions from parent class
209+
// Leave out leading type params, they are copied with wrong positions from parent class
210210
check(tree.mods)
211-
check(tree.vparamss)
211+
check(tree.trailingParamss)
212212
case tree: DefDef if tree.mods.is(ExtensionMethod) =>
213-
tree.vparamss match {
213+
tree.paramss match
214214
case vparams1 :: vparams2 :: rest if tree.name.isRightAssocOperatorName =>
215-
check(tree.tparams)
216-
check(vparams2)
217-
check(vparams1)
218-
check(rest)
215+
// omit check for right-associatiove extension methods; their parameters were swapped
219216
case _ =>
220-
check(tree.tparams)
221-
check(tree.vparamss)
222-
}
217+
check(tree.paramss)
223218
check(tree.tpt)
224219
check(tree.rhs)
225220
case _ =>

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

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ trait TreeInfo[T >: Untyped <: Type] { self: Trees.Instance[T] =>
2222
def unsplice(tree: Trees.Tree[T]): Trees.Tree[T] = tree
2323

2424
def isDeclarationOrTypeDef(tree: Tree): Boolean = unsplice(tree) match {
25-
case DefDef(_, _, _, _, EmptyTree)
25+
case DefDef(_, _, _, EmptyTree)
2626
| ValDef(_, _, EmptyTree)
2727
| TypeDef(_, _) => true
2828
case _ => false
@@ -171,12 +171,6 @@ trait TreeInfo[T >: Untyped <: Type] { self: Trees.Instance[T] =>
171171
case nil => EmptyTree
172172
}
173173

174-
/** The arguments to the first constructor in `stats`. */
175-
def firstConstructorArgs(stats: List[Tree]): List[Tree] = firstConstructor(stats) match {
176-
case DefDef(_, _, args :: _, _, _) => args
177-
case _ => Nil
178-
}
179-
180174
/** Is tpt a vararg type of the form T* or => T*? */
181175
def isRepeatedParamType(tpt: Tree)(using Context): Boolean = tpt match {
182176
case ByNameTypeTree(tpt1) => isRepeatedParamType(tpt1)
@@ -198,7 +192,7 @@ trait TreeInfo[T >: Untyped <: Type] { self: Trees.Instance[T] =>
198192

199193
/** All type and value parameter symbols of this DefDef */
200194
def allParamSyms(ddef: DefDef)(using Context): List[Symbol] =
201-
(ddef.tparams ::: ddef.vparamss.flatten).map(_.symbol)
195+
ddef.paramss.flatten.map(_.symbol)
202196

203197
/** Does this argument list end with an argument of the form <expr> : _* ? */
204198
def isWildcardStarArgList(trees: List[Tree])(using Context): Boolean =
@@ -246,13 +240,17 @@ trait TreeInfo[T >: Untyped <: Type] { self: Trees.Instance[T] =>
246240
def isGuardedCase(cdef: CaseDef): Boolean = cdef.guard ne EmptyTree
247241

248242
/** Is this parameter list a using clause? */
249-
def isUsingClause(vparams: List[ValDef])(using Context): Boolean = vparams match
250-
case vparam :: _ =>
243+
def isUsingClause(params: ParamClause)(using Context): Boolean = params match
244+
case ValDefs(vparam :: _) =>
251245
val sym = vparam.symbol
252246
if sym.exists then sym.is(Given) else vparam.mods.is(Given)
253247
case _ =>
254248
false
255249

250+
def isUsingOrTypeParamClause(params: ParamClause)(using Context): Boolean = params match
251+
case TypeDefs(_) => true
252+
case _ => isUsingClause(params)
253+
256254
/** The underlying pattern ignoring any bindings */
257255
def unbind(x: Tree): Tree = unsplice(x) match {
258256
case Bind(_, y) => unbind(y)
@@ -313,11 +311,11 @@ trait UntypedTreeInfo extends TreeInfo[Untyped] { self: Trees.Instance[Untyped]
313311
case Function((param: untpd.ValDef) :: _, _) => param.mods.is(Given)
314312
case Closure(_, meth, _) => true
315313
case Block(Nil, expr) => isContextualClosure(expr)
316-
case Block(DefDef(nme.ANON_FUN, _, params :: _, _, _) :: Nil, cl: Closure) =>
317-
params match {
318-
case param :: _ => param.mods.is(Given)
319-
case Nil => cl.tpt.eq(untpd.ContextualEmptyTree) || defn.isContextFunctionType(cl.tpt.typeOpt)
320-
}
314+
case Block(DefDef(nme.ANON_FUN, params :: _, _, _) :: Nil, cl: Closure) =>
315+
if params.isEmpty then
316+
cl.tpt.eq(untpd.ContextualEmptyTree) || defn.isContextFunctionType(cl.tpt.typeOpt)
317+
else
318+
isUsingClause(params)
321319
case _ => false
322320
}
323321

@@ -328,11 +326,17 @@ trait UntypedTreeInfo extends TreeInfo[Untyped] { self: Trees.Instance[Untyped]
328326
case EmptyTree | _: Import => NoInitsInterface
329327
case tree: TypeDef => if (tree.isClassDef) NoInits else NoInitsInterface
330328
case tree: DefDef =>
331-
if (tree.unforcedRhs == EmptyTree &&
332-
tree.vparamss.forall(_.forall(_.rhs.isEmpty))) NoInitsInterface
333-
else if (tree.mods.is(Given) && tree.tparams.isEmpty && tree.vparamss.isEmpty)
329+
if tree.unforcedRhs == EmptyTree
330+
&& tree.paramss.forall {
331+
case ValDefs(vparams) => vparams.forall(_.rhs.isEmpty)
332+
case _ => true
333+
}
334+
then
335+
NoInitsInterface
336+
else if tree.mods.is(Given) && tree.paramss.isEmpty then
334337
EmptyFlags // might become a lazy val: TODO: check whether we need to suppress NoInits once we have new lazy val impl
335-
else NoInits
338+
else
339+
NoInits
336340
case tree: ValDef => if (tree.unforcedRhs == EmptyTree) NoInitsInterface else EmptyFlags
337341
case _ => EmptyFlags
338342
}
@@ -354,8 +358,6 @@ trait UntypedTreeInfo extends TreeInfo[Untyped] { self: Trees.Instance[Untyped]
354358
case _ => None
355359
}
356360
}
357-
358-
// todo: fill with other methods from TreeInfo that only apply to untpd.Tree's
359361
}
360362

361363
trait TypedTreeInfo extends TreeInfo[Type] { self: Trees.Instance[Type] =>
@@ -371,7 +373,7 @@ trait TypedTreeInfo extends TreeInfo[Type] { self: Trees.Instance[Type] =>
371373
case EmptyTree
372374
| TypeDef(_, _)
373375
| Import(_, _)
374-
| DefDef(_, _, _, _, _) =>
376+
| DefDef(_, _, _, _) =>
375377
Pure
376378
case vdef @ ValDef(_, _, _) =>
377379
if (vdef.symbol.flags is Mutable) Impure else exprPurity(vdef.rhs) `min` Pure

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,9 @@ class TreeMapWithImplicits extends tpd.TreeMap {
9494
inContext(localCtx) {
9595
cpy.DefDef(tree)(
9696
tree.name,
97-
transformSub(tree.tparams),
98-
tree.vparamss mapConserve (transformSub(_)),
97+
transformParamss(tree.paramss),
9998
transform(tree.tpt),
100-
transform(tree.rhs)(using nestedScopeCtx(tree.vparamss.flatten)))
99+
transform(tree.rhs)(using nestedScopeCtx(tree.paramss.flatten)))
101100
}
102101
case EmptyValDef =>
103102
tree

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,10 @@ class TreeTypeMap(
9090
tree1.withType(mapType(tree1.tpe)) match {
9191
case id: Ident if tpd.needsSelect(id.tpe) =>
9292
ref(id.tpe.asInstanceOf[TermRef]).withSpan(id.span)
93-
case ddef @ DefDef(name, tparams, vparamss, tpt, _) =>
94-
val (tmap1, tparams1) = transformDefs(tparams)
95-
val (tmap2, vparamss1) = tmap1.transformVParamss(vparamss)
96-
val res = cpy.DefDef(ddef)(name, tparams1, vparamss1, tmap2.transform(tpt), tmap2.transform(ddef.rhs))
97-
res.symbol.setParamssFromDefs(tparams1, vparamss1)
93+
case ddef @ DefDef(name, paramss, tpt, _) =>
94+
val (tmap1, paramss1) = transformAllParamss(paramss)
95+
val res = cpy.DefDef(ddef)(name, paramss1, tmap1.transform(tpt), tmap1.transform(ddef.rhs))
96+
res.symbol.setParamssFromDefs(paramss1)
9897
res.symbol.transformAnnotations {
9998
case ann: BodyAnnotation => ann.derivedAnnotation(transform(ann.tree))
10099
case ann => ann
@@ -139,14 +138,15 @@ class TreeTypeMap(
139138
(tmap, tmap.transformSub(trees))
140139
}
141140

142-
private def transformVParamss(vparamss: List[List[ValDef]]): (TreeTypeMap, List[List[ValDef]]) = vparamss match {
143-
case vparams :: rest =>
144-
val (tmap1, vparams1) = transformDefs(vparams)
145-
val (tmap2, vparamss2) = tmap1.transformVParamss(rest)
146-
(tmap2, vparams1 :: vparamss2)
141+
private def transformAllParamss(paramss: List[ParamClause]): (TreeTypeMap, List[ParamClause]) = paramss match
142+
case params :: paramss1 =>
143+
val (tmap1, params1: ParamClause) = (params: @unchecked) match
144+
case ValDefs(vparams) => transformDefs(vparams)
145+
case TypeDefs(tparams) => transformDefs(tparams)
146+
val (tmap2, paramss2) = tmap1.transformAllParamss(paramss1)
147+
(tmap2, params1 :: paramss2)
147148
case nil =>
148-
(this, vparamss)
149-
}
149+
(this, paramss)
150150

151151
def apply[ThisTree <: tpd.Tree](tree: ThisTree): ThisTree = transform(tree).asInstanceOf[ThisTree]
152152

0 commit comments

Comments
 (0)