Skip to content

Commit fb09e82

Browse files
committed
Represent derives clauses in Template trees
with implementation of their desugaring
1 parent ae79a8a commit fb09e82

19 files changed

+315
-254
lines changed

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,8 @@ object desugar {
297297
/** The expansion of a class definition. See inline comments for what is involved */
298298
def classDef(cdef: TypeDef)(implicit ctx: Context): Tree = {
299299
val className = checkNotReservedName(cdef).asTypeName
300-
val impl @ Template(constr0, parents, self, _) = cdef.rhs
300+
val impl @ Template(_, _, self, _) = cdef.rhs
301+
val parents = impl.parents
301302
val mods = cdef.mods
302303
val companionMods = mods
303304
.withFlags((mods.flags & (AccessFlags | Final)).toCommonFlags)
@@ -312,7 +313,7 @@ object desugar {
312313
meth
313314
}
314315

315-
val constr1 = decompose(defDef(constr0, isPrimaryConstructor = true))
316+
val constr1 = decompose(defDef(impl.constr, isPrimaryConstructor = true))
316317

317318
// The original type and value parameters in the constructor already have the flags
318319
// needed to be type members (i.e. param, and possibly also private and local unless
@@ -557,12 +558,16 @@ object desugar {
557558
}
558559
def eqInstances = if (isEnum) eqInstance :: Nil else Nil
559560

561+
// derived type classes of non-module classes go to their companions
562+
val (clsDerived, companionDerived) =
563+
if (mods.is(Module)) (impl.derived, Nil) else (Nil, impl.derived)
564+
560565
// The thicket which is the desugared version of the companion object
561-
// synthetic object C extends parentTpt { defs }
566+
// synthetic object C extends parentTpt derives class-derived { defs }
562567
def companionDefs(parentTpt: Tree, defs: List[Tree]) =
563568
moduleDef(
564569
ModuleDef(
565-
className.toTermName, Template(emptyConstructor, parentTpt :: Nil, EmptyValDef, defs))
570+
className.toTermName, Template(emptyConstructor, parentTpt :: Nil, companionDerived, EmptyValDef, defs))
566571
.withMods(companionMods | Synthetic))
567572
.withSpan(cdef.span).toList
568573

@@ -613,10 +618,10 @@ object desugar {
613618
}
614619
companionDefs(companionParent, applyMeths ::: unapplyMeth :: companionMembers)
615620
}
616-
else if (companionMembers.nonEmpty)
621+
else if (companionMembers.nonEmpty || companionDerived.nonEmpty)
617622
companionDefs(anyRef, companionMembers)
618623
else if (isValueClass) {
619-
constr0.vparamss match {
624+
impl.constr.vparamss match {
620625
case (_ :: Nil) :: _ => companionDefs(anyRef, Nil)
621626
case _ => Nil // error will be emitted in typer
622627
}
@@ -675,7 +680,7 @@ object desugar {
675680
}
676681
cpy.TypeDef(cdef: TypeDef)(
677682
name = className,
678-
rhs = cpy.Template(impl)(constr, parents1, self1,
683+
rhs = cpy.Template(impl)(constr, parents1, clsDerived, self1,
679684
tparamAccessors ::: vparamAccessors ::: normalizedBody ::: caseClassMeths)): TypeDef
680685
}
681686

@@ -772,7 +777,7 @@ object desugar {
772777
val localType = tdef.withMods(Modifiers(Synthetic | Opaque).withPrivateWithin(tdef.name))
773778

774779
val companions = moduleDef(ModuleDef(
775-
moduleName, Template(emptyConstructor, Nil, EmptyValDef, localType :: Nil))
780+
moduleName, Template(emptyConstructor, Nil, Nil, EmptyValDef, localType :: Nil))
776781
.withFlags(Synthetic | Opaque))
777782
Thicket(aliasType :: companions.toList)
778783
}
@@ -1335,7 +1340,7 @@ object desugar {
13351340
val (classParents, self) =
13361341
if (parentCores.length == 1 && (parent.tpe eq parentCores.head)) (untpdParent :: Nil, EmptyValDef)
13371342
else (parentCores map TypeTree, ValDef(nme.WILDCARD, untpdParent, EmptyTree))
1338-
val impl = Template(emptyConstructor, classParents, self, refinements)
1343+
val impl = Template(emptyConstructor, classParents, Nil, self, refinements)
13391344
TypeDef(tpnme.REFINE_CLASS, impl).withFlags(Trait)
13401345
}
13411346

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ object DesugarEnums {
115115
val toStringDef =
116116
DefDef(nme.toString_, Nil, Nil, TypeTree(), Ident(nme.name))
117117
.withFlags(Override)
118-
def creator = New(Template(emptyConstructor, enumClassRef :: Nil, EmptyValDef,
118+
def creator = New(Template(emptyConstructor, enumClassRef :: Nil, Nil, EmptyValDef,
119119
List(enumTagDef, toStringDef) ++ registerCall))
120120
DefDef(nme.DOLLAR_NEW, Nil,
121121
List(List(param(nme.tag, defn.IntType), param(nme.name, defn.StringType))),
@@ -216,7 +216,7 @@ object DesugarEnums {
216216
if (!enumClass.exists) EmptyTree
217217
else if (enumClass.typeParams.nonEmpty) {
218218
val parent = interpolatedEnumParent(span)
219-
val impl = Template(emptyConstructor, parent :: Nil, EmptyValDef, Nil)
219+
val impl = Template(emptyConstructor, parent :: Nil, Nil, EmptyValDef, Nil)
220220
expandEnumModule(name, impl, mods, span)
221221
}
222222
else {

0 commit comments

Comments
 (0)