Skip to content

Commit 9ea1395

Browse files
committed
Disambiguate the names and trap labels of backing fields of extension properties
1 parent 6eb2935 commit 9ea1395

File tree

5 files changed

+36
-2
lines changed

5 files changed

+36
-2
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,8 @@ open class KotlinFileExtractor(
759759
with("field", f) {
760760
DeclarationStackAdjuster(f).use {
761761
declarationStack.push(f)
762-
return extractField(useField(f), f.name.asString(), f.type, parentId, tw.getLocation(f), f.visibility, f, isExternalDeclaration(f), f.isFinal)
762+
val fNameSuffix = getExtensionReceiverType(f)?.let { it.classFqName?.asString()?.replace(".", "$$") } ?: ""
763+
return extractField(useField(f), "${f.name.asString()}$fNameSuffix", f.type, parentId, tw.getLocation(f), f.visibility, f, isExternalDeclaration(f), f.isFinal)
763764
}
764765
}
765766
}

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1269,6 +1269,7 @@ open class KotlinUsesExtractor(
12691269

12701270
fun useValueParameter(vp: IrValueParameter, parent: Label<out DbCallable>?): Label<out DbParam> =
12711271
tw.getLabelFor(getValueParameterLabel(vp, parent))
1272+
12721273
fun isDirectlyExposedCompanionObjectField(f: IrField) =
12731274
f.hasAnnotation(FqName("kotlin.jvm.JvmField")) ||
12741275
f.correspondingPropertySymbol?.owner?.let {
@@ -1283,9 +1284,20 @@ open class KotlinUsesExtractor(
12831284
null
12841285
} ?: f.parent
12851286

1287+
// Gets a field's corresponding property's extension receiver type, if any
1288+
fun getExtensionReceiverType(f: IrField) =
1289+
f.correspondingPropertySymbol?.owner?.let {
1290+
(it.getter ?: it.setter)?.extensionReceiverParameter?.type
1291+
}
1292+
12861293
fun getFieldLabel(f: IrField): String {
12871294
val parentId = useDeclarationParent(getFieldParent(f), false)
1288-
return "@\"field;{$parentId};${f.name.asString()}\""
1295+
// Distinguish backing fields of properties based on their extension receiver type;
1296+
// otherwise two extension properties declared in the same enclosing context will get
1297+
// clashing trap labels. These are always private, so we can just make up a label without
1298+
// worrying about their names as seen from Java.
1299+
val extensionPropertyDiscriminator = getExtensionReceiverType(f)?.let { "extension;${useType(it)}" } ?: ""
1300+
return "@\"field;{$parentId};${extensionPropertyDiscriminator}${f.name.asString()}\""
12891301
}
12901302

12911303
fun useField(f: IrField): Label<out DbField> =
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
| |
2+
| <clinit> |
3+
| A |
4+
| B |
5+
| get |
6+
| getX |
7+
| invoke |
8+
| x$delegatepackagename$$subpackagename$$A |
9+
| x$delegatepackagename$$subpackagename$$B |
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package packagename.subpackagename
2+
3+
public class A { }
4+
public class B { }
5+
6+
val A.x : String by lazy { "HelloA" }
7+
val B.x : String by lazy { "HelloB" }
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import java
2+
3+
from Class c
4+
where c.fromSource()
5+
select c.getAMember().toString()

0 commit comments

Comments
 (0)