From 8f8ea8c57f8e2b40d7285d1d723436a10c9c3b53 Mon Sep 17 00:00:00 2001 From: "Denis.Zharkov" Date: Wed, 23 Nov 2022 14:02:04 +0100 Subject: [PATCH] K2: Optimize one-branch when-expr case only for independent context Using independent one where `when` is nested doesn't look correct --- ...ControlFlowStatementsResolveTransformer.kt | 2 +- .../specialConstructsAndPlatformTypes.fir.kt | 2 +- .../inference/regressions/kt37419.fir.kt | 44 ------------------- .../tests/inference/regressions/kt37419.kt | 1 + .../tests/regressions/kt30245.fir.kt | 8 ++-- .../testsWithStdLib/when/kt10807.fir.kt | 17 ------- .../testsWithStdLib/when/kt10807.kt | 3 +- 7 files changed, 9 insertions(+), 68 deletions(-) delete mode 100644 compiler/testData/diagnostics/tests/inference/regressions/kt37419.fir.kt delete mode 100644 compiler/testData/diagnostics/testsWithStdLib/when/kt10807.fir.kt diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirControlFlowStatementsResolveTransformer.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirControlFlowStatementsResolveTransformer.kt index c5d145fff10..2d3d6f41fa3 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirControlFlowStatementsResolveTransformer.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirControlFlowStatementsResolveTransformer.kt @@ -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 diff --git a/compiler/testData/diagnostics/tests/controlStructures/specialConstructsAndPlatformTypes.fir.kt b/compiler/testData/diagnostics/tests/controlStructures/specialConstructsAndPlatformTypes.fir.kt index cc477738e96..c41c7dd1439 100644 --- a/compiler/testData/diagnostics/tests/controlStructures/specialConstructsAndPlatformTypes.fir.kt +++ b/compiler/testData/diagnostics/tests/controlStructures/specialConstructsAndPlatformTypes.fir.kt @@ -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 = when { else -> J.m[""] } +val testWhen3: String = when { else -> J.m[""] } val testWhen4: String? = when { else -> J.m[""] } diff --git a/compiler/testData/diagnostics/tests/inference/regressions/kt37419.fir.kt b/compiler/testData/diagnostics/tests/inference/regressions/kt37419.fir.kt deleted file mode 100644 index b69f6b11527..00000000000 --- a/compiler/testData/diagnostics/tests/inference/regressions/kt37419.fir.kt +++ /dev/null @@ -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 id(x: K): K = x - -class SomeClass { - val e = E.VALUE - - val withoutType: LambdaWithReceiver - get() = when (e) { - E.VALUE -> { param -> - method(param) - } - } - - val withExplicitType: LambdaWithReceiver - get() = when (e) { - E.VALUE -> { param: Parameter -> - method(param) - } - } -} - -class OtherClass { - val ok: LambdaWithReceiver - get() = { param: Parameter -> - method(param) - } -} - -val e2 = E.VALUE -val staticWithExplicitType: LambdaWithReceiver - get() = when (e2) { - E.VALUE -> { param: Parameter -> - method(param) - } - } diff --git a/compiler/testData/diagnostics/tests/inference/regressions/kt37419.kt b/compiler/testData/diagnostics/tests/inference/regressions/kt37419.kt index e1f1b9ec045..ec62ca16c33 100644 --- a/compiler/testData/diagnostics/tests/inference/regressions/kt37419.kt +++ b/compiler/testData/diagnostics/tests/inference/regressions/kt37419.kt @@ -1,3 +1,4 @@ +// FIR_IDENTICAL // !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE interface Receiver diff --git a/compiler/testData/diagnostics/tests/regressions/kt30245.fir.kt b/compiler/testData/diagnostics/tests/regressions/kt30245.fir.kt index 27942150941..5658d137139 100644 --- a/compiler/testData/diagnostics/tests/regressions/kt30245.fir.kt +++ b/compiler/testData/diagnostics/tests/regressions/kt30245.fir.kt @@ -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(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 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 { i: Int, s -> i + s.length } // oi- ni- val i28: E1 = id { i: Int, s -> i + s.length } // oi- ni- diff --git a/compiler/testData/diagnostics/testsWithStdLib/when/kt10807.fir.kt b/compiler/testData/diagnostics/testsWithStdLib/when/kt10807.fir.kt deleted file mode 100644 index 7f207767e3a..00000000000 --- a/compiler/testData/diagnostics/testsWithStdLib/when/kt10807.fir.kt +++ /dev/null @@ -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 = - when { - else -> nullsLast(compareBy({ it.a }, { it.b })) - } - -fun getCompInverted(): Comparator = - nullsLast( - when { - else -> compareBy({ it.a }, { it.b }) - } - ) diff --git a/compiler/testData/diagnostics/testsWithStdLib/when/kt10807.kt b/compiler/testData/diagnostics/testsWithStdLib/when/kt10807.kt index 6d8c7badb00..f7005c69807 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/when/kt10807.kt +++ b/compiler/testData/diagnostics/testsWithStdLib/when/kt10807.kt @@ -1,3 +1,4 @@ +// FIR_IDENTICAL import java.util.* import kotlin.comparisons.compareBy import kotlin.comparisons.nullsLast @@ -14,4 +15,4 @@ fun getCompInverted(): Comparator = when { else -> compareBy({ it.a }, { it.b }) } - ) \ No newline at end of file + )