@@ -334,12 +334,19 @@ object desugar {
334
334
335
335
// a reference to the class type bound by `cdef`, with type parameters coming from the constructor
336
336
val classTypeRef = appliedRef(classTycon)
337
- // a refereence to `enumClass`, with type parameters coming from the constructor
337
+ // a reference to `enumClass`, with type parameters coming from the constructor
338
338
lazy val enumClassTypeRef = appliedRef(enumClassRef)
339
339
340
340
// new C[Ts](paramss)
341
341
lazy val creatorExpr = New (classTypeRef, constrVparamss nestedMap refOfDef)
342
342
343
+ // The return type of the `apply` and `copy` methods
344
+ val applyResultTpt =
345
+ if (isEnumCase)
346
+ if (parents.isEmpty) enumClassTypeRef
347
+ else parents.head
348
+ else TypeTree ()
349
+
343
350
// Methods to add to a case class C[..](p1: T1, ..., pN: Tn)(moreParams)
344
351
// def isDefined = true
345
352
// def productArity = N
@@ -380,7 +387,7 @@ object desugar {
380
387
cpy.ValDef (vparam)(rhs = copyDefault(vparam)))
381
388
val copyRestParamss = derivedVparamss.tail.nestedMap(vparam =>
382
389
cpy.ValDef (vparam)(rhs = EmptyTree ))
383
- DefDef (nme.copy, derivedTparams, copyFirstParams :: copyRestParamss, TypeTree () , creatorExpr)
390
+ DefDef (nme.copy, derivedTparams, copyFirstParams :: copyRestParamss, applyResultTpt , creatorExpr)
384
391
.withMods(synthetic) :: Nil
385
392
}
386
393
@@ -430,15 +437,15 @@ object desugar {
430
437
constrVparamss.length > 1 ||
431
438
mods.is(Abstract ) ||
432
439
constr.mods.is(Private )) anyRef
440
+ else
433
441
// todo: also use anyRef if constructor has a dependent method type (or rule that out)!
434
- else (constrVparamss :\ classTypeRef) ((vparams, restpe) => Function (vparams map (_.tpt), restpe))
442
+ (constrVparamss :\ (if (isEnumCase) applyResultTpt else classTypeRef)) (
443
+ (vparams, restpe) => Function (vparams map (_.tpt), restpe))
435
444
val applyMeths =
436
445
if (mods is Abstract ) Nil
437
- else {
438
- val restpe = if (isEnumCase) enumClassTypeRef else TypeTree ()
439
- DefDef (nme.apply, derivedTparams, derivedVparamss, restpe, creatorExpr)
446
+ else
447
+ DefDef (nme.apply, derivedTparams, derivedVparamss, applyResultTpt, creatorExpr)
440
448
.withFlags(Synthetic | (constr1.mods.flags & DefaultParameterized )) :: Nil
441
- }
442
449
val unapplyMeth = {
443
450
val unapplyParam = makeSyntheticParameter(tpt = classTypeRef)
444
451
val unapplyRHS = if (arity == 0 ) Literal (Constant (true )) else Ident (unapplyParam.name)
@@ -505,7 +512,10 @@ object desugar {
505
512
case _ =>
506
513
}
507
514
508
- flatTree(cdef1 :: companions ::: implicitWrappers)
515
+ val result = val flatTree(cdef1 :: companions ::: implicitWrappers)
516
+ // if (isEnum) println(i"enum $cdef\n --->\n$result")
517
+ // if (isEnumCase) println(i"enum case $cdef\n --->\n$result")
518
+ result
509
519
}
510
520
511
521
val AccessOrSynthetic = AccessFlags | Synthetic
0 commit comments