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
}
}
@@ -22,7 +22,7 @@ fun <T> runOnce(block: () -> T): T {
fun valueReassignment() {
val x: Int
<!UNINITIALIZED_VARIABLE!>x<!>.inc()
runTwice { x = 42 }
runTwice { <!VAL_REASSIGNMENT!>x<!> = 42 }
x.inc()
}
@@ -36,7 +36,7 @@ fun branchingFlow(a: Any?) {
val x: Int
<!UNINITIALIZED_VARIABLE!>x<!>.inc()
if (a is String) {
runTwice { x = 42 }
runTwice { <!VAL_REASSIGNMENT!>x<!> = 42 }
}
else {
x = 43
@@ -47,7 +47,7 @@ fun branchingFlow(a: Any?) {
fun branchingFlowWithMissingBranches(a: Any?) {
val x: Int
if (a is String) {
runTwice { x = 42 }
runTwice { <!VAL_REASSIGNMENT!>x<!> = 42 }
}
<!UNINITIALIZED_VARIABLE!>x<!>.inc()
@@ -58,7 +58,7 @@ fun repeatingFlow(n: Int) {
<!UNINITIALIZED_VARIABLE!>x<!>.inc()
for (i in 1..n) {
runTwice { x = 42 }
runTwice { <!VAL_REASSIGNMENT!>x<!> = 42 }
}
<!UNINITIALIZED_VARIABLE!>x<!>.inc()
@@ -68,7 +68,7 @@ fun repeatingFlow2(n: Int) {
val x: Int
for (i in 1..n) {
runTwice { x = 42 }
runTwice { <!VAL_REASSIGNMENT!>x<!> = 42 }
}
<!UNINITIALIZED_VARIABLE!>x<!>.inc()
@@ -1,28 +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 reassignmentInUsualFlow() {
val x: Int
myRun { x = 42 }
x = 43
x.inc()
}
fun reassignment() {
val x = 42
myRun {
x = 43
}
x.inc()
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
@@ -24,7 +24,7 @@ fun branchingIndetermineFlow(a: Any?) {
if (a is String) {
myRepeat(a.length) {
// Val reassignment because we know that repeat's lambda called in-place
myRun { x = 42 }
myRun { <!VAL_REASSIGNMENT!>x<!> = 42 }
}
}
else {
@@ -45,7 +45,7 @@ fun multipleAssignments() {
val x: Int
myRepeat(42) {
// Val reassignment because we know that repeat's lambda called in-place
myRun { x = 42 }
myRun { <!VAL_REASSIGNMENT!>x<!> = 42 }
}
<!UNINITIALIZED_VARIABLE!>x<!>.inc()
}
@@ -1,18 +0,0 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.contracts.*
fun <T> inPlace(block: () -> T): T {
contract {
callsInPlace(block)
}
return block()
}
fun reassignmentAndNoInitializaiton() {
val x: Int
inPlace { x = 42 }
<!UNINITIALIZED_VARIABLE!>x<!>.inc()
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
@@ -86,7 +86,7 @@ fun testRepeatOnVal(x: Int) {
val y: Int
repeat(x) {
// reassignment instead of captured val initialization
y = 42
<!VAL_REASSIGNMENT!>y<!> = 42
}
println(<!UNINITIALIZED_VARIABLE!>y<!>)
}