[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
File diff suppressed because it is too large Load Diff
@@ -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)
}
@@ -546,6 +546,52 @@ public class FirDiagnosticsWithStdlibTestGenerated extends AbstractFirDiagnostic
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/fir/analysis-tests/testData/resolveWithStdlib/contracts/fromSource"), Pattern.compile("^([^.]+)\\.kt$"), null, true);
}
@TestMetadata("compiler/fir/analysis-tests/testData/resolveWithStdlib/contracts/fromSource/bad")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Bad extends AbstractFirDiagnosticsWithStdlibTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
public void testAllFilesPresentInBad() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/fir/analysis-tests/testData/resolveWithStdlib/contracts/fromSource/bad"), Pattern.compile("^([^.]+)\\.kt$"), null, true);
}
@TestMetadata("compiler/fir/analysis-tests/testData/resolveWithStdlib/contracts/fromSource/bad/callsInPlace")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class CallsInPlace extends AbstractFirDiagnosticsWithStdlibTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
public void testAllFilesPresentInCallsInPlace() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/fir/analysis-tests/testData/resolveWithStdlib/contracts/fromSource/bad/callsInPlace"), Pattern.compile("^([^.]+)\\.kt$"), null, true);
}
@TestMetadata("inAnonymousObject.kt")
public void testInAnonymousObject() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/contracts/fromSource/bad/callsInPlace/inAnonymousObject.kt");
}
@TestMetadata("inLocalClass.kt")
public void testInLocalClass() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/contracts/fromSource/bad/callsInPlace/inLocalClass.kt");
}
@TestMetadata("inLocalFunction.kt")
public void testInLocalFunction() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/contracts/fromSource/bad/callsInPlace/inLocalFunction.kt");
}
@TestMetadata("toLocalVariables.kt")
public void testToLocalVariables() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/contracts/fromSource/bad/callsInPlace/toLocalVariables.kt");
}
}
}
@TestMetadata("compiler/fir/analysis-tests/testData/resolveWithStdlib/contracts/fromSource/good")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -580,11 +626,31 @@ public class FirDiagnosticsWithStdlibTestGenerated extends AbstractFirDiagnostic
runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/contracts/fromSource/good/callsInPlace/atMostOnce.kt");
}
@TestMetadata("contractsUsage.kt")
public void testContractsUsage() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/contracts/fromSource/good/callsInPlace/contractsUsage.kt");
}
@TestMetadata("exactlyOnce.kt")
public void testExactlyOnce() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/contracts/fromSource/good/callsInPlace/exactlyOnce.kt");
}
@TestMetadata("flow.kt")
public void testFlow() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/contracts/fromSource/good/callsInPlace/flow.kt");
}
@TestMetadata("inPlaceLambda.kt")
public void testInPlaceLambda() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/contracts/fromSource/good/callsInPlace/inPlaceLambda.kt");
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/contracts/fromSource/good/callsInPlace/simple.kt");
}
@TestMetadata("unknown.kt")
public void testUnknown() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/contracts/fromSource/good/callsInPlace/unknown.kt");
@@ -0,0 +1,310 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.fir.analysis.cfa
import kotlinx.collections.immutable.PersistentMap
import kotlinx.collections.immutable.persistentMapOf
import org.jetbrains.kotlin.contracts.description.EventOccurrencesRange
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.FirSourceElement
import org.jetbrains.kotlin.fir.analysis.checkers.cfa.FirControlFlowChecker
import org.jetbrains.kotlin.fir.analysis.diagnostics.DiagnosticReporter
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors
import org.jetbrains.kotlin.fir.contracts.FirContractDescription
import org.jetbrains.kotlin.fir.contracts.description.ConeCallsEffectDeclaration
import org.jetbrains.kotlin.fir.contracts.effects
import org.jetbrains.kotlin.fir.declarations.FirAnonymousFunction
import org.jetbrains.kotlin.fir.declarations.FirContractDescriptionOwner
import org.jetbrains.kotlin.fir.declarations.FirFunction
import org.jetbrains.kotlin.fir.expressions.FirExpression
import org.jetbrains.kotlin.fir.expressions.FirFunctionCall
import org.jetbrains.kotlin.fir.expressions.FirQualifiedAccess
import org.jetbrains.kotlin.fir.expressions.impl.FirResolvedArgumentList
import org.jetbrains.kotlin.fir.expressions.toResolvedCallableSymbol
import org.jetbrains.kotlin.fir.references.FirReference
import org.jetbrains.kotlin.fir.references.FirResolvedNamedReference
import org.jetbrains.kotlin.fir.references.FirThisReference
import org.jetbrains.kotlin.fir.resolve.dfa.cfg.*
import org.jetbrains.kotlin.fir.resolve.inference.isBuiltinFunctionalType
import org.jetbrains.kotlin.fir.resolve.isInvoke
import org.jetbrains.kotlin.fir.symbols.AbstractFirBasedSymbol
import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirFunctionSymbol
import org.jetbrains.kotlin.fir.types.ConeKotlinType
import org.jetbrains.kotlin.fir.types.FirTypeRef
import org.jetbrains.kotlin.fir.types.coneTypeSafe
import org.jetbrains.kotlin.utils.addIfNotNull
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract
object FirCallsEffectAnalyzer : FirControlFlowChecker() {
override fun analyze(graph: ControlFlowGraph, reporter: DiagnosticReporter) {
val function = (graph.declaration as? FirFunction<*>) ?: return
if (function !is FirContractDescriptionOwner) return
if (function.contractDescription.effects?.any { it is ConeCallsEffectDeclaration } != true) return
val functionalTypeEffects = mutableMapOf<AbstractFirBasedSymbol<*>, ConeCallsEffectDeclaration>()
function.valueParameters.forEachIndexed { index, parameter ->
if (parameter.returnTypeRef.isFunctionalTypeRef(function.session)) {
val effectDeclaration = function.contractDescription.getParameterCallsEffectDeclaration(index)
if (effectDeclaration != null) functionalTypeEffects[parameter.symbol] = effectDeclaration
}
}
if (function.receiverTypeRef.isFunctionalTypeRef(function.session)) {
val effectDeclaration = function.contractDescription.getParameterCallsEffectDeclaration(-1)
if (effectDeclaration != null) functionalTypeEffects[function.symbol] = effectDeclaration
}
if (functionalTypeEffects.isEmpty()) return
val leakedSymbols = mutableMapOf<AbstractFirBasedSymbol<*>, MutableList<FirSourceElement>>()
graph.traverse(
TraverseDirection.Forward,
CapturedLambdaFinder(function),
IllegalScopeContext(functionalTypeEffects.keys, leakedSymbols)
)
for ((symbol, leakedPlaces) in leakedSymbols) {
function.contractDescription.source?.let {
reporter.report(FirErrors.LEAKED_IN_PLACE_LAMBDA.on(it, symbol))
}
leakedPlaces.forEach {
reporter.report(FirErrors.LEAKED_IN_PLACE_LAMBDA.on(it, symbol))
}
}
val invocationData = graph.collectDataForNode(
TraverseDirection.Forward,
LambdaInvocationInfo.EMPTY,
InvocationDataCollector(functionalTypeEffects.keys.filterTo(mutableSetOf()) { it !in leakedSymbols })
)
for ((symbol, effectDeclaration) in functionalTypeEffects) {
graph.exitNode.previousCfgNodes.forEach { node ->
val requiredRange = effectDeclaration.kind
val foundRange = invocationData.getValue(node)[symbol] ?: EventOccurrencesRange.ZERO
if (foundRange !in requiredRange) {
function.contractDescription.source?.let {
reporter.report(FirErrors.WRONG_INVOCATION_KIND.on(it, symbol, requiredRange, foundRange))
}
}
}
}
}
private class IllegalScopeContext(
private val functionalTypeSymbols: Set<AbstractFirBasedSymbol<*>>,
private val leakedSymbols: MutableMap<AbstractFirBasedSymbol<*>, MutableList<FirSourceElement>>,
) {
private var scopeDepth: Int = 0
private var illegalScopeDepth: Int? = null
val inIllegalScope: Boolean get() = illegalScopeDepth != null
fun enterScope(legal: Boolean) {
scopeDepth++
if (illegalScopeDepth == null && !legal) illegalScopeDepth = scopeDepth
}
fun exitScope() {
if (scopeDepth == illegalScopeDepth) illegalScopeDepth = null
scopeDepth--
}
inline fun checkExpressionForLeakedSymbols(
fir: FirExpression?,
source: FirSourceElement? = fir?.source,
illegalUsage: () -> Boolean = { false }
) {
val symbol = referenceToSymbol(fir.toQualifiedReference())
if (symbol != null && symbol in functionalTypeSymbols && (inIllegalScope || illegalUsage())) {
leakedSymbols.getOrPut(symbol, ::mutableListOf).addIfNotNull(source)
}
}
}
private class CapturedLambdaFinder(val rootFunction: FirFunction<*>) : ControlFlowGraphVisitor<Unit, IllegalScopeContext>() {
override fun visitNode(node: CFGNode<*>, data: IllegalScopeContext) {}
override fun visitFunctionEnterNode(node: FunctionEnterNode, data: IllegalScopeContext) {
data.enterScope(node.fir === rootFunction || node.fir.isInPlaceLambda())
}
override fun visitFunctionExitNode(node: FunctionExitNode, data: IllegalScopeContext) {
data.exitScope()
}
override fun visitPropertyInitializerEnterNode(node: PropertyInitializerEnterNode, data: IllegalScopeContext) {
data.enterScope(false)
data.checkExpressionForLeakedSymbols(node.fir.initializer)
}
override fun visitPropertyInitializerExitNode(node: PropertyInitializerExitNode, data: IllegalScopeContext) {
data.exitScope()
}
override fun visitInitBlockEnterNode(node: InitBlockEnterNode, data: IllegalScopeContext) {
data.enterScope(false)
}
override fun visitInitBlockExitNode(node: InitBlockExitNode, data: IllegalScopeContext) {
data.exitScope()
}
override fun visitVariableAssignmentNode(node: VariableAssignmentNode, data: IllegalScopeContext) {
data.checkExpressionForLeakedSymbols(node.fir.rValue) { true }
}
override fun visitVariableDeclarationNode(node: VariableDeclarationNode, data: IllegalScopeContext) {
data.checkExpressionForLeakedSymbols(node.fir.initializer) { true }
}
override fun visitFunctionCallNode(node: FunctionCallNode, data: IllegalScopeContext) {
val functionSymbol = node.fir.toResolvedCallableSymbol() as? FirFunctionSymbol<*>?
val contractDescription = functionSymbol?.fir?.contractDescription
val callSource = node.fir.explicitReceiver?.source ?: node.fir.source
data.checkExpressionForLeakedSymbols(node.fir.explicitReceiver, callSource) {
functionSymbol?.callableId?.isInvoke() != true && contractDescription?.getParameterCallsEffect(-1) == null
}
for (arg in node.fir.argumentList.arguments) {
data.checkExpressionForLeakedSymbols(arg) {
node.fir.getArgumentCallsEffect(arg) == null
}
}
}
}
private class LambdaInvocationInfo(
map: PersistentMap<FirBasedSymbol<*>, EventOccurrencesRange> = persistentMapOf(),
) : ControlFlowInfo<LambdaInvocationInfo, FirBasedSymbol<*>, EventOccurrencesRange>(map) {
companion object {
val EMPTY = LambdaInvocationInfo()
}
override val constructor: (PersistentMap<FirBasedSymbol<*>, EventOccurrencesRange>) -> LambdaInvocationInfo =
::LambdaInvocationInfo
fun merge(other: LambdaInvocationInfo): LambdaInvocationInfo {
var result = this
for (symbol in keys.union(other.keys)) {
val kind1 = this[symbol] ?: EventOccurrencesRange.ZERO
val kind2 = other[symbol] ?: EventOccurrencesRange.ZERO
result = result.put(symbol, kind1 or kind2)
}
return result
}
}
private class InvocationDataCollector(
val functionalTypeSymbols: Set<AbstractFirBasedSymbol<*>>
) : ControlFlowGraphVisitor<LambdaInvocationInfo, Collection<LambdaInvocationInfo>>() {
override fun visitNode(node: CFGNode<*>, data: Collection<LambdaInvocationInfo>): LambdaInvocationInfo {
if (data.isEmpty()) return LambdaInvocationInfo.EMPTY
return data.reduce(LambdaInvocationInfo::merge)
}
override fun visitFunctionCallNode(
node: FunctionCallNode,
data: Collection<LambdaInvocationInfo>
): LambdaInvocationInfo {
var dataForNode = visitNode(node, data)
val functionSymbol = node.fir.toResolvedCallableSymbol() as? FirFunctionSymbol<*>?
val contractDescription = functionSymbol?.fir?.contractDescription
dataForNode = dataForNode.checkReference(node.fir.explicitReceiver.toQualifiedReference()) {
when {
functionSymbol?.callableId?.isInvoke() == true -> EventOccurrencesRange.EXACTLY_ONCE
else -> contractDescription.getParameterCallsEffect(-1) ?: EventOccurrencesRange.UNKNOWN
}
}
for (arg in node.fir.argumentList.arguments) {
dataForNode = dataForNode.checkReference(arg.toQualifiedReference()) {
node.fir.getArgumentCallsEffect(arg) ?: EventOccurrencesRange.ZERO
}
}
return dataForNode
}
@OptIn(ExperimentalContracts::class)
private fun collectDataForReference(reference: FirReference?): Boolean {
contract {
returns(true) implies (reference != null)
}
return reference != null && referenceToSymbol(reference) in functionalTypeSymbols
}
private inline fun LambdaInvocationInfo.checkReference(
reference: FirReference?,
rangeGetter: () -> EventOccurrencesRange
): LambdaInvocationInfo {
return if (collectDataForReference(reference)) addInvocationInfo(reference, rangeGetter()) else this
}
private fun LambdaInvocationInfo.addInvocationInfo(
reference: FirReference,
range: EventOccurrencesRange
): LambdaInvocationInfo {
val symbol = referenceToSymbol(reference)
return if (symbol != null) {
val existingKind = this[symbol] ?: EventOccurrencesRange.ZERO
val kind = existingKind + range
this.put(symbol, kind)
} else this
}
}
private fun FirTypeRef?.isFunctionalTypeRef(session: FirSession): Boolean {
return this?.coneTypeSafe<ConeKotlinType>()?.isBuiltinFunctionalType(session) == true
}
private val FirFunction<*>.contractDescription: FirContractDescription?
get() = (this as? FirContractDescriptionOwner)?.contractDescription
private fun FirContractDescription?.getParameterCallsEffectDeclaration(index: Int): ConeCallsEffectDeclaration? {
val effects = this?.effects
val callsEffect = effects?.find { it is ConeCallsEffectDeclaration && it.valueParameterReference.parameterIndex == index }
return callsEffect as? ConeCallsEffectDeclaration?
}
private fun FirFunctionCall.getArgumentCallsEffect(arg: FirExpression): EventOccurrencesRange? {
val function = (this.toResolvedCallableSymbol() as? FirFunctionSymbol<*>?)?.fir
val contractDescription = function?.contractDescription
val resolvedArguments = argumentList as? FirResolvedArgumentList
return if (function != null && resolvedArguments != null) {
val parameter = resolvedArguments.mapping[arg]
contractDescription.getParameterCallsEffect(function.valueParameters.indexOf(parameter))
} else null
}
private fun FirContractDescription?.getParameterCallsEffect(index: Int): EventOccurrencesRange? {
return getParameterCallsEffectDeclaration(index)?.kind
}
private fun FirFunction<*>.isInPlaceLambda(): Boolean {
return this is FirAnonymousFunction && this.isLambda && this.invocationKind != null
}
private fun FirExpression?.toQualifiedReference(): FirReference? = (this as? FirQualifiedAccess)?.calleeReference
private fun referenceToSymbol(reference: FirReference?): AbstractFirBasedSymbol<*>? = when (reference) {
is FirResolvedNamedReference -> reference.resolvedSymbol
is FirThisReference -> reference.boundSymbol
else -> null
}
}
@@ -5,6 +5,7 @@
package org.jetbrains.kotlin.fir.analysis.checkers.declaration
import org.jetbrains.kotlin.fir.analysis.cfa.FirCallsEffectAnalyzer
import org.jetbrains.kotlin.fir.analysis.cfa.FirPropertyInitializationAnalyzer
import org.jetbrains.kotlin.fir.analysis.checkers.cfa.FirControlFlowChecker
@@ -40,6 +41,7 @@ object CommonDeclarationCheckers : DeclarationCheckers() {
)
override val controlFlowAnalyserCheckers: List<FirControlFlowChecker> = listOf(
FirPropertyInitializationAnalyzer
FirPropertyInitializationAnalyzer,
FirCallsEffectAnalyzer
)
}
@@ -46,6 +46,7 @@ import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.INAPPLICABLE_INFI
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.INCOMPATIBLE_MODIFIERS
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.INFERENCE_ERROR
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.INVALID_TYPE_OF_ANNOTATION_MEMBER
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.LEAKED_IN_PLACE_LAMBDA
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.LOCAL_ANNOTATION_CLASS_ERROR
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.LOCAL_INTERFACE_NOT_ALLOWED
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.LOCAL_OBJECT_NOT_ALLOWED
@@ -85,6 +86,7 @@ import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.UNRESOLVED_REFERE
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.UPPER_BOUND_VIOLATED
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.VARIABLE_EXPECTED
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.VAR_ANNOTATION_PARAMETER
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.WRONG_INVOCATION_KIND
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.WRONG_NUMBER_OF_TYPE_ARGUMENTS
@Suppress("unused")
@@ -132,7 +134,11 @@ class FirDefaultErrorMessages : DefaultErrorMessages.Extension {
map.put(RECURSION_IN_SUPERTYPES, "Recursion in supertypes")
map.put(NOT_A_SUPERTYPE, "Not an immediate supertype")
map.put(SUPERCLASS_NOT_ACCESSIBLE_FROM_INTERFACE, "Superclass is not accessible from interface")
map.put(QUALIFIED_SUPERTYPE_EXTENDED_BY_OTHER_SUPERTYPE, "Explicitly qualified supertype is extended by another supertype ''{0}''", TO_STRING)
map.put(
QUALIFIED_SUPERTYPE_EXTENDED_BY_OTHER_SUPERTYPE,
"Explicitly qualified supertype is extended by another supertype ''{0}''",
TO_STRING
)
// Constructor problems
map.put(CONSTRUCTOR_IN_OBJECT, "Constructors are not allowed for objects")
@@ -144,11 +150,17 @@ class FirDefaultErrorMessages : DefaultErrorMessages.Extension {
map.put(SUPERTYPE_INITIALIZED_WITHOUT_PRIMARY_CONSTRUCTOR, "Supertype initialization is impossible without primary constructor")
map.put(DELEGATION_SUPER_CALL_IN_ENUM_CONSTRUCTOR, "Call to super is not allowed in enum constructor")
map.put(PRIMARY_CONSTRUCTOR_REQUIRED_FOR_DATA_CLASS, "Primary constructor required for data class")
map.put(EXPLICIT_DELEGATION_CALL_REQUIRED, "Explicit 'this' or 'super' call is required. There is no constructor in superclass that can be called without arguments")
map.put(
EXPLICIT_DELEGATION_CALL_REQUIRED,
"Explicit 'this' or 'super' call is required. There is no constructor in superclass that can be called without arguments"
)
// Annotations
map.put(ANNOTATION_CLASS_MEMBER, "Members are not allowed in annotation class")
map.put(ANNOTATION_PARAMETER_DEFAULT_VALUE_MUST_BE_CONSTANT, "Default value of annotation parameter must be a compile-time constant")
map.put(
ANNOTATION_PARAMETER_DEFAULT_VALUE_MUST_BE_CONSTANT,
"Default value of annotation parameter must be a compile-time constant"
)
map.put(LOCAL_ANNOTATION_CLASS_ERROR, "Annotation class cannot be local")
map.put(MISSING_VAL_ON_ANNOTATION_PARAMETER, "'val' keyword is missing on annotation parameter")
map.put(NULLABLE_TYPE_OF_ANNOTATION_MEMBER, "An annotation parameter cannot be nullable")
@@ -157,14 +169,32 @@ class FirDefaultErrorMessages : DefaultErrorMessages.Extension {
map.put(NOT_AN_ANNOTATION_CLASS, "Illegal annotation class: {0}", NULLABLE_STRING)
// Exposed visibility group // #
map.put(EXPOSED_TYPEALIAS_EXPANDED_TYPE, "{0} typealias exposes {2} in expanded type ''{1}''", TO_STRING, DECLARATION_NAME, TO_STRING)
map.put(EXPOSED_FUNCTION_RETURN_TYPE, "{0} function exposes its {2} return type ''{1}''", TO_STRING, DECLARATION_NAME, TO_STRING)
map.put(
EXPOSED_TYPEALIAS_EXPANDED_TYPE,
"{0} typealias exposes {2} in expanded type ''{1}''",
TO_STRING,
DECLARATION_NAME,
TO_STRING
)
map.put(
EXPOSED_FUNCTION_RETURN_TYPE,
"{0} function exposes its {2} return type ''{1}''",
TO_STRING,
DECLARATION_NAME,
TO_STRING
)
map.put(EXPOSED_RECEIVER_TYPE, "{0} member exposes its {2} receiver type ''{1}''", TO_STRING, DECLARATION_NAME, TO_STRING)
map.put(EXPOSED_PROPERTY_TYPE, "{0} property exposes its {2} type ''{1}''", TO_STRING, DECLARATION_NAME, TO_STRING)
map.put(EXPOSED_PARAMETER_TYPE, "{0} function exposes its {2} parameter type ''{1}''", TO_STRING, DECLARATION_NAME, TO_STRING)
map.put(EXPOSED_SUPER_INTERFACE, "{0} sub-interface exposes its {2} supertype ''{1}''", TO_STRING, DECLARATION_NAME, TO_STRING)
map.put(EXPOSED_SUPER_CLASS, "{0} subclass exposes its {2} supertype ''{1}''", TO_STRING, DECLARATION_NAME, TO_STRING)
map.put(EXPOSED_TYPE_PARAMETER_BOUND, "{0} generic exposes its {2} parameter bound type ''{1}''", TO_STRING, DECLARATION_NAME, TO_STRING)
map.put(
EXPOSED_TYPE_PARAMETER_BOUND,
"{0} generic exposes its {2} parameter bound type ''{1}''",
TO_STRING,
DECLARATION_NAME,
TO_STRING
)
// Modifiers
map.put(INAPPLICABLE_INFIX_MODIFIER, "''infix'' modifier is inapplicable on this function: {0}", TO_STRING)
@@ -188,7 +218,12 @@ class FirDefaultErrorMessages : DefaultErrorMessages.Extension {
map.put(PROJECTION_ON_NON_CLASS_TYPE_ARGUMENT, "Projections are not allowed on type arguments of functions and properties")
map.put(UPPER_BOUND_VIOLATED, "Type argument is not within its bounds: should be subtype of ''{0}''", TO_STRING, TO_STRING)
map.put(TYPE_ARGUMENTS_NOT_ALLOWED, "Type arguments are not allowed for type parameters") // *
map.put(WRONG_NUMBER_OF_TYPE_ARGUMENTS, "{0,choice,0#No type arguments|1#One type argument|1<{0,number,integer} type arguments} expected for {1}", null, TO_STRING)
map.put(
WRONG_NUMBER_OF_TYPE_ARGUMENTS,
"{0,choice,0#No type arguments|1#One type argument|1<{0,number,integer} type arguments} expected for {1}",
null,
TO_STRING
)
map.put(NO_TYPE_FOR_TYPE_PARAMETER, "There're no types suitable for this type parameter") // &
map.put(TYPE_PARAMETERS_IN_OBJECT, "Type parameters are not allowed for objects")
// map.put(ILLEGAL_PROJECTION_USAGE, ...) // &
@@ -199,11 +234,28 @@ class FirDefaultErrorMessages : DefaultErrorMessages.Extension {
map.put(REDECLARATION, "Conflicting declarations: {0}", TO_STRING) // *
// Invalid local declarations
map.put(LOCAL_OBJECT_NOT_ALLOWED, "Named object ''{0}'' is a singleton and cannot be local. Try to use anonymous object instead", TO_STRING) // +
map.put(LOCAL_INTERFACE_NOT_ALLOWED, "''{0}'' is an interface so it cannot be local. Try to use anonymous object or abstract class instead", TO_STRING)
map.put(
LOCAL_OBJECT_NOT_ALLOWED,
"Named object ''{0}'' is a singleton and cannot be local. Try to use anonymous object instead",
TO_STRING
) // +
map.put(
LOCAL_INTERFACE_NOT_ALLOWED,
"''{0}'' is an interface so it cannot be local. Try to use anonymous object or abstract class instead",
TO_STRING
)
// Control flow diagnostics
map.put(UNINITIALIZED_VARIABLE, "{0} must be initialized before access", PROPERTY_NAME)
map.put(
WRONG_INVOCATION_KIND,
"{2} wrong invocation kind: given {3} case, but {4} case is possible",
SYMBOL,
TO_STRING,
TO_STRING
)
map.put(LEAKED_IN_PLACE_LAMBDA, "Leaked in-place lambda: {2}", SYMBOL)
// Extended checkers group
// map.put(REDUNDANT_VISIBILITY_MODIFIER, ...) // &
@@ -7,12 +7,14 @@ package org.jetbrains.kotlin.fir.analysis.diagnostics
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiTypeElement
import org.jetbrains.kotlin.contracts.description.EventOccurrencesRange
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.fir.FirEffectiveVisibility
import org.jetbrains.kotlin.fir.FirSourceElement
import org.jetbrains.kotlin.fir.declarations.FirClass
import org.jetbrains.kotlin.fir.declarations.FirMemberDeclaration
import org.jetbrains.kotlin.fir.symbols.AbstractFirBasedSymbol
import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirPropertySymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirRegularClassSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirTypeParameterSymbol
@@ -127,6 +129,8 @@ object FirErrors {
// Control flow diagnostics
val UNINITIALIZED_VARIABLE by error1<FirSourceElement, PsiElement, FirPropertySymbol>()
val WRONG_INVOCATION_KIND by warning3<FirSourceElement, PsiElement, AbstractFirBasedSymbol<*>, EventOccurrencesRange, EventOccurrencesRange>()
val LEAKED_IN_PLACE_LAMBDA by error1<FirSourceElement, PsiElement, AbstractFirBasedSymbol<*>>()
// Extended checkers group
val REDUNDANT_VISIBILITY_MODIFIER by warning0<FirSourceElement, PsiElement>()
@@ -38,6 +38,7 @@ enum class EventOccurrencesRange(private val left: Int, private val right: Int)
infix fun or(other: EventOccurrencesRange): EventOccurrencesRange = Companion.or(this, other)
operator fun plus(other: EventOccurrencesRange): EventOccurrencesRange = Companion.plus(this, other)
operator fun contains(other: EventOccurrencesRange): Boolean = left <= other.left && other.right <= right
}
fun EventOccurrencesRange.isDefinitelyVisited(): Boolean = this == EventOccurrencesRange.EXACTLY_ONCE || this == EventOccurrencesRange.AT_LEAST_ONCE || this == EventOccurrencesRange.MORE_THAN_ONCE
@@ -6,9 +6,9 @@ import kotlin.contracts.*
inline fun <R> callIt(fn: () -> R): R = TODO()
inline fun <R> callItContracted(fn: () -> R): R {
contract {
<!WRONG_INVOCATION_KIND!>contract {
callsInPlace(fn, InvocationKind.EXACTLY_ONCE)
}
}<!>
TODO()
}
@@ -0,0 +1,25 @@
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// FILE: contracts.kt
package contracts
import kotlin.contracts.*
// TESTCASE NUMBER: 1
fun case_1(x: Double = 1.0, block: () -> Unit): Double {
<!WRONG_INVOCATION_KIND!>contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }<!>
return x
}
// FILE: main.kt
import contracts.*
// TESTCASE NUMBER: 1
fun case_1() {
val value_1: Int
contracts.case_1 { value_1 = 10 }
value_1.inc()
}
@@ -1,4 +1,3 @@
// FIR_IDENTICAL
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
/*
@@ -8,7 +8,7 @@ import kotlin.contracts.*
// TESTCASE NUMBER: 1
fun case_1(x: Double = 1.0, block: () -> Unit): Double {
contract { callsInPlace(block, InvocationKind.AT_LEAST_ONCE) }
<!WRONG_INVOCATION_KIND!>contract { callsInPlace(block, InvocationKind.AT_LEAST_ONCE) }<!>
return x
}
@@ -4,7 +4,7 @@ import kotlin.contracts.*
// TESTCASE NUMBER: 1
inline fun case_1(block: () -> Unit) {
contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }
<!WRONG_INVOCATION_KIND!>contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }<!>
block()
case_1(block)
}
@@ -27,9 +27,9 @@ var Boolean.case_3: () -> Unit
return {}
}
set(value) {
contract {
<!WRONG_INVOCATION_KIND!>contract {
callsInPlace(value, InvocationKind.EXACTLY_ONCE)
}
}<!>
}
// TESTCASE NUMBER: 4