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:
@@ -0,0 +1,24 @@
|
||||
interface Maybe<T>
|
||||
class Some<T>(val value: T) : Maybe<T>
|
||||
class None<T> : Maybe<T>
|
||||
|
||||
fun <T> none() : None<T> = TODO()
|
||||
|
||||
fun test1() : Maybe<String?> = if (true) none() else Some("")
|
||||
|
||||
fun test2() : Maybe<String?> = when {
|
||||
true -> none()
|
||||
else -> Some("")
|
||||
}
|
||||
|
||||
fun test3() : Maybe<String?> = when {
|
||||
true -> none()
|
||||
else -> Some<String?>("")
|
||||
}
|
||||
|
||||
fun test4() : Maybe<String?> {
|
||||
when ("") {
|
||||
"a" -> return none()
|
||||
else -> return Some<String?>("")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user