K2: Fix undesirable NEW_INFERENCE_ERROR for case of return + buildList

While plainly repeating K1 behavior might be a dumb solution,
but inventing another one might be quite complicated
because we need to stop fixing variables into Nothing in many cases,
but probably not in all of them (see KT-58232).

^KT-58149 Fixed
^KT-58232 Related
This commit is contained in:
Denis.Zharkov
2023-04-25 11:42:55 +02:00
committed by Space Team
parent 133992d0bc
commit 0a631ed8fc
8 changed files with 132 additions and 3 deletions
@@ -0,0 +1,40 @@
// WITH_STDLIB
// ISSUE: KT-58149
fun <U> myRun(calc: () -> U): U {
return calc()
}
fun foo() {
// OK, works because we explicitly avoid adding last "return" as lambda result
val dates1 = myRun {
return@myRun buildList {
add(1)
}
}
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.collections.List<kotlin.Int>")!>dates1<!>
// OK, because the type of when is inferred to List<*>
val dates2 = myRun {
when {
true -> return@myRun buildList {
add(2)
}
else -> return@myRun buildList {
add(3)
}
}
}
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.collections.List<*>")!>dates2<!>
// Doesn't work both in K1 and K2, but probably should (KT-58232 for tracking)
val dates3 = <!NEW_INFERENCE_ERROR!>myRun {
when {
else -> return@myRun buildList {
add(4)
}
}
}<!>
}
@@ -0,0 +1,40 @@
// WITH_STDLIB
// ISSUE: KT-58149
fun <U> myRun(calc: () -> U): U {
return calc()
}
fun foo() {
// OK, works because we explicitly avoid adding last "return" as lambda result
val dates1 = myRun {
return@myRun buildList {
add(1)
}
}
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.collections.List<kotlin.Int>")!>dates1<!>
// OK, because the type of when is inferred to List<*>
val dates2 = myRun {
when {
true -> return@myRun buildList {
add(2)
}
else -> return@myRun buildList {
add(3)
}
}
}
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.collections.List<*>")!>dates2<!>
// Doesn't work both in K1 and K2, but probably should (KT-58232 for tracking)
<!UNREACHABLE_CODE!>val dates3 =<!> <!TYPE_MISMATCH!>myRun<!> {
when {
else -> return@myRun <!TYPE_MISMATCH, TYPE_MISMATCH!>buildList {
add(4)
}<!>
}
}
}