[FIR] Add checker for uninitialized properties

This commit is contained in:
Dmitriy Novozhilov
2020-06-17 11:51:15 +03:00
parent 25621d699b
commit 26458875d5
96 changed files with 1476 additions and 1358 deletions
@@ -9,13 +9,13 @@ fun doSmth(i: Int) {}
fun t1(b : Boolean) {
val v : Int
if (v == 0) {}
if (<!UNINITIALIZED_VARIABLE!>v<!> == 0) {}
var u: String
if (b) {
u = "s"
}
doSmth(u)
doSmth(<!UNINITIALIZED_VARIABLE!>u<!>)
var r: String
if (b) {
@@ -28,10 +28,10 @@ fun t1(b : Boolean) {
var t: String
if (b)
doSmth(t)
doSmth(<!UNINITIALIZED_VARIABLE!>t<!>)
else
t = "ss"
doSmth(t) //repeat for t
doSmth(<!UNINITIALIZED_VARIABLE!>t<!>) //repeat for t
val i = 3
doSmth(i)
@@ -207,7 +207,7 @@ class LocalValsVsProperties(val a: Int, w: Int) : Open(a, w) {
val r : Int
doSmth(x)
doSmth(y)
doSmth(r)
doSmth(<!UNINITIALIZED_VARIABLE!>r<!>)
doSmth(a)
}
var xx = w
@@ -257,7 +257,7 @@ class ClassObject() {
fun foo() {
val a : Int
doSmth(a)
doSmth(<!UNINITIALIZED_VARIABLE!>a<!>)
}
}
}
@@ -317,7 +317,7 @@ object TestObjectDeclaration {
if (1 < 3) {
i = 10
}
doSmth(i)
doSmth(<!UNINITIALIZED_VARIABLE!>i<!>)
}
}
@@ -12,7 +12,7 @@ val a: Int
return d
}
return b
return <!UNINITIALIZED_VARIABLE!>b<!>
}
class A {
@@ -21,7 +21,7 @@ class A {
val b: Int
val c: Int
42
return b
return <!UNINITIALIZED_VARIABLE!>b<!>
}
fun foo() {
@@ -31,7 +31,7 @@ class A {
val b: Int
val c: Int
42
return b
return <!UNINITIALIZED_VARIABLE!>b<!>
}
}
}
@@ -11,7 +11,7 @@ fun foo() {
x = 42
}
// Error!
x.hashCode()
<!UNINITIALIZED_VARIABLE!>x<!>.hashCode()
}
fun bar() {
@@ -8,7 +8,7 @@ fun foo() {
}
}
// Error! See KT-10042
x.length
<!UNINITIALIZED_VARIABLE!>x<!>.length
}
fun bar() {
@@ -19,7 +19,7 @@ fun bar() {
}
}
// Ok
x.length
<!UNINITIALIZED_VARIABLE!>x<!>.length
}
fun gav() {
@@ -31,7 +31,7 @@ fun gav() {
}
}
// Error! See KT-10042
x.length
<!UNINITIALIZED_VARIABLE!>x<!>.length
val y: String
class C(val s: String) {
constructor(): this("") {
@@ -39,7 +39,7 @@ fun gav() {
y = s
}
}
y.length
<!UNINITIALIZED_VARIABLE!>y<!>.length
}
open class Gau(val s: String)
@@ -53,7 +53,7 @@ fun gau() {
}
}
// Ok
x.length
<!UNINITIALIZED_VARIABLE!>x<!>.length
val y: String
fun local() {
object: Any() {
@@ -1,8 +0,0 @@
fun foo() {
var x: Int
fun bar() {
x = 42
}
x.hashCode()
bar()
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
fun foo() {
var x: Int
fun bar() {
@@ -1,13 +0,0 @@
//KT-2369 Variable is not marked as uninitialized in 'finally' section
fun main() {
var x : Int
try {
throw Exception()
}
finally {
doSmth(x + 1)
}
}
fun doSmth(a: Any?) = a
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
//KT-2369 Variable is not marked as uninitialized in 'finally' section
fun main() {
@@ -12,7 +12,7 @@ private fun doTest() : Int {
return 0 ;
}
finally {
if(list != null) { // Must be an ERROR
if(<!UNINITIALIZED_VARIABLE!>list<!> != null) { // Must be an ERROR
}
}
}
@@ -15,7 +15,7 @@ val o = object {
p.x = 4
val z : Int
doSmth(z)
doSmth(<!UNINITIALIZED_VARIABLE!>z<!>)
}
}
@@ -28,7 +28,7 @@ class A {
val a : Int = 1
get() {
val x : Int
doSmth(x)
doSmth(<!UNINITIALIZED_VARIABLE!>x<!>)
return field
}
}
@@ -15,7 +15,7 @@ fun f1(flag: Boolean) {
// KT-13612: reassignment
n = 3
}
n.hashCode()
<!UNINITIALIZED_VARIABLE!>n<!>.hashCode()
}
fun f2(flag: Boolean) {
@@ -32,7 +32,7 @@ fun f2(flag: Boolean) {
// KT-13612: reassignment
n = 3
}
n.hashCode()
<!UNINITIALIZED_VARIABLE!>n<!>.hashCode()
}
}
@@ -50,6 +50,6 @@ fun f3(flag: Boolean) {
// KT-13612: reassignment
n = 3
}
n.hashCode()
<!UNINITIALIZED_VARIABLE!>n<!>.hashCode()
}
}
@@ -10,6 +10,6 @@ class C {
fun foo() {
val a : Int
a + 1
a + 1
<!UNINITIALIZED_VARIABLE!>a<!> + 1
<!UNINITIALIZED_VARIABLE!>a<!> + 1
}
@@ -23,14 +23,14 @@ fun test2() {
fun test3() {
val f = {
val a : Int
doSmth(a)
doSmth(<!UNINITIALIZED_VARIABLE!>a<!>)
}
}
fun test4() {
doSmth {
val a : Int
doSmth(a)
doSmth(<!UNINITIALIZED_VARIABLE!>a<!>)
}
}
@@ -1,6 +0,0 @@
fun bar(f: () -> Unit) = f()
fun foo() {
var v: Any
bar { v.hashCode() }
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
fun bar(f: () -> Unit) = f()
fun foo() {
+1 -1
View File
@@ -1,7 +1,7 @@
//FILE: foo.kt
fun main() {
val c: Type
when (c) {
when (<!UNINITIALIZED_VARIABLE!>c<!>) {
}
}
@@ -0,0 +1,24 @@
// !LANGUAGE: +LateinitLocalVariables
fun test1() {
lateinit var s: String
<!UNINITIALIZED_VARIABLE!>s<!>.length
}
fun test2() {
lateinit var s: String
run {
s = ""
}
s.length
}
fun almostAlwaysTrue(): Boolean = true
fun test3() {
lateinit var s: String
if (almostAlwaysTrue()) {
s = ""
}
<!UNINITIALIZED_VARIABLE!>s<!>.length
}
@@ -1,4 +1,3 @@
// FIR_IDENTICAL
// !LANGUAGE: +LateinitLocalVariables
fun test1() {
@@ -5,5 +5,5 @@ fun foo(k: Int): Int {
for (j in 1..k) {
i = j
}
return i
return <!UNINITIALIZED_VARIABLE!>i<!>
}
@@ -4,7 +4,7 @@ package kt455
fun foo() {
val a: Int
doSmth(a) //error
doSmth(a) //no repeat error
doSmth(<!UNINITIALIZED_VARIABLE!>a<!>) //error
doSmth(<!UNINITIALIZED_VARIABLE!>a<!>) //no repeat error
}
fun doSmth(i: Int) {}
@@ -4,7 +4,7 @@ public fun foo(x: String?): Int {
y = if (x == null) break else x
}
// In future we can infer this initialization
y.hashCode()
<!UNINITIALIZED_VARIABLE!>y<!>.hashCode()
// x is null because of the break
return x.<!INAPPLICABLE_CANDIDATE!>length<!>
}
@@ -18,5 +18,5 @@ fun bar(a: Boolean, b: Boolean): Int {
when (b) {
false -> x = 3
}
return x
return <!UNINITIALIZED_VARIABLE!>x<!>
}
@@ -18,5 +18,5 @@ fun bar(a: Boolean, b: Boolean): Int {
when (b) {
false -> x = 3
}
return x
return <!UNINITIALIZED_VARIABLE!>x<!>
}
@@ -12,7 +12,7 @@ fun testSimpleValInWhenSubject() {
fun testValWithoutInitializerWhenSubject() {
when (val y: Any) {
is String -> y.<!UNRESOLVED_REFERENCE!>length<!>
is String -> <!UNINITIALIZED_VARIABLE!>y<!>.<!UNRESOLVED_REFERENCE!>length<!>
}
}
@@ -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()
}
@@ -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)
@@ -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
@@ -21,7 +21,7 @@ fun withLabeledReturn() {
y = 42
}
println(y)
println(<!UNINITIALIZED_VARIABLE!>y<!>)
println(x)
}
@@ -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) {
@@ -44,5 +44,5 @@ fun catchThrowIfNotCalled() {
} catch (ignored: java.lang.IllegalArgumentException) { }
// x *isn't* initialized here!
println(x)
println(<!UNINITIALIZED_VARIABLE!>x<!>)
}
@@ -22,7 +22,7 @@ fun tryCatchInlined() {
x.inc()
}
catch (e: java.lang.Exception) {
x.inc()
<!UNINITIALIZED_VARIABLE!>x<!>.inc()
}
}
x = 42
@@ -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()
}
@@ -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,3 +1,4 @@
// FIR_IDENTICAL
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
@@ -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()
}
@@ -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 {
@@ -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()
}
@@ -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,3 +1,4 @@
// FIR_IDENTICAL
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
@@ -14,5 +14,5 @@ fun <T> inPlace(block: () -> T): T {
fun reassignmentAndNoInitializaiton() {
val x: Int
inPlace { x = 42 }
x.inc()
<!UNINITIALIZED_VARIABLE!>x<!>.inc()
}
@@ -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) {
@@ -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<!>)
}