[FIR] Add checker for uninitialized properties
This commit is contained in:
+2
-2
@@ -44,7 +44,7 @@ fun threeLevelsReturnNoInitialization(x: Int?): Int? {
|
||||
// Possible to report unreachable here
|
||||
y = 54
|
||||
}
|
||||
return y.inc()
|
||||
return <!UNINITIALIZED_VARIABLE!>y<!>.inc()
|
||||
}
|
||||
|
||||
fun threeLevelsReturnWithInitialization(x: Int?): Int? {
|
||||
@@ -82,5 +82,5 @@ fun threeLevelsReturnWithUnknown(x: Int?): Int? {
|
||||
}
|
||||
}
|
||||
}
|
||||
return y.inc()
|
||||
return <!UNINITIALIZED_VARIABLE!>y<!>.inc()
|
||||
}
|
||||
+1
-1
@@ -36,7 +36,7 @@ fun outerFinallyInitializes() {
|
||||
x = outerComputation()
|
||||
} catch (e: java.lang.Exception) {
|
||||
// can catch exception thrown by the inner, so x can be not initialized
|
||||
x.inc()
|
||||
<!UNINITIALIZED_VARIABLE!>x<!>.inc()
|
||||
log()
|
||||
} finally {
|
||||
// Possible reassignment (e.g. if everything finished)
|
||||
|
||||
+1
-1
@@ -37,7 +37,7 @@ fun innerTryCatchInitializes() {
|
||||
// Can get here if innerComputation() threw an exception that wasn't catched by the inner catch (x is not initialized)
|
||||
// OR if outerComputation() threw an exception (x is initialized because we reach outer computation only when inner finished ok)
|
||||
// So, x=I? here
|
||||
x.inc()
|
||||
<!UNINITIALIZED_VARIABLE!>x<!>.inc()
|
||||
|
||||
// Potential reasignment
|
||||
x = 42
|
||||
|
||||
+1
-1
@@ -21,7 +21,7 @@ fun withLabeledReturn() {
|
||||
y = 42
|
||||
}
|
||||
|
||||
println(y)
|
||||
println(<!UNINITIALIZED_VARIABLE!>y<!>)
|
||||
println(x)
|
||||
}
|
||||
|
||||
|
||||
+4
-4
@@ -33,7 +33,7 @@ fun exitOnlyThroughLocalReturns(b: Boolean) {
|
||||
}
|
||||
|
||||
x.inc()
|
||||
s.length
|
||||
<!UNINITIALIZED_VARIABLE!>s<!>.length
|
||||
}
|
||||
|
||||
fun exitOnlyThroughNonLocalReturns(b: Boolean?) {
|
||||
@@ -49,7 +49,7 @@ fun exitOnlyThroughNonLocalReturns(b: Boolean?) {
|
||||
x = 54
|
||||
}
|
||||
|
||||
if (x == 42) {
|
||||
if (<!UNINITIALIZED_VARIABLE!>x<!> == 42) {
|
||||
return
|
||||
}
|
||||
else {
|
||||
@@ -59,8 +59,8 @@ fun exitOnlyThroughNonLocalReturns(b: Boolean?) {
|
||||
}
|
||||
}
|
||||
|
||||
x.inc()
|
||||
s.length
|
||||
<!UNINITIALIZED_VARIABLE!>x<!>.inc()
|
||||
<!UNINITIALIZED_VARIABLE!>s<!>.length
|
||||
}
|
||||
|
||||
fun nonLocalReturnAndOrdinaryExit(b: Boolean) {
|
||||
|
||||
+1
-1
@@ -44,5 +44,5 @@ fun catchThrowIfNotCalled() {
|
||||
} catch (ignored: java.lang.IllegalArgumentException) { }
|
||||
|
||||
// x *isn't* initialized here!
|
||||
println(x)
|
||||
println(<!UNINITIALIZED_VARIABLE!>x<!>)
|
||||
}
|
||||
Vendored
+1
-1
@@ -22,7 +22,7 @@ fun tryCatchInlined() {
|
||||
x.inc()
|
||||
}
|
||||
catch (e: java.lang.Exception) {
|
||||
x.inc()
|
||||
<!UNINITIALIZED_VARIABLE!>x<!>.inc()
|
||||
}
|
||||
}
|
||||
x = 42
|
||||
|
||||
+7
-7
@@ -21,7 +21,7 @@ fun <T> runOnce(block: () -> T): T {
|
||||
|
||||
fun valueReassignment() {
|
||||
val x: Int
|
||||
x.inc()
|
||||
<!UNINITIALIZED_VARIABLE!>x<!>.inc()
|
||||
runTwice { x = 42 }
|
||||
x.inc()
|
||||
}
|
||||
@@ -29,12 +29,12 @@ fun valueReassignment() {
|
||||
fun shadowing() {
|
||||
val x: Int
|
||||
runTwice { val x: Int; x = 42; x.inc() }
|
||||
x.inc()
|
||||
<!UNINITIALIZED_VARIABLE!>x<!>.inc()
|
||||
}
|
||||
|
||||
fun branchingFlow(a: Any?) {
|
||||
val x: Int
|
||||
x.inc()
|
||||
<!UNINITIALIZED_VARIABLE!>x<!>.inc()
|
||||
if (a is String) {
|
||||
runTwice { x = 42 }
|
||||
}
|
||||
@@ -50,18 +50,18 @@ fun branchingFlowWithMissingBranches(a: Any?) {
|
||||
runTwice { x = 42 }
|
||||
}
|
||||
|
||||
x.inc()
|
||||
<!UNINITIALIZED_VARIABLE!>x<!>.inc()
|
||||
}
|
||||
|
||||
fun repeatingFlow(n: Int) {
|
||||
val x: Int
|
||||
x.inc()
|
||||
<!UNINITIALIZED_VARIABLE!>x<!>.inc()
|
||||
|
||||
for (i in 1..n) {
|
||||
runTwice { x = 42 }
|
||||
}
|
||||
|
||||
x.inc()
|
||||
<!UNINITIALIZED_VARIABLE!>x<!>.inc()
|
||||
}
|
||||
|
||||
fun repeatingFlow2(n: Int) {
|
||||
@@ -71,5 +71,5 @@ fun repeatingFlow2(n: Int) {
|
||||
runTwice { x = 42 }
|
||||
}
|
||||
|
||||
x.inc()
|
||||
<!UNINITIALIZED_VARIABLE!>x<!>.inc()
|
||||
}
|
||||
|
||||
-31
@@ -1,31 +0,0 @@
|
||||
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
|
||||
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
|
||||
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
|
||||
|
||||
import kotlin.contracts.*
|
||||
|
||||
fun <T> runTwice(block: () -> T): T {
|
||||
contract {
|
||||
callsInPlace(block, InvocationKind.AT_LEAST_ONCE)
|
||||
}
|
||||
block()
|
||||
return block();
|
||||
};
|
||||
|
||||
fun testInitialization() {
|
||||
var x: Int
|
||||
x.inc()
|
||||
runTwice { x = 42 }
|
||||
x.inc()
|
||||
x = 43
|
||||
x.inc()
|
||||
}
|
||||
|
||||
fun repeatingFlow(n: Int) {
|
||||
var x: Int
|
||||
for (i in 1..n) {
|
||||
runTwice { x = 42 }
|
||||
}
|
||||
x.inc()
|
||||
}
|
||||
|
||||
+1
@@ -1,3 +1,4 @@
|
||||
// FIR_IDENTICAL
|
||||
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
|
||||
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
|
||||
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
|
||||
|
||||
+2
-2
@@ -19,11 +19,11 @@ fun indefiniteFlow() {
|
||||
|
||||
funWithUnknownInvocations { runTwice { x = 42 } }
|
||||
|
||||
x.inc()
|
||||
<!UNINITIALIZED_VARIABLE!>x<!>.inc()
|
||||
}
|
||||
|
||||
fun shadowing() {
|
||||
var x: Int
|
||||
runTwice { val x: Int; x = 42; x.inc() }
|
||||
x.inc()
|
||||
<!UNINITIALIZED_VARIABLE!>x<!>.inc()
|
||||
}
|
||||
+4
-4
@@ -31,14 +31,14 @@ fun branchingIndetermineFlow(a: Any?) {
|
||||
myRun { x = 43 }
|
||||
}
|
||||
|
||||
x.inc()
|
||||
<!UNINITIALIZED_VARIABLE!>x<!>.inc()
|
||||
}
|
||||
|
||||
fun nonAnonymousLambdas() {
|
||||
val x: Int
|
||||
val initializer = { x = 42 }
|
||||
myRun(initializer)
|
||||
x.inc()
|
||||
<!UNINITIALIZED_VARIABLE!>x<!>.inc()
|
||||
}
|
||||
|
||||
fun multipleAssignments() {
|
||||
@@ -47,7 +47,7 @@ fun multipleAssignments() {
|
||||
// Val reassignment because we know that repeat's lambda called in-place
|
||||
myRun { x = 42 }
|
||||
}
|
||||
x.inc()
|
||||
<!UNINITIALIZED_VARIABLE!>x<!>.inc()
|
||||
}
|
||||
|
||||
fun funWithUnknownInvocations(block: () -> Unit) = block()
|
||||
@@ -56,7 +56,7 @@ fun nestedIndefiniteAssignment() {
|
||||
val x: Int
|
||||
// Captured val initialization reported, because we don't know anything about funWithUnknownInvocations
|
||||
funWithUnknownInvocations { myRun { x = 42 } }
|
||||
x.inc()
|
||||
<!UNINITIALIZED_VARIABLE!>x<!>.inc()
|
||||
}
|
||||
|
||||
class InitializationForbiddenInNonInitSection {
|
||||
|
||||
+4
-4
@@ -16,7 +16,7 @@ fun indefiniteVarReassignment(n: Int) {
|
||||
repeat(n) {
|
||||
myRun { x = 42 }
|
||||
}
|
||||
x.inc()
|
||||
<!UNINITIALIZED_VARIABLE!>x<!>.inc()
|
||||
}
|
||||
|
||||
fun nonAnonymousLambdas() {
|
||||
@@ -24,7 +24,7 @@ fun nonAnonymousLambdas() {
|
||||
var x: Int
|
||||
val initializer = { x = 42 }
|
||||
myRun(initializer)
|
||||
x.inc()
|
||||
<!UNINITIALIZED_VARIABLE!>x<!>.inc()
|
||||
}
|
||||
|
||||
fun branchingIndetermineFlow(a: Any) {
|
||||
@@ -39,7 +39,7 @@ fun branchingIndetermineFlow(a: Any) {
|
||||
myRun { x = 43 }
|
||||
}
|
||||
|
||||
x.inc()
|
||||
<!UNINITIALIZED_VARIABLE!>x<!>.inc()
|
||||
}
|
||||
|
||||
fun funWithUnknownInvocations(block: () -> Unit) = block()
|
||||
@@ -47,5 +47,5 @@ fun funWithUnknownInvocations(block: () -> Unit) = block()
|
||||
fun nestedIndefiniteAssignment() {
|
||||
val x: Int
|
||||
funWithUnknownInvocations { myRun { x = 42 } }
|
||||
x.inc()
|
||||
<!UNINITIALIZED_VARIABLE!>x<!>.inc()
|
||||
}
|
||||
-28
@@ -1,28 +0,0 @@
|
||||
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
|
||||
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
|
||||
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
|
||||
|
||||
import kotlin.contracts.*
|
||||
|
||||
fun <T, R> T.myLet(block: (T) -> R): R {
|
||||
contract {
|
||||
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
return block(this)
|
||||
}
|
||||
|
||||
fun initializationWithReceiver(y: String) {
|
||||
val x: Int
|
||||
y.myLet { x = 42 }
|
||||
x.inc()
|
||||
}
|
||||
|
||||
fun initializationWithSafeCall(y: String?) {
|
||||
val x: Int
|
||||
y?.myLet { x = 42 }
|
||||
x.inc()
|
||||
}
|
||||
|
||||
fun sanityCheck(x: Int, y: String): Int {
|
||||
y.let { return x }
|
||||
}
|
||||
+1
@@ -1,3 +1,4 @@
|
||||
// FIR_IDENTICAL
|
||||
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
|
||||
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
|
||||
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
|
||||
|
||||
+1
-1
@@ -14,5 +14,5 @@ fun <T> inPlace(block: () -> T): T {
|
||||
fun reassignmentAndNoInitializaiton() {
|
||||
val x: Int
|
||||
inPlace { x = 42 }
|
||||
x.inc()
|
||||
<!UNINITIALIZED_VARIABLE!>x<!>.inc()
|
||||
}
|
||||
+2
-2
@@ -88,7 +88,7 @@ fun testRepeatOnVal(x: Int) {
|
||||
// reassignment instead of captured val initialization
|
||||
y = 42
|
||||
}
|
||||
println(y)
|
||||
println(<!UNINITIALIZED_VARIABLE!>y<!>)
|
||||
}
|
||||
|
||||
fun testRepeatOnVar(x: Int) {
|
||||
@@ -98,7 +98,7 @@ fun testRepeatOnVar(x: Int) {
|
||||
y = 42
|
||||
}
|
||||
// but here we still unsure if 'y' was initialized
|
||||
println(y)
|
||||
println(<!UNINITIALIZED_VARIABLE!>y<!>)
|
||||
}
|
||||
|
||||
fun testRepeatOnInitializedVar(x: Int) {
|
||||
|
||||
+6
-6
@@ -34,14 +34,14 @@ fun inPresenceOfLazy(x: Any?, unknownBoolean: Boolean) {
|
||||
val y: Int
|
||||
|
||||
if (unknownBoolean && callsAndInverts(x !is String) { y = 42 }) {
|
||||
println(y)
|
||||
println(<!UNINITIALIZED_VARIABLE!>y<!>)
|
||||
x.length
|
||||
}
|
||||
else {
|
||||
println(y)
|
||||
println(<!UNINITIALIZED_VARIABLE!>y<!>)
|
||||
x.<!UNRESOLVED_REFERENCE!>length<!>
|
||||
}
|
||||
println(y)
|
||||
println(<!UNINITIALIZED_VARIABLE!>y<!>)
|
||||
}
|
||||
|
||||
fun isPresenceOfLazy2(x: Any?, unknownBoolean: Boolean) {
|
||||
@@ -50,10 +50,10 @@ fun isPresenceOfLazy2(x: Any?, unknownBoolean: Boolean) {
|
||||
x.length
|
||||
}
|
||||
else {
|
||||
println(y)
|
||||
println(<!UNINITIALIZED_VARIABLE!>y<!>)
|
||||
x.<!UNRESOLVED_REFERENCE!>length<!>
|
||||
}
|
||||
println(y)
|
||||
println(<!UNINITIALIZED_VARIABLE!>y<!>)
|
||||
}
|
||||
|
||||
fun isPresenceOfLazy3(x: Any?, unknownBoolean: Boolean) {
|
||||
@@ -64,5 +64,5 @@ fun isPresenceOfLazy3(x: Any?, unknownBoolean: Boolean) {
|
||||
else {
|
||||
x.<!UNRESOLVED_REFERENCE!>length<!>
|
||||
}
|
||||
println(y)
|
||||
println(<!UNINITIALIZED_VARIABLE!>y<!>)
|
||||
}
|
||||
Reference in New Issue
Block a user