@@ -112,7 +112,19 @@ object SymUtils:
112
112
self.isCoDefinedGiven(res.typeSymbol)
113
113
self.isAllOf(Given | Method ) && isCodefined(self.info)
114
114
115
- def useCompanionAsMirror (using Context ): Boolean = self.linkedClass.exists && ! self.is(Scala2x )
115
+ def useCompanionAsSumMirror (using Context ): Boolean =
116
+ self.linkedClass.exists
117
+ && ! self.is(Scala2x )
118
+ && (
119
+ // If the sum type is compiled from source, and `self` is a "generic sum"
120
+ // then its companion object will become a sum mirror in `posttyper`. (This method
121
+ // can be called from `typer` when summoning a Mirror.)
122
+ // However if `self` is from a prior run then we should check that its companion subclasses `Mirror.Sum`.
123
+ // e.g. before Scala 3.1, hierarchical sum types were not considered "generic sums", so their
124
+ // companion would not cache the mirror. Companions from TASTy will already be typed as `Mirror.Sum`.
125
+ self.isDefinedInCurrentRun
126
+ || self.linkedClass.isSubClass(defn.Mirror_SumClass )
127
+ )
116
128
117
129
/** Is this a sealed class or trait for which a sum mirror is generated?
118
130
* It must satisfy the following conditions:
@@ -129,7 +141,7 @@ object SymUtils:
129
141
s " it is not an abstract class "
130
142
else {
131
143
val children = self.children
132
- val companionMirror = self.useCompanionAsMirror
144
+ val companionMirror = self.useCompanionAsSumMirror
133
145
assert(! (companionMirror && (declScope ne self.linkedClass)))
134
146
def problem (child : Symbol ) = {
135
147
@@ -144,7 +156,7 @@ object SymUtils:
144
156
val s = child.whyNotGenericProduct
145
157
if (s.isEmpty) s
146
158
else if (child.is(Sealed )) {
147
- val s = child.whyNotGenericSum(if child.useCompanionAsMirror then child.linkedClass else ctx.owner)
159
+ val s = child.whyNotGenericSum(if child.useCompanionAsSumMirror then child.linkedClass else ctx.owner)
148
160
if (s.isEmpty) s
149
161
else i " its child $child is not a generic sum because $s"
150
162
} else i " its child $child is not a generic product because $s"
0 commit comments