Update box tests to 1.2.0-rc-39

This commit is contained in:
Pavel Punegov
2017-11-09 20:27:53 +03:00
committed by Pavel Punegov
parent 0121b6a185
commit ffa1ffa659
442 changed files with 20811 additions and 165 deletions
+1 -1
View File
@@ -33,7 +33,7 @@ repositories {
dependencies {
cli_bc project(path: ':backend.native', configuration: 'cli_bc')
update_tests (group: 'org', name: 'bt345', version: testDataVersion) {
update_tests (group: 'org', name: 'Kotlin_120_Compiler', version: testDataVersion) {
artifact {
name = 'internal/kotlin-test-data'
type = 'zip'
@@ -0,0 +1,10 @@
var result = "fail"
open class Base(val o: String, val k: String)
fun box(): String {
val obj1 = object : Base(k = { result = "O"; "K"}() , o = {result += "K"; "O"}()) {}
if (result != "OK") return "fail $result"
return obj1.o + obj1.k
}
@@ -0,0 +1,11 @@
var result = "fail"
open class Base(val o: String, val k: String)
class Derived : Base(k = { result = "O"; "K"}() , o = {result += "K"; "O"}()) {}
fun box(): String {
val derived = Derived()
if (result != "OK") return "fail $result"
return derived.o + derived.k
}
@@ -0,0 +1,54 @@
// NO_CHECK_LAMBDA_INLINING
// FILE: 1.kt
// WITH_RUNTIME
// TARGET_BACKEND: NATIVE
package test
open class A(val value: String)
var invokeOrder = ""
inline fun inlineFun(
vararg constraints: A,
receiver: String = { invokeOrder += " default receiver"; "DEFAULT" }(),
init: String
): String {
return constraints.map { it.value }.joinToString() + ", " + receiver + ", " + init
}
// FILE: 2.kt
import test.*
var result = ""
fun box(): String {
result = ""
invokeOrder = ""
result = inlineFun(constraints = { invokeOrder += "constraints";A("C") }(),
receiver = { invokeOrder += " receiver"; "R" }(),
init = { invokeOrder += " init"; "I" }())
if (result != "C, R, I") return "fail 1: $result"
if (invokeOrder != "constraints receiver init") return "fail 2: $invokeOrder"
result = ""
invokeOrder = ""
result = inlineFun(init = { invokeOrder += "init"; "I" }(),
constraints = { invokeOrder += "constraints";A("C") }(),
receiver = { invokeOrder += " receiver"; "R" }()
)
if (result != "C, R, I") return "fail 3: $result"
//Change test after KT-17691 FIX
if (invokeOrder != "init receiverconstraints") return "fail 4: $invokeOrder"
result = ""
invokeOrder = ""
result = inlineFun(init = { invokeOrder += "init"; "I" }(),
constraints = { invokeOrder += " constraints";A("C") }())
if (result != "C, DEFAULT, I") return "fail 5: $result"
if (invokeOrder != "init constraints default receiver") return "fail 6: $invokeOrder"
return "OK"
}
@@ -45,6 +45,7 @@ fun testFloatArray() {
}
throw AssertionError()
}
fun testDoubleArray() {
DoubleArray(5) { i ->
if (i == 3) return
@@ -1,9 +1,6 @@
// WITH_RUNTIME
import kotlin.test.assertTrue
import kotlin.test.assertFalse
import kotlin.test.assertEquals
import kotlin.test.assertFails
import kotlin.test.*
fun box(): String {
assertTrue(eqBoolean(booleanArrayOf(false), BooleanArray(1)))
@@ -0,0 +1,46 @@
inline fun ltx(a: Comparable<Any>, b: Any) = a < b
inline fun lex(a: Comparable<Any>, b: Any) = a <= b
inline fun gex(a: Comparable<Any>, b: Any) = a >= b
inline fun gtx(a: Comparable<Any>, b: Any) = a > b
inline fun lt(a: Any, b: Any) = ltx(a as Comparable<Any>, b)
inline fun le(a: Any, b: Any) = lex(a as Comparable<Any>, b)
inline fun ge(a: Any, b: Any) = gex(a as Comparable<Any>, b)
inline fun gt(a: Any, b: Any) = gtx(a as Comparable<Any>, b)
val ONE = 1
val ONEL = 1L
fun box(): String {
return when {
!lt(ONE, 42) -> "Fail 1 LT"
lt(42, ONE) -> "Fail 2 LT"
!le(ONE, 42) -> "Fail 1 LE"
le(42, ONE) -> "Fail 2 LE"
!le(1, ONE) -> "Fail 3 LE"
!ge(42, ONE) -> "Fail 1 GE"
ge(ONE, 42) -> "Fail 2 GE"
!ge(1, ONE) -> "Fail 3 GE"
gt(ONE, 42) -> "Fail 1 GT"
!gt(42, ONE) -> "Fail 2 GT"
!lt(ONEL, 42L) -> "Fail 1 LT L"
lt(42L, ONEL) -> "Fail 2 LT L"
!le(ONEL, 42L) -> "Fail 1 LE L"
le(42L, ONEL) -> "Fail 2 LE L"
!le(ONEL, 1L) -> "Fail 3 LE L"
!ge(42L, ONEL) -> "Fail 1 GE L"
ge(ONEL, 42L) -> "Fail 2 GE L"
!ge(ONEL, 1L) -> "Fail 3 GE L"
gt(ONEL, 42L) -> "Fail 1 GT L"
!gt(42L, ONEL) -> "Fail 2 GT L"
else -> "OK"
}
}
@@ -0,0 +1,21 @@
inline fun eq(a: Any, b: Any) = a == b
inline fun ne(a: Any, b: Any) = a != b
val ONE = 1
val ONEL = 1L
fun box(): String {
return when {
eq(ONE, 2) -> "Fail 1"
!eq(ONE, 1) -> "Fail 2"
!ne(ONE, 2) -> "Fail 3"
ne(ONE, 1) -> "Fail 4"
eq(ONEL, 42L) -> "Fail 1L"
!eq(ONEL, 1L) -> "Fail 2L"
!ne(ONEL, 42L) -> "Fail 3L"
ne(ONEL, 1L) -> "Fail 4L"
else -> "OK"
}
}
@@ -0,0 +1,82 @@
// IGNORE_BACKEND: JS, NATIVE
inline fun ltx(a: Comparable<Any>, b: Any) = a < b
inline fun lex(a: Comparable<Any>, b: Any) = a <= b
inline fun gex(a: Comparable<Any>, b: Any) = a >= b
inline fun gtx(a: Comparable<Any>, b: Any) = a > b
inline fun lt(a: Any, b: Any) = ltx(a as Comparable<Any>, b)
inline fun le(a: Any, b: Any) = lex(a as Comparable<Any>, b)
inline fun ge(a: Any, b: Any) = gex(a as Comparable<Any>, b)
inline fun gt(a: Any, b: Any) = gtx(a as Comparable<Any>, b)
val PLUS0F = 0.0F
val MINUS0F = -0.0F
val PLUS0D = 0.0
val MINUS0D = -0.0
fun box(): String {
return when {
!lt(1.0F, 42.0F) -> "Fail 1 LT F"
lt(42.0F, 1.0F) -> "Fail 2 LT F"
!le(1.0F, 42.0F) -> "Fail 1 LE F"
le(42.0F, 1.0F) -> "Fail 2 LE F"
!le(1.0F, 1.0F) -> "Fail 3 LE F"
!ge(42.0F, 1.0F) -> "Fail 1 GE F"
ge(1.0F, 42.0F) -> "Fail 2 GE F"
!ge(1.0F, 1.0F) -> "Fail 3 GE F"
gt(1.0F, 42.0F) -> "Fail 1 GT F"
!gt(42.0F, 1.0F) -> "Fail 2 GT F"
!lt(1.0, 42.0) -> "Fail 1 LT D"
lt(42.0, 1.0) -> "Fail 2 LT D"
!le(1.0, 42.0) -> "Fail 1 LE D"
le(42.0, 1.0) -> "Fail 2 LE D"
!le(1.0, 1.0) -> "Fail 3 LE D"
!ge(42.0, 1.0) -> "Fail 1 GE D"
ge(1.0, 42.0) -> "Fail 2 GE D"
!ge(1.0, 1.0) -> "Fail 3 GE D"
gt(1.0, 42.0) -> "Fail 1 GT D"
!gt(42.0, 1.0) -> "Fail 2 GT D"
!lt(MINUS0F, PLUS0F) -> "Fail 1 LT +-0 F"
lt(PLUS0F, MINUS0F) -> "Fail 2 LT +-0 F"
!le(MINUS0F, PLUS0F) -> "Fail 1 LE +-0 F"
le(PLUS0F, MINUS0F) -> "Fail 2 LE +-0 F"
!le(MINUS0F, MINUS0F) -> "Fail 3 LE +-0 F"
!le(PLUS0F, PLUS0F) -> "Fail 3 LE +-0 F"
ge(MINUS0F, PLUS0F) -> "Fail 1 GE +-0 F"
!ge(PLUS0F, MINUS0F) -> "Fail 2 GE +-0 F"
!ge(MINUS0F, MINUS0F) -> "Fail 3 GE +-0 F"
!ge(PLUS0F, PLUS0F) -> "Fail 3 GE +-0 F"
gt(MINUS0F, PLUS0F) -> "Fail 1 GT +-0 F"
!gt(PLUS0F, MINUS0F) -> "Fail 2 GT +-0 F"
!lt(MINUS0D, PLUS0D) -> "Fail 1 LT +-0 D"
lt(PLUS0D, MINUS0D) -> "Fail 2 LT +-0 D"
!le(MINUS0D, PLUS0D) -> "Fail 1 LE +-0 D"
le(PLUS0D, MINUS0D) -> "Fail 2 LE +-0 D"
!le(MINUS0D, MINUS0D) -> "Fail 3 LE +-0 D"
!le(PLUS0D, PLUS0D) -> "Fail 3 LE +-0 D"
ge(MINUS0D, PLUS0D) -> "Fail 1 GE +-0 D"
!ge(PLUS0D, MINUS0D) -> "Fail 2 GE +-0 D"
!ge(MINUS0D, MINUS0D) -> "Fail 3 GE +-0 D"
!ge(PLUS0D, PLUS0D) -> "Fail 3 GE +-0 D"
gt(MINUS0D, PLUS0D) -> "Fail 1 GT +-0 D"
!gt(PLUS0D, MINUS0D) -> "Fail 2 GT +-0 D"
else -> "OK"
}
}
@@ -0,0 +1,8 @@
fun box(): String {
val a: Any = 1
val b: Any = 42
val test = (a as Comparable<Any>).compareTo(b)
if (test != -1) return "Fail: $test"
return "OK"
}
@@ -0,0 +1,20 @@
// TARGET_BACKEND: JVM
fun test(a: Any) = when (a::class) {
String::class -> "String"
Int::class -> "Int"
Boolean::class -> "Boolean"
else -> "Else"
}
fun box(): String {
val s = ""
val i = 0
val b = false
if (test(s) != "String") return "Fail 1"
if (test(i) != "Int") return "Fail 2"
if (test(b) != "Boolean") return "Fail 3"
return "OK"
}
@@ -0,0 +1,17 @@
// TARGET_BACKEND: JVM
fun box(): String {
42.doSwitchInt()
"".doSwitchString()
return "OK"
}
inline fun <reified E> E.doSwitchInt(): String = when (E::class) {
Int::class -> "success!"
else -> throw AssertionError()
}
inline fun <reified E> E.doSwitchString(): String = when(E::class) {
String::class -> "success!"
else -> throw AssertionError()
}
@@ -0,0 +1,21 @@
fun foo(p: Int?): Boolean {
return M<Int>(p)?.nulled() == 1
}
fun foo2(p: Int?): Boolean {
return 1 == M<Int>(p)?.nulled()
}
class M<T: Any>(val z: T?) {
fun nulled(): T? = z
}
fun box(): String {
if (foo(null)) return "fail 1"
if (!foo(1)) return "fail 2"
if (foo2(null)) return "fail 1"
if (!foo2(1)) return "fail 2"
return "OK"
}
@@ -0,0 +1,6 @@
//WITH_RUNTIME
fun box(): String {
val map: Map<String, Boolean>? = mapOf()
return if (map?.get("") == true) "fail" else "OK"
}
@@ -0,0 +1,34 @@
// IGNORE_BACKEND: JS, NATIVE
// FILE: M.java
public class M {
private final Integer value;
public M(Integer value) {
this.value = value;
}
public Integer nulled() {
return value;
}
}
// FILE: Kotlin.kt
fun foo(p: Int?): Boolean {
return M(p)?.nulled() == 1
}
fun foo2(p: Int?): Boolean {
return 1 == M(p)?.nulled()
}
fun box(): String {
if (foo(null)) return "fail 1"
if (!foo(1)) return "fail 2"
if (foo2(null)) return "fail 1"
if (!foo2(1)) return "fail 2"
return "OK"
}
@@ -0,0 +1,23 @@
fun foo(p: Int?): Boolean {
return M<Int>(p)?.chain()?.nulled() == 1
}
fun foo2(p: Int?): Boolean {
return 1 == M<Int>(p)?.chain()?.nulled()
}
class M<T: Any>(val z: T?) {
fun nulled(): T? = z
fun chain(): M<T>? = this
}
fun box(): String {
if (foo(null)) return "fail 1"
if (!foo(1)) return "fail 2"
if (foo2(null)) return "fail 1"
if (!foo2(1)) return "fail 2"
return "OK"
}
@@ -0,0 +1,20 @@
// WITH_RUNTIME
fun box(): String {
val intList = listOf(1, 2, 3)
val longList = listOf(1L, 2L, 3L)
val intListMin = intList.minBy { it }
if (intListMin != 1) return "Fail intListMin=$intListMin"
val intListMax = intList.maxBy { it }
if (intListMax != 3) return "Fail intListMax=$intListMax"
val longListMin = longList.minBy { it }
if (longListMin != 1L) return "Fail longListMin=$longListMin"
val longListMax = longList.maxBy { it }
if (longListMax != 3L) return "Fail longListMax=$longListMax"
return "OK"
}
@@ -0,0 +1,57 @@
// LANGUAGE_VERSION: 1.2
var result = ""
class A {
fun memberFunction() { result += "A.mf," }
fun aMemberFunction() { result += "A.amf," }
val memberProperty: Int get() = 42.also { result += "A.mp," }
val aMemberProperty: Int get() = 42.also { result += "A.amp," }
fun test(): String {
(::memberFunction)()
(::aExtensionFunction)()
(::memberProperty)()
(::aExtensionProperty)()
return result
}
inner class B {
fun memberFunction() { result += "B.mf," }
val memberProperty: Int get() = 42.also { result += "B.mp," }
fun test(): String {
(::aMemberFunction)()
(::aExtensionFunction)()
(::aMemberProperty)()
(::aExtensionProperty)()
(::memberFunction)()
(::memberProperty)()
(::bExtensionFunction)()
(::bExtensionProperty)()
return result
}
}
}
fun A.aExtensionFunction() { result += "A.ef," }
val A.aExtensionProperty: Int get() = 42.also { result += "A.ep," }
fun A.B.bExtensionFunction() { result += "B.ef," }
val A.B.bExtensionProperty: Int get() = 42.also { result += "B.ep," }
fun box(): String {
val a = A().test()
if (a != "A.mf,A.ef,A.mp,A.ep,") return "Fail $a"
result = ""
val b = A().B().test()
if (b != "A.amf,A.ef,A.amp,A.ep,B.mf,B.mp,B.ef,B.ep,") return "Fail $b"
return "OK"
}
@@ -3,7 +3,8 @@
// WITH_REFLECT
import kotlin.reflect.*
import kotlin.reflect.KMutableProperty1
import kotlin.reflect.full.*
class C {
var prop = 42
@@ -3,7 +3,7 @@
// WITH_REFLECT
import kotlin.reflect.*
import kotlin.reflect.full.*
class C {
fun foo() {}
@@ -0,0 +1,9 @@
// TARGET_BACKEND: JVM
// WITH_RUNTIME
fun box(): String {
fun foo(): Unit {}
assert(Unit.javaClass.equals(foo().javaClass))
assert(Unit.javaClass.equals(foo()::class.java))
return "OK"
}
@@ -0,0 +1,20 @@
abstract class A {
inner class InnerInA {
fun returnOk() = "OK"
}
}
class B : A()
fun foo(a: A): String {
if (a is B) {
val v = a::InnerInA
return v().returnOk()
}
return "error"
}
fun box(): String {
return foo(B())
}
@@ -0,0 +1,48 @@
fun box(): String {
val n: Any? = null
val intV: Any? = 23
val floatV: Any? = 23.4F
val doubleV: Any? = 23.45
val longV: Any? = 234L
val stringV: Any? = "foo"
val booleanV: Any? = true
val functionV: Any? = { x: Int -> x + 1 }
if (n !is Int?) return "fail: null !is Int?"
if (n !is Float?) return "fail: null !is Float?"
if (n !is Double?) return "fail: null !is Double?"
if (n !is String?) return "fail: null !is String?"
if (n !is Boolean?) return "fail: null !is Boolean?"
if (n !is Function1<*, *>?) return "fail: null !is Function?"
if (n is Int) return "fail: null is Int"
if (n is Float) return "fail: null is Float"
if (n is Double) return "fail: null is Double"
if (n is String) return "fail: null is String"
if (n is Boolean) return "fail: null is Boolean"
if (n is Function1<*, *>) return "fail: null is Function"
if (intV !is Int?) return "fail: 23 !is Int?"
if (intV is String?) return "fail: 23 is String?"
if (floatV !is Float?) return "fail: 23.4F !is Float?"
if (floatV is String?) return "fail: 23.4F is String?"
if (doubleV !is Double?) return "fail: 23.45 !is Double?"
if (doubleV is String?) return "fail: 23.45 is String?"
if (longV !is Long?) return "fail: 234L !is Long?"
if (longV is String?) return "fail: 234L is String?"
if (stringV !is String?) return "fail: 'foo' !is String?"
if (stringV is Double?) return "fail: 'foo' is Double?"
if (booleanV !is Boolean?) return "fail: true !is Boolean?"
if (booleanV is Double?) return "fail: true is Double?"
if (functionV !is Function1<*, *>?) return "fail: <function> !is Function?"
if (functionV is String?) return "fail: <function> is String?"
return "OK"
}
@@ -1,6 +1,3 @@
// IGNORE_BACKEND: JS
// JS backend does not support Unit well. See KT-13932
val foo: () -> Unit = {}
fun box(): String {
@@ -1,6 +1,3 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
fun println(s: String) {
}
@@ -1,6 +1,3 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
fun println(s: String) {
}
@@ -0,0 +1,26 @@
class A
class B
class C
fun foo(parameters: Any?): Any? {
var payload: Any? = null
if (parameters != null) {
if (parameters is A || parameters is B) {
payload = parameters
} else {
payload = "O"
}
}
if (payload is String) {
payload += "K"
}
return payload
}
fun box(): String =
"${foo(C())}"
@@ -0,0 +1,19 @@
// WITH_RUNTIME
enum class ResultType constructor(val reason: String) {
SOMETHING("123"),
OK("OK"),
UNKNOWN("FAIL");
companion object {
fun getByVal(reason: String): ResultType {
return ResultType.values().firstOrDefault({ it.reason == reason }, UNKNOWN)
}
}
}
inline fun <T> Array<out T>.firstOrDefault(predicate: (T) -> Boolean, default: T): T {
return firstOrNull(predicate) ?: default
}
fun box(): String = ResultType.getByVal("OK").reason
@@ -0,0 +1,15 @@
// WITH_RUNTIME
class Host {
val ok = "OK"
fun foo() = run { bar(ok) }
companion object {
val ok = 0
fun bar(s: String) = s.substring(ok)
}
}
fun box() = Host().foo()
@@ -1,7 +1,3 @@
// TODO: Enable for JS when it supports Java class library.
// IGNORE_BACKEND: JS
// fails on JS with TypeError: imported$plus is not a function, it is undefined.
operator fun <K, V> MutableMap<K, V>.set(key : K, value : V) = put(key, value)
fun box() : String {
@@ -0,0 +1,9 @@
// IGNORE_BACKEND: JS
class `A!u00A0`() {
val ok = "OK"
}
fun box(): String {
return `A!u00A0`().ok
}
@@ -1,5 +1,5 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS, NATIVE
// IGNORE_BACKEND: NATIVE
object A {
const val a: String = "$"
@@ -7,7 +7,7 @@ object A {
const val c = 10000
val bNonConst = "1234$a"
val bNullable: String = "1234$a"
val bNullable: String? = "1234$a"
}
object B {
@@ -16,7 +16,7 @@ object B {
const val c = 10000
val bNonConst = "1234$a"
val bNullable: String = "1234$a"
val bNullable: String? = "1234$a"
}
fun box(): String {
@@ -26,8 +26,8 @@ fun box(): String {
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"
if (A.bNonConst !== B.bNonConst) return "Fail 4: A.bNonConst !== B.bNonConst"
if (A.bNullable !== B.bNullable) return "Fail 5: A.bNullable !== B.bNullable"
return "OK"
}
@@ -0,0 +1,34 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// LANGUAGE_VERSION: 1.0
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 4: A.bNonConst === B.bNonConst"
if (A.bNullable === B.bNullable) return "Fail 5: A.bNullable === B.bNullable"
return "OK"
}
@@ -0,0 +1,39 @@
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// FILE: test.kt
fun box(): String {
var count = 0
while (true) {
Foo(
logged("i", if (count == 0) 1 else break),
logged("j", 2)
)
count++
}
val result = log.toString()
if (result != "<clinit>ij<init>") return "Fail: '$result'"
return "OK"
}
// FILE: util.kt
val log = StringBuilder()
fun <T> logged(msg: String, value: T): T {
log.append(msg)
return value
}
// FILE: Foo.kt
class Foo(i: Int, j: Int) {
init {
log.append("<init>")
}
companion object {
init {
log.append("<clinit>")
}
}
}
@@ -0,0 +1,40 @@
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// FILE: test.kt
fun box(): String {
var count = 0
while (true) {
count++
if (count > 1) break
Foo(
logged("i", if (count == 1) 1 else continue),
logged("j", 2)
)
}
val result = log.toString()
if (result != "<clinit>ij<init>") return "Fail: '$result'"
return "OK"
}
// FILE: util.kt
val log = StringBuilder()
fun <T> logged(msg: String, value: T): T {
log.append(msg)
return value
}
// FILE: Foo.kt
class Foo(i: Int, j: Int) {
init {
log.append("<init>")
}
companion object {
init {
log.append("<clinit>")
}
}
}
@@ -0,0 +1,41 @@
// TARGET_BACKEND: JVM
// WITH_RUNTIME
inline fun ok(): String {
return foo(1, 1.0, 1.0f, 1L, "O", C(if (bar()) return "zap" else "K"))
}
fun box(): String {
val ok = ok()
if (ok != "OK") return "Fail: $ok"
val r = log.toString()
if (r != "<clinit>;bar;<init>;foo;") return "Fail: '$r'"
return "OK"
}
// FILE: C.kt
class C(val str: String) {
init {
log.append("<init>;")
}
companion object {
init {
log.append("<clinit>;")
}
}
}
// FILE: util.kt
fun foo(x: Int, a: Double, b: Float, y: Long, z: String, c: C) =
logged("foo;", z + c.str)
fun bar() = logged("bar;", false)
val log = StringBuilder()
fun <T> logged(msg: String, value: T): T {
log.append(msg)
return value
}
@@ -0,0 +1,40 @@
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// FILE: test.kt
fun box(): String {
Foo(
logged("i", 1.let { it }),
logged("j",
Foo(
logged("k", 2.let { it }),
null
)
)
)
val result = log.toString()
if (result != "<clinit>ik<init>j<init>") return "Fail: '$result'"
return "OK"
}
// FILE: util.kt
val log = StringBuilder()
fun <T> logged(msg: String, value: T): T {
log.append(msg)
return value
}
// FILE: Foo.kt
class Foo(i: Int, j: Foo?) {
init {
log.append("<init>")
}
companion object {
init {
log.append("<clinit>")
}
}
}
@@ -0,0 +1,41 @@
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// KOTLIN_CONFIGURATION_FLAGS: CONSTRUCTOR_CALL_NORMALIZATION_MODE=disable
// FILE: test.kt
fun box(): String {
Foo(
logged("i", 1.let { it }),
logged("j",
Foo(
logged("k", 2.let { it }),
null
)
)
)
val result = log.toString()
if (result != "<clinit>ik<init>j<init>") return "Fail: '$result'"
return "OK"
}
// FILE: util.kt
val log = StringBuilder()
fun <T> logged(msg: String, value: T): T {
log.append(msg)
return value
}
// FILE: Foo.kt
class Foo(i: Int, j: Foo?) {
init {
log.append("<init>")
}
companion object {
init {
log.append("<clinit>")
}
}
}
@@ -0,0 +1,41 @@
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// KOTLIN_CONFIGURATION_FLAGS: CONSTRUCTOR_CALL_NORMALIZATION_MODE=enable
// FILE: test.kt
fun box(): String {
Foo(
logged("i", 1.let { it }),
logged("j",
Foo(
logged("k", 2.let { it }),
null
)
)
)
val result = log.toString()
if (result != "ik<clinit><init>j<init>") return "Fail: '$result'"
return "OK"
}
// FILE: util.kt
val log = StringBuilder()
fun <T> logged(msg: String, value: T): T {
log.append(msg)
return value
}
// FILE: Foo.kt
class Foo(i: Int, j: Foo?) {
init {
log.append("<init>")
}
companion object {
init {
log.append("<clinit>")
}
}
}
@@ -0,0 +1,41 @@
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// KOTLIN_CONFIGURATION_FLAGS: CONSTRUCTOR_CALL_NORMALIZATION_MODE=preserve-class-initialization
// FILE: test.kt
fun box(): String {
Foo(
logged("i", 1.let { it }),
logged("j",
Foo(
logged("k", 2.let { it }),
null
)
)
)
val result = log.toString()
if (result != "<clinit>ik<init>j<init>") return "Fail: '$result'"
return "OK"
}
// FILE: util.kt
val log = StringBuilder()
fun <T> logged(msg: String, value: T): T {
log.append(msg)
return value
}
// FILE: Foo.kt
class Foo(i: Int, j: Foo?) {
init {
log.append("<init>")
}
companion object {
init {
log.append("<clinit>")
}
}
}
@@ -0,0 +1,43 @@
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// FILE: test.kt
fun box(): String {
Outer().Inner(
logged("i;", 1.let { it }),
logged("j;", 2.let { it })
)
val result = log.toString()
if (result != "Foo.<clinit>;i;j;Foo.<init>;Inner.<init>;") return "Fail: '$result'"
return "OK"
}
// FILE: util.kt
val log = StringBuilder()
fun <T> logged(msg: String, value: T): T {
log.append(msg)
return value
}
// FILE: Foo.kt
open class Foo {
init {
log.append("Foo.<init>;")
}
companion object {
init {
log.append("Foo.<clinit>;")
}
}
}
class Outer {
inner class Inner(val x: Int, val y: Int) : Foo() {
init {
log.append("Inner.<init>;")
}
}
}
@@ -0,0 +1,41 @@
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// FILE: test.kt
fun box(): String {
class Local(val i: Int, val j: Int) : Foo() {
init {
log.append("Local.<init>;")
}
}
Local(
logged("i;", 1.let { it }),
logged("j;", 2.let { it })
)
val result = log.toString()
if (result != "Foo.<clinit>;i;j;Foo.<init>;Local.<init>;") return "Fail: '$result'"
return "OK"
}
// FILE: util.kt
val log = StringBuilder()
fun <T> logged(msg: String, value: T): T {
log.append(msg)
return value
}
// FILE: Foo.kt
open class Foo {
init {
log.append("Foo.<init>;")
}
companion object {
init {
log.append("Foo.<clinit>;")
}
}
}
@@ -0,0 +1,36 @@
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// FILE: test.kt
fun box(): String {
for (count in 0..3) {
val test = Foo(count, Foo(1, "x", 2), if (count > 0) break else 3)
if (count > 0) return "Fail: count = $count"
if (test.toString() != "Foo(0,Foo(1,x,2),3)") return "Fail: ${test.toString()}"
}
return "OK"
}
// FILE: util.kt
val log = StringBuilder()
fun <T> logged(msg: String, value: T): T {
log.append(msg)
return value
}
// FILE: Foo.kt
class Foo(val a: Int, val b: Any, val c: Int) {
init {
log.append("<init>")
}
override fun toString() = "Foo($a,$b,$c)"
companion object {
init {
log.append("<clinit>")
}
}
}
@@ -0,0 +1,43 @@
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// FILE: test.kt
fun box(): String {
run L1@{
var count = 0
run {
while (true) {
Foo(
logged("i", if (count == 0) 1 else return@L1),
logged("j", 2)
)
count++
}
}
}
val result = log.toString()
if (result != "<clinit>ij<init>") return "Fail: '$result'"
return "OK"
}
// FILE: util.kt
val log = StringBuilder()
fun <T> logged(msg: String, value: T): T {
log.append(msg)
return value
}
// FILE: Foo.kt
class Foo(i: Int, j: Int) {
init {
log.append("<init>")
}
companion object {
init {
log.append("<clinit>")
}
}
}
@@ -0,0 +1,36 @@
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// FILE: test.kt
fun box(): String {
for (count in 0..3) {
val test = Foo(count, Foo(1, "x", if (count > 0) break else 2), 3)
if (count > 0) return "Fail: count = $count"
if (test.toString() != "Foo(0,Foo(1,x,2),3)") return "Fail: ${test.toString()}"
}
return "OK"
}
// FILE: util.kt
val log = StringBuilder()
fun <T> logged(msg: String, value: T): T {
log.append(msg)
return value
}
// FILE: Foo.kt
class Foo(val a: Int, val b: Any, val c: Int) {
init {
log.append("<init>")
}
override fun toString() = "Foo($a,$b,$c)"
companion object {
init {
log.append("<clinit>")
}
}
}
@@ -0,0 +1,32 @@
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// FILE: test.kt
fun box(): String {
Foo(logged("i", 1), logged("j", 2))
val result = log.toString()
if (result != "<clinit>ij<init>") return "Fail: '$result'"
return "OK"
}
// FILE: util.kt
val log = StringBuilder()
fun <T> logged(msg: String, value: T): T {
log.append(msg)
return value
}
// FILE: Foo.kt
class Foo(i: Int, j: Int) {
init {
log.append("<init>")
}
companion object {
init {
log.append("<clinit>")
}
}
}
@@ -0,0 +1,35 @@
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// FILE: test.kt
fun box(): String {
Foo(
logged("i", try { 1 } catch (e: Exception) { 42 }),
logged("j", 2)
)
val result = log.toString()
if (result != "<clinit>ij<init>") return "Fail: '$result'"
return "OK"
}
// FILE: util.kt
val log = StringBuilder()
fun <T> logged(msg: String, value: T): T {
log.append(msg)
return value
}
// FILE: Foo.kt
class Foo(i: Int, j: Int) {
init {
log.append("<init>")
}
companion object {
init {
log.append("<clinit>")
}
}
}
@@ -0,0 +1,8 @@
fun <T> test(a: T, b: T, operation: (x: T) -> T) {
operation(if (3 > 2) a else b)
}
fun box(): String {
test(1, 1, { it })
return "OK"
}
@@ -0,0 +1,18 @@
// WITH_RUNTIME
fun box(): String {
val a = arrayListOf<String>()
while (true) {
if (a.size == 0) {
break
}
}
for (i in 1..2) {
a.add(try { foo() } catch (e: Throwable) { "OK" })
}
return a[0]
}
fun foo(): String = throw RuntimeException()
@@ -0,0 +1,136 @@
// WITH_RUNTIME
// WITH_COROUTINES
import helpers.*
import kotlin.coroutines.experimental.*
import kotlin.coroutines.experimental.intrinsics.*
interface AsyncGenerator<in T> {
suspend fun yield(value: T)
}
interface AsyncSequence<out T> {
operator fun iterator(): AsyncIterator<T>
}
interface AsyncIterator<out T> {
operator suspend fun hasNext(): Boolean
operator suspend fun next(): T
}
fun <T> asyncGenerate(block: suspend AsyncGenerator<T>.() -> Unit): AsyncSequence<T> = object : AsyncSequence<T> {
override fun iterator(): AsyncIterator<T> {
val iterator = AsyncGeneratorIterator<T>()
iterator.nextStep = block.createCoroutine(receiver = iterator, completion = iterator)
return iterator
}
}
class AsyncGeneratorIterator<T>: AsyncIterator<T>, AsyncGenerator<T>, Continuation<Unit> {
var computedNext = false
var nextValue: T? = null
var nextStep: Continuation<Unit>? = null
// if (computesNext) computeContinuation is Continuation<T>
// if (!computesNext) computeContinuation is Continuation<Boolean>
var computesNext = false
var computeContinuation: Continuation<*>? = null
override val context = EmptyCoroutineContext
suspend fun computeHasNext(): Boolean = suspendCoroutineOrReturn { c ->
computesNext = false
computeContinuation = c
nextStep!!.resume(Unit)
COROUTINE_SUSPENDED
}
suspend fun computeNext(): T = suspendCoroutineOrReturn { c ->
computesNext = true
computeContinuation = c
nextStep!!.resume(Unit)
COROUTINE_SUSPENDED
}
@Suppress("UNCHECKED_CAST")
fun resumeIterator(exception: Throwable?) {
if (exception != null) {
done()
computeContinuation!!.resumeWithException(exception)
return
}
if (computesNext) {
computedNext = false
(computeContinuation as Continuation<T>).resume(nextValue as T)
} else {
(computeContinuation as Continuation<Boolean>).resume(nextStep != null)
}
}
override suspend fun hasNext(): Boolean {
if (!computedNext) return computeHasNext()
return nextStep != null
}
override suspend fun next(): T {
if (!computedNext) return computeNext()
computedNext = false
return nextValue as T
}
private fun done() {
computedNext = true
nextStep = null
}
// Completion continuation implementation
override fun resume(value: Unit) {
done()
resumeIterator(null)
}
override fun resumeWithException(exception: Throwable) {
done()
resumeIterator(exception)
}
// Generator implementation
override suspend fun yield(value: T): Unit = suspendCoroutineOrReturn { c ->
computedNext = true
nextValue = value
nextStep = c
resumeIterator(null)
COROUTINE_SUSPENDED
}
}
fun builder(c: suspend () -> Unit) {
c.startCoroutine(EmptyContinuation)
}
fun cst(a: Any?): String? = a as String?
fun any(a: Any?): Any? = a
fun box(): String {
val seq = asyncGenerate {
yield("O")
yield("K")
}
var res = ""
builder {
// type of `prev` should be j/l/Object everywhere (even in a expected type position)
var prev: Any? = null
for (i in seq) {
res += i
prev = any(res)
// merge of NULL_VALUE and j/l/Object should result in common j/l/Object value
// but it was NULL_VALUE and we do not spill null values, we just put
// ACONST_NULL after suspension point instead
}
res = cst(prev) ?: "fail 1"
}
return res
}
@@ -0,0 +1,35 @@
// WITH_RUNTIME
// WITH_COROUTINES
import helpers.*
import kotlin.coroutines.experimental.*
import kotlin.coroutines.experimental.intrinsics.*
class Controller {
var result = ""
suspend fun <T> suspendWithResult(value: T): T = suspendCoroutineOrReturn { c ->
c.resume(value)
COROUTINE_SUSPENDED
}
}
fun builder(c: suspend Controller.() -> Unit): String {
val controller = Controller()
c.startCoroutine(controller, EmptyContinuation)
return controller.result
}
fun box(): String {
val value = builder {
for (x: Long in 20L..30L step 5L) {
listOf("#").forEach {
result += it + suspendWithResult(x).toString()
}
}
result += "."
}
if (value != "#20#25#30.") return "fail: suspend in for body: $value"
return "OK"
}
@@ -0,0 +1,33 @@
// TARGET_BACKEND: JVM
// WITH_REFLECT
// WITH_COROUTINES
import helpers.*
import kotlin.coroutines.experimental.*
import kotlin.coroutines.experimental.intrinsics.*
class A<T : String> {
suspend fun foo() {}
suspend fun bar(): T {
foo()
return suspendCoroutineOrReturn { x ->
x.resume(x.toString() as T)
COROUTINE_SUSPENDED
}
}
}
fun builder(c: suspend () -> Unit) {
c.startCoroutine(EmptyContinuation)
}
fun box(): String {
var result = ""
builder {
result = A<String>().bar()
}
return if (result == "(kotlin.coroutines.experimental.Continuation<T>) -> kotlin.Any?") "OK" else "Fail: $result"
}
@@ -0,0 +1,40 @@
// WITH_RUNTIME
// WITH_COROUTINES
// IGNORE_BACKEND: NATIVE
import helpers.*
import kotlin.coroutines.experimental.*
import kotlin.coroutines.experimental.intrinsics.*
class A {
var result = mutableListOf("O", "K", null)
suspend fun foo(): String? = suspendCoroutineOrReturn { x ->
x.resume(result.removeAt(0))
COROUTINE_SUSPENDED
}
}
var result = ""
suspend fun append(ignore: String, x: String) {
result += x
}
fun builder(c: suspend () -> Unit) {
c.startCoroutine(EmptyContinuation)
}
suspend fun bar() {
val a = A()
while (true) {
append("ignore", a.foo() ?: break)
}
}
fun box(): String {
builder {
bar()
}
return result
}
@@ -0,0 +1,31 @@
// WITH_RUNTIME
// WITH_COROUTINES
import helpers.*
import kotlin.coroutines.experimental.*
import kotlin.coroutines.experimental.intrinsics.*
suspend fun suspendThere(v: Any?): String = suspendCoroutineOrReturn { x ->
x.resume(v?.toString() ?: "<empty>")
COROUTINE_SUSPENDED
}
class A<T>(val arg: T) {
var result = ""
inline suspend fun foo() {
result = suspendThere(arg)
}
}
fun builder(c: suspend () -> Unit) {
c.startCoroutine(EmptyContinuation)
}
fun box(): String {
val a = A<String>("OK")
builder {
a.foo()
}
return a.result
}
@@ -0,0 +1,39 @@
// WITH_RUNTIME
// WITH_COROUTINES
// WITH_REFLECT
import helpers.*
import kotlin.coroutines.experimental.*
import kotlin.coroutines.experimental.intrinsics.*
suspend fun suspendThere(v: String): String = suspendCoroutineOrReturn { x ->
x.resume(v)
COROUTINE_SUSPENDED
}
suspend fun foo(x: suspend () -> String): String = x()
abstract class A {
inline suspend fun <reified T : Any> baz(): String {
return foo {
suspendThere(T::class.simpleName!!)
}
}
}
class B : A() {
suspend fun bar(): String {
return baz<OK>()
}
}
fun builder(c: suspend () -> Unit) {
c.startCoroutine(EmptyContinuation)
}
class OK
fun box(): String {
var result = "fail"
builder {
result = B().bar()
}
return result
}
@@ -0,0 +1,31 @@
// WITH_RUNTIME
// WITH_COROUTINES
import helpers.*
import kotlin.coroutines.experimental.*
import kotlin.coroutines.experimental.intrinsics.*
suspend fun getLong(): Long = suspendCoroutineOrReturn { x ->
x.resume(1234567890123L)
COROUTINE_SUSPENDED
}
suspend fun suspendHere(r: LongRange): Long = suspendCoroutineOrReturn { x ->
x.resume(r.start + r.endInclusive)
COROUTINE_SUSPENDED
}
fun builder(c: suspend () -> Unit) {
c.startCoroutine(EmptyContinuation)
}
fun box(): String {
var result = 0L
builder {
result = suspendHere(1L..getLong())
}
if (result != 1234567890124L) return "fail 1: $result"
return "OK"
}
@@ -0,0 +1,26 @@
// WITH_RUNTIME
// WITH_COROUTINES
import helpers.*
import kotlin.coroutines.experimental.*
import kotlin.coroutines.experimental.intrinsics.*
suspend fun suspendHere(r: LongRange): Long = suspendCoroutineOrReturn { x ->
x.resume(r.start + r.endInclusive)
COROUTINE_SUSPENDED
}
fun builder(c: suspend () -> Unit) {
c.startCoroutine(EmptyContinuation)
}
fun box(): String {
var result = 0L
builder {
result = suspendHere(1L..1234567890123L)
}
if (result != 1234567890124L) return "fail 1: $result"
return "OK"
}
@@ -0,0 +1,69 @@
// IGNORE_BACKEND: NATIVE
// WITH_COROUTINES
// WITH_RUNTIME
// MODULE: lib(support)
// FILE: lib.kt
import helpers.*
import kotlin.coroutines.experimental.*
import kotlin.coroutines.experimental.intrinsics.*
var continuation: () -> Unit = { }
var log = ""
var finished = false
suspend fun <T> foo(v: T): T = suspendCoroutineOrReturn { x ->
continuation = {
x.resume(v)
}
log += "foo($v);"
COROUTINE_SUSPENDED
}
inline suspend fun bar(v: String) {
log += "before bar($v);"
foo("1:$v")
log += "inside bar($v);"
foo("2:$v")
log += "after bar($v);"
}
fun builder(c: suspend () -> Unit) {
c.startCoroutine(handleResultContinuation {
continuation = { }
finished = true
})
}
// MODULE: main(lib)
// FILE: main.kt
import kotlin.coroutines.experimental.*
import kotlin.coroutines.experimental.intrinsics.*
suspend fun baz() {
bar("A")
log += "between bar;"
bar("B")
}
val expectedString =
"before bar(A);foo(1:A);@;inside bar(A);foo(2:A);@;after bar(A);" +
"between bar;" +
"before bar(B);foo(1:B);@;inside bar(B);foo(2:B);@;after bar(B);"
fun box(): String {
builder {
baz()
}
while (!finished) {
log += "@;"
continuation()
}
if (log != expectedString) return "fail: $log"
return "OK"
}
@@ -0,0 +1,80 @@
// IGNORE_BACKEND: NATIVE
// IGNORE_BACKEND: JVM
// WITH_COROUTINES
// WITH_RUNTIME
// MODULE: lib(support)
// FILE: lib.kt
import helpers.*
import kotlin.coroutines.experimental.*
import kotlin.coroutines.experimental.intrinsics.*
var continuation: () -> Unit = { }
var log = ""
var finished = false
suspend fun <T> foo(v: T): T = suspendCoroutineOrReturn { x ->
continuation = {
x.resume(v)
}
log += "foo($v);"
COROUTINE_SUSPENDED
}
interface I {
suspend fun bar()
}
class A(val v: String) : I {
override inline suspend fun bar() {
log += "before bar($v);"
foo("1:$v")
log += "inside bar($v);"
foo("2:$v")
log += "after bar($v);"
}
}
fun builder(c: suspend () -> Unit) {
c.startCoroutine(handleResultContinuation {
continuation = { }
finished = true
})
}
// MODULE: main(lib)
// FILE: main.kt
import kotlin.coroutines.experimental.*
import kotlin.coroutines.experimental.intrinsics.*
suspend fun baz() {
val a = A("A")
a.bar()
log += "between bar;"
val b: I = A("B")
b.bar()
}
val expectedString =
"before bar(A);foo(1:A);@;inside bar(A);foo(2:A);@;after bar(A);" +
"between bar;" +
"before bar(B);foo(1:B);@;inside bar(B);foo(2:B);@;after bar(B);"
fun box(): String {
builder {
baz()
}
while (!finished) {
log += "@;"
continuation()
}
if (log != expectedString) return "fail: $log"
return "OK"
}
@@ -0,0 +1,77 @@
// IGNORE_BACKEND: NATIVE
// WITH_COROUTINES
// WITH_RUNTIME
// MODULE: lib(support)
// FILE: lib.kt
import helpers.*
import kotlin.coroutines.experimental.*
import kotlin.coroutines.experimental.intrinsics.*
var continuation: () -> Unit = { }
var log = ""
var finished = false
class C {
var v: String = ""
inline suspend fun bar() {
log += "before bar($v);"
foo("1:$v")
log += "inside bar($v);"
foo("2:$v")
log += "after bar($v);"
}
}
suspend fun <T> foo(v: T): T = suspendCoroutineOrReturn { x ->
continuation = {
x.resume(v)
}
log += "foo($v);"
COROUTINE_SUSPENDED
}
fun C.builder(c: suspend C.() -> Unit) {
c.startCoroutine(this, handleResultContinuation {
continuation = { }
finished = true
})
}
// MODULE: main(lib)
// FILE: main.kt
import kotlin.coroutines.experimental.*
import kotlin.coroutines.experimental.intrinsics.*
suspend fun C.baz() {
v = "A"
bar()
log += "between bar;"
v = "B"
bar()
}
val expectedString =
"before bar(A);foo(1:A);@;inside bar(A);foo(2:A);@;after bar(A);" +
"between bar;" +
"before bar(B);foo(1:B);@;inside bar(B);foo(2:B);@;after bar(B);"
fun box(): String {
var c = C()
c.builder {
baz()
}
while (!finished) {
log += "@;"
continuation()
}
if (log != expectedString) return "fail: $log"
return "OK"
}
@@ -0,0 +1,60 @@
// WITH_RUNTIME
// WITH_COROUTINES
// FILE: main.kt
// TARGET_BACKEND: JVM
import helpers.*
import kotlin.coroutines.experimental.*
import kotlin.coroutines.experimental.intrinsics.*
open class A(val v: String) {
suspend fun suspendThere(v: String): String = suspendCoroutineOrReturn { x ->
x.resume(v)
COROUTINE_SUSPENDED
}
open suspend fun suspendHere(): String = suspendThere("O") + suspendThere(v)
}
class B(v: String) : A(v) {
override suspend fun suspendHere(): String = super.suspendHere() + suspendThere("56")
}
fun builder(c: suspend A.() -> Unit) {
c.startCoroutine(B("K"), EmptyContinuation)
}
fun box(): String {
var result = JavaClass.foo()
if (result != "OK56") return "fail 1: $result"
return "OK"
}
// FILE: JavaClass.java
import kotlin.coroutines.experimental.*;
public class JavaClass {
public static String foo() {
final String[] res = new String[1];
new B("K").suspendHere(new Continuation<String>() {
@Override
public CoroutineContext getContext() {
return EmptyCoroutineContext.INSTANCE;
}
@Override
public void resume(String s) {
res[0] = s;
}
@Override
public void resumeWithException(Throwable throwable) {
}
});
return res[0];
}
}
@@ -0,0 +1,34 @@
// WITH_RUNTIME
// WITH_COROUTINES
import helpers.*
import kotlin.coroutines.experimental.*
import kotlin.coroutines.experimental.intrinsics.*
open class A(val v: String) {
suspend fun suspendThere(v: String): String = suspendCoroutineOrReturn { x ->
x.resume(v)
COROUTINE_SUSPENDED
}
open suspend fun suspendHere(): String = suspendThere("O") + suspendThere(v)
}
class B(v: String) : A(v) {
override suspend fun suspendHere(): String = super.suspendHere() + suspendThere("56")
}
fun builder(c: suspend A.() -> Unit) {
c.startCoroutine(B("K"), EmptyContinuation)
}
fun box(): String {
var result = ""
builder {
result = suspendHere()
}
if (result != "OK56") return "fail 1: $result"
return "OK"
}
@@ -0,0 +1,41 @@
// WITH_RUNTIME
// WITH_COROUTINES
import helpers.*
import kotlin.coroutines.experimental.*
import kotlin.coroutines.experimental.intrinsics.*
abstract class A(val v: String) {
suspend abstract fun foo(v: String): String
suspend fun suspendThere(v: String): String = suspendCoroutineOrReturn { x ->
x.resume(v)
COROUTINE_SUSPENDED
}
open suspend fun suspendHere(): String = foo("O") + suspendThere(v)
}
class B(v: String) : A(v) {
override suspend fun foo(v: String): String = suspendCoroutineOrReturn { x ->
x.resume(v)
COROUTINE_SUSPENDED
}
override suspend fun suspendHere(): String = super.suspendHere() + suspendThere("56")
}
fun builder(c: suspend A.() -> Unit) {
c.startCoroutine(B("K"), EmptyContinuation)
}
fun box(): String {
var result = ""
builder {
result = suspendHere()
}
if (result != "OK56") return "fail 1: $result"
return "OK"
}
@@ -0,0 +1,38 @@
// WITH_RUNTIME
// WITH_COROUTINES
import helpers.*
import kotlin.coroutines.experimental.*
import kotlin.coroutines.experimental.intrinsics.*
interface A {
val v: String
suspend fun suspendThere(v: String): String = suspendCoroutineOrReturn { x ->
x.resume(v)
COROUTINE_SUSPENDED
}
suspend fun suspendHere(): String = suspendThere("O") + suspendThere(v)
}
interface A2 : A {
override suspend fun suspendHere(): String = super.suspendHere() + suspendThere("56")
}
class B(override val v: String) : A2
fun builder(c: suspend A.() -> Unit) {
c.startCoroutine(B("K"), EmptyContinuation)
}
fun box(): String {
var result = ""
builder {
result = suspendHere()
}
if (result != "OK56") return "fail 1: $result"
return "OK"
}
@@ -0,0 +1,56 @@
// WITH_RUNTIME
// WITH_COROUTINES
import helpers.*
import kotlin.coroutines.experimental.*
import kotlin.coroutines.experimental.intrinsics.*
class Controller {
suspend fun suspendHere(): String = suspendCoroutineOrReturn { x ->
x.resume("K")
COROUTINE_SUSPENDED
}
}
fun builder(c: suspend Controller.() -> Unit) {
c.startCoroutine(Controller(), EmptyContinuation)
}
val logger = StringBuilder()
class A(val first: String, val second: String) {
init {
logger.append("A.<init>;")
}
override fun toString() = "$first$second"
companion object {
init {
logger.append("A.<clinit>;")
}
}
}
inline fun <T> logged(message: String, result: () -> T): T {
logger.append(message)
return result()
}
fun box(): String {
var result = "OK"
builder {
var local: Any = A(logged("args;") { "O" }, suspendHere())
if (local.toString() != "OK") {
result = "fail 1: $local"
return@builder
}
}
if (logger.toString() != "args;A.<clinit>;A.<init>;") {
return "Fail: '$logger'"
}
return result
}
@@ -0,0 +1,152 @@
// WITH_RUNTIME
// WITH_COROUTINES
import helpers.*
import kotlin.coroutines.experimental.*
import kotlin.coroutines.experimental.intrinsics.*
class Controller {
suspend fun suspendHere(): String = suspendCoroutineOrReturn { x ->
x.resume("K")
COROUTINE_SUSPENDED
}
suspend fun suspendWithArgument(v: String): String = suspendCoroutineOrReturn { x ->
x.resume(v)
COROUTINE_SUSPENDED
}
suspend fun suspendWithDouble(v: Double): Double = suspendCoroutineOrReturn { x ->
x.resume(v)
COROUTINE_SUSPENDED
}
}
fun builder(c: suspend Controller.() -> Unit) {
c.startCoroutine(Controller(), EmptyContinuation)
}
class A(val first: String, val second: String) {
override fun toString() = "$first$second"
}
class B(val first: String, val second: String, val third: String) {
override fun toString() = "$first$second$third"
}
class C(val a: Long, val b: Double, val c: Int, val d: String) {
override fun toString() = "$a#$b#$c#$d"
}
val condition = true
fun box(): String {
var result = "OK"
builder {
for (count in 0..3) {
val local = A(if (count > 0) break else "O", suspendHere())
if (count > 0) {
result = "fail 1: count=$count"
return@builder
}
if (local.toString() != "OK") {
result = "fail 1: $local"
return@builder
}
}
for (count in 0..3) {
val local = B(if (count > 0) break else "#", suspendWithArgument("O"), suspendHere())
if (count > 0) {
result = "fail 2: count=$count"
return@builder
}
if (local.toString() != "#OK") {
result = "fail 2: $local"
return@builder
}
}
for (count in 0..3) {
val local = B(suspendWithArgument("#"), if (count > 0) break else "O", suspendHere())
if (count > 0) {
result = "fail 3: count=$count"
return@builder
}
if (local.toString() != "#OK") {
result = "fail 3: $local"
return@builder
}
}
for (count in 0..3) {
val local = B(
"#",
B("",
if (count > 0) break else "O",
suspendWithArgument("")
).toString(),
suspendHere()
)
if (count > 0) {
result = "fail 4: count=$count"
return@builder
}
if (local.toString() != "#OK") {
result = "fail 4: $local"
return@builder
}
}
loop@for (count in 0..3) {
val local = B(
if (!condition) "1" else suspendWithArgument("#"),
when {
count > 0 -> break@loop
condition -> suspendWithArgument("O")
else -> "2"
},
if (condition) suspendHere() else suspendWithArgument("3")
)
if (count > 0) {
result = "fail 5: count=$count"
return@builder
}
if (local.toString() != "#OK") {
result = "fail 5: $local"
return@builder
}
}
for (count in 0..3) {
val local = C(
1234567890123L,
suspendWithDouble(3.14),
42,
if (count > 0) break else suspendWithArgument("OK")
)
if (count > 0) {
result = "fail 6: count=$count"
return@builder
}
if (local.toString() != "1234567890123#3.14#42#OK") {
result = "fail 6: $local"
return@builder
}
}
}
return result
}
@@ -1,6 +1,5 @@
// WITH_RUNTIME
// WITH_COROUTINES
// IGNORE_BACKEND: JS
import helpers.*
import kotlin.coroutines.experimental.*
import kotlin.coroutines.experimental.intrinsics.*
@@ -0,0 +1,32 @@
// WITH_RUNTIME
// WITH_COROUTINES
import helpers.*
import kotlin.coroutines.experimental.*
import kotlin.coroutines.experimental.intrinsics.*
suspend fun suspendHere(): String = suspendCoroutineOrReturn { x ->
x.resume("OK")
COROUTINE_SUSPENDED
}
fun builder(c: suspend () -> Unit) {
c.startCoroutine(EmptyContinuation)
}
fun box(): String {
var result = arrayListOf<String>()
builder {
while (true) {
if (result.size == 0) {
break
}
}
for (i in 1..2) {
result.add(suspendHere())
}
}
return result[0]
}
@@ -1,3 +1,5 @@
// LANGUAGE_VERSION: 1.1
fun box(): String {
val a: A = B(1)
a.copy(1)
@@ -1,6 +1,4 @@
// TARGET_BACKEND: JS
// This test causes JVM to generate incorrect bytecode
// Could not use IGNORE_BACKEND directive, since it makes LightAnalysisModeCodegenTestsGenerated fail on this test
// IGNORE_BACKEND: JVM
interface I<T> {
val prop: T
@@ -0,0 +1,17 @@
interface I {
fun foo(x: Int = 23): String
}
abstract class Base : I
class C : Base(), I {
override fun foo(x: Int) = "C:$x"
}
fun box(): String {
val x: I = C()
val r = x.foo() + ";" + x.foo(42)
if (r != "C:23;C:42") return "fail: $r"
return "OK"
}
@@ -0,0 +1,38 @@
// IGNORE_BACKEND: JS
import kotlin.reflect.KProperty
class Delegate<T>(var inner: T) {
operator fun getValue(t: Any?, p: KProperty<*>): T = inner
operator fun setValue(t: Any?, p: KProperty<*>, i: T) { inner = i }
}
val del = Delegate("zzz")
class A {
inner class B {
var prop: String by del
}
}
inline fun asFailsWithCCE(block: () -> Unit) {
try {
block()
}
catch (e: ClassCastException) {
return
}
catch (e: Throwable) {
throw AssertionError("Should throw ClassCastException, got $e")
}
throw AssertionError("Should throw ClassCastException, no exception thrown")
}
fun box(): String {
val c = A().B()
(del as Delegate<Int>).inner = 10
asFailsWithCCE { c.prop } // does not fail in JS due KT-8135.
return "OK"
}
@@ -0,0 +1,38 @@
// IGNORE_BACKEND: JVM, JS
import kotlin.reflect.KProperty
class Delegate<T>(var inner: T) {
operator fun getValue(t: Any?, p: KProperty<*>): T = inner
operator fun setValue(t: Any?, p: KProperty<*>, i: T) { inner = i }
}
val del = Delegate("zzz")
class A {
inner class B {
var prop: String by del
}
}
inline fun asFailsWithCCE(block: () -> Unit) {
try {
block()
}
catch (e: ClassCastException) {
return
}
catch (e: Throwable) {
throw AssertionError("Should throw ClassCastException, got $e")
}
throw AssertionError("Should throw ClassCastException, no exception thrown")
}
fun box(): String {
val c = A().B()
(del as Delegate<String?>).inner = null
asFailsWithCCE { c.prop } // does not fail in JVM, JS due KT-8135.
return "OK"
}
@@ -0,0 +1,25 @@
// FILE: Var.kt
package pvar
open class PVar<T>(private var value: T) {
protected operator fun getValue(thisRef: Any?, prop: Any?) = value
protected operator fun setValue(thisRef: Any?, prop: Any?, newValue: T) {
value = newValue
}
}
// FILE: test.kt
import pvar.*
class C : PVar<Long>(42L) {
inner class Inner {
var x by this@C
}
}
fun box(): String {
val inner = C().Inner()
inner.x = 1L
return "OK"
}
@@ -0,0 +1,13 @@
object Whatever {
operator fun getValue(thisRef: Any?, prop: Any?) = "OK"
}
fun box(): String {
val key by Whatever
return {
object {
val keys = key
}.keys
} ()
}
@@ -0,0 +1,17 @@
//WITH_REFLECT
import kotlin.properties.Delegates
interface MyInterface {
fun something(): String {
var foo: String by Delegates.notNull();
foo = "OK"
return foo
}
}
fun box(): String {
return object : MyInterface {
}.something()
}
@@ -3,7 +3,10 @@
// WITH_REFLECT
import kotlin.reflect.*
import kotlin.reflect.KProperty
import kotlin.reflect.KMutableProperty
import kotlin.reflect.KMutableProperty1
import kotlin.reflect.full.*
import kotlin.reflect.jvm.*
object Delegate {
@@ -10,15 +10,15 @@ open class MyClass(val value: String) {
}
}
inline fun <T> runLogged(entry: String, action: () -> T): T {
inline fun <L> runLogged(entry: String, action: () -> L): L {
log += entry
return action()
}
operator fun <T: MyClass> T.provideDelegate(host: Any?, p: Any): T =
operator fun <P: MyClass> P.provideDelegate(host: Any?, p: Any): P =
runLogged("tdf(${this.value});") { this }
operator fun <T> T.getValue(receiver: Any?, p: Any): T =
operator fun <V> V.getValue(receiver: Any?, p: Any): V =
runLogged("get($this);") { this }
val testO by runLogged("O;") { MyClass("O") }
@@ -0,0 +1,22 @@
// WITH_RUNTIME
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty
class Delegate: ReadOnlyProperty<Test, String> {
override fun getValue(thisRef: Test, property: KProperty<*>) = "OK"
}
class Provider {
operator fun provideDelegate(thisRef: Test, property: KProperty<*>) = Delegate()
}
class Test {
companion object {
val instance = Test()
}
val message by Provider()
}
fun box() = Test.instance.message
@@ -0,0 +1,60 @@
// IGNORE_BACKEND: JVM
var log = ""
fun log(a: String) {
log += a + ";"
}
interface C {
fun foo(x: Int): Unit {
log("C.foo($x)")
}
}
interface I {
fun foo(x: Int = 1): Unit
}
class G(c: C) : C by c, I
class H(c: C) : I, C by c
fun test1() {
log = ""
val g1 = G(object: C {})
g1.foo(2)
g1.foo()
val g2 = G(object: C {
override fun foo(x: Int) {
log("[2] object:C.foo($x)")
}
})
g2.foo(2)
g2.foo()
}
fun test2() {
log = ""
val h1 = H(object: C {})
h1.foo(2)
h1.foo()
val h2 = H(object: C {
override fun foo(x: Int) {
log("[2] object:C.foo($x)")
}
})
h2.foo(2)
h2.foo()
}
fun box(): String {
test1()
if (log != "C.foo(2);C.foo(1);[2] object:C.foo(2);[2] object:C.foo(1);") return "fail1: $log"
test2()
if (log != "C.foo(2);C.foo(1);[2] object:C.foo(2);[2] object:C.foo(1);") return "fail2: $log"
return "OK"
}
@@ -0,0 +1,24 @@
// LANGUAGE_VERSION: 1.2
enum class A {
X {
val x = "OK"
inner class Inner {
inner class Inner2 {
inner class Inner3 {
val y = x
}
}
}
val z = Inner().Inner2().Inner3()
override val test: String
get() = z.y
};
abstract val test: String
}
fun box() = A.X.test
@@ -0,0 +1,23 @@
// LANGUAGE_VERSION: 1.2
enum class A {
X {
val k = "K"
val anonObject = object {
inner class Inner {
val x = "O" + k
}
val innerX = Inner().x
override fun toString() = innerX
}
override val test = anonObject.toString()
};
abstract val test: String
}
fun box() = A.X.test
@@ -0,0 +1,24 @@
interface IFoo {
fun foo(): String
}
interface IBar {
fun bar(): String
}
abstract class Base(val x: IFoo)
enum class Test : IFoo, IBar {
FOO {
// FOO referenced from inner class constructor with uninitialized 'this'
inner class Inner : Base(FOO)
val z = Inner()
override fun foo() = "OK"
override fun bar() = z.x.foo()
}
}
fun box() = Test.FOO.bar()
@@ -0,0 +1,24 @@
interface IFoo {
fun foo(): String
}
interface IBar {
fun bar(): String
}
enum class Test : IFoo, IBar {
FOO {
// FOO referenced from inner class constructor with initialized 'this'
inner class Inner {
val fooFoo = FOO.foo()
}
val z = Inner()
override fun foo() = "OK"
override fun bar() = z.fooFoo
}
}
fun box() = Test.FOO.bar()
@@ -0,0 +1,23 @@
interface IFoo {
fun foo(): String
}
interface IBar {
fun bar(): String
}
enum class Test : IFoo, IBar {
FOO {
// FOO referenced from inner class constructor with initialized 'this',
// in delegate initializer
inner class Inner : IFoo by FOO
val z = Inner()
override fun foo() = "OK"
override fun bar() = z.foo()
}
}
fun box() = Test.FOO.bar()
@@ -0,0 +1,20 @@
// LANGUAGE_VERSION: 1.2
enum class A {
X {
val x = "OK"
inner class Inner {
val y = x
}
val z = Inner()
override val test: String
get() = z.y
};
abstract val test: String
}
fun box() = A.X.test
@@ -0,0 +1,19 @@
// LANGUAGE_VERSION: 1.2
enum class A {
X {
val x = "OK"
inner class Inner {
fun foo() = x
}
val z = Inner()
override val test = z.foo()
};
abstract val test: String
}
fun box() = A.X.test
@@ -0,0 +1,19 @@
// LANGUAGE_VERSION: 1.2
enum class A {
X {
val x = "OK"
inner class Inner {
fun foo() = this@X.x
}
val z = Inner()
override val test = z.foo()
};
abstract val test: String
}
fun box() = A.X.test
@@ -0,0 +1,14 @@
enum class Test(val x: String, val closure1: () -> String) {
FOO("O", { FOO.x }) {
override val y: String = "K"
val closure2 = { y } // Implicit 'FOO'
override val z: String = closure2()
};
abstract val y: String
abstract val z: String
fun test() = closure1() + z
}
fun box() = Test.FOO.test()
@@ -0,0 +1,14 @@
enum class Test(val x: String, val closure1: () -> String) {
FOO("O", run { { FOO.x } }) {
override val y: String = "K"
val closure2 = { y } // Implicit 'FOO'
override val z: String = closure2()
};
abstract val y: String
abstract val z: String
fun test() = closure1() + z
}
fun box() = Test.FOO.test()
@@ -0,0 +1,8 @@
enum class Foo(
val x: String,
val callback: () -> String
) {
FOO("OK", { FOO.x })
}
fun box() = Foo.FOO.callback()
@@ -0,0 +1,17 @@
interface Callback {
fun invoke(): String
}
enum class Foo(
val x: String,
val callback: Callback
) {
FOO(
"OK",
object : Callback {
override fun invoke() = FOO.x
}
)
}
fun box() = Foo.FOO.callback.invoke()
@@ -0,0 +1,10 @@
enum class X {
B {
val value2 = "K"
override val value = "O".let { it + value2 }
};
abstract val value: String
}
fun box() = X.B.value
@@ -0,0 +1,17 @@
enum class X {
B {
val value2 = "K"
val anonObject = object {
val value3 = "O" + value2
override fun toString(): String = value3
}
override val value = anonObject.toString()
};
abstract val value: String
}
fun box() = X.B.value
@@ -0,0 +1,16 @@
enum class X {
B {
val value2 = "K"
val anonObject = object {
override fun toString(): String =
"O" + value2
}
override val value = anonObject.toString()
};
abstract val value: String
}
fun box() = X.B.value
@@ -0,0 +1,21 @@
// LANGUAGE_VERSION: 1.2
enum class X {
B {
val k = "K"
inner class Inner {
fun foo() = "O" + k
}
val inner = Inner()
val bmr = inner::foo
override val value = bmr.invoke()
};
abstract val value: String
}
fun box() = X.B.value
@@ -0,0 +1,18 @@
enum class X {
B {
override val value = "OK"
val bmr = B::value.get()
override fun foo(): String {
return bmr
}
};
abstract val value: String
abstract fun foo(): String
}
fun box() = X.B.foo()
@@ -0,0 +1,15 @@
// LANGUAGE_VERSION: 1.2
enum class X {
B {
override val value = "OK"
override val test = ::value.get()
};
abstract val value: String
abstract val test: String
}
fun box() = X.B.test
@@ -0,0 +1,11 @@
enum class X {
B {
override val value2 = "K"
override val value = "O" + B.value2
};
abstract val value2: String
abstract val value: String
}
fun box() = X.B.value
@@ -0,0 +1,11 @@
enum class X {
B {
override val value2 = "K"
override val value = "O" + X.B.value2
};
abstract val value2: String
abstract val value: String
}
fun box() = X.B.value

Some files were not shown because too many files have changed in this diff Show More