Mark only unreachable parts of element if it has reachable parts

like for 'return todo()' mark only 'return'
This commit is contained in:
Svetlana Isakova
2014-06-11 19:35:00 +04:00
parent 88ecc5cc59
commit 79cec6411d
38 changed files with 597 additions and 84 deletions
@@ -1,3 +1,5 @@
// !DIAGNOSTICS: -UNREACHABLE_CODE
fun none() {}
fun unitEmptyInfer() {}
@@ -51,7 +53,7 @@ fun blockAndAndMismatch1() : Int {
return <!TYPE_MISMATCH!>true && false<!>
}
fun blockAndAndMismatch2() : Int {
<!UNREACHABLE_CODE!>(return <!CONSTANT_EXPECTED_TYPE_MISMATCH!>true<!>) && (return <!CONSTANT_EXPECTED_TYPE_MISMATCH!>false<!>)<!>
(return <!CONSTANT_EXPECTED_TYPE_MISMATCH!>true<!>) && (return <!CONSTANT_EXPECTED_TYPE_MISMATCH!>false<!>)
}
fun blockAndAndMismatch3() : Int {
@@ -61,7 +63,7 @@ fun blockAndAndMismatch4() : Int {
return <!TYPE_MISMATCH!>true || false<!>
}
fun blockAndAndMismatch5() : Int {
<!UNREACHABLE_CODE!>(return <!CONSTANT_EXPECTED_TYPE_MISMATCH!>true<!>) || (return <!CONSTANT_EXPECTED_TYPE_MISMATCH!>false<!>)<!>
(return <!CONSTANT_EXPECTED_TYPE_MISMATCH!>true<!>) || (return <!CONSTANT_EXPECTED_TYPE_MISMATCH!>false<!>)
}
fun blockReturnValueTypeMatch1() : Int {
return if (1 > 2) <!CONSTANT_EXPECTED_TYPE_MISMATCH!>1.0<!> else <!CONSTANT_EXPECTED_TYPE_MISMATCH!>2.0<!>
@@ -0,0 +1,12 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
fun testCommasAndWhitespaces() {
fun bar(i: Int, s: String, x: Any) {}
<!UNREACHABLE_CODE!>bar(<!> 1 , todo() , <!UNREACHABLE_CODE!>"" )<!>
}
fun todo() = throw Exception()
@@ -0,0 +1,11 @@
fun testInvoke() {
fun Nothing.invoke() = this
todo()<!UNREACHABLE_CODE!>()<!>
}
fun testInvokeWithLambda() {
fun Nothing.invoke(<!UNUSED_PARAMETER!>i<!>: Int, f: () -> Int) = f
todo()<!UNREACHABLE_CODE!>(1){ 42 }<!>
}
fun todo() = throw Exception()
@@ -0,0 +1,11 @@
fun test11() {
fun Any.bar(<!UNUSED_PARAMETER!>i<!>: Int) {}
todo().<!UNREACHABLE_CODE!>bar(1)<!>
}
fun test12() {
fun Any.bar(<!UNUSED_PARAMETER!>i<!>: Int) {}
todo()<!UNNECESSARY_SAFE_CALL!>?.<!><!UNREACHABLE_CODE!>bar(1)<!>
}
fun todo() = throw Exception()
@@ -1,3 +1,4 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_EXPRESSION
fun t1() : Int{
return 0
@@ -46,7 +47,7 @@ fun t3() : Any {
<!UNREACHABLE_CODE!>1<!>
}
fun t4(<!UNUSED_PARAMETER!>a<!> : Boolean) : Int {
fun t4(a : Boolean) : Int {
do {
return 1
}
@@ -54,7 +55,7 @@ fun t4(<!UNUSED_PARAMETER!>a<!> : Boolean) : Int {
<!UNREACHABLE_CODE!>1<!>
}
fun t4break(<!UNUSED_PARAMETER!>a<!> : Boolean) : Int {
fun t4break(a : Boolean) : Int {
do {
break
}
@@ -109,7 +110,7 @@ fun t7() : Int {
<!UNREACHABLE_CODE!>2<!>
}
catch (<!TYPE_MISMATCH!>e : Any<!>) {
<!UNUSED_EXPRESSION!>2<!>
2
}
return 1 // this is OK, like in Java
}
@@ -127,16 +128,16 @@ fun t8() : Int {
}
fun blockAndAndMismatch() : Boolean {
<!UNREACHABLE_CODE!>(return true) || (return false)<!>
(return true) <!UNREACHABLE_CODE!>|| (return false)<!>
<!UNREACHABLE_CODE!>return true<!>
}
fun tf() : Int {
try {<!UNREACHABLE_CODE!>return 1<!>} finally{return 1}
try {<!UNREACHABLE_CODE!>return<!> 1} finally{return 1}
<!UNREACHABLE_CODE!>return 1<!>
}
fun failtest(<!UNUSED_PARAMETER!>a<!> : Int) : Int {
fun failtest(a : Int) : Int {
if (fail() || <!UNREACHABLE_CODE!>true<!>) <!UNREACHABLE_CODE!>{
}<!>
@@ -144,8 +145,8 @@ fun failtest(<!UNUSED_PARAMETER!>a<!> : Int) : Int {
}
fun foo(a : Nothing) : Unit {
<!UNUSED_EXPRESSION!>1<!>
<!UNUSED_EXPRESSION!>a<!>
1
a
<!UNREACHABLE_CODE!>2<!>
}
@@ -161,7 +162,7 @@ fun nullIsNotNothing() : Unit {
fail()
}
fun returnInWhile(<!UNUSED_PARAMETER!>a<!>: Int) {
fun returnInWhile(a: Int) {
do {return}
while (<!UNREACHABLE_CODE!>1 > a<!>)
}
@@ -1,7 +1,7 @@
package c
fun test1() {
<!UNREACHABLE_CODE!>val <!UNUSED_VARIABLE!>r<!>: Nothing = null!!<!>
<!UNREACHABLE_CODE!>val <!UNUSED_VARIABLE!>r<!>: Nothing =<!> null!!
}
fun test2(a: A) {
@@ -0,0 +1,40 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
fun testArrayAccess1(array: Array<Any>) {
array<!UNREACHABLE_CODE!>[<!>todo()<!UNREACHABLE_CODE!>]<!>
}
fun testArrayAccess2() {
fun Nothing.get(i: Int, s: String) {}
todo()<!UNREACHABLE_CODE!>[1, ""]<!>
}
fun testAraryAccess3() {
fun Nothing.get(n: Nothing) {}
todo()<!UNREACHABLE_CODE!>[todo()]<!>
}
fun testArrayAssignment1(array: Array<Any>) {
array[todo()] <!UNREACHABLE_CODE!>= 11<!>
}
fun testArrayAssignment2(array: Array<Any>) {
array[1] <!UNREACHABLE_CODE!>=<!> todo()
}
fun testArrayAssignment3(n: Nothing) {
fun Nothing.set(i: Int, j: Int) {}
n<!UNREACHABLE_CODE!>[1] = 2<!>
}
fun testArrayAssignment4(n: Nothing) {
fun Nothing.set(i: Int, a: Any) {}
n<!UNREACHABLE_CODE!>[1] = todo()<!>
}
fun testArrayPlusAssign(array: Array<Any>) {
fun Any.plusAssign(a: Any) {}
array[1] <!UNREACHABLE_CODE!>+=<!> todo()
}
fun todo() = throw Exception()
@@ -0,0 +1,18 @@
fun testAssignment() {
var <!UNUSED_VARIABLE!>a<!> = 1
<!UNREACHABLE_CODE!>a =<!> todo()
}
fun testVariableDeclaration() {
<!UNREACHABLE_CODE!>val <!UNUSED_VARIABLE!>a<!> =<!> todo()
}
fun testPlusAssign() {
fun Int.plusAssign(<!UNUSED_PARAMETER!>i<!>: Int) {}
var a = 1
a <!UNREACHABLE_CODE!>+=<!> todo()
}
fun todo() = throw Exception()
@@ -0,0 +1,39 @@
fun testBinary1() {
fun Int.times(<!UNUSED_PARAMETER!>s<!>: String) {}
todo() <!UNREACHABLE_CODE!>* ""<!>
}
fun testBinary2() {
"1" <!UNREACHABLE_CODE!>+<!> todo()
}
fun testElvis1() {
<!USELESS_ELVIS!>todo()<!> <!UNREACHABLE_CODE!>?: ""<!>
}
fun testElvis2(s: String?) {
s ?: todo()
bar()
}
fun testAnd1(b: Boolean) {
b && todo()
bar()
}
fun testAnd2(<!UNUSED_PARAMETER!>b<!>: Boolean) {
todo() <!UNREACHABLE_CODE!>&& b<!>
}
fun returnInBinary1(): Boolean {
(return true) <!UNREACHABLE_CODE!>&& (return false)<!>
}
fun returnInBinary2(): Boolean {
(return true) <!UNREACHABLE_CODE!>|| (return false)<!>
}
fun todo() = throw Exception()
fun bar() {}
@@ -0,0 +1,13 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
fun testArgumentInCall() {
fun bar(i: Int, s: String, x: Any) {}
<!UNREACHABLE_CODE!>bar(<!>1, todo(), <!UNREACHABLE_CODE!>"")<!>
}
fun testArgumentInVariableAsFunctionCall(f: (Any) -> Unit) {
f<!UNREACHABLE_CODE!>(<!>todo()<!UNREACHABLE_CODE!>)<!>
}
fun todo() = throw Exception()
@@ -0,0 +1,24 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
fun unreachable0() {
return
<!UNREACHABLE_CODE!>return todo()<!>
}
fun unreachable2() {
return
<!UNREACHABLE_CODE!>val a = todo()<!>
}
fun unreachable3() {
return
<!UNREACHABLE_CODE!>bar(todo())<!>
}
fun unreachable4(array: Array<Any>) {
return
<!UNREACHABLE_CODE!>array[todo()]<!>
}
fun bar(a: Any) {}
fun todo() = throw Exception()
@@ -0,0 +1,12 @@
fun testIf() {
if (todo()) <!UNREACHABLE_CODE!>1<!> else <!UNREACHABLE_CODE!>2<!>
}
fun testIf1(b: Boolean) {
if (b) todo() else 1
bar()
}
fun todo() = throw Exception()
fun bar() {}
@@ -0,0 +1,13 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
fun testCompound() {
fun Nothing.get(i: Int) {}
todo()<!UNREACHABLE_CODE!><!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>[12]<!>
}
fun testCompound1() {
fun Int.times(s: String): Array<String> = throw Exception()
<!UNREACHABLE_CODE!>(<!>todo() <!UNREACHABLE_CODE!>* "")[1]<!>
}
fun todo() = throw Exception()
@@ -0,0 +1,36 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
fun testObject() {
<!UNREACHABLE_CODE!>object : Foo(<!>todo()<!UNREACHABLE_CODE!>) {
fun foo() = 1
}<!>
}
fun testObjectExpression() {
<!UNREACHABLE_CODE!>val <!UNUSED_VARIABLE!>a<!> = object : Foo(<!>todo()<!UNREACHABLE_CODE!>) {
fun foo() = 1
}<!>
}
fun testObjectExpression1() {
fun bar(i: Int, x: Any) {}
<!UNREACHABLE_CODE!>bar(<!>1, <!UNREACHABLE_CODE!>object : Foo(<!>todo()<!UNREACHABLE_CODE!>) {
fun foo() = 1
})<!>
}
fun testClassDeclaration() {
class C : Foo(todo()) {}
<!UNREACHABLE_CODE!>bar()<!>
}
fun testFunctionDefaultArgument() {
fun foo(x: Int = todo()) { bar() }
}
open class Foo(i: Int) {}
fun todo() = throw Exception()
fun bar() {}
@@ -0,0 +1,21 @@
fun testFor() {
fun Nothing.iterator() = (0..1).iterator()
<!UNREACHABLE_CODE!>for (i in<!> todo()<!UNREACHABLE_CODE!>) {}<!>
}
fun testWhile() {
<!UNREACHABLE_CODE!>while (<!>todo()<!UNREACHABLE_CODE!>) {
}<!>
}
fun testDoWhile() {
do {
} while(todo())
<!UNREACHABLE_CODE!>bar()<!>
}
fun todo() = throw Exception()
fun bar() {}
@@ -0,0 +1,5 @@
fun testReturn() {
<!UNREACHABLE_CODE!>return<!> todo()
}
fun todo() = throw Exception()
@@ -0,0 +1,15 @@
fun testPrefix() {
fun Any.not() {}
<!UNREACHABLE_CODE!>!<!>todo()
}
fun testPostfixWithCall(n: Nothing) {
fun Nothing.inc(): Nothing = this
n<!UNREACHABLE_CODE!>++<!>
}
fun testPostfixSpecial() {
todo()<!UNNECESSARY_NOT_NULL_ASSERTION, UNREACHABLE_CODE!>!!<!>
}
fun todo() = throw Exception()
@@ -3,20 +3,20 @@ fun bar(<!UNUSED_PARAMETER!>a<!>: Any, <!UNUSED_PARAMETER!>b<!>: Any) {}
fun test(arr: Array<Int>) {
while (true) {
<!UNREACHABLE_CODE!>foo<!>(break)
<!UNREACHABLE_CODE!>foo(<!>break<!UNREACHABLE_CODE!>)<!>
}
while (true) {
<!UNREACHABLE_CODE!>bar<!>(arr, break)
<!UNREACHABLE_CODE!>bar(<!>arr, break<!UNREACHABLE_CODE!>)<!>
}
while (true) {
<!UNREACHABLE_CODE!>arr[break]<!>
arr<!UNREACHABLE_CODE!>[<!>break<!UNREACHABLE_CODE!>]<!>
}
while (true) {
<!UNREACHABLE_CODE!>arr[1] = break<!>
arr[1] <!UNREACHABLE_CODE!>=<!> break
}
while (true) {
@@ -32,7 +32,7 @@ fun test(arr: Array<Int>) {
while (true) {
var <!UNUSED_VARIABLE!>x<!> = 1
<!UNREACHABLE_CODE!>x = break<!>
<!UNREACHABLE_CODE!>x =<!> break
}
// TODO: bug, should be fixed in CFA
@@ -50,6 +50,6 @@ fun test(arr: Array<Int>) {
}
while (true) {
<!USELESS_ELVIS!>break<!> ?: <!UNREACHABLE_CODE!>null<!>
<!USELESS_ELVIS!>break<!> <!UNREACHABLE_CODE!>?: null<!>
}
}
@@ -2,7 +2,7 @@
fun foo(x: String): String {
try {
<!UNREACHABLE_CODE!>throw RuntimeException()<!>
<!UNREACHABLE_CODE!>throw<!> RuntimeException()
} finally {
try {
} catch (e: Exception) {
@@ -2,7 +2,7 @@
fun foo() {
try {
<!UNREACHABLE_CODE!>throw RuntimeException()<!>
<!UNREACHABLE_CODE!>throw<!> RuntimeException()
} catch (e: Exception) {
<!UNREACHABLE_CODE!>return<!> // <- Wrong UNREACHABLE_CODE
} finally {
@@ -2,7 +2,7 @@
fun foo(<!UNUSED_PARAMETER!>x<!>: String): String {
try {
<!UNREACHABLE_CODE!>throw RuntimeException()<!> //should be marked as unreachable, but is not
<!UNREACHABLE_CODE!>throw<!> RuntimeException() //should be marked as unreachable, but is not
} finally {
throw NullPointerException()
}
@@ -1,3 +1,4 @@
// !DIAGNOSTICS: -UNREACHABLE_CODE
package kt770_351_735
@@ -120,9 +121,9 @@ fun testStatementInExpressionContext() {
val <!UNUSED_VARIABLE!>a1<!>: Unit = <!ASSIGNMENT_IN_EXPRESSION_CONTEXT!>z = <!UNUSED_VALUE!>334<!><!>
val <!UNUSED_VARIABLE!>f<!> = <!EXPRESSION_EXPECTED!>for (i in 1..10) {}<!>
if (true) return <!ASSIGNMENT_IN_EXPRESSION_CONTEXT!>z = <!UNUSED_VALUE!>34<!><!>
<!UNREACHABLE_CODE!>return <!EXPRESSION_EXPECTED!>while (true) {}<!><!>
return <!EXPRESSION_EXPECTED!>while (true) {}<!>
}
fun testStatementInExpressionContext2() {
<!UNREACHABLE_CODE!>val <!UNUSED_VARIABLE!>a2<!>: Unit = <!EXPRESSION_EXPECTED!>while(true) {}<!><!>
val <!UNUSED_VARIABLE!>a2<!>: Unit = <!EXPRESSION_EXPECTED!>while(true) {}<!>
}
@@ -5,7 +5,7 @@ fun foo() : Int {
doSmth()
}
catch (e: Exception) {
<!UNREACHABLE_CODE!>return <!TYPE_MISMATCH!>""<!><!>
<!UNREACHABLE_CODE!>return<!> <!TYPE_MISMATCH!>""<!>
}
finally {
return <!TYPE_MISMATCH!>""<!>
@@ -1,3 +1,4 @@
// !DIAGNOSTICS: -UNREACHABLE_CODE
//KT-2445 Calling method with function with generic parameter causes compile-time exception
package a
@@ -7,4 +8,4 @@ fun main(args: Array<String>) {
}
}
fun test<R>(callback: (R) -> Unit):Unit = <!UNREACHABLE_CODE!>callback<!>(null!!)
fun test<R>(callback: (R) -> Unit):Unit = callback(null!!)
@@ -1,3 +1,4 @@
// !DIAGNOSTICS: -UNREACHABLE_CODE
//KT-2838 Type inference failed on passing null as a nullable argument
package a
@@ -9,9 +10,9 @@ fun test(a: Int) {
bar(a, <!NULL_FOR_NONNULL_TYPE!>null<!>)
}
fun test1(a: Int) {
<!UNREACHABLE_CODE!>foo<!>(a, throw Exception())
foo(a, throw Exception())
}
fun test2(a: Int) {
<!UNREACHABLE_CODE!>bar<!>(a, throw Exception())
bar(a, throw Exception())
}
@@ -17,7 +17,7 @@ fun lock<T>(lock : Lock, body : () -> T) : T {
//more tests
fun t1() : Int {
try {
<!UNREACHABLE_CODE!>return 1<!>
<!UNREACHABLE_CODE!>return<!> 1
}
finally {
return 2