Support lateinit local vars in redundant null check elimination

Lateinit local vars are guaranteed to be non-null after store.
So we mark such stores as storing non-null value
(could be useful for some other constructs, too),
and optimize null checks accordingly.
This commit is contained in:
Dmitry Petrov
2017-06-01 10:29:50 +03:00
parent d0b0fdd732
commit 78b69cad77
8 changed files with 189 additions and 28 deletions

View File

@@ -0,0 +1,26 @@
// LANGUAGE_VERSION: 1.2
fun almostAlwaysTrue() = true
fun runNoInline(f: () -> Unit) = f()
fun test() {
lateinit var z: String
runNoInline {
// NB this code can be executed in a different thread multiple times, each time with different results.
// So, 'z' can be initialized at any moment, and should be checked on every read.
if (almostAlwaysTrue()) {
z = ""
}
}
println(z)
println(z)
println(z)
}
// 0 IFNULL
// 3 IFNONNULL
// 3 throwUninitializedPropertyAccessException

View File

@@ -0,0 +1,19 @@
// LANGUAGE_VERSION: 1.2
fun almostAlwaysTrue() = true
fun test() {
lateinit var z: String
run {
if (almostAlwaysTrue()) {
z = ""
}
}
println(z)
println(z)
println(z)
}
// 0 IFNULL
// 1 IFNONNULL
// 1 throwUninitializedPropertyAccessException

View File

@@ -0,0 +1,12 @@
// LANGUAGE_VERSION: 1.2
fun test() {
lateinit var z: String
run {
z = ""
}
println(z)
}
// 0 IFNULL
// 0 IFNONNULL