@@ -87,23 +87,28 @@ object SymUtils:
8787
8888 def isGenericProduct (using Context ): Boolean = whyNotGenericProduct.isEmpty
8989
90+ def useCompanionAsMirror (using Context ): Boolean = self.linkedClass.exists && ! self.is(Scala2x )
91+
9092 /** Is this a sealed class or trait for which a sum mirror is generated?
9193 * It must satisfy the following conditions:
9294 * - it has at least one child class or object
9395 * - none of its children are anonymous classes
94- * - all of its children are addressable through a path from its companion object
96+ * - all of its children are addressable through a path from the parent class
97+ * and also the location of the generated mirror.
9598 * - all of its children are generic products or singletons
9699 */
97- def whyNotGenericSum (using Context ): String =
100+ def whyNotGenericSum (declScope : Symbol )( using Context ): String =
98101 if (! self.is(Sealed ))
99102 s " it is not a sealed ${self.kindString}"
100103 else {
101104 val children = self.children
102- val companion = self.linkedClass
105+ val companionMirror = self.useCompanionAsMirror
106+ assert(! (companionMirror && (declScope ne self.linkedClass)))
103107 def problem (child : Symbol ) = {
104108
105109 def isAccessible (sym : Symbol ): Boolean =
106- companion.isContainedIn(sym) || sym.is(Module ) && isAccessible(sym.owner)
110+ (self.isContainedIn(sym) && (companionMirror || declScope.isContainedIn(sym)))
111+ || sym.is(Module ) && isAccessible(sym.owner)
107112
108113 if (child == self) " it has anonymous or inaccessible subclasses"
109114 else if (! isAccessible(child.owner)) i " its child $child is not accessible "
@@ -118,7 +123,7 @@ object SymUtils:
118123 else children.map(problem).find(! _.isEmpty).getOrElse(" " )
119124 }
120125
121- def isGenericSum (using Context ): Boolean = whyNotGenericSum.isEmpty
126+ def isGenericSum (declScope : Symbol )( using Context ): Boolean = whyNotGenericSum(declScope) .isEmpty
122127
123128 /** If this is a constructor, its owner: otherwise this. */
124129 final def skipConstructor (using Context ): Symbol =
0 commit comments