KT-39532 Support intention to convert reference to lambda and vice versa for adapted references (#3495)

* Convert lambda to reference: support a function which has default parameters/unit return type/suspendability

#KT-39532 Fixed
This commit is contained in:
Toshiaki Kameyama
2020-06-26 20:21:17 +09:00
committed by GitHub
parent a87b25d10e
commit e822e871f5
16 changed files with 133 additions and 9 deletions
@@ -1,4 +1,5 @@
// IS_APPLICABLE: false
// COMPILER_ARGUMENTS: -XXLanguage:-FunctionReferenceWithDefaultValueAsOtherType
fun foo(z: Int, y: Int = 0) = y + z
@@ -0,0 +1,5 @@
// IS_APPLICABLE: false
fun foo(z: Int, y: Int = 0) = y + z
val x = { arg: Int <caret>-> foo(arg) }
@@ -0,0 +1,7 @@
fun foo(f: () -> Unit) {}
fun bar(a: Int = 42) {}
fun test() {
foo <caret>{ bar() }
}
@@ -0,0 +1,7 @@
fun foo(f: () -> Unit) {}
fun bar(a: Int = 42) {}
fun test() {
foo(::bar)
}
@@ -0,0 +1,7 @@
fun foo(f: (x: Int) -> Unit) {}
fun bar(x: Int, y: Int = 42) {}
fun test() {
foo <caret>{ bar(it) }
}
@@ -0,0 +1,7 @@
fun foo(f: (x: Int) -> Unit) {}
fun bar(x: Int, y: Int = 42) {}
fun test() {
foo(::bar)
}
@@ -0,0 +1,7 @@
fun Int.foo(x: Int, y: Int = 42) = x + y
fun bar(f: (Int, Int) -> Int) {}
fun test() {
bar <caret>{ i, x -> i.foo(x) }
}
@@ -0,0 +1,7 @@
fun Int.foo(x: Int, y: Int = 42) = x + y
fun bar(f: (Int, Int) -> Int) {}
fun test() {
bar(Int::foo)
}
@@ -1,4 +1,5 @@
// IS_APPLICABLE: false
// COMPILER_ARGUMENTS: -XXLanguage:-SuspendConversion
fun coroutine(block: suspend () -> Unit) {}
@@ -0,0 +1,8 @@
// COMPILER_ARGUMENTS: -XXLanguage:+SuspendConversion
fun foo(a: suspend () -> Unit) {}
fun action() {}
fun usage() {
foo { action() <caret> }
}
@@ -0,0 +1,8 @@
// COMPILER_ARGUMENTS: -XXLanguage:+SuspendConversion
fun foo(a: suspend () -> Unit) {}
fun action() {}
fun usage() {
foo(::action)
}
@@ -1,4 +1,5 @@
// IS_APPLICABLE: false
// COMPILER_ARGUMENTS: -XXLanguage:-FunctionReferenceWithDefaultValueAsOtherType
fun Int.exec(f: (Int) -> Unit) = f(this)
@@ -0,0 +1,7 @@
fun Int.exec(f: (Int) -> Unit) = f(this)
fun bar(x: Int) = x
fun foo() {
2.exec {<caret> bar(it) }
}
@@ -0,0 +1,7 @@
fun Int.exec(f: (Int) -> Unit) = f(this)
fun bar(x: Int) = x
fun foo() {
2.exec(::bar)
}