Fix type inference issues for 'if' and 'when'.

Use 'expectedType' (when present) as an explicit type argument for a special construct call.
Unfortunately, this approach can't be used for elvis due to other elvis-related inference hacks.
Fixes KT-10807, KT-10811.
This also affects KT-6189: now we can infer proper type for 'if'.

If type inference for special call failed, and we found no type errors in sub-expressions,
report TYPE_INFERENCE_FAILED_ON_SPECIAL_CONSTRUCT error.
This (and the hack above) fixes KT-10809: code no longer compiles.
This commit is contained in:
Dmitry Petrov
2016-01-26 21:20:24 +03:00
parent e3e463ef70
commit 9db3440e72
28 changed files with 498 additions and 18 deletions
+50
View File
@@ -0,0 +1,50 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER -DEBUG_INFO_SMARTCAST
interface Data
interface Item
class FlagData(val value: Boolean) : Data
class ListData<T : Item>(val list: List<T>) : Data
fun <T> listOf(vararg items: T): List<T> = null!!
fun test1(o: Any) = <!TYPE_INFERENCE_FAILED_ON_SPECIAL_CONSTRUCT!>when<!> (o) {
is List<*> ->
ListData(listOf())
is Int -> when {
o < 0 ->
FlagData(true)
else ->
null
}
else ->
null
}
fun test1x(o: Any): Data? = when (o) {
is List<*> ->
ListData(listOf())
is Int -> when {
o < 0 ->
FlagData(true)
else ->
null
}
else ->
null
}
fun test2() =
<!TYPE_INFERENCE_FAILED_ON_SPECIAL_CONSTRUCT!>if<!> (true)
ListData(listOf())
else
FlagData(true)
fun test2x(): Data =
if (true) ListData(listOf()) else FlagData(true)
fun test2y(): Any =
if (true) ListData(listOf()) else FlagData(true)
fun test2z(): Any =
run { if (true) ListData(listOf()) else FlagData(true) }