Files
kotlin-fork/compiler/testData/diagnostics/tests/controlStructures/ifElseIntersection.fir.kt
T
Denis.Zharkov 33dcbaac16 K2: Change resolution rules of when/if expressions with expected type
When expected type is known, use it as expected type for branch bodies.
While it indeed becomes different from the usual select call resolution,
where expected type is applied only after completion starts,
it helps to support, e.g. callable references resolution just as powerful
as it was in K1.

Also, in some cases where diagnostics have been changed, they become
a bit more helpful since they are reported closer
to the problematic places

cannotCastToFunction.kt test has been removed because it relied
on the case erroneously supported by the hack removed from
the FirCallResolver in this commit.

^KT-45989 Fixed
^KT-55936 Fixed
^KT-56445 Fixed
^KT-54709 Related
^KT-55931 Related
2023-02-15 08:13:46 +00:00

55 lines
1.3 KiB
Kotlin
Vendored

// !CHECK_TYPE
// See also KT-10896: Wrong inference of if / else result type
interface Option<T>
class Some<T> : Option<T>
class None<T> : Option<T>
fun <T> bind(r: Option<T>): Option<T> {
return if (r is Some) {
// Ideally we should infer Option<T> here (see KT-10896)
(if (true) None() else r) checkType { _<Option<T>>() }
// Works correctly
if (true) None() else r
}
else r
}
fun <T> bind2(r: Option<T>): Option<T> {
return if (r is Some) {
// Works correctly
if (true) None<T>() else r
}
else r
}
fun <T, R> bind3(r: Option<T>): Option<T> {
return <!RETURN_TYPE_MISMATCH!>if (r is Some) {
// Diagnoses an error correctly
<!TYPE_MISMATCH!>if (true) <!TYPE_MISMATCH, TYPE_MISMATCH, TYPE_MISMATCH!>None<R>()<!> else r<!>
}
else r<!>
}
fun <T> bindWhen(r: Option<T>): Option<T> {
return when (r) {
is Some -> {
// Works correctly
if (true) None() else r
}
else -> r
}
}
interface SimpleOption
class SimpleSome : SimpleOption
class SimpleNone : SimpleOption
fun bindNoGeneric(r: SimpleOption): SimpleOption {
return if (r is SimpleSome) {
(if (true) SimpleNone() else r) checkType { _<SimpleOption>() }
if (true) SimpleNone() else r
}
else r
}