FIR checker: report SMARTCAST_IMPOSSIBLE for local variables

This commit is contained in:
Tianyu Geng
2021-05-13 17:23:18 -07:00
committed by TeamCityServer
parent 092750e215
commit 0ecc752813
38 changed files with 792 additions and 99 deletions
@@ -0,0 +1,56 @@
import kotlin.contracts.*
@Suppress("EXPERIMENTAL_API_USAGE_ERROR")
inline fun atLeastOnce(block: () -> Unit) {
contract {
callsInPlace(block, InvocationKind.AT_LEAST_ONCE)
}
block()
}
@Suppress("EXPERIMENTAL_API_USAGE_ERROR")
inline fun atMostOnce(block: () -> Unit) {
contract {
callsInPlace(block, InvocationKind.AT_MOST_ONCE)
}
block()
}
@Suppress("EXPERIMENTAL_API_USAGE_ERROR")
inline fun exactlyOnce(block: () -> Unit) {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
block()
}
fun test() {
var s: String? = <!VARIABLE_WITH_REDUNDANT_INITIALIZER!>null<!>
s = ""
atLeastOnce {
<!SMARTCAST_IMPOSSIBLE!>s<!>.length // unstable since lambda can be called twice
s = null
var s2: String? = <!VARIABLE_WITH_REDUNDANT_INITIALIZER!>null<!>
s2 = ""
<!DEBUG_INFO_SMARTCAST!>s2<!>.length // local variable declared inside lambda is stable
s2 = null
}
s = ""
exactlyOnce {
<!SMARTCAST_IMPOSSIBLE!>s<!>.length // stable since lambda can be called only once
s = null
var s2: String? = <!VARIABLE_WITH_REDUNDANT_INITIALIZER!>null<!>
s2 = ""
<!SMARTCAST_IMPOSSIBLE!>s2<!>.length // local variable declared inside lambda is stable
s2 = null
}
s = ""
atMostOnce {
<!SMARTCAST_IMPOSSIBLE!>s<!>.length // stable since lambda can be called at most once
s = null
var s2: String? = <!VARIABLE_WITH_REDUNDANT_INITIALIZER!>null<!>
s2 = ""
<!SMARTCAST_IMPOSSIBLE!>s2<!>.length // local variable declared inside lambda is stable
s2 = null
}
}