From 46928eaa0bc9fee849fdd568469ceacda65f2b4e Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Tue, 29 Nov 2022 11:01:35 +0200 Subject: [PATCH] [FIR] Unconditionally enable BooleanElvisBoundSmartCasts feature in K2 ^KT-54694 Fixed ^KT-26357 --- .../fir/resolve/dfa/FirDataFlowAnalyzer.kt | 7 +-- .../tests/smartCasts/elvis/basicOff.fir.kt | 50 +++++++++++++++++++ .../tests/smartCasts/elvis/basicOff.kt | 3 +- 3 files changed, 52 insertions(+), 8 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/smartCasts/elvis/basicOff.fir.kt diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/dfa/FirDataFlowAnalyzer.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/dfa/FirDataFlowAnalyzer.kt index 6fd51b3d056..71daff9be3d 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/dfa/FirDataFlowAnalyzer.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/dfa/FirDataFlowAnalyzer.kt @@ -5,7 +5,6 @@ package org.jetbrains.kotlin.fir.resolve.dfa -import org.jetbrains.kotlin.config.LanguageFeature import org.jetbrains.kotlin.contracts.description.EventOccurrencesRange import org.jetbrains.kotlin.descriptors.Modality import org.jetbrains.kotlin.fir.* @@ -1179,11 +1178,7 @@ abstract class FirDataFlowAnalyzer( val lhsVariable = variableStorage.getOrCreateIfReal(flow, elvisExpression.lhs) ?: return if (isLhsNotNull) { flow.commitOperationStatement(lhsVariable notEq null) - } else if ( - components.session.languageVersionSettings.supportsFeature(LanguageFeature.BooleanElvisBoundSmartCasts) && - rhs is FirConstExpression<*> && - rhs.kind == ConstantValueKind.Boolean - ) { + } else if (rhs is FirConstExpression<*> && rhs.kind == ConstantValueKind.Boolean) { // (a ?: x) != x -> a != null. The logic system can only handle cases where x is Boolean. // Or null...but nobody would write that, right? val elvisVariable = variableStorage.createSynthetic(elvisExpression) diff --git a/compiler/testData/diagnostics/tests/smartCasts/elvis/basicOff.fir.kt b/compiler/testData/diagnostics/tests/smartCasts/elvis/basicOff.fir.kt new file mode 100644 index 00000000000..3e47a847ae2 --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/elvis/basicOff.fir.kt @@ -0,0 +1,50 @@ +// !LANGUAGE: -BooleanElvisBoundSmartCasts + +interface Order { + val expired: Boolean? + + fun notExpired(): Boolean + + fun doSomething() +} + +fun foo(o: Any) { + val order = o as? Order + if (order?.expired ?: false) { + order.doSomething() + } + else { + + } + if (order?.notExpired() ?: false) { + order.doSomething() + } +} + +fun bar(o: Any) { + val order = o as? Order + if (order?.expired ?: true) { + + } + else { + order!!.doSomething() + } + if (order?.notExpired() ?: true) { + + } + else { + order!!.doSomething() + } +} + +fun baz(o: Boolean?) { + if (o ?: false) { + o.hashCode() + } + if (o ?: true) { + + } + else { + o.hashCode() + } +} diff --git a/compiler/testData/diagnostics/tests/smartCasts/elvis/basicOff.kt b/compiler/testData/diagnostics/tests/smartCasts/elvis/basicOff.kt index c15d4fe3ffc..882eac3580a 100644 --- a/compiler/testData/diagnostics/tests/smartCasts/elvis/basicOff.kt +++ b/compiler/testData/diagnostics/tests/smartCasts/elvis/basicOff.kt @@ -1,4 +1,3 @@ -// FIR_IDENTICAL // !LANGUAGE: -BooleanElvisBoundSmartCasts interface Order { @@ -48,4 +47,4 @@ fun baz(o: Boolean?) { else { o.hashCode() } -} \ No newline at end of file +}