diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/PropertyCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/PropertyCodegen.java index a1041001318..0d4c2be9575 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/PropertyCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/PropertyCodegen.java @@ -17,6 +17,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.Annotated; import org.jetbrains.kotlin.descriptors.annotations.AnnotationSplitter; @@ -481,6 +482,9 @@ public class PropertyCodegen { } 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); diff --git a/compiler/testData/codegen/box/properties/const/nonConstValsAreProperlyInitialized.kt b/compiler/testData/codegen/box/properties/const/nonConstValsAreProperlyInitialized.kt new file mode 100644 index 00000000000..a375126f5c4 --- /dev/null +++ b/compiler/testData/codegen/box/properties/const/nonConstValsAreProperlyInitialized.kt @@ -0,0 +1,67 @@ +// !LANGUAGE: +NoConstantValueAttributeForNonConstVals +JvmFieldInInterface +// TARGET_BACKEND: JVM +// WITH_RUNTIME + +import kotlin.test.assertEquals + +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 + +fun box(): String { + assertEquals(100, C().testClassVal) + assertEquals(105, C().testJvmFieldVal) + assertEquals(110, C.testCompanionObjectVal) + assertEquals(120, C.testJvmStaticCompanionObjectVal) + assertEquals(130, C.testJvmFieldCompanionObjectVal) + assertEquals(200, IFoo.testInterfaceCompanionObjectVal) + assertEquals(210, IBar.testJvmFieldInInterfaceCompanionObject) + assertEquals(300, Obj.testObjectVal) + assertEquals(310, Obj.testJvmStaticObjectVal) + assertEquals(320, Obj.testJvmFieldObjectVal) + assertEquals(400, testTopLevelVal) + + return "OK" +} \ No newline at end of file diff --git a/compiler/testData/codegen/bytecodeText/constProperty/nonConstValHasNoDefaultValue_after.kt b/compiler/testData/codegen/bytecodeText/constProperty/nonConstValHasNoDefaultValue_after.kt new file mode 100644 index 00000000000..0a623ebad81 --- /dev/null +++ b/compiler/testData/codegen/bytecodeText/constProperty/nonConstValHasNoDefaultValue_after.kt @@ -0,0 +1,71 @@ +// !LANGUAGE: +NoConstantValueAttributeForNonConstVals +JvmFieldInInterface + +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 + + +// 0 final I testClassVal = 100 +// 1 final I testClassVal +// 0 final I testJvmFieldVal = 105 +// 1 final I testJvmFieldVal +// 0 final static I testCompanionObjectVal = 110 +// 1 final static I testCompanionObjectVal +// 0 final static I testJvmStaticCompanionObjectVal = 120 +// 1 final static I testJvmStaticCompanionObjectVal +// 0 final static I testJvmFieldCompanionObjectVal = 130 +// 1 final static I testJvmFieldCompanionObjectVal +// 0 final static I testInterfaceCompanionObjectVal = 200 +// 1 final static I testInterfaceCompanionObjectVal +// 0 final static I testJvmFieldInInterfaceCompanionObject = 210 +// 1 final static I testJvmFieldInInterfaceCompanionObject +// 0 final static I testObjectVal = 300 +// 1 final static I testObjectVal +// 0 final static I testJvmStaticObjectVal = 310 +// 1 final static I testJvmStaticObjectVal +// 0 final static I testJvmFieldObjectVal = 320 +// 1 final static I testJvmFieldObjectVal +// 0 final static I testTopLevelVal = 400 +// 1 final static I testTopLevelVal 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..bfeff9540ba --- /dev/null +++ b/compiler/testData/codegen/bytecodeText/constProperty/nonConstValHasNoDefaultValue_before.kt @@ -0,0 +1,61 @@ +// !LANGUAGE: -NoConstantValueAttributeForNonConstVals +JvmFieldInInterface + +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/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index 6487a4ca09e..531c0a22776 100644 --- a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -15390,6 +15390,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes public void testInterfaceCompanion() throws Exception { runTest("compiler/testData/codegen/box/properties/const/interfaceCompanion.kt"); } + + @TestMetadata("nonConstValsAreProperlyInitialized.kt") + public void testNonConstValsAreProperlyInitialized() throws Exception { + runTest("compiler/testData/codegen/box/properties/const/nonConstValsAreProperlyInitialized.kt"); + } } @TestMetadata("compiler/testData/codegen/box/properties/lateinit") diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index 91ab3cb0d78..cd1ea2ee654 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -15390,6 +15390,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { public void testInterfaceCompanion() throws Exception { runTest("compiler/testData/codegen/box/properties/const/interfaceCompanion.kt"); } + + @TestMetadata("nonConstValsAreProperlyInitialized.kt") + public void testNonConstValsAreProperlyInitialized() throws Exception { + runTest("compiler/testData/codegen/box/properties/const/nonConstValsAreProperlyInitialized.kt"); + } } @TestMetadata("compiler/testData/codegen/box/properties/lateinit") diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java index 8f2fbca069c..cfdf81c4ad3 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java @@ -910,6 +910,16 @@ public class BytecodeTextTestGenerated extends AbstractBytecodeTextTest { public void testNoInlineInCmp() throws Exception { runTest("compiler/testData/codegen/bytecodeText/constProperty/noInlineInCmp.kt"); } + + @TestMetadata("nonConstValHasNoDefaultValue_after.kt") + public void testNonConstValHasNoDefaultValue_after() throws Exception { + runTest("compiler/testData/codegen/bytecodeText/constProperty/nonConstValHasNoDefaultValue_after.kt"); + } + + @TestMetadata("nonConstValHasNoDefaultValue_before.kt") + public void testNonConstValHasNoDefaultValue_before() throws Exception { + runTest("compiler/testData/codegen/bytecodeText/constProperty/nonConstValHasNoDefaultValue_before.kt"); + } } @TestMetadata("compiler/testData/codegen/bytecodeText/constantConditions") diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 9df888063c0..042d1c6859a 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -15390,6 +15390,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes public void testInterfaceCompanion() throws Exception { runTest("compiler/testData/codegen/box/properties/const/interfaceCompanion.kt"); } + + @TestMetadata("nonConstValsAreProperlyInitialized.kt") + public void testNonConstValsAreProperlyInitialized() throws Exception { + runTest("compiler/testData/codegen/box/properties/const/nonConstValsAreProperlyInitialized.kt"); + } } @TestMetadata("compiler/testData/codegen/box/properties/lateinit") diff --git a/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt b/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt index 2da04d84e7d..9960005d1b2 100644 --- a/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt +++ b/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt @@ -81,6 +81,7 @@ enum class LanguageFeature( ProhibitAssigningSingleElementsToVarargsInNamedForm(KOTLIN_1_3, kind = BUG_FIX), FunctionTypesWithBigArity(KOTLIN_1_3, sinceApiVersion = ApiVersion.KOTLIN_1_3), RestrictRetentionForExpressionAnnotations(KOTLIN_1_3, kind = BUG_FIX), + NoConstantValueAttributeForNonConstVals(KOTLIN_1_3, kind = BUG_FIX), StrictJavaNullabilityAssertions(sinceVersion = null, defaultState = State.DISABLED), ProperIeee754Comparisons(sinceVersion = null, defaultState = State.DISABLED, kind = BUG_FIX),