[NI] Fix unit coercion
Consider following case:
fun foo(): Unit = run { "hello" }
Previously, NI would analyze lambda body without expected type, because
it is a type variable 'R' from 'run', which hasn't been fixed yet. This
leads to treating "hello" as lambda-return argument and adding bogus
'String' constraint on 'R', and, consequently, type mismatch.
Now, we peek into current constraint system and check if return-type of
lambda is type variable with upper-Unit constraint (which is exactly
condition for its body to be Unit-coerced). If so, then we provide
expected Unit-type for body explicitly, and the rest will be done
automatically (in particular, in aforementioned example "hello" wouldn't
be treated as lambda return argument).
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
// !WITH_NEW_INFERENCE
|
||||
// !CHECK_TYPE
|
||||
|
||||
fun noCoercionLastExpressionUsedAsReturnArgument() {
|
||||
val a = {
|
||||
42
|
||||
}
|
||||
|
||||
a checkType { _<() -> Int>() }
|
||||
}
|
||||
|
||||
fun noCoercionBlockHasExplicitType() {
|
||||
val <!UNUSED_VARIABLE!>b<!>: () -> Int = {
|
||||
<!TYPE_MISMATCH!>if (true) <!UNUSED_EXPRESSION!>42<!><!>
|
||||
}
|
||||
}
|
||||
|
||||
fun noCoercionBlockHasExplicitReturn() {
|
||||
val <!UNUSED_VARIABLE!>c<!> = l@{
|
||||
if (true) return@l 42
|
||||
|
||||
<!INVALID_IF_AS_EXPRESSION!>if<!> (true) 239
|
||||
}
|
||||
}
|
||||
|
||||
fun noCoercionInExpressionBody(): Unit = <!TYPE_MISMATCH!>"hello"<!>
|
||||
Reference in New Issue
Block a user