@@ -296,18 +296,8 @@ object desugar {
296296 val isValueClass = parents.nonEmpty && isAnyVal(parents.head)
297297 // This is not watertight, but `extends AnyVal` will be replaced by `inline` later.
298298
299- lazy val reconstitutedTypeParams = reconstitutedEnumTypeParams(cdef.pos.startPos)
300-
301- val originalTparams =
302- if (isEnumCase && parents.isEmpty) {
303- if (constr1.tparams.nonEmpty) {
304- if (reconstitutedTypeParams.nonEmpty)
305- ctx.error(em " case with type parameters needs extends clause " , constr1.tparams.head.pos)
306- constr1.tparams
307- }
308- else reconstitutedTypeParams
309- }
310- else constr1.tparams
299+
300+ val originalTparams = constr1.tparams
311301 val originalVparamss = constr1.vparamss
312302 val constrTparams = originalTparams.map(toDefParam)
313303 val constrVparamss =
@@ -328,9 +318,9 @@ object desugar {
328318 case stat =>
329319 stat
330320 }
321+ def anyRef = ref(defn.AnyRefAlias .typeRef)
331322
332- val derivedTparams =
333- if (isEnumCase) constrTparams else constrTparams map derivedTypeParam
323+ val derivedTparams = constrTparams map derivedTypeParam
334324 val derivedVparamss = constrVparamss nestedMap derivedTermParam
335325 val arity = constrVparamss.head.length
336326
@@ -343,10 +333,23 @@ object desugar {
343333
344334 // a reference to the class type bound by `cdef`, with type parameters coming from the constructor
345335 val classTypeRef = appliedRef(classTycon)
346- // a reference to `enumClass`, with type parameters coming from the constructor
347- lazy val enumClassTypeRef =
348- if (reconstitutedTypeParams.isEmpty) enumClassRef
349- else appliedRef(enumClassRef)
336+
337+ // a reference to `enumClass`, with type parameters coming from the case constructor
338+ lazy val enumClassTypeRef = enumClass.primaryConstructor.info match {
339+ case info : PolyType =>
340+ if (constrTparams.isEmpty)
341+ interpolatedEnumParent(cdef.pos.startPos)
342+ else if ((constrTparams.corresponds(info.paramNames))((param, name) => param.name == name))
343+ appliedRef(enumClassRef)
344+ else {
345+ ctx.error(i " explicit extends clause needed because type parameters of case and enum class differ "
346+ , cdef.pos.startPos)
347+ AppliedTypeTree (enumClassRef, constrTparams map (_ => anyRef))
348+ .withPos(cdef.pos.startPos)
349+ }
350+ case _ =>
351+ enumClassRef
352+ }
350353
351354 // new C[Ts](paramss)
352355 lazy val creatorExpr = New (classTypeRef, constrVparamss nestedMap refOfDef)
@@ -399,8 +402,6 @@ object desugar {
399402 else Nil
400403 }
401404
402- def anyRef = ref(defn.AnyRefAlias .typeRef)
403-
404405 // Case classes and case objects get Product parents
405406 var parents1 = parents
406407 if (isEnumCase && parents.isEmpty)
0 commit comments