Don't initialize const properties in constructor
#KT-9532 Fixed
This commit is contained in:
committed by
Michael Bogdanov
parent
7d7d37719b
commit
3452fc8d02
@@ -374,6 +374,11 @@ public abstract class MemberCodegen<T extends JetElement/* TODO: & JetDeclaratio
|
||||
PropertyDescriptor propertyDescriptor = (PropertyDescriptor) bindingContext.get(VARIABLE, property);
|
||||
assert propertyDescriptor != null;
|
||||
|
||||
if (propertyDescriptor.isConst()) {
|
||||
//const initializer always inlined
|
||||
return false;
|
||||
}
|
||||
|
||||
JetExpression initializer = property.getInitializer();
|
||||
|
||||
ConstantValue<?> initializerValue = computeInitializerValue(property, propertyDescriptor, initializer);
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
object A {
|
||||
const val a: String = "$"
|
||||
const val b = "1234$a"
|
||||
const val c = 10000
|
||||
|
||||
val bNonConst = "1234$a"
|
||||
val bNullable: String = "1234$a"
|
||||
}
|
||||
|
||||
object B {
|
||||
const val a: String = "$"
|
||||
const val b = "1234$a"
|
||||
const val c = 10000
|
||||
|
||||
val bNonConst = "1234$a"
|
||||
val bNullable: String = "1234$a"
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
if (A.a !== B.a) return "Fail 1: A.a !== B.a"
|
||||
|
||||
if (A.b !== B.b) return "Fail 2: A.b !== B.b"
|
||||
|
||||
if (A.c !== B.c) return "Fail 3: A.c !== B.c"
|
||||
|
||||
if (A.bNonConst === B.bNonConst) return "Fail 5: A.bNonConst == B.bNonConst"
|
||||
if (A.bNullable === B.bNullable) return "Fail 6: A.bNullable == B.bNullable"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
public class JavaClass {
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@interface Foo {
|
||||
int value();
|
||||
}
|
||||
|
||||
@Foo(KotlinInterface.FOO_INT)
|
||||
public String test() throws NoSuchMethodException {
|
||||
return KotlinInterface.FOO_STRING +
|
||||
JavaClass.class.getMethod("test").getAnnotation(Foo.class).value();
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
interface KotlinInterface {
|
||||
companion object {
|
||||
const val FOO_INT: Int = 10
|
||||
const val FOO_STRING: String = "OK"
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val test = JavaClass().test()
|
||||
return if (test == "OK10") "OK" else "fail : $test"
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
interface KInt {
|
||||
|
||||
companion object {
|
||||
const val a = "a"
|
||||
const val b = "b$a"
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val a = KInt::class.java.getField("a").get(null)
|
||||
val b = KInt::class.java.getField("b").get(null)
|
||||
|
||||
if (a !== KInt.a) return "fail 1: KInt.a !== KInt.Companion.a"
|
||||
if (b !== KInt.b) return "fail 2: KInt.b !== KInt.Companion.b"
|
||||
if (b !== "ba") return "fail 2: 'ba' !== KInt.Companion.b"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
object A {
|
||||
private const val a = "$"
|
||||
private const val b = "1234$a"
|
||||
private const val c = 10000
|
||||
}
|
||||
|
||||
//check that constant initializers inlined
|
||||
|
||||
// 0 GETSTATIC
|
||||
// 2 PUTSTATIC A.INSTANCE
|
||||
@@ -469,6 +469,12 @@ public class BytecodeTextTestGenerated extends AbstractBytecodeTextTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("kt9532.kt")
|
||||
public void testKt9532() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/constants/kt9532.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("nullableByteAndShort.kt")
|
||||
public void testNullableByteAndShort() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/constants/nullableByteAndShort.kt");
|
||||
|
||||
+6
@@ -1960,6 +1960,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("kt9532.kt")
|
||||
public void testKt9532() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/box/constants/kt9532.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("long.kt")
|
||||
public void testLong() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/box/constants/long.kt");
|
||||
|
||||
+6
@@ -387,6 +387,12 @@ public class BlackBoxWithJavaCodegenTestGenerated extends AbstractBlackBoxCodege
|
||||
doTestWithJava(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("annotationWithKotlinPropertyFromInterfaceCompanion")
|
||||
public void testAnnotationWithKotlinPropertyFromInterfaceCompanion() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithJava/properties/annotationWithKotlinPropertyFromInterfaceCompanion/");
|
||||
doTestWithJava(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("classObjectProperties")
|
||||
public void testClassObjectProperties() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithJava/properties/classObjectProperties/");
|
||||
|
||||
+15
@@ -1070,6 +1070,21 @@ public class BlackBoxWithStdlibCodegenTestGenerated extends AbstractBlackBoxCode
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("compiler/testData/codegen/boxWithStdlib/const")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class Const extends AbstractBlackBoxCodegenTest {
|
||||
public void testAllFilesPresentInConst() throws Exception {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/boxWithStdlib/const"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("interfaceCompanion.kt")
|
||||
public void testInterfaceCompanion() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithStdlib/const/interfaceCompanion.kt");
|
||||
doTestWithStdlib(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("compiler/testData/codegen/boxWithStdlib/controlStructures")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
|
||||
Reference in New Issue
Block a user