Files
kotlin-fork/compiler/testData/diagnostics/tests/smartCasts/variables/reassignedInRhs.fir.kt
T
Brian Norman b309786353 [FIR] RHS assignment during equality comparison should invalidate DFA
If the right-hand side of an equality comparison contains an assignment
to the variable used in the left-hand side, then implications about the
equality comparison within data-flow analysis cannot be applied, as the
value of the variable will be different after the comparison.

^KT-55096 Fixed
2023-11-06 22:12:22 +00:00

52 lines
1.2 KiB
Kotlin
Vendored

// ISSUE: KT-55096
// SKIP_TXT
import kotlin.contracts.*
class C(val x: Int)
@OptIn(ExperimentalContracts::class)
fun isNotNullAlsoCall(a: String?, b: () -> Unit): Boolean {
contract {
returns(true) implies (a != null)
callsInPlace(b, InvocationKind.EXACTLY_ONCE)
}
b()
return a != null
}
fun binaryBooleanExpression() {
var x: String? = ""
if (x is String || (x is String).also { x = null }) {
x<!UNSAFE_CALL!>.<!>length // bad (x#0 is String, x#1 is Nothing?, this is either)
}
}
fun unoverriddenEquals(a: Any?) {
val c = C(1)
var b: Any?
b = c
if (b == c.also { b = a }) {
a.<!UNRESOLVED_REFERENCE!>x<!> // bad (b#0 is C, b#1 = a)
b.<!UNRESOLVED_REFERENCE!>x<!> // bad (b#0 is C, this is b#1)
if (b is C) { // b#1
a.x // ok (b#1 = a)
b.x // ok
}
}
}
fun safeCall() {
var x: String? = ""
if (x?.let { x = null; 1 } != null) {
x<!UNSAFE_CALL!>.<!>length // bad (#2 != null => x#0 != null; but x#1 = null and this is either)
}
}
fun contractFunction() {
var x: String? = ""
if (isNotNullAlsoCall(x) { x = null }) {
x.length // bad (#2 == true => x#0 != null; but this is x#1 = null)
}
}