Effects: add diagnostic tests on contracts

- Make AbstractDiagnosticsTest dump function contracts
- Add diagnostics tests on parsing contracts
- Add diagnostics tests on smartcats in presence of functions with
contracts
- Add diagnostics tests on initialization and flow in presence of
in-place called lambdas

==========
Introduction of EffectSystem: 16/18
This commit is contained in:
Dmitry Savvinov
2017-10-03 15:02:51 +03:00
parent f487525a1d
commit 4434db4d69
120 changed files with 4179 additions and 1 deletions
@@ -0,0 +1,74 @@
// !LANGUAGE: +CallsInPlaceEffect
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.internal.contracts.*
fun <T> runTwice(block: () -> T): T {
contract {
callsInPlace(block, InvocationKind.AT_LEAST_ONCE)
}
block()
return block();
};
fun <T> runOnce(block: () -> T): T {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return block();
};
fun valueReassignment() {
val x: Int
<!UNINITIALIZED_VARIABLE!>x<!>.inc()
runTwice { <!VAL_REASSIGNMENT!>x<!> = 42 }
x.inc()
}
fun shadowing() {
val x: Int
runTwice { val <!NAME_SHADOWING!>x<!>: Int; x = 42; x.inc() }
<!UNINITIALIZED_VARIABLE!>x<!>.inc()
}
fun branchingFlow(a: Any?) {
val x: Int
<!UNINITIALIZED_VARIABLE!>x<!>.inc()
if (a is String) {
runTwice { <!VAL_REASSIGNMENT!>x<!> = 42 }
}
else {
x = 43
}
x.inc()
}
fun branchingFlowWithMissingBranches(a: Any?) {
val x: Int
if (a is String) {
runTwice { <!VAL_REASSIGNMENT!>x<!> = 42 }
}
<!UNINITIALIZED_VARIABLE!>x<!>.inc()
}
fun repeatingFlow(n: Int) {
val x: Int
<!UNINITIALIZED_VARIABLE!>x<!>.inc()
for (i in 1..n) {
runTwice { <!VAL_REASSIGNMENT!>x<!> = 42 }
}
x.inc()
}
fun repeatingFlow2(n: Int) {
val x: Int
for (i in 1..n) {
runTwice { <!VAL_REASSIGNMENT!>x<!> = 42 }
}
<!UNINITIALIZED_VARIABLE!>x<!>.inc()
}
@@ -0,0 +1,14 @@
package
public fun branchingFlow(/*0*/ a: kotlin.Any?): kotlin.Unit
public fun branchingFlowWithMissingBranches(/*0*/ a: kotlin.Any?): kotlin.Unit
public fun repeatingFlow(/*0*/ n: kotlin.Int): kotlin.Unit
public fun repeatingFlow2(/*0*/ n: kotlin.Int): kotlin.Unit
public fun </*0*/ T> runOnce(/*0*/ block: () -> T): T
CallsInPlace(block, EXACTLY_ONCE)
public fun </*0*/ T> runTwice(/*0*/ block: () -> T): T
CallsInPlace(block, AT_LEAST_ONCE)
public fun shadowing(): kotlin.Unit
public fun valueReassignment(): kotlin.Unit
@@ -0,0 +1,30 @@
// !LANGUAGE: +CallsInPlaceEffect
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.internal.contracts.*
fun <T> runTwice(block: () -> T): T {
contract {
callsInPlace(block, InvocationKind.AT_LEAST_ONCE)
}
block()
return block();
};
fun testInitialization() {
var x: Int
<!UNINITIALIZED_VARIABLE!>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 }
}
<!UNINITIALIZED_VARIABLE!>x<!>.inc()
}
@@ -0,0 +1,7 @@
package
public fun repeatingFlow(/*0*/ n: kotlin.Int): kotlin.Unit
public fun </*0*/ T> runTwice(/*0*/ block: () -> T): T
CallsInPlace(block, AT_LEAST_ONCE)
public fun testInitialization(): kotlin.Unit
@@ -0,0 +1,28 @@
// !LANGUAGE: +CallsInPlaceEffect
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.internal.contracts.*
fun <T> runTwice(block: () -> T): T {
contract {
callsInPlace(block, InvocationKind.AT_LEAST_ONCE)
}
block()
return block();
};
fun <T> funWithUnknownInvocations(block: () -> T) = block()
fun indefiniteFlow() {
var x: Int
funWithUnknownInvocations { runTwice { x = 42 } }
<!UNINITIALIZED_VARIABLE!>x<!>.inc()
}
fun shadowing() {
var x: Int
runTwice { val <!NAME_SHADOWING!>x<!>: Int; x = 42; x.inc() }
<!UNINITIALIZED_VARIABLE!>x<!>.inc()
}
@@ -0,0 +1,8 @@
package
public fun </*0*/ T> funWithUnknownInvocations(/*0*/ block: () -> T): T
public fun indefiniteFlow(): kotlin.Unit
public fun </*0*/ T> runTwice(/*0*/ block: () -> T): T
CallsInPlace(block, AT_LEAST_ONCE)
public fun shadowing(): kotlin.Unit