mirror of
https://github.com/jlengrand/kotlin.git
synced 2026-05-07 00:21:28 +00:00
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:
26
compiler/testData/codegen/bytecodeText/nullCheckOptimization/localLateinit/checkedAlways.kt
vendored
Normal file
26
compiler/testData/codegen/bytecodeText/nullCheckOptimization/localLateinit/checkedAlways.kt
vendored
Normal 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
|
||||
19
compiler/testData/codegen/bytecodeText/nullCheckOptimization/localLateinit/checkedOnce.kt
vendored
Normal file
19
compiler/testData/codegen/bytecodeText/nullCheckOptimization/localLateinit/checkedOnce.kt
vendored
Normal 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
|
||||
12
compiler/testData/codegen/bytecodeText/nullCheckOptimization/localLateinit/initialized.kt
vendored
Normal file
12
compiler/testData/codegen/bytecodeText/nullCheckOptimization/localLateinit/initialized.kt
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
// LANGUAGE_VERSION: 1.2
|
||||
|
||||
fun test() {
|
||||
lateinit var z: String
|
||||
run {
|
||||
z = ""
|
||||
}
|
||||
println(z)
|
||||
}
|
||||
|
||||
// 0 IFNULL
|
||||
// 0 IFNONNULL
|
||||
Reference in New Issue
Block a user