Don't generate ConstantValue for non-const vals in Kotlin 1.3+
This commit is contained in:
@@ -17,6 +17,7 @@ import org.jetbrains.kotlin.codegen.context.MultifileClassFacadeContext;
|
|||||||
import org.jetbrains.kotlin.codegen.context.MultifileClassPartContext;
|
import org.jetbrains.kotlin.codegen.context.MultifileClassPartContext;
|
||||||
import org.jetbrains.kotlin.codegen.state.GenerationState;
|
import org.jetbrains.kotlin.codegen.state.GenerationState;
|
||||||
import org.jetbrains.kotlin.codegen.state.KotlinTypeMapper;
|
import org.jetbrains.kotlin.codegen.state.KotlinTypeMapper;
|
||||||
|
import org.jetbrains.kotlin.config.LanguageFeature;
|
||||||
import org.jetbrains.kotlin.descriptors.*;
|
import org.jetbrains.kotlin.descriptors.*;
|
||||||
import org.jetbrains.kotlin.descriptors.annotations.Annotated;
|
import org.jetbrains.kotlin.descriptors.annotations.Annotated;
|
||||||
import org.jetbrains.kotlin.descriptors.annotations.AnnotationSplitter;
|
import org.jetbrains.kotlin.descriptors.annotations.AnnotationSplitter;
|
||||||
@@ -481,6 +482,9 @@ public class PropertyCodegen {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private boolean shouldWriteFieldInitializer(@NotNull PropertyDescriptor descriptor) {
|
private boolean shouldWriteFieldInitializer(@NotNull PropertyDescriptor descriptor) {
|
||||||
|
if (!descriptor.isConst() && state.getLanguageVersionSettings().supportsFeature(LanguageFeature.NoConstantValueAttributeForNonConstVals))
|
||||||
|
return false;
|
||||||
|
|
||||||
//final field of primitive or String type
|
//final field of primitive or String type
|
||||||
if (!descriptor.isVar()) {
|
if (!descriptor.isVar()) {
|
||||||
Type type = typeMapper.mapType(descriptor);
|
Type type = typeMapper.mapType(descriptor);
|
||||||
|
|||||||
+67
@@ -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"
|
||||||
|
}
|
||||||
+71
@@ -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
|
||||||
Vendored
+61
@@ -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
|
||||||
Generated
+5
@@ -15390,6 +15390,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
|
|||||||
public void testInterfaceCompanion() throws Exception {
|
public void testInterfaceCompanion() throws Exception {
|
||||||
runTest("compiler/testData/codegen/box/properties/const/interfaceCompanion.kt");
|
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")
|
@TestMetadata("compiler/testData/codegen/box/properties/lateinit")
|
||||||
|
|||||||
+5
@@ -15390,6 +15390,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
|
|||||||
public void testInterfaceCompanion() throws Exception {
|
public void testInterfaceCompanion() throws Exception {
|
||||||
runTest("compiler/testData/codegen/box/properties/const/interfaceCompanion.kt");
|
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")
|
@TestMetadata("compiler/testData/codegen/box/properties/lateinit")
|
||||||
|
|||||||
@@ -910,6 +910,16 @@ public class BytecodeTextTestGenerated extends AbstractBytecodeTextTest {
|
|||||||
public void testNoInlineInCmp() throws Exception {
|
public void testNoInlineInCmp() throws Exception {
|
||||||
runTest("compiler/testData/codegen/bytecodeText/constProperty/noInlineInCmp.kt");
|
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")
|
@TestMetadata("compiler/testData/codegen/bytecodeText/constantConditions")
|
||||||
|
|||||||
+5
@@ -15390,6 +15390,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
|
|||||||
public void testInterfaceCompanion() throws Exception {
|
public void testInterfaceCompanion() throws Exception {
|
||||||
runTest("compiler/testData/codegen/box/properties/const/interfaceCompanion.kt");
|
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")
|
@TestMetadata("compiler/testData/codegen/box/properties/lateinit")
|
||||||
|
|||||||
@@ -81,6 +81,7 @@ enum class LanguageFeature(
|
|||||||
ProhibitAssigningSingleElementsToVarargsInNamedForm(KOTLIN_1_3, kind = BUG_FIX),
|
ProhibitAssigningSingleElementsToVarargsInNamedForm(KOTLIN_1_3, kind = BUG_FIX),
|
||||||
FunctionTypesWithBigArity(KOTLIN_1_3, sinceApiVersion = ApiVersion.KOTLIN_1_3),
|
FunctionTypesWithBigArity(KOTLIN_1_3, sinceApiVersion = ApiVersion.KOTLIN_1_3),
|
||||||
RestrictRetentionForExpressionAnnotations(KOTLIN_1_3, kind = BUG_FIX),
|
RestrictRetentionForExpressionAnnotations(KOTLIN_1_3, kind = BUG_FIX),
|
||||||
|
NoConstantValueAttributeForNonConstVals(KOTLIN_1_3, kind = BUG_FIX),
|
||||||
|
|
||||||
StrictJavaNullabilityAssertions(sinceVersion = null, defaultState = State.DISABLED),
|
StrictJavaNullabilityAssertions(sinceVersion = null, defaultState = State.DISABLED),
|
||||||
ProperIeee754Comparisons(sinceVersion = null, defaultState = State.DISABLED, kind = BUG_FIX),
|
ProperIeee754Comparisons(sinceVersion = null, defaultState = State.DISABLED, kind = BUG_FIX),
|
||||||
|
|||||||
Reference in New Issue
Block a user