Skip to content

Commit e5bf22e

Browse files
committed
Tighten check to cache Mirror.Sum in companion
cache the mirror in the companion if: - the class is not scala 2 defined & the class has a companion & the class was either: - defined in source - from tasty and its companion is a subtype of Mirror.Sum
1 parent 2fe2f03 commit e5bf22e

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

compiler/src/dotty/tools/dotc/transform/SymUtils.scala

+10-3
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,14 @@ object SymUtils:
112112
self.isCoDefinedGiven(res.typeSymbol)
113113
self.isAllOf(Given | Method) && isCodefined(self.info)
114114

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+
// self is from source, or companion is a subtype of Sum
120+
self.isDefinedInCurrentRun
121+
|| self.linkedClass.isSubClass(defn.Mirror_SumClass)
122+
)
116123

117124
/** Is this a sealed class or trait for which a sum mirror is generated?
118125
* It must satisfy the following conditions:
@@ -129,7 +136,7 @@ object SymUtils:
129136
s"it is not an abstract class"
130137
else {
131138
val children = self.children
132-
val companionMirror = self.useCompanionAsMirror
139+
val companionMirror = self.useCompanionAsSumMirror
133140
assert(!(companionMirror && (declScope ne self.linkedClass)))
134141
def problem(child: Symbol) = {
135142

@@ -144,7 +151,7 @@ object SymUtils:
144151
val s = child.whyNotGenericProduct
145152
if (s.isEmpty) s
146153
else if (child.is(Sealed)) {
147-
val s = child.whyNotGenericSum(if child.useCompanionAsMirror then child.linkedClass else ctx.owner)
154+
val s = child.whyNotGenericSum(if child.useCompanionAsSumMirror then child.linkedClass else ctx.owner)
148155
if (s.isEmpty) s
149156
else i"its child $child is not a generic sum because $s"
150157
} else i"its child $child is not a generic product because $s"

compiler/src/dotty/tools/dotc/typer/Synthesizer.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
271271

272272
private def sumMirror(mirroredType: Type, formal: Type, span: Span)(using Context): Tree =
273273
val cls = mirroredType.classSymbol
274-
val useCompanion = cls.useCompanionAsMirror
274+
val useCompanion = cls.useCompanionAsSumMirror
275275

276276
if cls.isGenericSum(if useCompanion then cls.linkedClass else ctx.owner) then
277277
val elemLabels = cls.children.map(c => ConstantType(Constant(c.name.toString)))

0 commit comments

Comments
 (0)