Support expected type from explicit cast

This commit support the following case.
Suppose we have such declaration:
  fun <T> foo(): T { ... }

Then in code we want to use it like this: `foo() as String`.
But in LV <= 1.1 we have type inference error: "Not enough
information for type parameter `T`". This error happened because we
do not use type from cast as expected type for call.

In this commit we fix this problem and use this type as expected type
in following cases:
 - our function has only one type parameter (this can be relaxed later)
 - function parameter types and extension receiver type not contains `T`

Also this fix problem with `findViewById`.
Already signature was: `fun findViewById(...): View`
and was used like: `findViewById() as MyView`.
New signature is `fun <T : View> findViewById(...): T`
and old usage was broken because of problem described above
This commit is contained in:
Stanislav Erokhin
2017-09-21 16:05:41 +03:00
committed by Ilya Gorbunov
parent ac508a510e
commit 4932fa1ddd
21 changed files with 412 additions and 1 deletions
@@ -0,0 +1,19 @@
// IGNORE_BACKEND: JS, NATIVE
// LANGUAGE_VERSION: 1.2
// WITH_RUNTIME
import kotlin.test.assertEquals
inline fun <reified T> foo(): T {
return T::class.java.getName() as T
}
fun box(): String {
val fooCall = foo() as String
assertEquals("java.lang.String", fooCall)
val safeFooCall = foo() as? String
assertEquals("java.lang.String", safeFooCall)
return "OK"
}