From a92d64b02b59cc8250a14f33e7bb2ec895017ce1 Mon Sep 17 00:00:00 2001 From: Julien Dramaix Date: Fri, 8 Nov 2024 06:43:59 -0800 Subject: [PATCH] [KT] Fix the logic the get the constant value of a field in preparation of migrating to Kotlin 2.0.21. Kotlin 2.0.21 will erroneously resolved public static final fields from java as const val property breaking our current logic to determine if the a field is a compile time constant field. This has an impact on RTA and transitively to the J2CL minifier. References to these field won't be tracked by RTA and the J2CL minifier will remove the code for the field's getter and possibly the clinit of the enclosing class. This will break our kotlinjavainterop integration test when it runs in uncompiled mode. The extra logic for retrieving the constant value is intended to target only fields defined in Kotlin, so it's safe to simply test whether the field originates from Java code. Links to the Kotlin bug: https://youtrack.jetbrains.com/issue/KT-72960 PiperOrigin-RevId: 694479053 --- .../j2cl/transpiler/frontend/kotlin/KotlinEnvironment.kt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/transpiler/java/com/google/j2cl/transpiler/frontend/kotlin/KotlinEnvironment.kt b/transpiler/java/com/google/j2cl/transpiler/frontend/kotlin/KotlinEnvironment.kt index 09e285e53d..21d3534b91 100644 --- a/transpiler/java/com/google/j2cl/transpiler/frontend/kotlin/KotlinEnvironment.kt +++ b/transpiler/java/com/google/j2cl/transpiler/frontend/kotlin/KotlinEnvironment.kt @@ -574,7 +574,11 @@ class KotlinEnvironment( var constantValue = irField.constantValue()?.value?.let { Literal.fromValue(it, fieldTypeDescriptor) } - if (constantValue == null && irField.correspondingPropertySymbol?.owner?.isConst == true) { + if ( + constantValue == null && + !irField.isFromJava() && + irField.correspondingPropertySymbol?.owner?.isConst == true + ) { // In Kotlin, const val initialized to their default value does not have initializer and // `irField.constantValue()` returns null. In that case use the default value of the field // type.