[JVM_IR] Limit inner class attributes to types in class file (#5081)

* [JVM_IR] Limit inner class attributes to types in class file

Inner class attributes should only be recorded for types that
are materialized in the result class file. In particular, we
should not emit inner classes attributes for types that appear
only in fake overrides. We do map these types to track the
fake overrides for JVM signature clashes but they are not
materialized in the class file.

^KT-56104 Fixed

* [JVM_IR] Consistently pass around materialized boolean in mapType.
This commit is contained in:
Mads Sig Ager
2023-01-30 14:11:07 +01:00
committed by GitHub
parent c9aeadd31f
commit 7c3cc1d5c1
8 changed files with 86 additions and 42 deletions
@@ -28,15 +28,16 @@ interface TypeMappingContext<Writer : JvmDescriptorTypeWriter<Type>> {
object AbstractTypeMapper {
fun <Writer : JvmDescriptorTypeWriter<Type>> mapClass(
context: TypeMappingContext<Writer>,
typeConstructor: TypeConstructorMarker
typeConstructor: TypeConstructorMarker,
materialized: Boolean
): Type {
return with(context.typeContext) {
when {
typeConstructor.isClassTypeConstructor() -> {
mapType(context, typeConstructor.defaultType(), TypeMappingMode.CLASS_DECLARATION)
mapType(context, typeConstructor.defaultType(), TypeMappingMode.CLASS_DECLARATION, materialized = materialized)
}
typeConstructor.isTypeParameter() -> {
mapType(context, typeConstructor.defaultType())
mapType(context, typeConstructor.defaultType(), materialized = materialized)
}
else -> error("Unknown type constructor: $typeConstructor")
}
@@ -47,15 +48,17 @@ object AbstractTypeMapper {
context: TypeMappingContext<Writer>,
type: KotlinTypeMarker,
mode: TypeMappingMode = TypeMappingMode.DEFAULT,
sw: Writer? = null
): Type = context.typeContext.mapType(context, type, mode, sw)
sw: Writer? = null,
materialized: Boolean = true,
): Type = context.typeContext.mapType(context, type, mode, sw, materialized)
// NB: The counterpart, [descriptorBasedTypeSignatureMapping#mapType] doesn't have restriction on [type].
private fun <Writer : JvmDescriptorTypeWriter<Type>> TypeSystemCommonBackendContextForTypeMapping.mapType(
context: TypeMappingContext<Writer>,
type: KotlinTypeMarker,
mode: TypeMappingMode = TypeMappingMode.DEFAULT,
sw: Writer? = null
mode: TypeMappingMode,
sw: Writer?,
materialized: Boolean,
): Type {
if (type.isError()) {
val name = type.getNameForErrorType() ?: NON_EXISTENT_CLASS_NAME
@@ -75,15 +78,15 @@ object AbstractTypeMapper {
}
if (type.isSuspendFunction()) {
return mapSuspendFunctionType(type, context, mode, sw)
return mapSuspendFunctionType(type, context, mode, sw, materialized)
}
if (type.isArrayOrNullableArray()) {
return mapArrayType(type, sw, context, mode)
return mapArrayType(type, sw, context, mode, materialized)
}
if (typeConstructor.isClassTypeConstructor()) {
return mapClassType(typeConstructor, mode, type, context, sw)
return mapClassType(typeConstructor, mode, type, context, sw, materialized)
}
}
@@ -97,17 +100,17 @@ object AbstractTypeMapper {
upperBound.makeNullable()
else upperBound
val asmType = mapType(context, newType, mode, null)
val asmType = mapType(context, newType, mode, null, materialized)
sw?.writeTypeVariable(typeParameter.getName(), asmType)
asmType
}
type.isFlexible() -> {
mapType(context, type.upperBoundIfFlexible(), mode, sw)
mapType(context, type.upperBoundIfFlexible(), mode, sw, materialized)
}
type is DefinitelyNotNullTypeMarker ->
mapType(context, type.original(), mode, sw)
mapType(context, type.original(), mode, sw, materialized)
typeConstructor.isScript() ->
Type.getObjectType(context.getScriptInternalName(typeConstructor)).let {
@@ -124,7 +127,8 @@ object AbstractTypeMapper {
type: SimpleTypeMarker,
context: TypeMappingContext<Writer>,
mode: TypeMappingMode,
sw: Writer?
sw: Writer?,
materialized: Boolean,
): Type {
val argumentsCount = type.argumentsCount()
val argumentsList = type.asArgumentList()
@@ -136,14 +140,15 @@ object AbstractTypeMapper {
this += nullableAnyType()
}
val runtimeFunctionType = functionNTypeConstructor(arguments.size - 1).typeWithArguments(arguments)
return mapType(context, runtimeFunctionType, mode, sw)
return mapType(context, runtimeFunctionType, mode, sw, materialized)
}
private fun <Writer : JvmDescriptorTypeWriter<Type>> TypeSystemCommonBackendContextForTypeMapping.mapArrayType(
type: SimpleTypeMarker,
sw: Writer?,
context: TypeMappingContext<Writer>,
mode: TypeMappingMode
mode: TypeMappingMode,
materialized: Boolean
): Type {
val typeArgument = type.asArgumentList()[0]
val (variance, memberType) = when {
@@ -158,7 +163,7 @@ object AbstractTypeMapper {
arrayElementType = AsmTypes.OBJECT_TYPE
sw?.writeClass(arrayElementType)
} else {
arrayElementType = mapType(context, memberType, mode.toGenericArgumentMode(variance, ofArray = true), sw)
arrayElementType = mapType(context, memberType, mode.toGenericArgumentMode(variance, ofArray = true), sw, materialized)
}
sw?.writeArrayEnd()
return AsmUtil.getArrayType(arrayElementType)
@@ -169,13 +174,14 @@ object AbstractTypeMapper {
mode: TypeMappingMode,
type: SimpleTypeMarker,
context: TypeMappingContext<Writer>,
sw: Writer?
sw: Writer?,
materialized: Boolean
): Type {
if (typeConstructor.isInlineClass() && !mode.needInlineClassWrapping) {
val expandedType = computeExpandedTypeForInlineClass(type)
require(expandedType is SimpleTypeMarker?)
if (expandedType != null) {
return mapType(context, expandedType, mode.wrapInlineClassesMode(), sw)
return mapType(context, expandedType, mode.wrapInlineClassesMode(), sw, materialized)
}
}