Skip to content

Commit de918fc

Browse files
hungvietnguyenSpace Team
authored and
Space Team
committed
IC: Fix regression in detecting constants in KotlinClassInfo.kt
A constant is a static final field with non-null value. In a previous commit (0b09be7), we accidentally removed the *non-null value* filter when looking for constants in the bytecode. This commit re-adds that filter to make sure the detection is correct. Test: Added KotlinOnlyClasspathChangesComputerTest.testDelegatedProperties ^KT-58986: Fixed (cherry picked from commit 0783561)
1 parent 5964edc commit de918fc

File tree

7 files changed

+47
-2
lines changed

7 files changed

+47
-2
lines changed

build-common/src/org/jetbrains/kotlin/incremental/KotlinClassInfo.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ private fun getExtraInfo(classHeader: KotlinClassHeader, classContents: ByteArra
180180

181181
val constantSnapshots: Map<String, Long> = classNode.fields.associate { fieldNode ->
182182
// Note: `fieldNode` is a constant because we kept only fields that are (non-private) constants in `classNode`
183-
fieldNode.name to (fieldNode.value?.let { ConstantValueExternalizer.toByteArray(it).hashToLong() } ?: 0L)
183+
fieldNode.name to ConstantValueExternalizer.toByteArray(fieldNode.value!!).hashToLong()
184184
}
185185

186186
val inlineFunctionOrAccessorSnapshots: Map<InlineFunctionOrAccessor, Long> = classNode.methods.associate { methodNode ->
@@ -210,7 +210,11 @@ class SelectiveClassVisitor(
210210
) : ClassVisitor(Opcodes.API_VERSION, cv) {
211211

212212
override fun visitField(access: Int, name: String, desc: String, signature: String?, value: Any?): FieldVisitor? {
213-
return if (shouldVisitField(JvmMemberSignature.Field(name, desc), access.isPrivate(), access.isStaticFinal())) {
213+
// Note: A constant's value must be not-null. A static final field with a `null` value at the bytecode declaration is not a constant
214+
// (whether the value is initialized later in the static initializer or not, it won't be inlined by the compiler).
215+
val isConstant = access.isStaticFinal() && value != null
216+
217+
return if (shouldVisitField(JvmMemberSignature.Field(name, desc), access.isPrivate(), isConstant)) {
214218
cv.visitField(access, name, desc, signature, value)
215219
} else null
216220
}

compiler/incremental-compilation-impl/test/org/jetbrains/kotlin/incremental/classpathDiff/ClasspathChangesComputerTest.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,19 @@ class KotlinOnlyClasspathChangesComputerTest : ClasspathChangesComputerTest() {
290290
).assertEquals(changes)
291291
}
292292

293+
/** Regression test for KT-58986.*/
294+
@Test
295+
fun testDelegatedProperties() {
296+
// Check that classpath changes computation doesn't fail
297+
val changes = computeClasspathChanges(File(testDataDir, "KotlinOnly/testDelegatedProperties/src"), tmpDir)
298+
Changes(
299+
lookupSymbols = setOf(
300+
LookupSymbol(name = "delegatedProperty", scope = "com.example"),
301+
),
302+
fqNames = setOf("com.example")
303+
).assertEquals(changes)
304+
}
305+
293306
/** Tests [SupertypesInheritorsImpact]. */
294307
@Test
295308
override fun testImpactComputation_SupertypesInheritors() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.example
2+
3+
import kotlin.reflect.KProperty
4+
5+
var delegatedProperty: String by Delegate() // Added
6+
7+
class Delegate {
8+
9+
operator fun getValue(thisRef: Any?, property: KProperty<*>): String {
10+
return "<Delegated>"
11+
}
12+
13+
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: String) {
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.example
2+
3+
import kotlin.reflect.KProperty
4+
5+
class Delegate {
6+
7+
operator fun getValue(thisRef: Any?, property: KProperty<*>): String {
8+
return "<Delegated>"
9+
}
10+
11+
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: String) {
12+
}
13+
}

0 commit comments

Comments
 (0)