Skip to content

Commit 6eb2935

Browse files
authored
Merge pull request github#9220 from smowton/smowton/fix/promoted-companion-object-fields
Associate certain companion object fields with the parent class
2 parents 2dcd7e1 + a204c74 commit 6eb2935

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ open class KotlinFileExtractor(
135135
Unit
136136
}
137137
is IrField -> {
138-
val parentId = useDeclarationParent(declaration.parent, false)?.cast<DbReftype>()
138+
val parentId = useDeclarationParent(getFieldParent(declaration), false)?.cast<DbReftype>()
139139
if (parentId != null) {
140140
extractField(declaration, parentId)
141141
}
@@ -829,10 +829,13 @@ open class KotlinFileExtractor(
829829
}
830830

831831
if (bf != null && extractBackingField) {
832-
val fieldId = extractField(bf, parentId)
833-
tw.writeKtPropertyBackingFields(id, fieldId)
834-
if (p.isDelegated) {
835-
tw.writeKtPropertyDelegates(id, fieldId)
832+
val fieldParentId = useDeclarationParent(getFieldParent(bf), false)
833+
if (fieldParentId != null) {
834+
val fieldId = extractField(bf, fieldParentId.cast())
835+
tw.writeKtPropertyBackingFields(id, fieldId)
836+
if (p.isDelegated) {
837+
tw.writeKtPropertyDelegates(id, fieldId)
838+
}
836839
}
837840
}
838841

java/kotlin-extractor/src/main/kotlin/KotlinUsesExtractor.kt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.semmle.extractor.java.OdasaOutput
66
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
77
import org.jetbrains.kotlin.backend.common.ir.allOverridden
88
import org.jetbrains.kotlin.backend.common.lower.parentsWithSelf
9+
import org.jetbrains.kotlin.backend.jvm.ir.getJvmNameFromAnnotation
910
import org.jetbrains.kotlin.backend.jvm.ir.propertyIfAccessor
1011
import org.jetbrains.kotlin.builtins.StandardNames
1112
import org.jetbrains.kotlin.descriptors.*
@@ -1268,9 +1269,22 @@ open class KotlinUsesExtractor(
12681269

12691270
fun useValueParameter(vp: IrValueParameter, parent: Label<out DbCallable>?): Label<out DbParam> =
12701271
tw.getLabelFor(getValueParameterLabel(vp, parent))
1272+
fun isDirectlyExposedCompanionObjectField(f: IrField) =
1273+
f.hasAnnotation(FqName("kotlin.jvm.JvmField")) ||
1274+
f.correspondingPropertySymbol?.owner?.let {
1275+
it.isConst || it.isLateinit
1276+
} ?: false
1277+
1278+
fun getFieldParent(f: IrField) =
1279+
f.parentClassOrNull?.let {
1280+
if (it.isCompanion && isDirectlyExposedCompanionObjectField(f))
1281+
it.parent
1282+
else
1283+
null
1284+
} ?: f.parent
12711285

12721286
fun getFieldLabel(f: IrField): String {
1273-
val parentId = useDeclarationParent(f.parent, false)
1287+
val parentId = useDeclarationParent(getFieldParent(f), false)
12741288
return "@\"field;{$parentId};${f.name.asString()}\""
12751289
}
12761290

0 commit comments

Comments
 (0)