diff --git a/kotlin-native/backend.native/tests/build.gradle b/kotlin-native/backend.native/tests/build.gradle index 352ffbb9aae..ef5568fab9e 100644 --- a/kotlin-native/backend.native/tests/build.gradle +++ b/kotlin-native/backend.native/tests/build.gradle @@ -1639,6 +1639,11 @@ linkTest("initializers_sharedVarInInitBlock") { lib = "codegen/initializers/sharedVarInInitBlock_lib.kt" } +standaloneTest("initializers_static") { + source = "codegen/initializers/static.kt" + flags = ['-Xopt-in=kotlin.native.internal.InternalForKotlinNative', "-tr"] +} + task arithmetic_basic(type: KonanLocalTest) { source = "codegen/arithmetic/basic.kt" } diff --git a/kotlin-native/backend.native/tests/codegen/boxing/boxing0.kt b/kotlin-native/backend.native/tests/codegen/boxing/boxing0.kt index c5d14cbfeb6..203ff68674c 100644 --- a/kotlin-native/backend.native/tests/codegen/boxing/boxing0.kt +++ b/kotlin-native/backend.native/tests/codegen/boxing/boxing0.kt @@ -14,5 +14,8 @@ class Box(t: T) { @Test fun runTest() { val box: Box = Box(17) println(box.value) + val nonConst = 17 + val box2: Box = Box(nonConst) + println(box2.value) } diff --git a/kotlin-native/backend.native/tests/codegen/boxing/boxing0.out b/kotlin-native/backend.native/tests/codegen/boxing/boxing0.out index 98d9bcb75a6..e6cc5d6c0ed 100644 --- a/kotlin-native/backend.native/tests/codegen/boxing/boxing0.out +++ b/kotlin-native/backend.native/tests/codegen/boxing/boxing0.out @@ -1 +1,2 @@ 17 +17 diff --git a/kotlin-native/backend.native/tests/codegen/boxing/boxing1.kt b/kotlin-native/backend.native/tests/codegen/boxing/boxing1.kt index 0f02a747bfe..000428eb784 100644 --- a/kotlin-native/backend.native/tests/codegen/boxing/boxing1.kt +++ b/kotlin-native/backend.native/tests/codegen/boxing/boxing1.kt @@ -13,6 +13,15 @@ fun foo(arg: Any) { @Test fun runTest() { foo(1) + foo(2u) foo(false) foo("Hello") + val nonConstInt = 1 + val nonConstUInt = 2u + val nonConstBool = false + val nonConstString = "Hello" + foo(nonConstInt) + foo(nonConstUInt) + foo(nonConstBool) + foo(nonConstString) } \ No newline at end of file diff --git a/kotlin-native/backend.native/tests/codegen/boxing/boxing1.out b/kotlin-native/backend.native/tests/codegen/boxing/boxing1.out index 3e2003b20c2..901bd222426 100644 --- a/kotlin-native/backend.native/tests/codegen/boxing/boxing1.out +++ b/kotlin-native/backend.native/tests/codegen/boxing/boxing1.out @@ -1,3 +1,8 @@ 1 +2 +false +Hello +1 +2 false Hello diff --git a/kotlin-native/backend.native/tests/codegen/boxing/boxing12.kt b/kotlin-native/backend.native/tests/codegen/boxing/boxing12.kt index 0e1da69f866..6f472463aef 100644 --- a/kotlin-native/backend.native/tests/codegen/boxing/boxing12.kt +++ b/kotlin-native/backend.native/tests/codegen/boxing/boxing12.kt @@ -13,4 +13,6 @@ fun foo(x: Number) { @Test fun runTest() { foo(18) + val nonConst = 18 + foo(nonConst) } \ No newline at end of file diff --git a/kotlin-native/backend.native/tests/codegen/boxing/boxing12.out b/kotlin-native/backend.native/tests/codegen/boxing/boxing12.out index 3c032078a4a..43b14c32412 100644 --- a/kotlin-native/backend.native/tests/codegen/boxing/boxing12.out +++ b/kotlin-native/backend.native/tests/codegen/boxing/boxing12.out @@ -1 +1,2 @@ 18 +18 diff --git a/kotlin-native/backend.native/tests/codegen/boxing/boxing13.kt b/kotlin-native/backend.native/tests/codegen/boxing/boxing13.kt index 2e09d4c39ae..d45b0fb7a65 100644 --- a/kotlin-native/backend.native/tests/codegen/boxing/boxing13.kt +++ b/kotlin-native/backend.native/tests/codegen/boxing/boxing13.kt @@ -16,4 +16,10 @@ fun is42(x: Any?) { is42(16) is42(42) is42("42") + val nonConst16 = 16 + val nonConst42 = 42 + val nonConst42String = "42" + is42(nonConst16) + is42(nonConst42) + is42(nonConst42String) } \ No newline at end of file diff --git a/kotlin-native/backend.native/tests/codegen/boxing/boxing13.out b/kotlin-native/backend.native/tests/codegen/boxing/boxing13.out index d3e48b55722..e33febd159a 100644 --- a/kotlin-native/backend.native/tests/codegen/boxing/boxing13.out +++ b/kotlin-native/backend.native/tests/codegen/boxing/boxing13.out @@ -4,3 +4,9 @@ true true false false +false +false +true +true +false +false diff --git a/kotlin-native/backend.native/tests/codegen/boxing/boxing14.kt b/kotlin-native/backend.native/tests/codegen/boxing/boxing14.kt index d27c00d625b..fb25d7ee24f 100644 --- a/kotlin-native/backend.native/tests/codegen/boxing/boxing14.kt +++ b/kotlin-native/backend.native/tests/codegen/boxing/boxing14.kt @@ -9,6 +9,8 @@ import kotlin.test.* @Test fun runTest() { 42.println() + val nonConst = 42 + nonConst.println() } fun T.println() = println(this.toString()) \ No newline at end of file diff --git a/kotlin-native/backend.native/tests/codegen/boxing/boxing14.out b/kotlin-native/backend.native/tests/codegen/boxing/boxing14.out index d81cc0710eb..daaac9e3030 100644 --- a/kotlin-native/backend.native/tests/codegen/boxing/boxing14.out +++ b/kotlin-native/backend.native/tests/codegen/boxing/boxing14.out @@ -1 +1,2 @@ 42 +42 diff --git a/kotlin-native/backend.native/tests/codegen/boxing/boxing15.kt b/kotlin-native/backend.native/tests/codegen/boxing/boxing15.kt index 5ad45971230..255ccf5e3db 100644 --- a/kotlin-native/backend.native/tests/codegen/boxing/boxing15.kt +++ b/kotlin-native/backend.native/tests/codegen/boxing/boxing15.kt @@ -9,6 +9,8 @@ import kotlin.test.* @Test fun runTest() { println(foo(17)) + val nonConst = 17 + println(foo(nonConst)) } fun foo(x: T): Int = x \ No newline at end of file diff --git a/kotlin-native/backend.native/tests/codegen/boxing/boxing15.out b/kotlin-native/backend.native/tests/codegen/boxing/boxing15.out index 98d9bcb75a6..e6cc5d6c0ed 100644 --- a/kotlin-native/backend.native/tests/codegen/boxing/boxing15.out +++ b/kotlin-native/backend.native/tests/codegen/boxing/boxing15.out @@ -1 +1,2 @@ 17 +17 diff --git a/kotlin-native/backend.native/tests/codegen/boxing/boxing2.kt b/kotlin-native/backend.native/tests/codegen/boxing/boxing2.kt index ab629b63a5f..c85c7ea0a9c 100644 --- a/kotlin-native/backend.native/tests/codegen/boxing/boxing2.kt +++ b/kotlin-native/backend.native/tests/codegen/boxing/boxing2.kt @@ -9,18 +9,30 @@ import kotlin.test.* fun printInt(x: Int) = println(x) fun printBoolean(x: Boolean) = println(x) +fun printUInt(x: UInt) = println(x) fun foo(arg: Any) { if (arg is Int) printInt(arg) else if (arg is Boolean) printBoolean(arg) + else if (arg is UInt) + printUInt(arg) else println("other") } @Test fun runTest() { foo(1) + foo(2u) foo(true) foo("Hello") + val nonConstInt = 1 + val nonConstUInt = 2u + val nonConstBool = true + val nonConstString = "Hello" + foo(nonConstInt) + foo(nonConstUInt) + foo(nonConstBool) + foo(nonConstString) } \ No newline at end of file diff --git a/kotlin-native/backend.native/tests/codegen/boxing/boxing2.out b/kotlin-native/backend.native/tests/codegen/boxing/boxing2.out index 1171a5b5384..99fc0dc75b6 100644 --- a/kotlin-native/backend.native/tests/codegen/boxing/boxing2.out +++ b/kotlin-native/backend.native/tests/codegen/boxing/boxing2.out @@ -1,3 +1,8 @@ 1 +2 +true +other +1 +2 true other diff --git a/kotlin-native/backend.native/tests/codegen/boxing/boxing3.kt b/kotlin-native/backend.native/tests/codegen/boxing/boxing3.kt index 5b4a8cc6545..87c068e30ff 100644 --- a/kotlin-native/backend.native/tests/codegen/boxing/boxing3.kt +++ b/kotlin-native/backend.native/tests/codegen/boxing/boxing3.kt @@ -16,4 +16,6 @@ fun foo(arg: Int?) { @Test fun runTest() { foo(42) + val nonConst = 42 + foo(nonConst) } \ No newline at end of file diff --git a/kotlin-native/backend.native/tests/codegen/boxing/boxing3.out b/kotlin-native/backend.native/tests/codegen/boxing/boxing3.out index d81cc0710eb..daaac9e3030 100644 --- a/kotlin-native/backend.native/tests/codegen/boxing/boxing3.out +++ b/kotlin-native/backend.native/tests/codegen/boxing/boxing3.out @@ -1 +1,2 @@ 42 +42 diff --git a/kotlin-native/backend.native/tests/codegen/boxing/boxing4.kt b/kotlin-native/backend.native/tests/codegen/boxing/boxing4.kt index dc353896d54..63bf3a08e61 100644 --- a/kotlin-native/backend.native/tests/codegen/boxing/boxing4.kt +++ b/kotlin-native/backend.native/tests/codegen/boxing/boxing4.kt @@ -16,4 +16,6 @@ fun foo(arg: Any?) { @Test fun runTest() { foo(16) + val nonConst = 16 + foo(nonConst) } \ No newline at end of file diff --git a/kotlin-native/backend.native/tests/codegen/boxing/boxing4.out b/kotlin-native/backend.native/tests/codegen/boxing/boxing4.out index b6a7d89c68e..d5567d44519 100644 --- a/kotlin-native/backend.native/tests/codegen/boxing/boxing4.out +++ b/kotlin-native/backend.native/tests/codegen/boxing/boxing4.out @@ -1 +1,2 @@ 16 +16 diff --git a/kotlin-native/backend.native/tests/codegen/boxing/boxing5.kt b/kotlin-native/backend.native/tests/codegen/boxing/boxing5.kt index abca34851f4..bb232eb4bdc 100644 --- a/kotlin-native/backend.native/tests/codegen/boxing/boxing5.kt +++ b/kotlin-native/backend.native/tests/codegen/boxing/boxing5.kt @@ -16,4 +16,8 @@ fun foo(arg: Int?) { @Test fun runTest() { foo(null) foo(42) + val nonConstNull = null + val nonConstInt = 42 + foo(nonConstNull) + foo(nonConstInt) } \ No newline at end of file diff --git a/kotlin-native/backend.native/tests/codegen/boxing/boxing5.out b/kotlin-native/backend.native/tests/codegen/boxing/boxing5.out index ef5d1a8685d..cd2b0a0903f 100644 --- a/kotlin-native/backend.native/tests/codegen/boxing/boxing5.out +++ b/kotlin-native/backend.native/tests/codegen/boxing/boxing5.out @@ -1,2 +1,4 @@ 16 42 +16 +42 diff --git a/kotlin-native/backend.native/tests/codegen/boxing/boxing6.kt b/kotlin-native/backend.native/tests/codegen/boxing/boxing6.kt index dc5fe1d6e62..92903b1ddd9 100644 --- a/kotlin-native/backend.native/tests/codegen/boxing/boxing6.kt +++ b/kotlin-native/backend.native/tests/codegen/boxing/boxing6.kt @@ -16,4 +16,8 @@ fun foo(arg: Any) { @Test fun runTest() { foo(42) foo("Hello") + val nonConstInt = 42 + val nonConstString = "Hello" + foo(nonConstInt) + foo(nonConstString) } \ No newline at end of file diff --git a/kotlin-native/backend.native/tests/codegen/boxing/boxing6.out b/kotlin-native/backend.native/tests/codegen/boxing/boxing6.out index 5174adaee24..1221344683c 100644 --- a/kotlin-native/backend.native/tests/codegen/boxing/boxing6.out +++ b/kotlin-native/backend.native/tests/codegen/boxing/boxing6.out @@ -1,2 +1,4 @@ 42 16 +42 +16 diff --git a/kotlin-native/backend.native/tests/codegen/boxing/boxing7.kt b/kotlin-native/backend.native/tests/codegen/boxing/boxing7.kt index 6544e4ee9b6..4778ea10a54 100644 --- a/kotlin-native/backend.native/tests/codegen/boxing/boxing7.kt +++ b/kotlin-native/backend.native/tests/codegen/boxing/boxing7.kt @@ -21,4 +21,8 @@ fun foo(arg: Any) { @Test fun runTest() { foo(1) foo("Hello") + val nonConstInt = 1 + val nonConstString = "Hello" + foo(nonConstInt) + foo(nonConstString) } \ No newline at end of file diff --git a/kotlin-native/backend.native/tests/codegen/boxing/boxing7.out b/kotlin-native/backend.native/tests/codegen/boxing/boxing7.out index b261da18d51..d80fc78e03d 100644 --- a/kotlin-native/backend.native/tests/codegen/boxing/boxing7.out +++ b/kotlin-native/backend.native/tests/codegen/boxing/boxing7.out @@ -1,2 +1,4 @@ 1 0 +1 +0 diff --git a/kotlin-native/backend.native/tests/codegen/boxing/boxing8.kt b/kotlin-native/backend.native/tests/codegen/boxing/boxing8.kt index feff969e87c..9922a29e320 100644 --- a/kotlin-native/backend.native/tests/codegen/boxing/boxing8.kt +++ b/kotlin-native/backend.native/tests/codegen/boxing/boxing8.kt @@ -15,4 +15,9 @@ fun foo(vararg args: Any?) { @Test fun runTest() { foo(1, null, true, "Hello") + val nonConstInt = 1 + val nonConstNull = null + val nonConstBool = true + val nonConstString = "Hello" + foo(nonConstInt, nonConstNull, nonConstBool, nonConstString) } \ No newline at end of file diff --git a/kotlin-native/backend.native/tests/codegen/boxing/boxing8.out b/kotlin-native/backend.native/tests/codegen/boxing/boxing8.out index f0084dc294f..013406ebd4e 100644 --- a/kotlin-native/backend.native/tests/codegen/boxing/boxing8.out +++ b/kotlin-native/backend.native/tests/codegen/boxing/boxing8.out @@ -2,3 +2,7 @@ null true Hello +1 +null +true +Hello diff --git a/kotlin-native/backend.native/tests/codegen/initializers/static.kt b/kotlin-native/backend.native/tests/codegen/initializers/static.kt new file mode 100644 index 00000000000..9aa9a367dde --- /dev/null +++ b/kotlin-native/backend.native/tests/codegen/initializers/static.kt @@ -0,0 +1,175 @@ +/* + * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ +@file:OptIn(kotlin.ExperimentalStdlibApi::class) + +package codegen.initializers.static + +import kotlin.test.* +import kotlin.native.internal.* +import kotlin.reflect.* + +class Delegate { + operator fun getValue(thisRef: Any?, property: KProperty<*>) : String { + assertTrue(property.isPermanent()); + assertTrue(property.returnType.isPermanent()) + return property.name + } +} + +class A { + val z by Delegate() +} + +fun f() = 5 + +@Test fun testPermanent() { + val x = typeOf>() + assertTrue(x.isPermanent()) + val a = A() + assertTrue(a.z.isPermanent()) + assertEquals("z", a.z) + val t = ::f + assertTrue(t.isPermanent()) + assertEquals(5, t()) + val z = { 6 } + assertTrue(z.isPermanent()) + assertEquals(6, z()) +} + +@Test fun testVarargChange() { + fun varargGetter(position:Int, vararg x: Int): Int { + x[position] *= 5; + return x[position] + } + + repeat(3) { + assertEquals(10, varargGetter(0, 2, 3, 4)) + assertEquals(10, varargGetter(0, 2, 3, 4)) + assertEquals(15, varargGetter(1, 2, 3, 4)) + assertEquals(15, varargGetter(1, 2, 3, 4)) + assertEquals(20, varargGetter(2, 2, 3, 4)) + assertEquals(20, varargGetter(2, 2, 3, 4)) + assertFailsWith { varargGetter(3, 2, 3, 4) } + } +} + +@Test fun testArrays() { + assertEquals("1, 2, 3", intArrayOf(1, 2, 3).joinToString()) + assertEquals("4, 5, 6", longArrayOf(4.toLong(), 5.toLong(), 6.toLong()).joinToString()) + assertEquals("7, 8, 9", shortArrayOf(7.toShort(), 8.toShort(), 9.toShort()).joinToString()) + assertEquals("10, 11, 12", byteArrayOf(10.toByte(), 11.toByte(), 12.toByte()).joinToString()) + assertEquals("abc", charArrayOf('a', 'b', 'c').joinToString("")) + assertEquals("1.5, 2.5, -3.5", floatArrayOf(1.5f, 2.5f, -3.5f).joinToString()) + assertEquals("4.5, 5.5, -6.5", doubleArrayOf(4.5, 5.5, -6.5).joinToString()) + assertEquals("13, 14, 4294967295", uintArrayOf(13u, 14u, 4294967295u).joinToString()) + assertEquals("15, 16, 17", ulongArrayOf(15.toULong(), 16.toULong(), 17.toULong()).joinToString()) + assertEquals("18, 19, 40000", ushortArrayOf(18.toUShort(), 19.toUShort(), 40000.toUShort()).joinToString()) + assertEquals("20, 21, 200", ubyteArrayOf(20.toUByte(), 21.toUByte(), 200.toUByte()).joinToString()) + + assertEquals("abc, def, ghi", arrayOf("abc", "def", "ghi").joinToString()) + assertEquals("1, 2, 3", arrayOf(1, 2, 3).joinToString()) + assertEquals("4, 5, 6", arrayOf(4.toLong(), 5.toLong(), 6.toLong()).joinToString()) + assertEquals("7, 8, 9", arrayOf(7.toShort(), 8.toShort(), 9.toShort()).joinToString()) + assertEquals("10, 11, 12", arrayOf(10.toByte(), 11.toByte(), 12.toByte()).joinToString()) + assertEquals("abc", arrayOf('a', 'b', 'c').joinToString("")) + assertEquals("1.5, 2.5, -3.5", arrayOf(1.5f, 2.5f, -3.5f).joinToString()) + assertEquals("4.5, 5.5, -6.5", arrayOf(4.5, 5.5, -6.5).joinToString()) + assertEquals("13, 14, 4294967295", arrayOf(13u, 14u, 4294967295u).joinToString()) + assertEquals("15, 16, 17", arrayOf(15.toULong(), 16.toULong(), 17.toULong()).joinToString()) + assertEquals("18, 19, 40000", arrayOf(18.toUShort(), 19.toUShort(), 40000.toUShort()).joinToString()) + assertEquals("20, 21, 200", arrayOf(20.toUByte(), 21.toUByte(), 200.toUByte()).joinToString()) + + assertEquals("abc, 1, 2, 3, 4, a, 1.5, 2.5, 5, 6, 7, 8", + arrayOf("abc", 1, 2.toLong(), 3.toShort(), 4.toByte(), 'a', 1.5f, 2.5, 5u, 6.toULong(), 7.toUShort(), 8.toUByte()).joinToString()) +} + +@Test fun testList() { + assertEquals("abc, def, ghi", listOf("abc", "def", "ghi").joinToString()) + assertEquals("1, 2, 3", listOf(1, 2, 3).joinToString()) + assertEquals("4, 5, 6", listOf(4.toLong(), 5.toLong(), 6.toLong()).joinToString()) + assertEquals("7, 8, 9", listOf(7.toShort(), 8.toShort(), 9.toShort()).joinToString()) + assertEquals("10, 11, 12", listOf(10.toByte(), 11.toByte(), 12.toByte()).joinToString()) + assertEquals("abc", listOf('a', 'b', 'c').joinToString("")) + assertEquals("1.5, 2.5, -3.5", listOf(1.5f, 2.5f, -3.5f).joinToString()) + assertEquals("4.5, 5.5, -6.5", listOf(4.5, 5.5, -6.5).joinToString()) + assertEquals("13, 14, 4294967295", listOf(13u, 14u, 4294967295u).joinToString()) + assertEquals("15, 16, 17", listOf(15.toULong(), 16.toULong(), 17.toULong()).joinToString()) + assertEquals("18, 19, 40000", listOf(18.toUShort(), 19.toUShort(), 40000.toUShort()).joinToString()) + assertEquals("20, 21, 200", listOf(20.toUByte(), 21.toUByte(), 200.toUByte()).joinToString()) + + assertEquals("abc, 1, 2, 3, 4, a, 1.5, 2.5, 5, 6, 7, 8", + listOf("abc", 1, 2.toLong(), 3.toShort(), 4.toByte(), 'a', 1.5f, 2.5, 5u, 6.toULong(), 7.toUShort(), 8.toUByte()).joinToString()) +} + +@Test fun testKType() { + val ktype = typeOf>?>() + assertTrue(ktype.isPermanent()) + assertEquals("Map", (ktype.classifier as? KClass<*>)?.simpleName) + assertSame(Map::class, ktype.classifier) + assertTrue(ktype.isMarkedNullable) + assertTrue(ktype.arguments.isPermanent()) + assertEquals(2, ktype.arguments.size) + assertSame(KVariance.IN, ktype.arguments[0].variance) + assertSame(KVariance.OUT, ktype.arguments[1].variance) + + val arg0type = ktype.arguments[0].type!! + assertTrue(arg0type.isPermanent()) + assertEquals("String", (arg0type.classifier as? KClass<*>)?.simpleName) + assertSame(String::class, arg0type.classifier) + assertTrue(arg0type.isMarkedNullable) + assertTrue(arg0type.arguments.isPermanent()) + assertTrue(arg0type.arguments.isEmpty()) + + val arg1type = ktype.arguments[1].type!! + assertTrue(arg1type.isPermanent()) + assertEquals("List", (arg1type.classifier as? KClass<*>)?.simpleName) + assertSame(List::class, arg1type.classifier) + assertFalse(arg1type.isMarkedNullable) + assertTrue(arg1type.arguments.isPermanent()) + assertTrue(arg1type.arguments.size == 1) + assertSame(null, arg1type.arguments[0].variance) + assertSame(null, arg1type.arguments[0].type) +} +class R +interface S + +@Test fun testReifiedKType() { + + inline fun kTypeOf() where V : List, V : S, U : T = typeOf>() + + class XX(val x:List) : List by x, S + + val type = kTypeOf, ArrayList, XX>() + assertEquals("codegen.initializers.static.R, in U, out V, *>", type.toString()) + assertEquals("[T]", (type.arguments[1].type!!.classifier as KTypeParameter).upperBounds.toString()) + assertEquals("[kotlin.collections.List, codegen.initializers.static.S]", + (type.arguments[2].type!!.classifier as KTypeParameter).upperBounds.toString()) +} + +inline fun invokeAndReturnKClass(block: ()->Boolean) : KClass<*> { + try { + if (block()) { + return Double::class + } + } catch (e: Exception) { + return String::class + } finally { + return Int::class + } +} + +@Test fun testConstantObjectInFinally() { + for (i in 0..2) { + val clazz = invokeAndReturnKClass { + when (i) { + 0 -> true + 1 -> false + else -> TODO("test") + } + } + assertTrue(clazz.isPermanent()) + assertEquals("kotlin.Int", clazz.qualifiedName) + } +} \ No newline at end of file