diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/PropertyCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/PropertyCodegen.java index e5766f9838f..cc3c0046a4b 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/PropertyCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/PropertyCodegen.java @@ -16,6 +16,7 @@ import org.jetbrains.kotlin.codegen.context.MultifileClassFacadeContext; import org.jetbrains.kotlin.codegen.context.MultifileClassPartContext; import org.jetbrains.kotlin.codegen.state.GenerationState; import org.jetbrains.kotlin.codegen.state.KotlinTypeMapper; +import org.jetbrains.kotlin.config.LanguageFeature; import org.jetbrains.kotlin.descriptors.*; import org.jetbrains.kotlin.descriptors.annotations.Annotations; import org.jetbrains.kotlin.load.java.DescriptorsJvmAbiUtil; @@ -343,7 +344,7 @@ public class PropertyCodegen { defaultValue = null; } else if (Boolean.TRUE.equals(bindingContext.get(BindingContext.BACKING_FIELD_REQUIRED, descriptor))) { - if (descriptor.isConst()) { + if (shouldWriteFieldInitializer(descriptor)) { ConstantValue initializer = descriptor.getCompileTimeInitializer(); defaultValue = initializer == null ? null : initializer.getValue(); } @@ -487,6 +488,20 @@ public class PropertyCodegen { return delegateType; } + private boolean shouldWriteFieldInitializer(@NotNull PropertyDescriptor descriptor) { + if (!descriptor.isConst() && + state.getLanguageVersionSettings().supportsFeature(LanguageFeature.NoConstantValueAttributeForNonConstVals)) { + return false; + } + + //final field of primitive or String type + if (!descriptor.isVar()) { + Type type = typeMapper.mapType(descriptor); + return AsmUtil.isPrimitive(type) || "java.lang.String".equals(type.getClassName()); + } + return false; + } + private void generateGetter(@NotNull PropertyDescriptor descriptor, @Nullable KtPropertyAccessor getter) { generateAccessor( getter, diff --git a/compiler/fir/analysis-tests/legacy-fir-tests/tests-gen/org/jetbrains/kotlin/fir/FirLoadCompiledKotlinGenerated.java b/compiler/fir/analysis-tests/legacy-fir-tests/tests-gen/org/jetbrains/kotlin/fir/FirLoadCompiledKotlinGenerated.java index cd0037a6fb6..cb2be00ecdf 100644 --- a/compiler/fir/analysis-tests/legacy-fir-tests/tests-gen/org/jetbrains/kotlin/fir/FirLoadCompiledKotlinGenerated.java +++ b/compiler/fir/analysis-tests/legacy-fir-tests/tests-gen/org/jetbrains/kotlin/fir/FirLoadCompiledKotlinGenerated.java @@ -2534,6 +2534,11 @@ public class FirLoadCompiledKotlinGenerated extends AbstractFirLoadCompiledKotli runTest("compiler/testData/loadJava/compiledKotlin/prop/ExtVarl.kt"); } + @TestMetadata("nonConstValWithConstantValueAttribute.kt") + public void testNonConstValWithConstantValueAttribute() throws Exception { + runTest("compiler/testData/loadJava/compiledKotlin/prop/nonConstValWithConstantValueAttribute.kt"); + } + @TestMetadata("NsVal.kt") public void testNsVal() throws Exception { runTest("compiler/testData/loadJava/compiledKotlin/prop/NsVal.kt"); diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBytecodeTextTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBytecodeTextTestGenerated.java index f65835d4a8e..a198392cc3a 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBytecodeTextTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBytecodeTextTestGenerated.java @@ -1457,9 +1457,15 @@ public class FirBytecodeTextTestGenerated extends AbstractFirBytecodeTextTest { } @Test - @TestMetadata("nonConstValHasNoDefaultValue.kt") - public void testNonConstValHasNoDefaultValue() throws Exception { - runTest("compiler/testData/codegen/bytecodeText/constProperty/nonConstValHasNoDefaultValue.kt"); + @TestMetadata("nonConstValHasNoDefaultValue_after.kt") + public void testNonConstValHasNoDefaultValue_after() throws Exception { + runTest("compiler/testData/codegen/bytecodeText/constProperty/nonConstValHasNoDefaultValue_after.kt"); + } + + @Test + @TestMetadata("nonConstValHasNoDefaultValue_before.kt") + public void testNonConstValHasNoDefaultValue_before() throws Exception { + runTest("compiler/testData/codegen/bytecodeText/constProperty/nonConstValHasNoDefaultValue_before.kt"); } } diff --git a/compiler/testData/codegen/bytecodeText/constProperty/nonConstValHasNoDefaultValue.kt b/compiler/testData/codegen/bytecodeText/constProperty/nonConstValHasNoDefaultValue_after.kt similarity index 95% rename from compiler/testData/codegen/bytecodeText/constProperty/nonConstValHasNoDefaultValue.kt rename to compiler/testData/codegen/bytecodeText/constProperty/nonConstValHasNoDefaultValue_after.kt index 365dd9e3b01..0a623ebad81 100644 --- a/compiler/testData/codegen/bytecodeText/constProperty/nonConstValHasNoDefaultValue.kt +++ b/compiler/testData/codegen/bytecodeText/constProperty/nonConstValHasNoDefaultValue_after.kt @@ -1,3 +1,5 @@ +// !LANGUAGE: +NoConstantValueAttributeForNonConstVals +JvmFieldInInterface + class C { val testClassVal = 100 diff --git a/compiler/testData/codegen/bytecodeText/constProperty/nonConstValHasNoDefaultValue_before.kt b/compiler/testData/codegen/bytecodeText/constProperty/nonConstValHasNoDefaultValue_before.kt new file mode 100644 index 00000000000..68a10cf6872 --- /dev/null +++ b/compiler/testData/codegen/bytecodeText/constProperty/nonConstValHasNoDefaultValue_before.kt @@ -0,0 +1,66 @@ +// TARGET_BACKEND: JVM +// IGNORE_BACKEND: JVM_IR +// ^ Disables a language feature introduced in 1.4. This test checks old backend's behavior and is needed as long as we support language version 1.3. +// IGNORE_BACKEND_FIR: JVM_IR +// FIR status: don't support legacy feature +// !LANGUAGE: -NoConstantValueAttributeForNonConstVals + +class C { + val testClassVal = 100 + + @JvmField + val testJvmFieldVal = 105 + + companion object { + val testCompanionObjectVal = 110 + + @JvmStatic + val testJvmStaticCompanionObjectVal = 120 + + @JvmField + val testJvmFieldCompanionObjectVal = 130 + } +} + + +interface IFoo { + companion object { + val testInterfaceCompanionObjectVal = 200 + } +} + + +interface IBar { + companion object { + @JvmField + val testJvmFieldInInterfaceCompanionObject = 210 + } +} + + + +object Obj { + val testObjectVal = 300 + + @JvmStatic + val testJvmStaticObjectVal = 310 + + @JvmField + val testJvmFieldObjectVal = 320 +} + + +val testTopLevelVal = 400 + + +// 1 final I testClassVal = 100 +// 1 final I testJvmFieldVal = 105 +// 1 final static I testCompanionObjectVal = 110 +// 1 final static I testJvmStaticCompanionObjectVal = 120 +// 1 final static I testJvmFieldCompanionObjectVal = 130 +// 1 final static I testInterfaceCompanionObjectVal = 200 +// 1 final static I testJvmFieldInInterfaceCompanionObject = 210 +// 1 final static I testObjectVal = 300 +// 1 final static I testJvmStaticObjectVal = 310 +// 1 final static I testJvmFieldObjectVal = 320 +// 1 final static I testTopLevelVal = 400 diff --git a/compiler/testData/loadJava/compiledKotlin/prop/nonConstValWithConstantValueAttribute.kt b/compiler/testData/loadJava/compiledKotlin/prop/nonConstValWithConstantValueAttribute.kt new file mode 100644 index 00000000000..f6b1c34f991 --- /dev/null +++ b/compiler/testData/loadJava/compiledKotlin/prop/nonConstValWithConstantValueAttribute.kt @@ -0,0 +1,20 @@ +// !LANGUAGE: -NoConstantValueAttributeForNonConstVals +// IGNORE_BACKEND: JVM_IR +//ALLOW_AST_ACCESS + +package test +val nonConstVal1 = 1 + +class C { + val nonConstVal2 = 2 + + companion object { + val nonConstVal3 = 3 + } +} + +interface I { + companion object { + val nonConstVal4 = 4 + } +} diff --git a/compiler/testData/loadJava/compiledKotlin/prop/nonConstValWithConstantValueAttribute.txt b/compiler/testData/loadJava/compiledKotlin/prop/nonConstValWithConstantValueAttribute.txt new file mode 100644 index 00000000000..a23c262c4a6 --- /dev/null +++ b/compiler/testData/loadJava/compiledKotlin/prop/nonConstValWithConstantValueAttribute.txt @@ -0,0 +1,25 @@ +package test + +public val nonConstVal1: kotlin.Int = 1 + public fun (): kotlin.Int + +public final class C { + /*primary*/ public constructor C() + public final val nonConstVal2: kotlin.Int = 2 + public final fun (): kotlin.Int + + public companion object Companion { + /*primary*/ private constructor Companion() + public final val nonConstVal3: kotlin.Int = 3 + public final fun (): kotlin.Int + } +} + +public interface I { + + public companion object Companion { + /*primary*/ private constructor Companion() + public final val nonConstVal4: kotlin.Int = 4 + public final fun (): kotlin.Int + } +} diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BytecodeTextTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BytecodeTextTestGenerated.java index f505c319636..848552e88c1 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BytecodeTextTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BytecodeTextTestGenerated.java @@ -1439,9 +1439,15 @@ public class BytecodeTextTestGenerated extends AbstractBytecodeTextTest { } @Test - @TestMetadata("nonConstValHasNoDefaultValue.kt") - public void testNonConstValHasNoDefaultValue() throws Exception { - runTest("compiler/testData/codegen/bytecodeText/constProperty/nonConstValHasNoDefaultValue.kt"); + @TestMetadata("nonConstValHasNoDefaultValue_after.kt") + public void testNonConstValHasNoDefaultValue_after() throws Exception { + runTest("compiler/testData/codegen/bytecodeText/constProperty/nonConstValHasNoDefaultValue_after.kt"); + } + + @Test + @TestMetadata("nonConstValHasNoDefaultValue_before.kt") + public void testNonConstValHasNoDefaultValue_before() throws Exception { + runTest("compiler/testData/codegen/bytecodeText/constProperty/nonConstValHasNoDefaultValue_before.kt"); } } diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBytecodeTextTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBytecodeTextTestGenerated.java index 451525b0f52..e21670e3c61 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBytecodeTextTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBytecodeTextTestGenerated.java @@ -1457,9 +1457,15 @@ public class IrBytecodeTextTestGenerated extends AbstractIrBytecodeTextTest { } @Test - @TestMetadata("nonConstValHasNoDefaultValue.kt") - public void testNonConstValHasNoDefaultValue() throws Exception { - runTest("compiler/testData/codegen/bytecodeText/constProperty/nonConstValHasNoDefaultValue.kt"); + @TestMetadata("nonConstValHasNoDefaultValue_after.kt") + public void testNonConstValHasNoDefaultValue_after() throws Exception { + runTest("compiler/testData/codegen/bytecodeText/constProperty/nonConstValHasNoDefaultValue_after.kt"); + } + + @Test + @TestMetadata("nonConstValHasNoDefaultValue_before.kt") + public void testNonConstValHasNoDefaultValue_before() throws Exception { + runTest("compiler/testData/codegen/bytecodeText/constProperty/nonConstValHasNoDefaultValue_before.kt"); } } diff --git a/compiler/tests-gen/org/jetbrains/kotlin/jvm/compiler/LoadJavaTestGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/jvm/compiler/LoadJavaTestGenerated.java index c0948bb356b..1d909b24fa5 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/jvm/compiler/LoadJavaTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/jvm/compiler/LoadJavaTestGenerated.java @@ -4212,6 +4212,11 @@ public class LoadJavaTestGenerated extends AbstractLoadJavaTest { runTest("compiler/testData/loadJava/compiledKotlin/prop/ExtVarl.kt"); } + @TestMetadata("nonConstValWithConstantValueAttribute.kt") + public void testNonConstValWithConstantValueAttribute() throws Exception { + runTest("compiler/testData/loadJava/compiledKotlin/prop/nonConstValWithConstantValueAttribute.kt"); + } + @TestMetadata("NsVal.kt") public void testNsVal() throws Exception { runTest("compiler/testData/loadJava/compiledKotlin/prop/NsVal.kt"); diff --git a/compiler/tests-gen/org/jetbrains/kotlin/jvm/compiler/LoadKotlinWithTypeTableTestGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/jvm/compiler/LoadKotlinWithTypeTableTestGenerated.java index de545c0c8fd..19c32986e01 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/jvm/compiler/LoadKotlinWithTypeTableTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/jvm/compiler/LoadKotlinWithTypeTableTestGenerated.java @@ -2534,6 +2534,11 @@ public class LoadKotlinWithTypeTableTestGenerated extends AbstractLoadKotlinWith runTest("compiler/testData/loadJava/compiledKotlin/prop/ExtVarl.kt"); } + @TestMetadata("nonConstValWithConstantValueAttribute.kt") + public void testNonConstValWithConstantValueAttribute() throws Exception { + runTest("compiler/testData/loadJava/compiledKotlin/prop/nonConstValWithConstantValueAttribute.kt"); + } + @TestMetadata("NsVal.kt") public void testNsVal() throws Exception { runTest("compiler/testData/loadJava/compiledKotlin/prop/NsVal.kt"); diff --git a/compiler/tests-gen/org/jetbrains/kotlin/jvm/compiler/ir/IrLoadJavaTestGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/jvm/compiler/ir/IrLoadJavaTestGenerated.java index 88190c79010..c7dd0eb931f 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/jvm/compiler/ir/IrLoadJavaTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/jvm/compiler/ir/IrLoadJavaTestGenerated.java @@ -4213,6 +4213,11 @@ public class IrLoadJavaTestGenerated extends AbstractIrLoadJavaTest { runTest("compiler/testData/loadJava/compiledKotlin/prop/ExtVarl.kt"); } + @TestMetadata("nonConstValWithConstantValueAttribute.kt") + public void testNonConstValWithConstantValueAttribute() throws Exception { + runTest("compiler/testData/loadJava/compiledKotlin/prop/nonConstValWithConstantValueAttribute.kt"); + } + @TestMetadata("NsVal.kt") public void testNsVal() throws Exception { runTest("compiler/testData/loadJava/compiledKotlin/prop/NsVal.kt"); diff --git a/compiler/tests-gen/org/jetbrains/kotlin/jvm/compiler/javac/LoadJavaUsingJavacTestGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/jvm/compiler/javac/LoadJavaUsingJavacTestGenerated.java index 86f2be8281e..ca1e5b6ec37 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/jvm/compiler/javac/LoadJavaUsingJavacTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/jvm/compiler/javac/LoadJavaUsingJavacTestGenerated.java @@ -4212,6 +4212,11 @@ public class LoadJavaUsingJavacTestGenerated extends AbstractLoadJavaUsingJavacT runTest("compiler/testData/loadJava/compiledKotlin/prop/ExtVarl.kt"); } + @TestMetadata("nonConstValWithConstantValueAttribute.kt") + public void testNonConstValWithConstantValueAttribute() throws Exception { + runTest("compiler/testData/loadJava/compiledKotlin/prop/nonConstValWithConstantValueAttribute.kt"); + } + @TestMetadata("NsVal.kt") public void testNsVal() throws Exception { runTest("compiler/testData/loadJava/compiledKotlin/prop/NsVal.kt"); diff --git a/core/descriptors.runtime/tests/org/jetbrains/kotlin/jvm/runtime/JvmRuntimeDescriptorLoaderTestGenerated.java b/core/descriptors.runtime/tests/org/jetbrains/kotlin/jvm/runtime/JvmRuntimeDescriptorLoaderTestGenerated.java index 1b8a5fb864c..d53ff04d4bf 100644 --- a/core/descriptors.runtime/tests/org/jetbrains/kotlin/jvm/runtime/JvmRuntimeDescriptorLoaderTestGenerated.java +++ b/core/descriptors.runtime/tests/org/jetbrains/kotlin/jvm/runtime/JvmRuntimeDescriptorLoaderTestGenerated.java @@ -2536,6 +2536,11 @@ public class JvmRuntimeDescriptorLoaderTestGenerated extends AbstractJvmRuntimeD runTest("compiler/testData/loadJava/compiledKotlin/prop/ExtVarl.kt"); } + @TestMetadata("nonConstValWithConstantValueAttribute.kt") + public void testNonConstValWithConstantValueAttribute() throws Exception { + runTest("compiler/testData/loadJava/compiledKotlin/prop/nonConstValWithConstantValueAttribute.kt"); + } + @TestMetadata("NsVal.kt") public void testNsVal() throws Exception { runTest("compiler/testData/loadJava/compiledKotlin/prop/NsVal.kt");