Skip to content

Commit 3110a00

Browse files
committed
separate widenEnumClass logic to own method
1 parent db22123 commit 3110a00

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

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

+3-9
Original file line numberDiff line numberDiff line change
@@ -527,12 +527,6 @@ 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-
536530
// a reference to `enumClass`, with type parameters coming from the case constructor
537531
lazy val enumClassTypeRef =
538532
if (enumClass.typeParams.isEmpty)
@@ -611,7 +605,7 @@ object desugar {
611605
cpy.ValDef(vparam)(rhs = copyDefault(vparam)))
612606
val copyRestParamss = derivedVparamss.tail.nestedMap(vparam =>
613607
cpy.ValDef(vparam)(rhs = EmptyTree))
614-
DefDef(nme.copy, derivedTparams, copyFirstParams :: copyRestParamss, applyResultTpt, creatorExpr)
608+
DefDef(nme.copy, derivedTparams, copyFirstParams :: copyRestParamss, classTypeRef, creatorExpr)
615609
.withMods(Modifiers(Synthetic | constr1.mods.flags & copiedAccessFlags, constr1.mods.privateWithin)) :: Nil
616610
}
617611
}
@@ -704,7 +698,7 @@ object desugar {
704698
val appParamss =
705699
derivedVparamss.nestedZipWithConserve(constrVparamss)((ap, cp) =>
706700
ap.withMods(ap.mods | (cp.mods.flags & HasDefault)))
707-
DefDef(nme.apply, derivedTparams, appParamss, applyResultTpt, creatorExpr)
701+
DefDef(nme.apply, derivedTparams, appParamss, classTypeRef, creatorExpr)
708702
.withMods(appMods) :: Nil
709703
}
710704
val unapplyMeth = {
@@ -714,7 +708,7 @@ object desugar {
714708
val methName = if (hasRepeatedParam) nme.unapplySeq else nme.unapply
715709
val unapplyParam = makeSyntheticParameter(tpt = classTypeRef)
716710
val unapplyRHS = if (arity == 0) Literal(Constant(true)) else Ident(unapplyParam.name)
717-
val unapplyResTp = if (arity == 0) Literal(Constant(true)) else applyResultTpt
711+
val unapplyResTp = if arity == 0 then Literal(Constant(true)) else classTypeRef
718712
DefDef(methName, derivedTparams, (unapplyParam :: Nil) :: Nil, unapplyResTp, unapplyRHS)
719713
.withMods(synthetic)
720714
}

compiler/src/dotty/tools/dotc/core/ConstraintHandling.scala

+9-4
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,10 @@ trait ConstraintHandling {
346346
val tpw = tp.widenUnion
347347
if (tpw ne tp) && (tpw <:< bound) then tpw else tp
348348

349+
def widenEnum(tp: Type) =
350+
val tpw = tp.widenEnumClass
351+
if (tpw ne tp) && (tpw <:< bound) then tpw else tp
352+
349353
def widenSingle(tp: Type) =
350354
val tpw = tp.widenSingletons
351355
if (tpw ne tp) && (tpw <:< bound) then tpw else tp
@@ -354,12 +358,13 @@ trait ConstraintHandling {
354358
case WildcardType(optBounds) => optBounds.exists && isSingleton(optBounds.bounds.hi)
355359
case _ => isSubTypeWhenFrozen(tp, defn.SingletonType)
356360

357-
def isEnumBound(tp: Type): Boolean =
358-
bound.isValueType && bound.typeSymbol.is(Enum, butNot=JavaDefined) && tp <:< bound
361+
def isEnum(tp: Type): Boolean = tp match
362+
case WildcardType(optBounds) => optBounds.exists && isEnum(optBounds.bounds.hi)
363+
case _ => tp.typeSymbol.is(Enum, butNot=JavaDefined)
359364

360365
val wideInst =
361-
if isSingleton(bound) || isEnumBound(bound) then inst
362-
else dropSuperTraits(widenOr(widenSingle(inst)))
366+
if isSingleton(bound) || isEnum(bound) then inst
367+
else dropSuperTraits(widenOr(widenEnum(widenSingle(inst))))
363368
wideInst match
364369
case wideInst: TypeRef if wideInst.symbol.is(Module) =>
365370
TermRef(wideInst.prefix, wideInst.symbol.sourceModule)

compiler/src/dotty/tools/dotc/core/Types.scala

+7
Original file line numberDiff line numberDiff line change
@@ -1173,6 +1173,13 @@ object Types {
11731173
tp
11741174
}
11751175

1176+
def widenEnumClass(using Context): Type = dealias match {
1177+
case tp: (TypeRef | AppliedType) if tp.typeSymbol.isAllOf(EnumCase) =>
1178+
tp.parents.head
1179+
case _ =>
1180+
this
1181+
}
1182+
11761183
/** Widen all top-level singletons reachable by dealiasing
11771184
* and going to the operands of & and |.
11781185
* Overridden and cached in OrType.

0 commit comments

Comments
 (0)