Add support for contract feature in inliner

This commit is contained in:
Mikhael Bogdanov
2017-12-14 17:41:49 +01:00
parent 4890979476
commit 819a3a95b3
15 changed files with 610 additions and 72 deletions
@@ -0,0 +1,28 @@
// !LANGUAGE: +CallsInPlaceEffect
// FILE: 1.kt
package test
import kotlin.internal.contracts.*
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
public inline fun <R> myrun(block: () -> R): R {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return block()
}
// FILE: 2.kt
import test.*
fun cond() = true
fun test(s: String) = s
fun box(): String {
val x: String
myrun {
x = if (cond()) test("OK") else test("fail")
}
return x
}
@@ -0,0 +1,27 @@
// !LANGUAGE: +CallsInPlaceEffect
// FILE: 1.kt
package test
import kotlin.internal.contracts.*
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
public inline fun <R> myrun(block: () -> R): R {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return block()
}
// FILE: 2.kt
import test.*
fun test(s: String) = s
fun box(): String {
val x: String
myrun {
x = try { test("OK") } catch (e: Exception) { test("fail") }
}
return x
}
@@ -0,0 +1,24 @@
// !LANGUAGE: +CallsInPlaceEffect
// FILE: 1.kt
package test
import kotlin.internal.contracts.*
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
public inline fun <R> myrun(block: () -> R): R {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return block()
}
// FILE: 2.kt
import test.*
fun box(): String {
val x: Long
myrun {
x = 42L
}
return if (x.inc() == 43L) "OK" else "Fail: ${x.inc()}"
}
@@ -0,0 +1,27 @@
// !LANGUAGE: +CallsInPlaceEffect
// FILE: 1.kt
package test
import kotlin.internal.contracts.*
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
public inline fun <R> myrun(block: () -> R): R {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return block()
}
// FILE: 2.kt
import test.*
fun box(): String {
val x: Int
myrun {
myrun {
x = 42
}
}
return if (x.inc() == 43) "OK" else "Fail: ${x.inc()}"
}
@@ -0,0 +1,25 @@
// !LANGUAGE: +CallsInPlaceEffect
// FILE: 1.kt
package test
import kotlin.internal.contracts.*
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
public inline fun <R> myrun(block: () -> R): R {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return block()
}
// FILE: 2.kt
import test.*
fun box(): String {
val x: Int
myrun {
x = 42
}
return if (x.inc() == 43) "OK" else "Fail: ${x.inc()}"
}
@@ -0,0 +1,25 @@
// !LANGUAGE: +CallsInPlaceEffect
// FILE: 1.kt
package test
import kotlin.internal.contracts.*
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
public inline fun <R> myrun(block: () -> R): R {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return block()
}
// FILE: 2.kt
import test.*
fun box(): String {
val x: Long
myrun {
x = 42L
if (x == 42L) return "OK"
}
return "fail"
}
@@ -0,0 +1,39 @@
// !LANGUAGE: +CallsInPlaceEffect
// WITH_RUNTIME
// FILE: 1.kt
package test
import kotlin.internal.contracts.*
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
public inline fun <R> myrun(block: () -> R): R {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return block()
}
// FILE: 2.kt
import test.*
fun test(xs: List<String>): String {
var result = ""
myrun L@ {
for (x in xs) {
val y: String
myrun {
y = x
if (y.length > 1) return@L
}
result += y
}
}
return result
}
fun box(): String {
return test(listOf("O", "K", "fail"))
}
@@ -0,0 +1,30 @@
// !LANGUAGE: +CallsInPlaceEffect
// FILE: 1.kt
package test
import kotlin.internal.contracts.*
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
public inline fun <R> myrun(block: () -> R): R {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return block()
}
// FILE: 2.kt
import test.*
class A {
val z: String
init {
myrun {
z = "OK"
}
}
}
fun box(): String {
return A().z
}
@@ -0,0 +1,29 @@
// !LANGUAGE: +CallsInPlaceEffect
// FILE: 1.kt
package test
import kotlin.internal.contracts.*
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
public inline fun <R> myrun(block: () -> R): R {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return block()
}
// FILE: 2.kt
// NO_CHECK_LAMBDA_INLINING
import test.*
fun box(): String {
val x: Int
val res = myrun {
x = 42
{
x
}()
}
return if (res == 42 && x.inc() == 43) "OK" else "Fail: ${x.inc()}"
}