[FIR] Add CallsInPlace contract analyzer

This commit is contained in:
Oleg Ivanov
2020-07-30 17:13:11 +03:00
parent cc9c5b9e3c
commit 4367d6631f
39 changed files with 4021 additions and 2064 deletions
@@ -0,0 +1,119 @@
digraph inAnonymousObject_kt {
graph [nodesep=3]
node [shape=box penwidth=2]
edge [penwidth=2]
subgraph cluster_0 {
color=red
0 [label="Enter function foo" style="filled" fillcolor=red];
subgraph cluster_1 {
color=blue
1 [label="Enter block"];
2 [label="Exit anonymous object"];
3 [label="Variable declaration: lval obj: R|<anonymous>|"];
4 [label="Access variable R|<local>/obj|"];
5 [label="Function call: R|<local>/obj|.R|/<anonymous>.run|()"];
6 [label="Function call: R|<local>/d|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()"];
7 [label="Exit block"];
}
8 [label="Exit function foo" style="filled" fillcolor=red];
}
subgraph cluster_2 {
color=blue
9 [label="Enter class <anonymous object>" style="filled" fillcolor=red];
subgraph cluster_3 {
color=blue
10 [label="Part of class initialization"];
subgraph cluster_4 {
color=blue
11 [label="Part of class initialization"];
12 [label="Exit class <anonymous object>" style="filled" fillcolor=red];
}
0 -> {1};
1 -> {2};
1 -> {13 18 16 21 23 29} [color=red];
2 -> {3};
2 -> {13 29 9} [color=green];
2 -> {13 29 9} [style=dashed];
3 -> {4};
4 -> {5};
5 -> {6};
6 -> {7};
7 -> {8};
9 -> {10} [color=green];
10 -> {11} [style=dotted];
10 -> {18} [color=green];
10 -> {18} [style=dashed];
11 -> {12} [style=dotted];
11 -> {23} [color=green];
11 -> {23} [style=dashed];
subgraph cluster_5 {
color=red
13 [label="Enter function <init>" style="filled" fillcolor=red];
14 [label="Delegated constructor call: super<R|kotlin/Any|>()"];
15 [label="Exit function <init>" style="filled" fillcolor=red];
}
13 -> {14};
14 -> {15};
subgraph cluster_6 {
color=red
16 [label="Enter function getter" style="filled" fillcolor=red];
17 [label="Exit function getter" style="filled" fillcolor=red];
}
16 -> {17};
subgraph cluster_7 {
color=red
18 [label="Enter property" style="filled" fillcolor=red];
19 [label="Access variable R|<local>/a|"];
20 [label="Exit property" style="filled" fillcolor=red];
}
18 -> {19};
19 -> {20};
20 -> {11} [color=green];
subgraph cluster_8 {
color=red
21 [label="Enter function getter" style="filled" fillcolor=red];
22 [label="Exit function getter" style="filled" fillcolor=red];
}
21 -> {22};
subgraph cluster_9 {
color=red
23 [label="Enter init block" style="filled" fillcolor=red];
subgraph cluster_10 {
color=blue
24 [label="Enter block"];
25 [label="Access variable R|<local>/b|"];
26 [label="Assignment: R|/<anonymous>.leaked|"];
27 [label="Exit block"];
}
28 [label="Exit init block" style="filled" fillcolor=red];
}
23 -> {24};
24 -> {25};
25 -> {26};
26 -> {27};
27 -> {28};
28 -> {12} [color=green];
subgraph cluster_11 {
color=red
29 [label="Enter function run" style="filled" fillcolor=red];
subgraph cluster_12 {
color=blue
30 [label="Enter block"];
31 [label="Function call: R|<local>/c|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()"];
32 [label="Exit block"];
}
33 [label="Exit function run" style="filled" fillcolor=red];
}
29 -> {30};
30 -> {31};
31 -> {32};
32 -> {33};
}
@@ -0,0 +1,31 @@
// !DUMP_CFG
import kotlin.contracts.*
@ExperimentalContracts
fun foo(a: () -> Unit, b: () -> Unit, c: () -> Unit, d: () -> Unit) {
<!LEAKED_IN_PLACE_LAMBDA, LEAKED_IN_PLACE_LAMBDA, LEAKED_IN_PLACE_LAMBDA!>contract {
callsInPlace(a, InvocationKind.AT_MOST_ONCE)
callsInPlace(b, InvocationKind.AT_MOST_ONCE)
callsInPlace(c, InvocationKind.AT_MOST_ONCE)
callsInPlace(d, InvocationKind.AT_MOST_ONCE)
}<!>
val obj = object : Runnable {
val leakedVal = <!LEAKED_IN_PLACE_LAMBDA!>a<!>
val leaked: Any
init {
leaked = <!LEAKED_IN_PLACE_LAMBDA!>b<!>
}
override fun run() {
<!LEAKED_IN_PLACE_LAMBDA!>c()<!>
}
}
obj.run()
d()
}
@@ -0,0 +1,35 @@
FILE: inAnonymousObject.kt
@R|kotlin/contracts/ExperimentalContracts|() public final fun foo(a: R|() -> kotlin/Unit|, b: R|() -> kotlin/Unit|, c: R|() -> kotlin/Unit|, d: R|() -> kotlin/Unit|): R|kotlin/Unit|
[R|Contract description]
<
CallsInPlace(a, AT_MOST_ONCE)
CallsInPlace(b, AT_MOST_ONCE)
CallsInPlace(c, AT_MOST_ONCE)
CallsInPlace(d, AT_MOST_ONCE)
>
{
[StubStatement]
lval obj: R|<anonymous>| = object : R|java/lang/Runnable| {
private constructor(): R|<anonymous>| {
super<R|kotlin/Any|>()
}
public final val leakedVal: R|() -> kotlin/Unit| = R|<local>/a|
public get(): R|() -> kotlin/Unit|
public final val leaked: R|kotlin/Any|
public get(): R|kotlin/Any|
init {
this@R|/<anonymous>|.R|/<anonymous>.leaked| = R|<local>/b|
}
public final override fun run(): R|kotlin/Unit| {
R|<local>/c|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()
}
}
R|<local>/obj|.R|/<anonymous>.run|()
R|<local>/d|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()
}
@@ -0,0 +1,126 @@
digraph inLocalClass_kt {
graph [nodesep=3]
node [shape=box penwidth=2]
edge [penwidth=2]
subgraph cluster_0 {
color=red
0 [label="Enter function foo" style="filled" fillcolor=red];
subgraph cluster_1 {
color=blue
1 [label="Enter block"];
2 [label="Exit local class foo"];
3 [label="Function call: R|/LocalClass.LocalClass|()"];
4 [label="Function call: R|/LocalClass.LocalClass|().R|/LocalClass.run|()"];
5 [label="Function call: R|<local>/e|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()"];
6 [label="Exit block"];
}
7 [label="Exit function foo" style="filled" fillcolor=red];
}
subgraph cluster_2 {
color=blue
8 [label="Enter class LocalClass" style="filled" fillcolor=red];
subgraph cluster_3 {
color=blue
9 [label="Part of class initialization"];
subgraph cluster_4 {
color=blue
10 [label="Part of class initialization"];
11 [label="Exit class LocalClass" style="filled" fillcolor=red];
}
0 -> {1};
1 -> {2};
1 -> {14 12 17 19 25 31} [color=red];
2 -> {3};
2 -> {19 31 8} [color=green];
2 -> {19 31 8} [style=dashed];
3 -> {4};
4 -> {5};
5 -> {6};
6 -> {7};
8 -> {9} [color=green];
9 -> {10} [style=dotted];
9 -> {14} [color=green];
9 -> {14} [style=dashed];
10 -> {11} [style=dotted];
10 -> {25} [color=green];
10 -> {25} [style=dashed];
subgraph cluster_5 {
color=red
12 [label="Enter function getter" style="filled" fillcolor=red];
13 [label="Exit function getter" style="filled" fillcolor=red];
}
12 -> {13};
subgraph cluster_6 {
color=red
14 [label="Enter property" style="filled" fillcolor=red];
15 [label="Access variable R|<local>/a|"];
16 [label="Exit property" style="filled" fillcolor=red];
}
14 -> {15};
15 -> {16};
16 -> {10} [color=green];
subgraph cluster_7 {
color=red
17 [label="Enter function getter" style="filled" fillcolor=red];
18 [label="Exit function getter" style="filled" fillcolor=red];
}
17 -> {18};
subgraph cluster_8 {
color=red
19 [label="Enter function <init>" style="filled" fillcolor=red];
20 [label="Delegated constructor call: super<R|kotlin/Any|>()"];
subgraph cluster_9 {
color=blue
21 [label="Enter block"];
22 [label="Function call: R|<local>/b|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()"];
23 [label="Exit block"];
}
24 [label="Exit function <init>" style="filled" fillcolor=red];
}
19 -> {20};
20 -> {21};
21 -> {22};
22 -> {23};
23 -> {24};
subgraph cluster_10 {
color=red
25 [label="Enter init block" style="filled" fillcolor=red];
subgraph cluster_11 {
color=blue
26 [label="Enter block"];
27 [label="Access variable R|<local>/c|"];
28 [label="Assignment: R|/LocalClass.leaked|"];
29 [label="Exit block"];
}
30 [label="Exit init block" style="filled" fillcolor=red];
}
25 -> {26};
26 -> {27};
27 -> {28};
28 -> {29};
29 -> {30};
30 -> {11} [color=green];
subgraph cluster_12 {
color=red
31 [label="Enter function run" style="filled" fillcolor=red];
subgraph cluster_13 {
color=blue
32 [label="Enter block"];
33 [label="Function call: R|<local>/d|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()"];
34 [label="Exit block"];
}
35 [label="Exit function run" style="filled" fillcolor=red];
}
31 -> {32};
32 -> {33};
33 -> {34};
34 -> {35};
}
@@ -0,0 +1,35 @@
// !DUMP_CFG
import kotlin.contracts.*
@ExperimentalContracts
fun foo(a: () -> Unit, b: () -> Unit, c: () -> Unit, d: () -> Unit, e: () -> Unit) {
<!LEAKED_IN_PLACE_LAMBDA, LEAKED_IN_PLACE_LAMBDA, LEAKED_IN_PLACE_LAMBDA, LEAKED_IN_PLACE_LAMBDA!>contract {
callsInPlace(a, InvocationKind.AT_MOST_ONCE)
callsInPlace(b, InvocationKind.AT_MOST_ONCE)
callsInPlace(c, InvocationKind.AT_MOST_ONCE)
callsInPlace(d, InvocationKind.AT_MOST_ONCE)
callsInPlace(e, InvocationKind.AT_MOST_ONCE)
}<!>
class LocalClass {
val leakedVal = <!LEAKED_IN_PLACE_LAMBDA!>a<!>
val leaked: Any
constructor() {
<!LEAKED_IN_PLACE_LAMBDA!>b()<!>
}
init {
leaked = <!LEAKED_IN_PLACE_LAMBDA!>c<!>
}
fun run() {
<!LEAKED_IN_PLACE_LAMBDA!>d()<!>
}
}
LocalClass().run()
e()
}
@@ -0,0 +1,37 @@
FILE: inLocalClass.kt
@R|kotlin/contracts/ExperimentalContracts|() public final fun foo(a: R|() -> kotlin/Unit|, b: R|() -> kotlin/Unit|, c: R|() -> kotlin/Unit|, d: R|() -> kotlin/Unit|, e: R|() -> kotlin/Unit|): R|kotlin/Unit|
[R|Contract description]
<
CallsInPlace(a, AT_MOST_ONCE)
CallsInPlace(b, AT_MOST_ONCE)
CallsInPlace(c, AT_MOST_ONCE)
CallsInPlace(d, AT_MOST_ONCE)
CallsInPlace(e, AT_MOST_ONCE)
>
{
[StubStatement]
local final class LocalClass : R|kotlin/Any| {
public final val leakedVal: R|() -> kotlin/Unit| = R|<local>/a|
public get(): R|() -> kotlin/Unit|
public final val leaked: R|kotlin/Any|
public get(): R|kotlin/Any|
public constructor(): R|LocalClass| {
super<R|kotlin/Any|>()
R|<local>/b|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()
}
init {
this@R|/LocalClass|.R|/LocalClass.leaked| = R|<local>/c|
}
public final fun run(): R|kotlin/Unit| {
R|<local>/d|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()
}
}
R|/LocalClass.LocalClass|().R|/LocalClass.run|()
R|<local>/e|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()
}
@@ -0,0 +1,47 @@
digraph inLocalFunction_kt {
graph [nodesep=3]
node [shape=box penwidth=2]
edge [penwidth=2]
subgraph cluster_0 {
color=red
0 [label="Enter function foo" style="filled" fillcolor=red];
subgraph cluster_1 {
color=blue
1 [label="Enter block"];
2 [label="Local function declaration foo"];
3 [label="Function call: R|<local>/localFun|()"];
4 [label="Function call: R|<local>/b|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()"];
5 [label="Exit block"];
}
6 [label="Exit function foo" style="filled" fillcolor=red];
}
subgraph cluster_2 {
color=blue
7 [label="Enter function localFun" style="filled" fillcolor=red];
subgraph cluster_3 {
color=blue
8 [label="Enter block"];
9 [label="Access variable R|<local>/a|"];
10 [label="Function call: R|<local>/a|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()"];
11 [label="Function call: R|<local>/a|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()"];
12 [label="Exit block"];
}
13 [label="Exit function localFun" style="filled" fillcolor=red];
}
0 -> {1};
1 -> {2};
2 -> {3};
2 -> {7} [color=red];
2 -> {7} [style=dashed];
3 -> {4};
4 -> {5};
5 -> {6};
7 -> {8};
8 -> {9};
9 -> {10};
10 -> {11};
11 -> {12};
12 -> {13};
}
@@ -0,0 +1,18 @@
// !DUMP_CFG
import kotlin.contracts.*
@ExperimentalContracts
fun foo(a: () -> Unit, b: () -> Unit) {
<!LEAKED_IN_PLACE_LAMBDA!>contract {
callsInPlace(a, InvocationKind.AT_MOST_ONCE)
}<!>
fun localFun() {
<!LEAKED_IN_PLACE_LAMBDA!>a<!>.invoke()
<!LEAKED_IN_PLACE_LAMBDA!>a()<!>
}
localFun()
b()
}
@@ -0,0 +1,16 @@
FILE: inLocalFunction.kt
@R|kotlin/contracts/ExperimentalContracts|() public final fun foo(a: R|() -> kotlin/Unit|, b: R|() -> kotlin/Unit|): R|kotlin/Unit|
[R|Contract description]
<
CallsInPlace(a, AT_MOST_ONCE)
>
{
[StubStatement]
local final fun localFun(): R|kotlin/Unit| {
R|<local>/a|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()
R|<local>/a|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()
}
R|<local>/localFun|()
R|<local>/b|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()
}
@@ -0,0 +1,97 @@
digraph toLocalVariables_kt {
graph [nodesep=3]
node [shape=box penwidth=2]
edge [penwidth=2]
subgraph cluster_0 {
color=red
0 [label="Enter function bar" style="filled" fillcolor=red];
subgraph cluster_1 {
color=blue
1 [label="Enter block"];
2 [label="Exit block"];
}
3 [label="Exit function bar" style="filled" fillcolor=red];
}
0 -> {1};
1 -> {2};
2 -> {3};
subgraph cluster_2 {
color=red
4 [label="Enter function foo" style="filled" fillcolor=red];
subgraph cluster_3 {
color=blue
5 [label="Enter block"];
subgraph cluster_4 {
color=blue
6 [label="Enter when"];
subgraph cluster_5 {
color=blue
7 [label="Enter when branch condition "];
8 [label="Const: Boolean(true)"];
9 [label="Exit when branch condition"];
}
subgraph cluster_6 {
color=blue
10 [label="Enter when branch condition else"];
11 [label="Exit when branch condition"];
}
12 [label="Enter when branch result"];
subgraph cluster_7 {
color=blue
13 [label="Enter block"];
14 [label="Access variable R|<local>/y|"];
15 [label="Variable declaration: lval yCopy: R|() -> kotlin/Unit|"];
16 [label="Function call: R|<local>/yCopy|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()"];
17 [label="Exit block"];
}
18 [label="Exit when branch result"];
19 [label="Enter when branch result"];
subgraph cluster_8 {
color=blue
20 [label="Enter block"];
21 [label="Access variable R|<local>/x|"];
22 [label="Function call: R|/bar|(...)"];
23 [label="Exit block"];
}
24 [label="Exit when branch result"];
25 [label="Exit when"];
}
26 [label="Variable declaration: lval zCopy: R|() -> kotlin/Unit|"];
27 [label="Access variable R|<local>/z|"];
28 [label="Assignment: R|<local>/zCopy|"];
29 [label="Function call: R|<local>/zCopy|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()"];
30 [label="Exit block"];
}
31 [label="Exit function foo" style="filled" fillcolor=red];
}
4 -> {5};
5 -> {6};
6 -> {7};
7 -> {8};
8 -> {9};
9 -> {19 10};
10 -> {11};
11 -> {12};
12 -> {13};
13 -> {14};
14 -> {15};
15 -> {16};
16 -> {17};
17 -> {18};
18 -> {25};
19 -> {20};
20 -> {21};
21 -> {22};
22 -> {23};
23 -> {24};
24 -> {25};
25 -> {26};
26 -> {27};
27 -> {28};
28 -> {29};
29 -> {30};
30 -> {31};
}
@@ -0,0 +1,26 @@
// !DUMP_CFG
import kotlin.contracts.*
fun bar(x: () -> Unit) {
}
@ExperimentalContracts
fun foo(x: () -> Unit, y: () -> Unit, z: () -> Unit) {
<!LEAKED_IN_PLACE_LAMBDA, LEAKED_IN_PLACE_LAMBDA, LEAKED_IN_PLACE_LAMBDA!>contract {
callsInPlace(x, InvocationKind.AT_MOST_ONCE)
callsInPlace(y, InvocationKind.AT_MOST_ONCE)
callsInPlace(z, InvocationKind.AT_MOST_ONCE)
}<!>
if (true) {
bar(<!LEAKED_IN_PLACE_LAMBDA!>x<!>)
} else {
val yCopy = <!LEAKED_IN_PLACE_LAMBDA!>y<!>
yCopy()
}
val zCopy: () -> Unit
zCopy = <!LEAKED_IN_PLACE_LAMBDA!>z<!>
zCopy()
}
@@ -0,0 +1,26 @@
FILE: toLocalVariables.kt
public final fun bar(x: R|() -> kotlin/Unit|): R|kotlin/Unit| {
}
@R|kotlin/contracts/ExperimentalContracts|() public final fun foo(x: R|() -> kotlin/Unit|, y: R|() -> kotlin/Unit|, z: R|() -> kotlin/Unit|): R|kotlin/Unit|
[R|Contract description]
<
CallsInPlace(x, AT_MOST_ONCE)
CallsInPlace(y, AT_MOST_ONCE)
CallsInPlace(z, AT_MOST_ONCE)
>
{
[StubStatement]
when () {
Boolean(true) -> {
R|/bar|(R|<local>/x|)
}
else -> {
lval yCopy: R|() -> kotlin/Unit| = R|<local>/y|
R|<local>/yCopy|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()
}
}
lval zCopy: R|() -> kotlin/Unit|
R|<local>/zCopy| = R|<local>/z|
R|<local>/zCopy|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()
}
@@ -0,0 +1,131 @@
digraph contractsUsage_kt {
graph [nodesep=3]
node [shape=box penwidth=2]
edge [penwidth=2]
subgraph cluster_0 {
color=red
0 [label="Enter function bar" style="filled" fillcolor=red];
subgraph cluster_1 {
color=blue
1 [label="Enter block"];
2 [label="Access variable R|<local>/x|"];
3 [label="Function call: R|<local>/x|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()"];
4 [label="Exit block"];
}
5 [label="Exit function bar" style="filled" fillcolor=red];
}
0 -> {1};
1 -> {2};
2 -> {3};
3 -> {4};
4 -> {5};
subgraph cluster_2 {
color=red
6 [label="Enter function baz" style="filled" fillcolor=red];
subgraph cluster_3 {
color=blue
7 [label="Enter block"];
subgraph cluster_4 {
color=blue
8 [label="Enter when"];
subgraph cluster_5 {
color=blue
9 [label="Enter when branch condition "];
10 [label="Const: Boolean(true)"];
11 [label="Exit when branch condition"];
}
12 [label="Synthetic else branch"];
13 [label="Enter when branch result"];
subgraph cluster_6 {
color=blue
14 [label="Enter block"];
15 [label="Access variable this@R|/baz|"];
16 [label="Function call: this@R|/baz|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()"];
17 [label="Exit block"];
}
18 [label="Exit when branch result"];
19 [label="Exit when"];
}
20 [label="Exit block"];
}
21 [label="Exit function baz" style="filled" fillcolor=red];
}
6 -> {7};
7 -> {8};
8 -> {9};
9 -> {10};
10 -> {11};
11 -> {13 12};
12 -> {19};
13 -> {14};
14 -> {15};
15 -> {16};
16 -> {17};
17 -> {18};
18 -> {19};
19 -> {20};
20 -> {21};
subgraph cluster_7 {
color=red
22 [label="Enter function foo" style="filled" fillcolor=red];
subgraph cluster_8 {
color=blue
23 [label="Enter block"];
subgraph cluster_9 {
color=blue
24 [label="Enter when"];
subgraph cluster_10 {
color=blue
25 [label="Enter when branch condition "];
26 [label="Const: Boolean(true)"];
27 [label="Exit when branch condition"];
}
28 [label="Synthetic else branch"];
29 [label="Enter when branch result"];
subgraph cluster_11 {
color=blue
30 [label="Enter block"];
31 [label="Access variable R|<local>/x|"];
32 [label="Function call: R|<local>/x|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()"];
33 [label="Access variable R|<local>/y|"];
34 [label="Function call: R|<local>/y|.R|/baz|()"];
35 [label="Jump: ^foo Unit"];
36 [label="Stub" style="filled" fillcolor=gray];
37 [label="Exit block" style="filled" fillcolor=gray];
}
38 [label="Exit when branch result" style="filled" fillcolor=gray];
39 [label="Exit when"];
}
40 [label="Access variable R|<local>/x|"];
41 [label="Function call: R|/bar|(...)"];
42 [label="Exit block"];
}
43 [label="Exit function foo" style="filled" fillcolor=red];
}
22 -> {23};
23 -> {24};
24 -> {25};
25 -> {26};
26 -> {27};
27 -> {29 28};
28 -> {39};
29 -> {30};
30 -> {31};
31 -> {32};
32 -> {33};
33 -> {34};
34 -> {35};
35 -> {43};
35 -> {36} [style=dotted];
36 -> {37} [style=dotted];
37 -> {38} [style=dotted];
38 -> {39} [style=dotted];
39 -> {40};
40 -> {41};
41 -> {42};
42 -> {43};
}
@@ -0,0 +1,38 @@
// !DUMP_CFG
import kotlin.contracts.*
@ExperimentalContracts
fun bar(x: () -> Unit) {
contract {
callsInPlace(x, InvocationKind.EXACTLY_ONCE)
}
x.invoke()
}
@ExperimentalContracts
fun (() -> Unit).baz() {
contract {
callsInPlace(this@baz, InvocationKind.AT_MOST_ONCE)
}
if(true){
this.invoke()
}
}
@ExperimentalContracts
fun foo(x: () -> Unit, y: () -> Unit) {
contract {
callsInPlace(x, InvocationKind.AT_LEAST_ONCE)
callsInPlace(y, InvocationKind.AT_MOST_ONCE)
}
if (true) {
x.invoke()
y.baz()
return
}
bar(x)
}
@@ -0,0 +1,42 @@
FILE: contractsUsage.kt
@R|kotlin/contracts/ExperimentalContracts|() public final fun bar(x: R|() -> kotlin/Unit|): R|kotlin/Unit|
[R|Contract description]
<
CallsInPlace(x, EXACTLY_ONCE)
>
{
[StubStatement]
R|<local>/x|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()
}
@R|kotlin/contracts/ExperimentalContracts|() public final fun R|() -> kotlin/Unit|.baz(): R|kotlin/Unit|
[R|Contract description]
<
CallsInPlace(this, AT_MOST_ONCE)
>
{
[StubStatement]
when () {
Boolean(true) -> {
this@R|/baz|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()
}
}
}
@R|kotlin/contracts/ExperimentalContracts|() public final fun foo(x: R|() -> kotlin/Unit|, y: R|() -> kotlin/Unit|): R|kotlin/Unit|
[R|Contract description]
<
CallsInPlace(x, AT_LEAST_ONCE)
CallsInPlace(y, AT_MOST_ONCE)
>
{
[StubStatement]
when () {
Boolean(true) -> {
R|<local>/x|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()
R|<local>/y|.R|/baz|()
^foo Unit
}
}
R|/bar|(R|<local>/x|)
}
@@ -0,0 +1,274 @@
digraph flow_kt {
graph [nodesep=3]
node [shape=box penwidth=2]
edge [penwidth=2]
subgraph cluster_0 {
color=red
0 [label="Enter function bar" style="filled" fillcolor=red];
subgraph cluster_1 {
color=blue
1 [label="Enter block"];
subgraph cluster_2 {
color=blue
2 [label="Enter when"];
subgraph cluster_3 {
color=blue
3 [label="Enter when branch condition "];
4 [label="Const: Boolean(true)"];
5 [label="Exit when branch condition"];
}
6 [label="Synthetic else branch"];
7 [label="Enter when branch result"];
subgraph cluster_4 {
color=blue
8 [label="Enter block"];
9 [label="Access variable R|<local>/x|"];
10 [label="Function call: R|<local>/x|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()"];
11 [label="Jump: ^bar Unit"];
12 [label="Stub" style="filled" fillcolor=gray];
13 [label="Exit block" style="filled" fillcolor=gray];
}
14 [label="Exit when branch result" style="filled" fillcolor=gray];
15 [label="Exit when"];
}
16 [label="Access variable R|<local>/x|"];
17 [label="Function call: R|/bar|(...)"];
18 [label="Exit block"];
}
19 [label="Exit function bar" style="filled" fillcolor=red];
}
0 -> {1};
1 -> {2};
2 -> {3};
3 -> {4};
4 -> {5};
5 -> {7 6};
6 -> {15};
7 -> {8};
8 -> {9};
9 -> {10};
10 -> {11};
11 -> {19};
11 -> {12} [style=dotted];
12 -> {13} [style=dotted];
13 -> {14} [style=dotted];
14 -> {15} [style=dotted];
15 -> {16};
16 -> {17};
17 -> {18};
18 -> {19};
subgraph cluster_5 {
color=red
20 [label="Enter function foo" style="filled" fillcolor=red];
subgraph cluster_6 {
color=blue
21 [label="Enter block"];
subgraph cluster_7 {
color=blue
22 [label="Enter when"];
subgraph cluster_8 {
color=blue
23 [label="Enter when branch condition "];
24 [label="Const: Boolean(true)"];
25 [label="Exit when branch condition"];
}
subgraph cluster_9 {
color=blue
26 [label="Enter when branch condition else"];
27 [label="Exit when branch condition"];
}
28 [label="Enter when branch result"];
subgraph cluster_10 {
color=blue
29 [label="Enter block"];
subgraph cluster_11 {
color=blue
30 [label="Enter when"];
subgraph cluster_12 {
color=blue
31 [label="Enter when branch condition "];
32 [label="Const: Boolean(false)"];
33 [label="Exit when branch condition"];
}
subgraph cluster_13 {
color=blue
34 [label="Enter when branch condition else"];
35 [label="Exit when branch condition"];
}
36 [label="Enter when branch result"];
subgraph cluster_14 {
color=blue
37 [label="Enter block"];
38 [label="Access variable R|<local>/y|"];
39 [label="Function call: R|<local>/y|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()"];
40 [label="Access variable R|<local>/z|"];
41 [label="Function call: R|<local>/z|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()"];
42 [label="Jump: ^foo Unit"];
43 [label="Stub" style="filled" fillcolor=gray];
44 [label="Exit block" style="filled" fillcolor=gray];
}
45 [label="Exit when branch result" style="filled" fillcolor=gray];
46 [label="Enter when branch result"];
subgraph cluster_15 {
color=blue
47 [label="Enter block"];
48 [label="Access variable R|<local>/y|"];
49 [label="Function call: R|<local>/y|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()"];
50 [label="Exit block"];
}
51 [label="Exit when branch result"];
52 [label="Exit when"];
}
53 [label="Exit block"];
}
54 [label="Exit when branch result"];
55 [label="Enter when branch result"];
subgraph cluster_16 {
color=blue
56 [label="Enter block"];
57 [label="Const: Int(0)"];
58 [label="Const: Int(0)"];
59 [label="Function call: Int(0).R|kotlin/Int.rangeTo|(...)"];
60 [label="Function call: Int(0).R|kotlin/Int.rangeTo|(...).R|kotlin/ranges/IntProgression.iterator|()"];
61 [label="Variable declaration: lval <iterator>: R|kotlin/collections/IntIterator|"];
subgraph cluster_17 {
color=blue
62 [label="Enter while loop"];
subgraph cluster_18 {
color=blue
63 [label="Enter loop condition"];
64 [label="Access variable R|<local>/<iterator>|"];
65 [label="Function call: R|<local>/<iterator>|.R|kotlin/collections/Iterator.hasNext|()"];
66 [label="Exit loop condition"];
}
subgraph cluster_19 {
color=blue
67 [label="Enter loop block"];
subgraph cluster_20 {
color=blue
68 [label="Enter block"];
69 [label="Access variable R|<local>/<iterator>|"];
70 [label="Function call: R|<local>/<iterator>|.R|kotlin/collections/IntIterator.next|()"];
71 [label="Variable declaration: lval i: R|kotlin/Int|"];
72 [label="Access variable R|<local>/x|"];
73 [label="Function call: R|<local>/x|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()"];
74 [label="Exit block"];
}
75 [label="Exit loop block"];
}
76 [label="Exit whileloop"];
}
77 [label="Access variable R|<local>/y|"];
78 [label="Function call: R|<local>/y|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()"];
79 [label="Exit block"];
}
80 [label="Exit when branch result"];
81 [label="Exit when"];
}
subgraph cluster_21 {
color=blue
82 [label="Enter do-while loop"];
subgraph cluster_22 {
color=blue
83 [label="Enter loop block"];
subgraph cluster_23 {
color=blue
84 [label="Enter block"];
85 [label="Access variable R|<local>/z|"];
86 [label="Function call: R|/bar|(...)"];
87 [label="Exit block"];
}
88 [label="Exit loop block"];
}
subgraph cluster_24 {
color=blue
89 [label="Enter loop condition"];
90 [label="Const: Boolean(true)"];
91 [label="Exit loop condition"];
}
92 [label="Exit do-whileloop" style="filled" fillcolor=gray];
}
93 [label="Exit block" style="filled" fillcolor=gray];
}
94 [label="Exit function foo" style="filled" fillcolor=red];
}
20 -> {21};
21 -> {22};
22 -> {23};
23 -> {24};
24 -> {25};
25 -> {55 26};
26 -> {27};
27 -> {28};
28 -> {29};
29 -> {30};
30 -> {31};
31 -> {32};
32 -> {33};
33 -> {46 34};
34 -> {35};
35 -> {36};
36 -> {37};
37 -> {38};
38 -> {39};
39 -> {40};
40 -> {41};
41 -> {42};
42 -> {94};
42 -> {43} [style=dotted];
43 -> {44} [style=dotted];
44 -> {45} [style=dotted];
45 -> {52} [style=dotted];
46 -> {47};
47 -> {48};
48 -> {49};
49 -> {50};
50 -> {51};
51 -> {52};
52 -> {53};
53 -> {54};
54 -> {81};
55 -> {56};
56 -> {57};
57 -> {58};
58 -> {59};
59 -> {60};
60 -> {61};
61 -> {62};
62 -> {63};
63 -> {64};
64 -> {65};
65 -> {66};
66 -> {76 67};
67 -> {68};
68 -> {69};
69 -> {70};
70 -> {71};
71 -> {72};
72 -> {73};
73 -> {74};
74 -> {75};
75 -> {63} [color=green style=dashed];
76 -> {77};
77 -> {78};
78 -> {79};
79 -> {80};
80 -> {81};
81 -> {82};
82 -> {83};
83 -> {84};
84 -> {85};
85 -> {86};
86 -> {87};
87 -> {88};
88 -> {89};
89 -> {90};
90 -> {91};
91 -> {92} [style=dotted];
91 -> {83} [color=green style=dashed];
92 -> {93} [style=dotted];
93 -> {94} [style=dotted];
}
@@ -0,0 +1,45 @@
// !DUMP_CFG
import kotlin.contracts.*
@ExperimentalContracts
fun bar(x: () -> Unit) {
contract {
callsInPlace(x, InvocationKind.EXACTLY_ONCE)
}
if (true) {
x.invoke()
return
}
bar(x)
}
@ExperimentalContracts
fun foo(x: () -> Unit, y: () -> Unit, z: () -> Unit) {
contract {
callsInPlace(x, InvocationKind.UNKNOWN)
callsInPlace(y, InvocationKind.EXACTLY_ONCE)
callsInPlace(z, InvocationKind.AT_LEAST_ONCE)
}
if (true) {
for (i in 0..0) {
x.invoke()
}
y.invoke()
} else {
if (false) {
y.invoke()
} else {
y.invoke()
z.invoke()
return
}
}
do {
bar(z)
} while (true)
}
@@ -0,0 +1,56 @@
FILE: flow.kt
@R|kotlin/contracts/ExperimentalContracts|() public final fun bar(x: R|() -> kotlin/Unit|): R|kotlin/Unit|
[R|Contract description]
<
CallsInPlace(x, EXACTLY_ONCE)
>
{
[StubStatement]
when () {
Boolean(true) -> {
R|<local>/x|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()
^bar Unit
}
}
R|/bar|(R|<local>/x|)
}
@R|kotlin/contracts/ExperimentalContracts|() public final fun foo(x: R|() -> kotlin/Unit|, y: R|() -> kotlin/Unit|, z: R|() -> kotlin/Unit|): R|kotlin/Unit|
[R|Contract description]
<
CallsInPlace(x, UNKNOWN)
CallsInPlace(y, EXACTLY_ONCE)
CallsInPlace(z, AT_LEAST_ONCE)
>
{
[StubStatement]
when () {
Boolean(true) -> {
lval <iterator>: R|kotlin/collections/IntIterator| = Int(0).R|kotlin/Int.rangeTo|(Int(0)).R|kotlin/ranges/IntProgression.iterator|()
while(R|<local>/<iterator>|.R|kotlin/collections/Iterator.hasNext|()) {
lval i: R|kotlin/Int| = R|<local>/<iterator>|.R|kotlin/collections/IntIterator.next|()
R|<local>/x|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()
}
R|<local>/y|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()
}
else -> {
when () {
Boolean(false) -> {
R|<local>/y|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()
}
else -> {
R|<local>/y|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()
R|<local>/z|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()
^foo Unit
}
}
}
}
do {
R|/bar|(R|<local>/z|)
}
while(Boolean(true))
}
@@ -0,0 +1,91 @@
digraph inPlaceLambda_kt {
graph [nodesep=3]
node [shape=box penwidth=2]
edge [penwidth=2]
subgraph cluster_0 {
color=red
0 [label="Enter function bar" style="filled" fillcolor=red];
subgraph cluster_1 {
color=blue
1 [label="Enter block"];
subgraph cluster_2 {
color=blue
2 [label="Enter when"];
subgraph cluster_3 {
color=blue
3 [label="Enter when branch condition "];
4 [label="Const: Boolean(true)"];
5 [label="Exit when branch condition"];
}
6 [label="Synthetic else branch"];
7 [label="Enter when branch result"];
subgraph cluster_4 {
color=blue
8 [label="Enter block"];
9 [label="Function call: R|<local>/x|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()"];
10 [label="Exit block"];
}
11 [label="Exit when branch result"];
12 [label="Exit when"];
}
13 [label="Exit block"];
}
14 [label="Exit function bar" style="filled" fillcolor=red];
}
0 -> {1};
1 -> {2};
2 -> {3};
3 -> {4};
4 -> {5};
5 -> {7 6};
6 -> {12};
7 -> {8};
8 -> {9};
9 -> {10};
10 -> {11};
11 -> {12};
12 -> {13};
13 -> {14};
subgraph cluster_5 {
color=red
15 [label="Enter function foo" style="filled" fillcolor=red];
subgraph cluster_6 {
color=blue
16 [label="Enter block"];
17 [label="Function call: R|<local>/x|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()"];
18 [label="Postponed enter to lambda"];
subgraph cluster_7 {
color=blue
23 [label="Enter function anonymousFunction" style="filled" fillcolor=red];
subgraph cluster_8 {
color=blue
24 [label="Enter block"];
25 [label="Function call: R|<local>/x|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()"];
26 [label="Exit block"];
}
27 [label="Exit function anonymousFunction" style="filled" fillcolor=red];
}
19 [label="Postponed exit from lambda"];
20 [label="Function call: R|/bar|(...)"];
21 [label="Exit block"];
}
22 [label="Exit function foo" style="filled" fillcolor=red];
}
15 -> {16};
16 -> {17};
17 -> {18};
18 -> {23};
18 -> {19} [color=red];
18 -> {23} [style=dashed];
19 -> {20};
20 -> {21};
21 -> {22};
23 -> {27 24};
24 -> {25};
25 -> {26};
26 -> {27};
27 -> {19} [color=green];
}
@@ -0,0 +1,26 @@
// !DUMP_CFG
import kotlin.contracts.*
@ExperimentalContracts
fun bar(x: () -> Unit) {
contract {
callsInPlace(x, InvocationKind.AT_MOST_ONCE)
}
if (true) {
x()
}
}
@ExperimentalContracts
fun foo(x: () -> Unit) {
contract {
callsInPlace(x, InvocationKind.AT_LEAST_ONCE)
}
x()
bar {
x()
}
}
@@ -0,0 +1,28 @@
FILE: inPlaceLambda.kt
@R|kotlin/contracts/ExperimentalContracts|() public final fun bar(x: R|() -> kotlin/Unit|): R|kotlin/Unit|
[R|Contract description]
<
CallsInPlace(x, AT_MOST_ONCE)
>
{
[StubStatement]
when () {
Boolean(true) -> {
R|<local>/x|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()
}
}
}
@R|kotlin/contracts/ExperimentalContracts|() public final fun foo(x: R|() -> kotlin/Unit|): R|kotlin/Unit|
[R|Contract description]
<
CallsInPlace(x, AT_LEAST_ONCE)
>
{
[StubStatement]
R|<local>/x|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()
R|/bar|(<L> = bar@fun <anonymous>(): R|kotlin/Unit| <kind=AT_MOST_ONCE> {
R|<local>/x|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()
}
)
}
@@ -0,0 +1,77 @@
digraph simple_kt {
graph [nodesep=3]
node [shape=box penwidth=2]
edge [penwidth=2]
subgraph cluster_0 {
color=red
0 [label="Enter function bar" style="filled" fillcolor=red];
subgraph cluster_1 {
color=blue
1 [label="Enter block"];
2 [label="Access variable R|<local>/x|"];
3 [label="Function call: R|<local>/x|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()"];
4 [label="Exit block"];
}
5 [label="Exit function bar" style="filled" fillcolor=red];
}
0 -> {1};
1 -> {2};
2 -> {3};
3 -> {4};
4 -> {5};
subgraph cluster_2 {
color=red
6 [label="Enter function foo" style="filled" fillcolor=red];
subgraph cluster_3 {
color=blue
7 [label="Enter block"];
8 [label="Access variable R|<local>/x|"];
9 [label="Function call: R|<local>/x|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()"];
subgraph cluster_4 {
color=blue
10 [label="Enter when"];
subgraph cluster_5 {
color=blue
11 [label="Enter when branch condition "];
12 [label="Const: Boolean(true)"];
13 [label="Exit when branch condition"];
}
14 [label="Synthetic else branch"];
15 [label="Enter when branch result"];
subgraph cluster_6 {
color=blue
16 [label="Enter block"];
17 [label="Function call: R|<local>/y|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()"];
18 [label="Exit block"];
}
19 [label="Exit when branch result"];
20 [label="Exit when"];
}
21 [label="Access variable R|<local>/z|"];
22 [label="Function call: R|/bar|(...)"];
23 [label="Exit block"];
}
24 [label="Exit function foo" style="filled" fillcolor=red];
}
6 -> {7};
7 -> {8};
8 -> {9};
9 -> {10};
10 -> {11};
11 -> {12};
12 -> {13};
13 -> {15 14};
14 -> {20};
15 -> {16};
16 -> {17};
17 -> {18};
18 -> {19};
19 -> {20};
20 -> {21};
21 -> {22};
22 -> {23};
23 -> {24};
}
@@ -0,0 +1,28 @@
// !DUMP_CFG
import kotlin.contracts.*
@ExperimentalContracts
fun bar(x: () -> Unit) {
contract {
callsInPlace(x, InvocationKind.EXACTLY_ONCE)
}
x.invoke()
}
@ExperimentalContracts
fun foo(x: () -> Unit, y: () -> Unit, z: () -> Unit) {
contract {
callsInPlace(x, InvocationKind.EXACTLY_ONCE)
callsInPlace(y, InvocationKind.AT_MOST_ONCE)
callsInPlace(z, InvocationKind.EXACTLY_ONCE)
}
x.invoke()
if (true) {
y()
}
bar(z)
}
@@ -0,0 +1,28 @@
FILE: simple.kt
@R|kotlin/contracts/ExperimentalContracts|() public final fun bar(x: R|() -> kotlin/Unit|): R|kotlin/Unit|
[R|Contract description]
<
CallsInPlace(x, EXACTLY_ONCE)
>
{
[StubStatement]
R|<local>/x|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()
}
@R|kotlin/contracts/ExperimentalContracts|() public final fun foo(x: R|() -> kotlin/Unit|, y: R|() -> kotlin/Unit|, z: R|() -> kotlin/Unit|): R|kotlin/Unit|
[R|Contract description]
<
CallsInPlace(x, EXACTLY_ONCE)
CallsInPlace(y, AT_MOST_ONCE)
CallsInPlace(z, EXACTLY_ONCE)
>
{
[StubStatement]
R|<local>/x|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()
when () {
Boolean(true) -> {
R|<local>/y|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()
}
}
R|/bar|(R|<local>/z|)
}
@@ -11,7 +11,7 @@ FILE: fromLocalMembers.kt
lval x: R|kotlin/Int|
lval y: R|kotlin/Int|
object : R|kotlin/Any| {
private[local] constructor(): R|<anonymous>| {
private constructor(): R|<anonymous>| {
super<R|kotlin/Any|>()
}
@@ -19,7 +19,7 @@ FILE: fromLocalMembers.kt
R|<local>/x| = Int(0)
}
public[local] final fun localFunc(): R|kotlin/Unit| {
public final fun localFunc(): R|kotlin/Unit| {
R|<local>/y| = Int(0)
}
@@ -32,7 +32,7 @@ FILE: fromLocalMembers.kt
lval x: R|kotlin/Int|
lval y: R|kotlin/Int|
local final class A : R|kotlin/Any| {
public[local] constructor(): R|A| {
public constructor(): R|A| {
super<R|kotlin/Any|>()
}
@@ -40,7 +40,7 @@ FILE: fromLocalMembers.kt
R|<local>/x| = Int(0)
}
public[local] final fun localFunc(): R|kotlin/Unit| {
public final fun localFunc(): R|kotlin/Unit| {
R|<local>/y| = Int(0)
}