From 00f323c8bd75e521a0a1462536fae3ce143967ee Mon Sep 17 00:00:00 2001 From: Chris Smowton Date: Tue, 6 Dec 2022 18:34:28 +0000 Subject: [PATCH] Fix: extract directly exposed fields with `static` modifier --- .../src/main/kotlin/KotlinFileExtractor.kt | 13 ++++++++----- .../src/main/kotlin/KotlinUsesExtractor.kt | 6 ++++-- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt b/java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt index 33e1fa2fb11..093967a68c3 100644 --- a/java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt +++ b/java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt @@ -1262,13 +1262,13 @@ open class KotlinFileExtractor( DeclarationStackAdjuster(f).use { val fNameSuffix = getExtensionReceiverType(f)?.let { it.classFqName?.asString()?.replace(".", "$$") } ?: "" val extractType = if (isAnnotationClassField(f)) kClassToJavaClass(f.type) else f.type - return extractField(useField(f), "${f.name.asString()}$fNameSuffix", extractType, parentId, tw.getLocation(f), f.visibility, f, isExternalDeclaration(f), f.isFinal) + return extractField(useField(f), "${f.name.asString()}$fNameSuffix", extractType, parentId, tw.getLocation(f), f.visibility, f, isExternalDeclaration(f), f.isFinal, isDirectlyExposedCompanionObjectField(f)) } } } - private fun extractField(id: Label, name: String, type: IrType, parentId: Label, locId: Label, visibility: DescriptorVisibility, errorElement: IrElement, isExternalDeclaration: Boolean, isFinal: Boolean): Label { + private fun extractField(id: Label, name: String, type: IrType, parentId: Label, locId: Label, visibility: DescriptorVisibility, errorElement: IrElement, isExternalDeclaration: Boolean, isFinal: Boolean, isStatic: Boolean): Label { val t = useType(type) tw.writeFields(id, name, t.javaResult.id, parentId, id) tw.writeFieldsKotlinType(id, t.kotlinResult.id) @@ -1278,6 +1278,9 @@ open class KotlinFileExtractor( if (isFinal) { addModifiers(id, "final") } + if (isStatic) { + addModifiers(id, "static") + } if (!isExternalDeclaration) { val fieldDeclarationId = tw.getFreshIdLabel() @@ -4209,12 +4212,12 @@ open class KotlinFileExtractor( val firstAssignmentStmtIdx = 1 if (dispatchReceiverInfo != null) { - extractField(dispatchReceiverInfo.field, "", dispatchReceiverInfo.type, classId, locId, DescriptorVisibilities.PRIVATE, callableReferenceExpr, isExternalDeclaration = false, isFinal = true) + extractField(dispatchReceiverInfo.field, "", dispatchReceiverInfo.type, classId, locId, DescriptorVisibilities.PRIVATE, callableReferenceExpr, isExternalDeclaration = false, isFinal = true, isStatic = false) extractParameterToFieldAssignmentInConstructor("", dispatchReceiverInfo.type, dispatchReceiverInfo.field, 0 + dispatchReceiverInfo.indexOffset, firstAssignmentStmtIdx + dispatchReceiverInfo.indexOffset) } if (extensionReceiverInfo != null) { - extractField(extensionReceiverInfo.field, "", extensionReceiverInfo.type, classId, locId, DescriptorVisibilities.PRIVATE, callableReferenceExpr, isExternalDeclaration = false, isFinal = true) + extractField(extensionReceiverInfo.field, "", extensionReceiverInfo.type, classId, locId, DescriptorVisibilities.PRIVATE, callableReferenceExpr, isExternalDeclaration = false, isFinal = true, isStatic = false) extractParameterToFieldAssignmentInConstructor( "", extensionReceiverInfo.type, extensionReceiverInfo.field, 0 + extensionReceiverInfo.indexOffset, firstAssignmentStmtIdx + extensionReceiverInfo.indexOffset) } } @@ -5282,7 +5285,7 @@ open class KotlinFileExtractor( // add field val fieldId = tw.getFreshIdLabel() - extractField(fieldId, "", functionType, classId, locId, DescriptorVisibilities.PRIVATE, e, isExternalDeclaration = false, isFinal = true) + extractField(fieldId, "", functionType, classId, locId, DescriptorVisibilities.PRIVATE, e, isExternalDeclaration = false, isFinal = true, isStatic = false) // adjust constructor helper.extractParameterToFieldAssignmentInConstructor("", functionType, fieldId, 0, 1) diff --git a/java/kotlin-extractor/src/main/kotlin/KotlinUsesExtractor.kt b/java/kotlin-extractor/src/main/kotlin/KotlinUsesExtractor.kt index 0fc33fc2a64..dbacf42a192 100644 --- a/java/kotlin-extractor/src/main/kotlin/KotlinUsesExtractor.kt +++ b/java/kotlin-extractor/src/main/kotlin/KotlinUsesExtractor.kt @@ -1649,7 +1649,7 @@ open class KotlinUsesExtractor( fun useValueParameter(vp: IrValueParameter, parent: Label?): Label = tw.getLabelFor(getValueParameterLabel(vp, parent)) - private fun isDirectlyExposedCompanionObjectField(f: IrField) = + private fun isDirectlyExposableCompanionObjectField(f: IrField) = f.hasAnnotation(FqName("kotlin.jvm.JvmField")) || f.correspondingPropertySymbol?.owner?.let { it.isConst || it.isLateinit @@ -1657,12 +1657,14 @@ open class KotlinUsesExtractor( fun getFieldParent(f: IrField) = f.parentClassOrNull?.let { - if (it.isCompanion && isDirectlyExposedCompanionObjectField(f)) + if (it.isCompanion && isDirectlyExposableCompanionObjectField(f)) it.parent else null } ?: f.parent + fun isDirectlyExposedCompanionObjectField(f: IrField) = getFieldParent(f) != f.parent + // Gets a field's corresponding property's extension receiver type, if any fun getExtensionReceiverType(f: IrField) = f.correspondingPropertySymbol?.owner?.let {