[FIR] Fix translation of invokes & add return expressions for lambdas

* fixed NoSuchMethod caused by mismatched signatures of the "invoke" method generated for lambda arguments
* added test cases in invoke.kt for KFunction and anonymous functions
* added a transformer to wrap the last expression in the bodies of lambdas with return
This commit is contained in:
Juan Chen
2020-02-01 18:18:03 -08:00
committed by Mikhail Glukhikh
parent 5eedba3903
commit 7249d2f889
194 changed files with 397 additions and 506 deletions
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// IGNORE_BACKEND: JS_IR
// IGNORE_BACKEND: JS
// reason - no error from division by zero in JS
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
private fun <T> upcast(value: T): T = value
fun box(): String {
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
fun foo(): String = "foo1"
fun foo(i: Int): String = "foo2"
-1
View File
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// IGNORE_BACKEND: JS_IR
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
val foo: () -> Unit = {}
fun box(): String {
-1
View File
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
class MyClass(var fnc : () -> String) {
fun test(): String {
-1
View File
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
class T(val f : () -> Any?) {
fun call() : Any? = f()
}
-1
View File
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
class Foo(
var state : Int,
val f : (Int) -> Int){
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
open class Base(val fn: () -> String)
fun box(): String {
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
open class Base(val fn1: () -> String, val fn2: () -> String)
fun box(): String {
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
fun box() : String {
return apply( "OK", {arg: String -> arg } )
}
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
fun box() : String {
return if (apply( 5, {arg: Int -> arg + 13 } ) == 18) "OK" else "fail"
}
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
fun box() : String {
val cl = 39
return if (sum(200, { val ff = {cl}; ff() }) == 239) "OK" else "FAIL"
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
fun box() : String {
val cl = 39
return if (sum(200, { val m = { val r = { cl }; r() }; m() }) == 239) "OK" else "FAIL"
-1
View File
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
open class A(val s: Int) {
}
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
fun box(): String {
val o = "O"
val ok_L = {o + "K"}
-1
View File
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
fun box(): String {
val a = 1
val explicitlyReturned = run1 f@{
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
fun box(): String {
val a = 1
val explicitlyReturned = run1 {
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
fun box() : String {
return invoker( {"OK"} )
}
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
fun box() : String {
return if (int_invoker( { 7 } ) == 7) "OK" else "fail"
}
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
fun foo(block: (String, String, String) -> String): String = block("O", "fail", "K")
fun box() = foo { x, _, y -> x + y }
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// IGNORE_BACKEND: JS_IR
// IGNORE_BACKEND: JS
// reason - no error from division by zero in JS
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
private const val z = "OK";
fun box(): String {
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
fun zap(s: String): String? = s
inline fun tryZap(s: String, fn: (String) -> String): String {
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
inline fun catchAll(x: String, block: () -> Unit): String {
try {
block()
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
inline fun <T> tryOrElse(f1: () -> T, f2: () -> T): T {
try {
return f1()
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
inline fun <T> tryOrElse(f1: () -> T, f2: () -> T): T =
try { f1() } catch (e: Exception) { f2() }
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
inline fun <T> tryAndThen(f1: () -> Unit, f2: () -> Unit, f3: () -> T): T {
try {
f1()
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
fun zap(s: String) = s
inline fun tryZap(string: String, fn: (String) -> String) =
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
fun zap(s: String) = s
inline fun tryZap(s1: String, s2: String, fn: (String, String) -> String) =
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
fun zap(s: String) = s
inline fun tryZap(string: String, fn: (String) -> String) =
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
fun zap(s: String) = s
inline fun tryZap(string: String, fn: (String) -> String) =
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
fun zap(s: String) = s
inline fun tryZap1(string: String, fn: (String) -> String) =
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
inline fun test(s: () -> Int): Int =
try {
val i = s()
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// WITH_RUNTIME
import kotlin.test.*
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
data class A(val x: String, val y: String)
inline fun foo(a: A, block: (A) -> String): String = block(a)
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
data class A(val x: String, val y: String)
fun foo(a: A, block: (Int, A, String) -> String): String = block(1, a, "#")
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
data class A(val x: String, val y: String)
fun foo(a: A, block: (A) -> String): String = block(a)
@@ -1,5 +1,4 @@
// !DIAGNOSTICS: -DEBUG_INFO_SMARTCAST
// IGNORE_BACKEND_FIR: JVM_IR
fun <R> foo(f: () -> R): R = f()
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
//KT-1249 IllegalStateException invoking function property
class TestClass(val body : () -> Unit) : Any() {
fun run() {
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
infix fun <T> T.mustBe(t : T) {
assert("$this must be $t") {this == t}
}
@@ -1,5 +1,4 @@
// !LANGUAGE: +FunctionTypesWithBigArity
// IGNORE_BACKEND_FIR: JVM_IR
class A(val value: Int)
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
fun foo(block: (String, String, String) -> String): String = block("O", "fail", "K")
fun box() = foo(fun(x: String, _: String, y: String) = x + y)
+4 -2
View File
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
package invoke
fun test1(predicate: (Int) -> Int, i: Int) = predicate(i)
@@ -24,6 +23,9 @@ fun box() : String {
if (test2({ it }, 2) != 2) return "fail 2"
if (test3(Method(), 3) != 3) return "fail 3"
if (test4(Method(), 4) != 4) return "fail 4"
if (test5(Method2(), "s") != "s") return "fail5"
if (test5(Method2(), "s") != "s") return "fail 5"
if (test1(Int::dec, 1) != 0) return "fail 6"
if (test2(Int::dec, 1) != 0) return "fail 7"
if (test1(fun(x: Int) = x - 1, 1) != 0) return "fail 8"
return "OK"
}
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
//KT-3217 Invoke convention after function invocation doesn't work
//KT-2728 Can't compile A()()
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
//KT-3189 Function invoke is called with no reason
fun box(): String {
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
//KT-3297 Calling the wrong function inside an extension method to the Function0 class
infix fun <R> Function0<R>.or(alt: () -> R): R {
-1
View File
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// IGNORE_BACKEND: JS_IR
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS, NATIVE
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
fun foo(x: Int) {}
fun loop(times : Int) {
@@ -1,5 +1,4 @@
// !LANGUAGE: +NewInference
// IGNORE_BACKEND_FIR: JVM_IR
// WITH_RUNTIME
inline fun <T> foo(f: () -> T): String {
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// TARGET_BACKEND: JVM
// WITH_RUNTIME
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// TARGET_BACKEND: JVM
// WITH_RUNTIME
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// TARGET_BACKEND: JVM
// WITH_RUNTIME
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// TARGET_BACKEND: JVM
// WITH_RUNTIME
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// TARGET_BACKEND: JVM
// WITH_RUNTIME
-1
View File
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
fun foo() : String {
val u = {
class B(val data : String)
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
class A {
operator fun component1() = 1
operator fun component2() = 2
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
class Range(val from : C, val to: C) {
operator fun iterator() = It(from, to)
}
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
class Range(val from : C, val to: C) {
operator fun iterator() = It(from, to)
}
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
operator fun Int.component1() = this + 1
operator fun Int.component2() = this + 2
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
operator fun Long.component1() = this + 1
operator fun Long.component2() = this + 2
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
class Range(val from : C, val to: C) {
operator fun iterator() = It(from, to)
}
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
operator fun Int.component1() = this + 1
operator fun Int.component2() = this + 2
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
operator fun Long.component1() = this + 1
operator fun Long.component2() = this + 2
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
operator fun Int.component1() = this + 1
operator fun Int.component2() = this + 2
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
operator fun Long.component1() = this + 1
operator fun Long.component2() = this + 2
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// FILE: box.kt
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// TARGET_BACKEND: JVM
// IGNORE_LIGHT_ANALYSIS
// WITH_RUNTIME
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
fun box(): String {
defineFunc<String>()
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
abstract class Base(val fn: () -> String)
object Test : Base({ Test.ok() }) {
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
private var x = "O"
private fun f() = "K"
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
var order: String = ""
fun a(i: Int): Int? {
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
val my: String = "O"
get() = { field }() + "K"
-1
View File
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
val Int.ext: () -> Int get() = { 5 }
val Long.ext: Long get() = 4.ext().toLong() //(c.kt:4)
val y: Long get() = 10L.ext
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
fun runNoInline(f: () -> Unit) = f()
fun box(): String {
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// IGNORE_BACKEND: JS_IR
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
package test
private val prop = "O"
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// TARGET_BACKEND: JVM
// WITH_RUNTIME
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
fun isNull(x: Unit?) = x == null
fun box(): String {
@@ -1,5 +1,4 @@
// !LANGUAGE: +VariableDeclarationInWhenSubject
// IGNORE_BACKEND_FIR: JVM_IR
fun box(): String {
var y: String = "OK"