Skip to content

Commit 5958143

Browse files
tamasvajksmowton
authored andcommitted
Fix colliding property accessor and function names
1 parent a51c2c4 commit 5958143

File tree

6 files changed

+33
-50
lines changed

6 files changed

+33
-50
lines changed

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -726,8 +726,26 @@ open class KotlinUsesExtractor(
726726
}
727727

728728
when (f) {
729-
getter -> return FunctionNames(getJvmName(getter) ?: JvmAbi.getterName(propName), JvmAbi.getterName(propName))
730-
setter -> return FunctionNames(getJvmName(setter) ?: JvmAbi.setterName(propName), JvmAbi.setterName(propName))
729+
getter -> {
730+
val defaultFunctionName = JvmAbi.getterName(propName)
731+
val defaultDbName = if (getter.visibility == DescriptorVisibilities.PRIVATE && getter.origin == IrDeclarationOrigin.DEFAULT_PROPERTY_ACCESSOR) {
732+
// In JVM these functions don't exist, instead the backing field is accessed directly
733+
defaultFunctionName + "\$private"
734+
} else {
735+
defaultFunctionName
736+
}
737+
return FunctionNames(getJvmName(getter) ?: defaultDbName, defaultFunctionName)
738+
}
739+
setter -> {
740+
val defaultFunctionName = JvmAbi.setterName(propName)
741+
val defaultDbName = if (setter.visibility == DescriptorVisibilities.PRIVATE && setter.origin == IrDeclarationOrigin.DEFAULT_PROPERTY_ACCESSOR) {
742+
// In JVM these functions don't exist, instead the backing field is accessed directly
743+
defaultFunctionName + "\$private"
744+
} else {
745+
defaultFunctionName
746+
}
747+
return FunctionNames(getJvmName(setter) ?: defaultDbName, defaultFunctionName)
748+
}
731749
else -> {
732750
logger.error(
733751
"Function has a corresponding property, but is neither the getter nor the setter"

java/ql/test/kotlin/library-tests/properties/CONSISTENCY/children.expected

Lines changed: 0 additions & 4 deletions
This file was deleted.

java/ql/test/kotlin/library-tests/properties/CONSISTENCY/toString.expected

Lines changed: 0 additions & 1 deletion
This file was deleted.

java/ql/test/kotlin/library-tests/properties/DB-CHECK.expected

Lines changed: 0 additions & 40 deletions
This file was deleted.

java/ql/test/kotlin/library-tests/properties/properties.expected

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ fieldDeclarations
2121
| properties.kt:67:1:67:23 | int constVal; | properties.kt:67:1:67:23 | constVal | 0 |
2222
| properties.kt:70:5:70:16 | int prop; | properties.kt:70:5:70:16 | prop | 0 |
2323
| properties.kt:84:5:84:29 | int data; | properties.kt:84:5:84:29 | data | 0 |
24+
| properties.kt:92:5:93:18 | int data; | properties.kt:92:5:93:18 | data | 0 |
2425
#select
2526
| properties.kt:2:27:2:50 | constructorProp | properties.kt:2:27:2:50 | getConstructorProp | file://:0:0:0:0 | <none> | properties.kt:2:27:2:50 | constructorProp | public |
2627
| properties.kt:2:53:2:83 | mutableConstructorProp | properties.kt:2:53:2:83 | getMutableConstructorProp | properties.kt:2:53:2:83 | setMutableConstructorProp | properties.kt:2:53:2:83 | mutableConstructorProp | public |
@@ -41,13 +42,13 @@ fieldDeclarations
4142
| properties.kt:30:5:31:29 | overrideGetterUseField | properties.kt:31:13:31:29 | getOverrideGetterUseField | properties.kt:30:5:31:29 | setOverrideGetterUseField | properties.kt:30:5:31:29 | overrideGetterUseField | public |
4243
| properties.kt:32:5:33:29 | useField | properties.kt:33:13:33:29 | getUseField | file://:0:0:0:0 | <none> | properties.kt:32:5:33:29 | useField | public |
4344
| properties.kt:34:5:34:36 | lateInitVar | properties.kt:34:14:34:36 | getLateInitVar | properties.kt:34:14:34:36 | setLateInitVar | properties.kt:34:5:34:36 | lateInitVar | public |
44-
| properties.kt:35:5:35:32 | privateProp | properties.kt:35:13:35:32 | getPrivateProp | file://:0:0:0:0 | <none> | properties.kt:35:5:35:32 | privateProp | private |
45+
| properties.kt:35:5:35:32 | privateProp | properties.kt:35:13:35:32 | getPrivateProp$private | file://:0:0:0:0 | <none> | properties.kt:35:5:35:32 | privateProp | private |
4546
| properties.kt:36:5:36:36 | protectedProp | properties.kt:36:15:36:36 | getProtectedProp | file://:0:0:0:0 | <none> | properties.kt:36:5:36:36 | protectedProp | protected |
4647
| properties.kt:37:5:37:30 | publicProp | properties.kt:37:12:37:30 | getPublicProp | file://:0:0:0:0 | <none> | properties.kt:37:5:37:30 | publicProp | public |
4748
| properties.kt:38:5:38:34 | internalProp | properties.kt:38:14:38:34 | getInternalProp | file://:0:0:0:0 | <none> | properties.kt:38:5:38:34 | internalProp | internal |
4849
| properties.kt:67:1:67:23 | constVal | properties.kt:67:7:67:23 | getConstVal | file://:0:0:0:0 | <none> | properties.kt:67:1:67:23 | constVal | public |
4950
| properties.kt:70:5:70:16 | prop | properties.kt:70:5:70:16 | getProp | file://:0:0:0:0 | <none> | properties.kt:70:5:70:16 | prop | public |
5051
| properties.kt:78:1:79:13 | x | properties.kt:79:5:79:13 | getX | file://:0:0:0:0 | <none> | file://:0:0:0:0 | <none> | public |
5152
| properties.kt:80:1:81:13 | x | properties.kt:81:5:81:13 | getX | file://:0:0:0:0 | <none> | file://:0:0:0:0 | <none> | public |
52-
| properties.kt:84:5:84:29 | data | properties.kt:84:13:84:29 | getData | properties.kt:84:13:84:29 | setData | properties.kt:84:5:84:29 | data | private |
53-
| properties.kt:84:5:84:29 | data | properties.kt:84:13:84:29 | getData | properties.kt:86:5:88:5 | setData | properties.kt:84:5:84:29 | data | private |
53+
| properties.kt:84:5:84:29 | data | properties.kt:84:13:84:29 | getData$private | properties.kt:84:13:84:29 | setData$private | properties.kt:84:5:84:29 | data | private |
54+
| properties.kt:92:5:93:18 | data | properties.kt:93:9:93:18 | getData | properties.kt:92:13:93:18 | setData$private | properties.kt:92:5:93:18 | data | private |

java/ql/test/kotlin/library-tests/properties/properties.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,12 @@ class A {
8787
data = p
8888
}
8989
}
90+
91+
class B {
92+
private var data: Int = 5
93+
get() = 42
94+
95+
fun setData(p: Int) {
96+
data = p
97+
}
98+
}

0 commit comments

Comments
 (0)