Initial support of type inference for callable references
There are two main changes here: - In CallCompleter, there was a bug: we assumed that the return type of a candidate must be a subtype of the expected type and were adding a corresponding constraint to the system. However, this is not true for callable references where the type of the expression is KFunctionN<...> and the return type of the candidate must be a subtype of the _last generic argument_ of the functional type. - In CandidateResolver, we use a more correct (although still not precise) heuristic to determine if a candidate fits based on the non-substituted type of the callable reference expression which it would produce. This can be further improved, see TODOs in CallCompleter. Also this does not influence resolution of callable references being passed as arguments to generic calls (that happens in GenericCandidateResolver) #KT-10968 Fixed #KT-11075 Fixed #KT-12286 Fixed #KT-12963 Open #KT-12964 Open
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
// !DIAGNOSTICS: -UNUSED_VARIABLE
|
||||
// KT-10968 Callable reference: type inference by function return type
|
||||
|
||||
fun <T> getT(): T = null!!
|
||||
|
||||
fun getString() = ""
|
||||
|
||||
fun test() {
|
||||
val a : () -> String = ::getString
|
||||
val b : () -> String = ::getT
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package
|
||||
|
||||
public fun getString(): kotlin.String
|
||||
public fun </*0*/ T> getT(): T
|
||||
public fun test(): kotlin.Unit
|
||||
@@ -0,0 +1,10 @@
|
||||
// KT-11075 NONE_APPLICABLE reported for callable reference to an overloaded generic function with expected type provided
|
||||
|
||||
object TestCallableReferences {
|
||||
fun <A> foo(x: A) = x
|
||||
fun <B> foo(x: List<B>) = x
|
||||
|
||||
fun test0(): (String) -> String = TestCallableReferences::foo
|
||||
|
||||
fun <T> test1(): (List<T>) -> List<T> = TestCallableReferences::foo
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package
|
||||
|
||||
public object TestCallableReferences {
|
||||
private constructor TestCallableReferences()
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public final fun </*0*/ A> foo(/*0*/ x: A): A
|
||||
public final fun </*0*/ B> foo(/*0*/ x: kotlin.collections.List<B>): kotlin.collections.List<B>
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public final fun test0(): (kotlin.String) -> kotlin.String
|
||||
public final fun </*0*/ T> test1(): (kotlin.collections.List<T>) -> kotlin.collections.List<T>
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// !DIAGNOSTICS: -UNUSED_VARIABLE
|
||||
// KT-12286 Strange type is required for generic callable reference
|
||||
|
||||
fun <T: Comparable<T>> maxOf(a: T, b: T): T = if (a < b) b else a
|
||||
|
||||
fun <T: Comparable<T>> useMaxOf() {
|
||||
val f: (T, T) -> T = ::maxOf
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package
|
||||
|
||||
public fun </*0*/ T : kotlin.Comparable<T>> maxOf(/*0*/ a: T, /*1*/ b: T): T
|
||||
public fun </*0*/ T : kotlin.Comparable<T>> useMaxOf(): kotlin.Unit
|
||||
Reference in New Issue
Block a user