FIR checker: report val reassignment

This commit is contained in:
Jinseong Jeon
2021-02-19 00:51:16 -08:00
committed by Dmitriy Novozhilov
parent b128577508
commit f1fa290d49
66 changed files with 386 additions and 309 deletions
@@ -33,12 +33,12 @@ fun test() {
// Loop executed exectly once, initializing x
myRun { x.inc() }
myRun { x = 42 }
myRun { <!VAL_REASSIGNMENT!>x<!> = 42 }
break
}
// x is I?D here because loop could've been execited
// x is ID? here because loop could've been execited
// VAL_REASSIGNMENT isn't reported because of repeating diagnostic
x = 42
<!VAL_REASSIGNMENT!>x<!> = 42
// x is ID now
}
else
@@ -36,7 +36,7 @@ fun test() {
myRun { <!VAL_REASSIGNMENT!>x<!> = 42 }
break
}
// x is I?D here because loop could've been execited
// x is ID? here because loop could've been execited
// VAL_REASSIGNMENT isn't reported because of repeating diagnostic
x = 42
// x is ID now
@@ -42,7 +42,7 @@ fun threeLevelsReturnNoInitialization(x: Int?): Int? {
}
}
// Possible to report unreachable here
y = 54
<!VAL_REASSIGNMENT!>y<!> = 54
}
return <!UNINITIALIZED_VARIABLE!>y<!>.inc()
}
@@ -27,13 +27,13 @@ fun outerFinallyInitializes() {
log()
}
// possible reassignment if innerComputation finished
x = 42
<!VAL_REASSIGNMENT!>x<!> = 42
// x is ID here
}
// Definite reassignment here, cause can get here only if myRun finished
// Not reported because of repeating diagnostic
x = outerComputation()
<!VAL_REASSIGNMENT!>x<!> = outerComputation()
} catch (e: java.lang.Exception) {
// can catch exception thrown by the inner, so x can be not initialized
<!UNINITIALIZED_VARIABLE!>x<!>.inc()
@@ -41,7 +41,7 @@ fun outerFinallyInitializes() {
} finally {
// Possible reassignment (e.g. if everything finished)
// Not reported because of repeating diagnostic
x = 42
<!VAL_REASSIGNMENT!>x<!> = 42
}
// Properly initialized
@@ -25,7 +25,7 @@ fun innerTryCatchInitializes() {
}
catch (e: java.lang.Exception) {
// Potential reassignment because x.inc() could threw
x = 42
<!VAL_REASSIGNMENT!>x<!> = 42
x.inc()
}
}
@@ -40,7 +40,7 @@ fun innerTryCatchInitializes() {
<!UNINITIALIZED_VARIABLE!>x<!>.inc()
// Potential reasignment
x = 42
<!VAL_REASSIGNMENT!>x<!> = 42
}
// Here x=I because outer try-catch either exited normally (x=I) or catched exception (x=I, with reassingment, though)
x.inc()
@@ -1,58 +0,0 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.contracts.*
fun <T> myRun(block: () -> T): T {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return block()
}
fun someComputation(): Int = 42
fun tryCatchInlined() {
val x: Int
myRun {
try {
x = someComputation()
x.inc()
}
catch (e: java.lang.Exception) {
<!UNINITIALIZED_VARIABLE!>x<!>.inc()
}
}
x = 42
x.inc()
}
fun possibleReassignmentInTryCatch() {
val x: Int
myRun {
try {
x = someComputation()
x.inc()
}
catch (e: java.lang.Exception) {
x = 42
x.inc()
}
x.inc()
}
x.inc()
}
fun tryCatchOuter() {
val x: Int
try {
myRun { x = someComputation() }
x.inc()
}
catch (e: java.lang.Exception) {
<!UNINITIALIZED_VARIABLE!>x<!>.inc()
}
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
@@ -23,10 +23,10 @@ fun innerTryCatchFinally() {
x = someComputation()
report(x)
} catch (e: java.lang.Exception) {
x = 42
<!VAL_REASSIGNMENT!>x<!> = 42
report(x)
} finally {
x = 0
<!VAL_REASSIGNMENT!>x<!> = 0
}
}