[FIR] Unconditionally enable BooleanElvisBoundSmartCasts feature in K2

^KT-54694 Fixed
^KT-26357
This commit is contained in:
Dmitriy Novozhilov
2022-11-29 11:01:35 +02:00
committed by Space Team
parent ac7fddaad5
commit 46928eaa0b
3 changed files with 52 additions and 8 deletions
@@ -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<FLOW : Flow>(
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)
@@ -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<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>.doSomething()
}
if (order?.notExpired() ?: true) {
}
else {
order<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>.doSomething()
}
}
fun baz(o: Boolean?) {
if (o ?: false) {
o.hashCode()
}
if (o ?: true) {
}
else {
o.hashCode()
}
}
@@ -1,4 +1,3 @@
// FIR_IDENTICAL
// !LANGUAGE: -BooleanElvisBoundSmartCasts
interface Order {
@@ -48,4 +47,4 @@ fun baz(o: Boolean?) {
else {
o<!UNSAFE_CALL!>.<!>hashCode()
}
}
}