Don't initialize const properties in constructor

#KT-9532 Fixed
This commit is contained in:
Michael Bogdanov
2015-10-17 12:32:41 +03:00
committed by Michael Bogdanov
parent 7d7d37719b
commit 3452fc8d02
10 changed files with 123 additions and 0 deletions
+30
View File
@@ -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"
}
@@ -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();
}
}
@@ -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