added tests for KT-2369, KT-2585 and KT-2972

KT-2369 Variable is not marked as uninitialized in 'finally' section
 #KT-2369 fixed
KT-2585 Code in try-finally is incorrectly marked as unreachable
 #KT-2585 fixed
KT-2972 Wrong "unused value" warning when finally is present
 #KT-2972 fixed
This commit is contained in:
Svetlana Isakova
2012-12-12 18:37:42 +04:00
parent 97058e628e
commit d7a60f5fed
6 changed files with 110 additions and 0 deletions
@@ -0,0 +1,13 @@
//KT-2369 Variable is not marked as uninitialized in 'finally' section
fun main(args: Array<String>) {
var x : Int
try {
throw Exception()
}
finally {
doSmth(<!UNINITIALIZED_VARIABLE!>x<!> + 1)
}
}
fun doSmth(a: Any?) = a
@@ -0,0 +1,12 @@
//KT-2585 Code in try-finally is incorrectly marked as unreachable
fun foo(x: String): String {
try {
<!UNREACHABLE_CODE!>throw RuntimeException()<!>
} finally {
try {
} catch (e: Exception) {
}
return x // <- Wrong UNREACHABLE_CODE
}
}
@@ -0,0 +1,23 @@
//KT-2585 Code in try-finally is incorrectly marked as unreachable
fun foo() {
try {
<!UNREACHABLE_CODE!>throw RuntimeException()<!>
} catch (e: Exception) {
<!UNREACHABLE_CODE!>return<!> // <- Wrong UNREACHABLE_CODE
} finally {
while (true);
}
}
fun bar() {
try {
throw RuntimeException()
} catch (e: Exception) {
return // <- Wrong UNREACHABLE_CODE
} finally {
while (cond());
}
}
fun cond() = true
@@ -0,0 +1,9 @@
//KT-2585 Code in try-finally is incorrectly marked as unreachable
fun foo(<!UNUSED_PARAMETER!>x<!>: String): String {
try {
<!UNREACHABLE_CODE!>throw RuntimeException()<!> //should be marked as unreachable, but is not
} finally {
throw NullPointerException()
}
}
@@ -0,0 +1,28 @@
//KT-2972 Wrong "unused value" warning when finally is present
import java.io.Closeable
public inline fun <T: Closeable, R> T.use(block: (T)-> R) : R {
var closed = false
try {
return block(this)
} catch (e: Exception) {
closed = true // warning here
try {
this.close()
} catch (closeException: Exception) {
// eat the closeException as we are already throwing the original cause
// and we don't want to mask the real exception
// TODO on Java 7 we should call
// e.addSuppressed(closeException)
// to work like try-with-resources
// http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html#suppressed-exceptions
}
throw e
} finally {
if (!closed) {
this.close()
}
}
}