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:
+15
-3
@@ -8,9 +8,21 @@ interface D: A, B
|
||||
interface E: A, B
|
||||
|
||||
fun foo(c: C?, d: D?, e: E?) {
|
||||
val a: A? = <!TYPE_MISMATCH!>c ?: d<!> ?: e
|
||||
val test1: A? = <!TYPE_MISMATCH!>c ?: d<!> ?: e
|
||||
|
||||
val b: B? = if (false) <!TYPE_MISMATCH!>if (true) c else d<!> else e
|
||||
val test2: B? = if (false) if (true) c else d else e
|
||||
|
||||
//outer elvis operator and if-expression have error types
|
||||
val test3: A? = when {
|
||||
true -> c
|
||||
else -> when {
|
||||
true -> d
|
||||
else -> e
|
||||
}
|
||||
}
|
||||
|
||||
val test4: B? = when (1) {
|
||||
1 -> c
|
||||
2 -> d
|
||||
else -> e
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user