K2: Optimize one-branch when-expr case only for independent context

Using independent one where `when` is nested doesn't look correct
This commit is contained in:
Denis.Zharkov
2022-11-23 14:02:04 +01:00
committed by Space Team
parent 04d3996e68
commit 8f8ea8c57f
7 changed files with 9 additions and 68 deletions
@@ -68,7 +68,7 @@ class FirControlFlowStatementsResolveTransformer(transformer: FirAbstractBodyRes
context.withWhenSubjectType(subjectType, components) {
when {
whenExpression.branches.isEmpty() -> {}
whenExpression.isOneBranch() -> {
whenExpression.isOneBranch() && data is ResolutionMode.ContextIndependent -> {
whenExpression = whenExpression.transformBranches(transformer, ResolutionMode.ContextIndependent)
whenExpression.resultType = whenExpression.branches.first().result.resultType
// when with one branch cannot be completed if it's not already complete in the first place
@@ -35,5 +35,5 @@ val testIf4: String? = if (true) J.m[""] else J.m[""]
val testWhen1: String = when { else -> J.s }
val testWhen2: String? = when { else -> J.s }
val testWhen3: String = <!INITIALIZER_TYPE_MISMATCH!>when { else -> J.m[""] }<!>
val testWhen3: String = <!INITIALIZER_TYPE_MISMATCH, TYPE_MISMATCH!>when { else -> J.m[""] }<!>
val testWhen4: String? = when { else -> J.m[""] }
@@ -1,44 +0,0 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE
interface Receiver
interface Parameter
typealias LambdaWithReceiver = Receiver.(Parameter) -> Unit
fun Receiver.method(param: Parameter): LambdaWithReceiver = TODO()
enum class E { VALUE }
fun <K> id(x: K): K = x
class SomeClass {
val e = E.VALUE
val withoutType: LambdaWithReceiver
get() = <!RETURN_TYPE_MISMATCH!>when (e) {
E.VALUE -> { param ->
<!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>method<!>(param)
}
}<!>
val withExplicitType: LambdaWithReceiver
get() = <!RETURN_TYPE_MISMATCH!>when (e) {
E.VALUE -> { param: Parameter ->
<!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>method<!>(param)
}
}<!>
}
class OtherClass {
val ok: LambdaWithReceiver
get() = { param: Parameter ->
method(param)
}
}
val e2 = E.VALUE
val staticWithExplicitType: LambdaWithReceiver
get() = <!RETURN_TYPE_MISMATCH!>when (e2) {
E.VALUE -> { param: Parameter ->
<!UNRESOLVED_REFERENCE!>method<!>(param)
}
}<!>
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE
interface Receiver
@@ -77,10 +77,10 @@ fun test2() { // to extension lambda 1
val i26: E1 = id { s: String -> this + s.length } // oi- ni+
val i26a: E1 = id { s -> this + s.length } // oi+ ni+
val e = E.VALUE
val w27 = W2(<!ARGUMENT_TYPE_MISMATCH!>when (e) { E.VALUE -> { s: String -> <!NO_THIS!>this<!> + s.length } }<!>) // oi- ni+
val w27a = W2(<!ARGUMENT_TYPE_MISMATCH!>when (e) { E.VALUE -> { s -> <!NO_THIS!>this<!> + s.<!UNRESOLVED_REFERENCE!>length<!> } }<!>) // oi+ ni+
val i27: E1 = <!INITIALIZER_TYPE_MISMATCH!>when (e) { E.VALUE -> { s: String -> <!NO_THIS!>this<!> + s.length } }<!> // oi+ ni+
val i27a: E1 = <!INITIALIZER_TYPE_MISMATCH!>when (e) { E.VALUE -> { s -> <!NO_THIS!>this<!> + s.<!UNRESOLVED_REFERENCE!>length<!> } }<!> // oi+ ni+
val w27 = W2(when (e) { E.VALUE -> { s: String -> this + s.length } }) // oi- ni+
val w27a = W2(when (e) { E.VALUE -> { s -> this + s.length } }) // oi+ ni+
val i27: E1 = when (e) { E.VALUE -> { s: String -> this + s.length } } // oi+ ni+
val i27a: E1 = when (e) { E.VALUE -> { s -> this + s.length } } // oi+ ni+
val w28 = W2 <!ARGUMENT_TYPE_MISMATCH!>{ i: Int, <!CANNOT_INFER_PARAMETER_TYPE!>s<!> -> i <!OVERLOAD_RESOLUTION_AMBIGUITY!>+<!> s.<!UNRESOLVED_REFERENCE!>length<!> }<!> // oi- ni-
val i28: E1 = id { i: Int, <!CANNOT_INFER_PARAMETER_TYPE!>s<!> -> i <!OVERLOAD_RESOLUTION_AMBIGUITY!>+<!> s.<!UNRESOLVED_REFERENCE!>length<!> } // oi- ni-
@@ -1,17 +0,0 @@
import java.util.*
import kotlin.comparisons.compareBy
import kotlin.comparisons.nullsLast
class Foo(val a: String, val b: Int)
fun getComp(): Comparator<Foo?> =
when {
else -> <!NEW_INFERENCE_NO_INFORMATION_FOR_PARAMETER, NEW_INFERENCE_NO_INFORMATION_FOR_PARAMETER!>nullsLast<!>(compareBy({ it.<!UNRESOLVED_REFERENCE!>a<!> }, { it.<!UNRESOLVED_REFERENCE!>b<!> }))
}
fun getCompInverted(): Comparator<Foo?> =
nullsLast(
when {
else -> <!NEW_INFERENCE_NO_INFORMATION_FOR_PARAMETER!>compareBy<!>({ it.<!UNRESOLVED_REFERENCE!>a<!> }, { it.<!UNRESOLVED_REFERENCE!>b<!> })
}
)
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
import java.util.*
import kotlin.comparisons.compareBy
import kotlin.comparisons.nullsLast
@@ -14,4 +15,4 @@ fun getCompInverted(): Comparator<Foo?> =
when {
else -> compareBy({ it.a }, { it.b })
}
)
)