Files
kotlin-fork/compiler/testData/codegen/box/classes/kt496.kt
T
Mads Ager fb6eafddf1 JVM_IR: Generate better code for null checks.
Simplify ifs when branches have condition true/false.

Simplify blocks containing only a variable declaration
and a variable get of the same variable. Simplify to
just the condition.

Do not introduce temporary variables for constants for
null checks. Constants have no side-effects and can be
reloaded freely instead of going through a local.

This simplifies code such as "42.toLong()!!" so that the
resulting code has no branches and uses no locals. The
simplifications happen as follows:

```
block
  temp = 42.toLong()
  when
    (temp == null) throw NPE
    (true) load temp

---> null test simplification

block
  temp = 42.toLong()
  when
    (false) throw NPE
    (true) load temp

---> when simplification

block
  temp = 42.toLong()
  load temp

---> block simplification

42.toLong()
```
2019-01-23 15:11:14 +01:00

84 lines
1.2 KiB
Kotlin
Vendored

fun test1() : Boolean {
try {
return true
} finally {
if(true) // otherwise we wisely have unreachable code
return false
}
}
var x = true
fun test2() : Boolean {
try {
} finally {
x = false;
}
return x
}
fun test3() : Int {
var y = 0
try {
++y
} finally {
++y
}
return y
}
var z = 0
fun test4() : Int {
z = 0
return try {
try {
z++
}
finally {
z++
}
} finally {
++z
}
}
fun test5() : Int {
var x = 0
while(true) {
try {
if(x < 10)
x++
else
break
}
finally {
x++
}
}
return x
}
fun test6() : Int {
var x = 0
while(x < 10) {
try {
x++
continue
}
finally {
x++
}
}
return x
}
fun box() : String {
if(test1()) return "test1 failed"
if(test2()) return "test2 failed"
if(test3() != 2) return "test3 failed"
if(test4() != 0) return "test4 failed"
if(test5() != 11) return "test5 failed"
if(test6() != 10) return "test6 failed"
return "OK"
}