@@ -280,22 +280,49 @@ private class StubRenderer(declarations: List<DeclarationModel>) {
280280 /* * Class qualified name -> its bound constructors, in table-key order; filled by [renderModuleDefs]. */
281281 private val constructorsByClass = sortedMapOf<String , MutableList <DeclarationModel >>()
282282
283+ private val supertypes: Map <String , List <String >> = bound
284+ .filter { it.returnSupertypes.isNotEmpty() }
285+ .groupBy { it.returnType.qualifiedName }
286+ .mapValues { (_, entries) -> entries.first().returnSupertypes }
287+
283288 /* *
284- * Module prefixes (every module and every directory above it) that name one directory on a
285- * case-insensitive filesystem together with a sibling (issue #44). In each such group the
286- * all-lower-case spelling keeps its path -- a Kotlin package -- and every other spelling is a
287- * class module that is emitted inside its parent package's `__init__.pyi` instead.
289+ * Module prefixes folded into their parent package's `__init__.pyi`.
290+ *
291+ * Two reasons fold a class module into its parent:
292+ * (a) Case-insensitive path collision with a sibling (issue #44): in each such group the
293+ * all-lower-case spelling keeps its path -- a Kotlin package -- and every other spelling
294+ * is a class module emitted inside its parent package's `__init__.pyi`.
295+ * (b) A receiver class (with instance methods or properties and a companion handle) whose companion
296+ * module only publishes constants (`Material3DesignSystem.Dark`), where the runtime
297+ * (`class_wins_over_module`, issue #322) serves the class on the parent package and the constants
298+ * on the class, provided `module` is not a supertype of another bound type (so an interface like
299+ * `DrawScope` stays a module while a concrete receiver class like `Material3DesignSystem` is folded).
288300 */
289301 private val foldedPrefixes: Set <String > = run {
290302 val prefixes = byModule.keys.flatMap { module ->
291303 val parts = module.split(' .' )
292304 parts.indices.map { parts.take(it + 1 ).joinToString(" ." ) }
293305 }.toSet()
294- prefixes.groupBy { it.lowercase() }.values
306+ val caseColliding = prefixes.groupBy { it.lowercase() }.values
295307 .filter { it.size > 1 }
296308 .flatten()
297309 .filter { ' .' in it && it.substringAfterLast(' .' ) != it.substringAfterLast(' .' ).lowercase() }
298310 .toSet()
311+ val allSupertypes = supertypes.values.flatten().toSet()
312+ val companionConstantClasses = byModule.keys.filter { module ->
313+ module !in allSupertypes &&
314+ module in companionHandles &&
315+ (module in methods || module in properties) &&
316+ byModule[module]?.all { it.kind == " STATIC_GETTER" } == true &&
317+ module.substringAfterLast(' .' ) !in takenNames[module.substringBeforeLast(' .' , " " )].orEmpty() &&
318+ run {
319+ val prefix = " $module ."
320+ bound.none { it.returnType.qualifiedName.startsWith(prefix) || it.owner.startsWith(prefix) } &&
321+ methods.keys.none { it.startsWith(prefix) } &&
322+ properties.keys.none { it.startsWith(prefix) }
323+ }
324+ }.toSet()
325+ caseColliding + companionConstantClasses
299326 }
300327
301328 /* * The shortest folded prefix of [module] (the module itself included), or `null` if it keeps its own path. */
@@ -307,11 +334,6 @@ private class StubRenderer(declarations: List<DeclarationModel>) {
307334 /* * The modules that are still files: a folded class module is a class, not a module, for `classReference`. */
308335 private val moduleNames: Set <String > = byModule.keys.filter { foldedPrefixOf(it) == null }.toSet()
309336
310- private val supertypes: Map <String , List <String >> = bound
311- .filter { it.returnSupertypes.isNotEmpty() }
312- .groupBy { it.returnType.qualifiedName }
313- .mapValues { (_, entries) -> entries.first().returnSupertypes }
314-
315337 // ----------------------------------------------------------------------- per-module output
316338
317339 private class ClassNode (val name : String ) {
0 commit comments