Skip to content

Commit f177478

Browse files
lunakolySpace Team
authored andcommitted
[Commonizer] Don't write protecteds of final classes into the metadata
They are inaccessible and provoke an error. See: `ClassifierCommonizationFromSourcesTest#testConstructors`. ^KT-28850
1 parent a21c6d3 commit f177478

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

native/commonizer/src/org/jetbrains/kotlin/commonizer/metadata/CirTreeSerializer.kt

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,23 +161,35 @@ private class CirTreeSerializationVisitor(
161161
nestedClass
162162
}
163163

164+
// `protected` members are prohibited inside final expect classes, as they can't be accessed.
165+
fun <T> T.takeIfNeeded(visibility: Visibility): T? = when {
166+
cirClass.modality == org.jetbrains.kotlin.descriptors.Modality.FINAL && visibility == Visibility.PROTECTED -> null
167+
else -> this
168+
}
169+
164170
val nestedConstructors: Collection<KmConstructor> = node.constructors.mapNotNull { (constructorKey, constructorNode) ->
165171
val constructorContext = classContext.callableMemberContext(DEFAULT_CONSTRUCTOR_NAME, classTypeParametersCount)
166-
val constructor: KmConstructor = constructorNode.accept(this, constructorContext)?.cast() ?: return@mapNotNull null
172+
val constructor: KmConstructor = (constructorNode.accept(this, constructorContext) as? KmConstructor)
173+
?.let { it.takeIfNeeded(it.visibility) }
174+
?: return@mapNotNull null
167175
statsCollector?.logClassConstructor(constructor, constructorContext, constructorKey)
168176
constructor
169177
}
170178

171179
val nestedFunctions: Collection<KmFunction> = node.functions.mapNotNull { (functionKey, functionNode) ->
172180
val functionContext = classContext.callableMemberContext(functionKey.name, classTypeParametersCount)
173-
val function: KmFunction = functionNode.accept(this, functionContext)?.cast() ?: return@mapNotNull null
181+
val function: KmFunction = (functionNode.accept(this, functionContext) as? KmFunction)
182+
?.let { it.takeIfNeeded(it.visibility) }
183+
?: return@mapNotNull null
174184
statsCollector?.logFunction(function, functionContext, functionKey)
175185
function
176186
}
177187

178188
val nestedProperties: Collection<KmProperty> = node.properties.mapNotNull { (propertyKey, propertyNode) ->
179189
val propertyContext = classContext.callableMemberContext(propertyKey.name, classTypeParametersCount)
180-
val property: KmProperty = propertyNode.accept(this, propertyContext)?.cast() ?: return@mapNotNull null
190+
val property: KmProperty = (propertyNode.accept(this, propertyContext) as? KmProperty)
191+
?.let { it.takeIfNeeded(it.visibility) }
192+
?: return@mapNotNull null
181193
statsCollector?.logProperty(propertyContext, propertyKey, propertyNode)
182194
property
183195
}

native/commonizer/testData/classifierCommonization/constructors/commonized/common/package_root.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ expect class A3
1010
expect class A4
1111
expect class A5
1212

13-
expect class B1 protected constructor(text: String) {
14-
protected constructor(number: Int)
15-
}
13+
expect class B1
1614

1715
expect class B2
1816
expect class B3

0 commit comments

Comments
 (0)