@@ -297,7 +297,8 @@ object desugar {
297
297
/** The expansion of a class definition. See inline comments for what is involved */
298
298
def classDef (cdef : TypeDef )(implicit ctx : Context ): Tree = {
299
299
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
301
302
val mods = cdef.mods
302
303
val companionMods = mods
303
304
.withFlags((mods.flags & (AccessFlags | Final )).toCommonFlags)
@@ -312,7 +313,7 @@ object desugar {
312
313
meth
313
314
}
314
315
315
- val constr1 = decompose(defDef(constr0 , isPrimaryConstructor = true ))
316
+ val constr1 = decompose(defDef(impl.constr , isPrimaryConstructor = true ))
316
317
317
318
// The original type and value parameters in the constructor already have the flags
318
319
// needed to be type members (i.e. param, and possibly also private and local unless
@@ -557,12 +558,16 @@ object desugar {
557
558
}
558
559
def eqInstances = if (isEnum) eqInstance :: Nil else Nil
559
560
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
+
560
565
// 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 }
562
567
def companionDefs (parentTpt : Tree , defs : List [Tree ]) =
563
568
moduleDef(
564
569
ModuleDef (
565
- className.toTermName, Template (emptyConstructor, parentTpt :: Nil , EmptyValDef , defs))
570
+ className.toTermName, Template (emptyConstructor, parentTpt :: Nil , companionDerived, EmptyValDef , defs))
566
571
.withMods(companionMods | Synthetic ))
567
572
.withSpan(cdef.span).toList
568
573
@@ -613,10 +618,10 @@ object desugar {
613
618
}
614
619
companionDefs(companionParent, applyMeths ::: unapplyMeth :: companionMembers)
615
620
}
616
- else if (companionMembers.nonEmpty)
621
+ else if (companionMembers.nonEmpty || companionDerived.nonEmpty )
617
622
companionDefs(anyRef, companionMembers)
618
623
else if (isValueClass) {
619
- constr0 .vparamss match {
624
+ impl.constr .vparamss match {
620
625
case (_ :: Nil ) :: _ => companionDefs(anyRef, Nil )
621
626
case _ => Nil // error will be emitted in typer
622
627
}
@@ -675,7 +680,7 @@ object desugar {
675
680
}
676
681
cpy.TypeDef (cdef : TypeDef )(
677
682
name = className,
678
- rhs = cpy.Template (impl)(constr, parents1, self1,
683
+ rhs = cpy.Template (impl)(constr, parents1, clsDerived, self1,
679
684
tparamAccessors ::: vparamAccessors ::: normalizedBody ::: caseClassMeths)): TypeDef
680
685
}
681
686
@@ -772,7 +777,7 @@ object desugar {
772
777
val localType = tdef.withMods(Modifiers (Synthetic | Opaque ).withPrivateWithin(tdef.name))
773
778
774
779
val companions = moduleDef(ModuleDef (
775
- moduleName, Template (emptyConstructor, Nil , EmptyValDef , localType :: Nil ))
780
+ moduleName, Template (emptyConstructor, Nil , Nil , EmptyValDef , localType :: Nil ))
776
781
.withFlags(Synthetic | Opaque ))
777
782
Thicket (aliasType :: companions.toList)
778
783
}
@@ -1335,7 +1340,7 @@ object desugar {
1335
1340
val (classParents, self) =
1336
1341
if (parentCores.length == 1 && (parent.tpe eq parentCores.head)) (untpdParent :: Nil , EmptyValDef )
1337
1342
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)
1339
1344
TypeDef (tpnme.REFINE_CLASS , impl).withFlags(Trait )
1340
1345
}
1341
1346
0 commit comments