@@ -527,6 +527,12 @@ object desugar {
527527 // a reference to the class type bound by `cdef`, with type parameters coming from the constructor
528528 val classTypeRef = appliedRef(classTycon)
529529
530+ def applyResultTpt =
531+ if isEnumCase then
532+ classTypeRef
533+ else
534+ TypeTree ()
535+
530536 // a reference to `enumClass`, with type parameters coming from the case constructor
531537 lazy val enumClassTypeRef =
532538 if (enumClass.typeParams.isEmpty)
@@ -605,7 +611,7 @@ object desugar {
605611 cpy.ValDef (vparam)(rhs = copyDefault(vparam)))
606612 val copyRestParamss = derivedVparamss.tail.nestedMap(vparam =>
607613 cpy.ValDef (vparam)(rhs = EmptyTree ))
608- DefDef (nme.copy, derivedTparams, copyFirstParams :: copyRestParamss, TypeTree () , creatorExpr)
614+ DefDef (nme.copy, derivedTparams, copyFirstParams :: copyRestParamss, applyResultTpt , creatorExpr)
609615 .withMods(Modifiers (Synthetic | constr1.mods.flags & copiedAccessFlags, constr1.mods.privateWithin)) :: Nil
610616 }
611617 }
@@ -656,15 +662,6 @@ object desugar {
656662 // For all other classes, the parent is AnyRef.
657663 val companions =
658664 if (isCaseClass) {
659- // The return type of the `apply` method, and an (empty or singleton) list
660- // of widening coercions
661- val (applyResultTpt, widenDefs) =
662- if (! isEnumCase)
663- (TypeTree (), Nil )
664- else if (parents.isEmpty || enumClass.typeParams.isEmpty)
665- (enumClassTypeRef, Nil )
666- else
667- enumApplyResult(cdef, parents, derivedEnumParams, appliedRef(enumClassRef, derivedEnumParams))
668665
669666 // true if access to the apply method has to be restricted
670667 // i.e. if the case class constructor is either private or qualified private
@@ -695,8 +692,6 @@ object desugar {
695692 then anyRef
696693 else
697694 constrVparamss.foldRight(classTypeRef)((vparams, restpe) => Function (vparams map (_.tpt), restpe))
698- def widenedCreatorExpr =
699- widenDefs.foldLeft(creatorExpr)((rhs, meth) => Apply (Ident (meth.name), rhs :: Nil ))
700695 val applyMeths =
701696 if (mods.is(Abstract )) Nil
702697 else {
@@ -709,9 +704,8 @@ object desugar {
709704 val appParamss =
710705 derivedVparamss.nestedZipWithConserve(constrVparamss)((ap, cp) =>
711706 ap.withMods(ap.mods | (cp.mods.flags & HasDefault )))
712- val app = DefDef (nme.apply, derivedTparams, appParamss, applyResultTpt, widenedCreatorExpr)
713- .withMods(appMods)
714- app :: widenDefs
707+ DefDef (nme.apply, derivedTparams, appParamss, applyResultTpt, creatorExpr)
708+ .withMods(appMods) :: Nil
715709 }
716710 val unapplyMeth = {
717711 val hasRepeatedParam = constrVparamss.head.exists {
@@ -720,7 +714,7 @@ object desugar {
720714 val methName = if (hasRepeatedParam) nme.unapplySeq else nme.unapply
721715 val unapplyParam = makeSyntheticParameter(tpt = classTypeRef)
722716 val unapplyRHS = if (arity == 0 ) Literal (Constant (true )) else Ident (unapplyParam.name)
723- val unapplyResTp = if (arity == 0 ) Literal (Constant (true )) else TypeTree ()
717+ val unapplyResTp = if (arity == 0 ) Literal (Constant (true )) else applyResultTpt
724718 DefDef (methName, derivedTparams, (unapplyParam :: Nil ) :: Nil , unapplyResTp, unapplyRHS)
725719 .withMods(synthetic)
726720 }
0 commit comments