Skip to content

Commit

Permalink
[KT] Fix the logic the get the constant value of a field in preparati…
Browse files Browse the repository at this point in the history
…on 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
  • Loading branch information
jDramaix authored and copybara-github committed Nov 8, 2024
1 parent 45b1240 commit a92d64b
Showing 1 changed file with 5 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit a92d64b

Please sign in to comment.