backend: add tests for try-catch-finally and throwing exceptions
also add some tests for bugs with unreachable code handling and variables
This commit is contained in:
@@ -420,3 +420,123 @@ task array_list1(type: RunKonanTest) {
|
|||||||
source = "runtime/collections/array_list1.kt"
|
source = "runtime/collections/array_list1.kt"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
task catch1(type: RunKonanTest) {
|
||||||
|
goldValue = "Before\nCaught Throwable\nDone\n"
|
||||||
|
source = "runtime/exceptions/catch1.kt"
|
||||||
|
}
|
||||||
|
|
||||||
|
task catch2(type: RunKonanTest) {
|
||||||
|
goldValue = "Before\nCaught Error\nDone\n"
|
||||||
|
source = "runtime/exceptions/catch2.kt"
|
||||||
|
}
|
||||||
|
|
||||||
|
task catch7(type: RunKonanTest) {
|
||||||
|
goldValue = "Error happens\n"
|
||||||
|
source = "runtime/exceptions/catch7.kt"
|
||||||
|
}
|
||||||
|
|
||||||
|
task catch3(type: RunKonanTest) {
|
||||||
|
goldValue = "Before\nCaught Throwable\nDone\n"
|
||||||
|
source = "codegen/try/catch3.kt"
|
||||||
|
}
|
||||||
|
|
||||||
|
task catch4(type: RunKonanTest) {
|
||||||
|
goldValue = "Before\nCaught Error\nDone\n"
|
||||||
|
source = "codegen/try/catch4.kt"
|
||||||
|
}
|
||||||
|
|
||||||
|
task catch5(type: RunKonanTest) {
|
||||||
|
goldValue = "Before\nCaught Error\nDone\n"
|
||||||
|
source = "codegen/try/catch5.kt"
|
||||||
|
}
|
||||||
|
|
||||||
|
task catch6(type: RunKonanTest) {
|
||||||
|
goldValue = "Before\nCaught Error\nDone\n"
|
||||||
|
source = "codegen/try/catch6.kt"
|
||||||
|
}
|
||||||
|
|
||||||
|
task catch8(type: RunKonanTest) {
|
||||||
|
goldValue = "Error happens\n"
|
||||||
|
source = "codegen/try/catch8.kt"
|
||||||
|
}
|
||||||
|
|
||||||
|
task finally1(type: RunKonanTest) {
|
||||||
|
goldValue = "Try\nFinally\nDone\n"
|
||||||
|
source = "codegen/try/finally1.kt"
|
||||||
|
}
|
||||||
|
|
||||||
|
task finally2(type: RunKonanTest) {
|
||||||
|
goldValue = "Try\nCaught Error\nFinally\nDone\n"
|
||||||
|
source = "codegen/try/finally2.kt"
|
||||||
|
}
|
||||||
|
|
||||||
|
task finally3(type: RunKonanTest) {
|
||||||
|
goldValue = "Try\nFinally\nCaught Error\nDone\n"
|
||||||
|
source = "codegen/try/finally3.kt"
|
||||||
|
}
|
||||||
|
|
||||||
|
task finally4(type: RunKonanTest) {
|
||||||
|
goldValue = "Try\nCatch\nFinally\nCaught Exception\nDone\n"
|
||||||
|
source = "codegen/try/finally4.kt"
|
||||||
|
}
|
||||||
|
|
||||||
|
task finally5(type: RunKonanTest) {
|
||||||
|
goldValue = "Done\nFinally\n0\n"
|
||||||
|
source = "codegen/try/finally5.kt"
|
||||||
|
}
|
||||||
|
|
||||||
|
task finally6(type: RunKonanTest) {
|
||||||
|
goldValue = "Done\nFinally\n1\n"
|
||||||
|
source = "codegen/try/finally6.kt"
|
||||||
|
}
|
||||||
|
|
||||||
|
task finally7(type: RunKonanTest) {
|
||||||
|
goldValue = "Done\nFinally\n1\n"
|
||||||
|
source = "codegen/try/finally7.kt"
|
||||||
|
}
|
||||||
|
|
||||||
|
task finally8(type: RunKonanTest) {
|
||||||
|
goldValue = "Finally 1\nFinally 2\n42\n"
|
||||||
|
source = "codegen/try/finally8.kt"
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
TODO: enable after implementing break and continue
|
||||||
|
task finally9(type: RunKonanTest) {
|
||||||
|
goldValue = "Finally 1\nFinally 2\nAfter\n"
|
||||||
|
source = "codegen/try/finally9.kt"
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
TODO: enable after improving LLVM variable naming
|
||||||
|
task scope1(type: RunKonanTest) {
|
||||||
|
goldValue = "1\n"
|
||||||
|
source = "codegen/dataflow/scope1.kt"
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
task try1(type: RunKonanTest) {
|
||||||
|
goldValue = "5\n"
|
||||||
|
source = "codegen/try/try1.kt"
|
||||||
|
}
|
||||||
|
|
||||||
|
task try2(type: RunKonanTest) {
|
||||||
|
goldValue = "6\n"
|
||||||
|
source = "codegen/try/try2.kt"
|
||||||
|
}
|
||||||
|
|
||||||
|
task try3(type: RunKonanTest) {
|
||||||
|
goldValue = "6\n"
|
||||||
|
source = "codegen/try/try3.kt"
|
||||||
|
}
|
||||||
|
|
||||||
|
task try4(type: RunKonanTest) {
|
||||||
|
goldValue = "Try\n5\n"
|
||||||
|
source = "codegen/try/try4.kt"
|
||||||
|
}
|
||||||
|
|
||||||
|
task unreachable1(type: RunKonanTest) {
|
||||||
|
goldValue = "1\n"
|
||||||
|
source = "codegen/controlflow/unreachable1.kt"
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
fun main(args: Array<String>) {
|
||||||
|
println(foo())
|
||||||
|
}
|
||||||
|
|
||||||
|
fun foo(): Int {
|
||||||
|
return 1
|
||||||
|
println("After return")
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
var b = true
|
||||||
|
|
||||||
|
fun main(args: Array<String>) {
|
||||||
|
var x = 1
|
||||||
|
if (b) {
|
||||||
|
var x = 2
|
||||||
|
}
|
||||||
|
println(x)
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
fun main(args : Array<String>) {
|
||||||
|
try {
|
||||||
|
println("Before")
|
||||||
|
throw Error("Error happens")
|
||||||
|
println("After")
|
||||||
|
} catch (e: Throwable) {
|
||||||
|
println("Caught Throwable")
|
||||||
|
}
|
||||||
|
|
||||||
|
println("Done")
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
fun main(args : Array<String>) {
|
||||||
|
try {
|
||||||
|
println("Before")
|
||||||
|
throw Error("Error happens")
|
||||||
|
println("After")
|
||||||
|
} catch (e: Exception) {
|
||||||
|
println("Caught Exception")
|
||||||
|
} catch (e: Error) {
|
||||||
|
println("Caught Error")
|
||||||
|
} catch (e: Throwable) {
|
||||||
|
println("Caught Throwable")
|
||||||
|
}
|
||||||
|
|
||||||
|
println("Done")
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
fun main(args : Array<String>) {
|
||||||
|
try {
|
||||||
|
try {
|
||||||
|
println("Before")
|
||||||
|
foo()
|
||||||
|
println("After")
|
||||||
|
} catch (e: Exception) {
|
||||||
|
println("Caught Exception")
|
||||||
|
}
|
||||||
|
|
||||||
|
println("After nested try")
|
||||||
|
|
||||||
|
} catch (e: Error) {
|
||||||
|
println("Caught Error")
|
||||||
|
} catch (e: Throwable) {
|
||||||
|
println("Caught Throwable")
|
||||||
|
}
|
||||||
|
|
||||||
|
println("Done")
|
||||||
|
}
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
throw Error("Error happens")
|
||||||
|
println("After in foo()")
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
fun main(args : Array<String>) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
println("Before")
|
||||||
|
foo()
|
||||||
|
println("After")
|
||||||
|
} catch (e: Exception) {
|
||||||
|
println("Caught Exception")
|
||||||
|
} catch (e: Error) {
|
||||||
|
println("Caught Error")
|
||||||
|
}
|
||||||
|
|
||||||
|
println("Done")
|
||||||
|
}
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
try {
|
||||||
|
throw Error("Error happens")
|
||||||
|
} catch (e: Exception) {
|
||||||
|
println("Caught Exception")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
fun main(args : Array<String>) {
|
||||||
|
try {
|
||||||
|
throw Error("Error happens")
|
||||||
|
} catch (e: Throwable) {
|
||||||
|
val message = e.message
|
||||||
|
if (message != null) {
|
||||||
|
println(message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
fun main(args : Array<String>) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
println("Try")
|
||||||
|
} finally {
|
||||||
|
println("Finally")
|
||||||
|
}
|
||||||
|
|
||||||
|
println("Done")
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
fun main(args : Array<String>) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
println("Try")
|
||||||
|
throw Error("Error happens")
|
||||||
|
println("After throw")
|
||||||
|
} catch (e: Error) {
|
||||||
|
println("Caught Error")
|
||||||
|
} finally {
|
||||||
|
println("Finally")
|
||||||
|
}
|
||||||
|
|
||||||
|
println("Done")
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
fun main(args : Array<String>) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
try {
|
||||||
|
println("Try")
|
||||||
|
throw Error("Error happens")
|
||||||
|
println("After throw")
|
||||||
|
} finally {
|
||||||
|
println("Finally")
|
||||||
|
}
|
||||||
|
|
||||||
|
println("After nested try")
|
||||||
|
|
||||||
|
} catch (e: Error) {
|
||||||
|
println("Caught Error")
|
||||||
|
}
|
||||||
|
|
||||||
|
println("Done")
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
fun main(args : Array<String>) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
try {
|
||||||
|
println("Try")
|
||||||
|
throw Error("Error happens")
|
||||||
|
println("After throw")
|
||||||
|
} catch (e: Error) {
|
||||||
|
println("Catch")
|
||||||
|
throw Exception()
|
||||||
|
println("After throw")
|
||||||
|
} finally {
|
||||||
|
println("Finally")
|
||||||
|
}
|
||||||
|
|
||||||
|
println("After nested try")
|
||||||
|
|
||||||
|
} catch (e: Error) {
|
||||||
|
println("Caught Error")
|
||||||
|
} catch (e: Exception) {
|
||||||
|
println("Caught Exception")
|
||||||
|
}
|
||||||
|
|
||||||
|
println("Done")
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
fun main(args : Array<String>) {
|
||||||
|
println(foo())
|
||||||
|
}
|
||||||
|
|
||||||
|
fun foo(): Int {
|
||||||
|
try {
|
||||||
|
println("Done")
|
||||||
|
return 0
|
||||||
|
} finally {
|
||||||
|
println("Finally")
|
||||||
|
}
|
||||||
|
|
||||||
|
println("After")
|
||||||
|
return 1
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
fun main(args : Array<String>) {
|
||||||
|
println(foo())
|
||||||
|
}
|
||||||
|
|
||||||
|
fun foo(): Int {
|
||||||
|
try {
|
||||||
|
println("Done")
|
||||||
|
return 0
|
||||||
|
} finally {
|
||||||
|
println("Finally")
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
println("After")
|
||||||
|
return 2
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
fun main(args : Array<String>) {
|
||||||
|
println(foo())
|
||||||
|
}
|
||||||
|
|
||||||
|
fun foo(): Int {
|
||||||
|
try {
|
||||||
|
println("Done")
|
||||||
|
throw Error()
|
||||||
|
} finally {
|
||||||
|
println("Finally")
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
println("After")
|
||||||
|
return 2
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
fun main(args : Array<String>) {
|
||||||
|
println(foo())
|
||||||
|
}
|
||||||
|
|
||||||
|
fun foo(): Int {
|
||||||
|
try {
|
||||||
|
try {
|
||||||
|
return 42
|
||||||
|
} finally {
|
||||||
|
println("Finally 1")
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
println("Finally 2")
|
||||||
|
}
|
||||||
|
|
||||||
|
println("After")
|
||||||
|
return 2
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
fun main(args : Array<String>) {
|
||||||
|
do {
|
||||||
|
try {
|
||||||
|
break
|
||||||
|
} finally {
|
||||||
|
println("Finally 1")
|
||||||
|
}
|
||||||
|
} while (false)
|
||||||
|
|
||||||
|
var stop = false
|
||||||
|
while (!stop) {
|
||||||
|
try {
|
||||||
|
stop = true
|
||||||
|
continue
|
||||||
|
} finally {
|
||||||
|
println("Finally 2")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
println("After")
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
fun main(args : Array<String>) {
|
||||||
|
val x = try {
|
||||||
|
5
|
||||||
|
} catch (e: Throwable) {
|
||||||
|
6
|
||||||
|
}
|
||||||
|
|
||||||
|
println(x)
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
fun main(args : Array<String>) {
|
||||||
|
val x = try {
|
||||||
|
throw Error()
|
||||||
|
5
|
||||||
|
} catch (e: Throwable) {
|
||||||
|
6
|
||||||
|
}
|
||||||
|
|
||||||
|
println(x)
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
fun main(args : Array<String>) {
|
||||||
|
val x = try {
|
||||||
|
throw Error()
|
||||||
|
} catch (e: Throwable) {
|
||||||
|
6
|
||||||
|
}
|
||||||
|
|
||||||
|
println(x)
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
fun main(args : Array<String>) {
|
||||||
|
val x = try {
|
||||||
|
println("Try")
|
||||||
|
5
|
||||||
|
} catch (e: Throwable) {
|
||||||
|
throw e
|
||||||
|
}
|
||||||
|
|
||||||
|
println(x)
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
fun main(args : Array<String>) {
|
||||||
|
try {
|
||||||
|
println("Before")
|
||||||
|
foo()
|
||||||
|
println("After")
|
||||||
|
} catch (e: Throwable) {
|
||||||
|
println("Caught Throwable")
|
||||||
|
}
|
||||||
|
|
||||||
|
println("Done")
|
||||||
|
}
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
throw Error("Error happens")
|
||||||
|
println("After in foo()")
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
fun main(args : Array<String>) {
|
||||||
|
try {
|
||||||
|
println("Before")
|
||||||
|
foo()
|
||||||
|
println("After")
|
||||||
|
} catch (e: Exception) {
|
||||||
|
println("Caught Exception")
|
||||||
|
} catch (e: Error) {
|
||||||
|
println("Caught Error")
|
||||||
|
} catch (e: Throwable) {
|
||||||
|
println("Caught Throwable")
|
||||||
|
}
|
||||||
|
|
||||||
|
println("Done")
|
||||||
|
}
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
throw Error("Error happens")
|
||||||
|
println("After in foo()")
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
fun main(args : Array<String>) {
|
||||||
|
try {
|
||||||
|
foo()
|
||||||
|
} catch (e: Throwable) {
|
||||||
|
val message = e.message
|
||||||
|
if (message != null) {
|
||||||
|
println(message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
throw Error("Error happens")
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user