FIR CFG: when unifying flows, group statements by assignment

Consider a function `run2` that has 2 lambda arguments called in place.
We don't know the order in which they're called, so here:

    var x: Any? = something
    run2(
      { x = null },
      { x as String },
    )
    // <--

it's not correct to simply `&&` the statements together, as that would
produce `x is Nothing? && x is String && x is Any?`. Instead, statements
should be grouped by assignment first, and different groups are `||`-ed.
This means in the above example we now get `x is Nothing? || (x is Any?
&& x is String)` == `x is String?`.
This commit is contained in:
pyos
2022-06-15 11:20:48 +02:00
committed by teamcity
parent 25f66b4e0e
commit c2ae74c7cd
11 changed files with 514 additions and 51 deletions
@@ -0,0 +1,342 @@
digraph flowFromTwoInplaceLambdas_kt {
graph [nodesep=3]
node [shape=box penwidth=2]
edge [penwidth=2]
subgraph cluster_0 {
color=red
0 [label="Enter function n" style="filled" fillcolor=red];
subgraph cluster_1 {
color=blue
1 [label="Enter block"];
2 [label="Const: Null(null)"];
3 [label="Jump: ^n Null(null)"];
4 [label="Stub" style="filled" fillcolor=gray];
5 [label="Exit block" style="filled" fillcolor=gray];
}
6 [label="Exit function n" style="filled" fillcolor=red];
}
0 -> {1};
1 -> {2};
2 -> {3};
3 -> {6};
3 -> {4} [style=dotted];
4 -> {5} [style=dotted];
5 -> {6} [style=dotted];
subgraph cluster_2 {
color=red
7 [label="Enter function run2" style="filled" fillcolor=red];
subgraph cluster_3 {
color=blue
8 [label="Enter block"];
9 [label="Function call: R|<local>/x|.R|SubstitutionOverride<kotlin/Function0.invoke: R|T|>|()"];
10 [label="Function call: R|<local>/y|.R|SubstitutionOverride<kotlin/Function0.invoke: R|T|>|()"];
11 [label="Exit block"];
}
12 [label="Exit function run2" style="filled" fillcolor=red];
}
7 -> {8};
8 -> {9};
9 -> {10};
10 -> {11};
11 -> {12};
subgraph cluster_4 {
color=red
13 [label="Enter function test1" style="filled" fillcolor=red];
subgraph cluster_5 {
color=blue
14 [label="Enter block"];
15 [label="Access variable R|<local>/x|"];
16 [label="Variable declaration: lvar p: R|kotlin/String?|"];
subgraph cluster_6 {
color=blue
17 [label="Enter when"];
subgraph cluster_7 {
color=blue
18 [label="Enter when branch condition "];
19 [label="Access variable R|<local>/p|"];
20 [label="Const: Null(null)"];
21 [label="Equality operator !="];
22 [label="Exit when branch condition"];
}
23 [label="Synthetic else branch"];
24 [label="Enter when branch result"];
subgraph cluster_8 {
color=blue
25 [label="Enter block"];
26 [label="Postponed enter to lambda"];
subgraph cluster_9 {
color=blue
39 [label="Enter function anonymousFunction" style="filled" fillcolor=red];
subgraph cluster_10 {
color=blue
40 [label="Enter block"];
41 [label="Const: Null(null)"];
42 [label="Assignment: R|<local>/p|"];
43 [label="Function call: R|/n|<R|kotlin/Int?|>()"];
44 [label="Exit block"];
}
45 [label="Exit function anonymousFunction" style="filled" fillcolor=red];
}
28 [label="Postponed exit from lambda"];
29 [label="Postponed enter to lambda"];
subgraph cluster_11 {
color=blue
46 [label="Enter function anonymousFunction" style="filled" fillcolor=red];
subgraph cluster_12 {
color=blue
47 [label="Enter block"];
48 [label="Access variable R|<local>/p|"];
49 [label="Access variable <Inapplicable(UNSTABLE_SMARTCAST): kotlin/String.length>#"];
50 [label="Const: Int(123)"];
51 [label="Exit block"];
}
52 [label="Exit function anonymousFunction" style="filled" fillcolor=red];
}
27 [label="Call arguments union" style="filled" fillcolor=yellow];
30 [label="Postponed exit from lambda"];
31 [label="Function call: R|/run2|<R|kotlin/Int?|>(...)"];
32 [label="Access variable R|<local>/p|"];
33 [label="Access variable <Inapplicable(UNSAFE_CALL): kotlin/String.length>#"];
34 [label="Exit block"];
}
35 [label="Exit when branch result"];
36 [label="Exit when"];
}
37 [label="Exit block"];
}
38 [label="Exit function test1" style="filled" fillcolor=red];
}
13 -> {14};
14 -> {15};
15 -> {16};
16 -> {17};
17 -> {18};
18 -> {19};
19 -> {20};
20 -> {21};
21 -> {22};
22 -> {24 23};
23 -> {36};
24 -> {25};
25 -> {26};
26 -> {39};
26 -> {28} [color=red];
26 -> {39} [style=dashed];
27 -> {31} [color=red];
28 -> {29};
29 -> {46};
29 -> {30} [color=red];
29 -> {46} [style=dashed];
30 -> {31} [color=green];
31 -> {32};
32 -> {33};
33 -> {34};
34 -> {35};
35 -> {36};
36 -> {37};
37 -> {38};
39 -> {40};
40 -> {41};
41 -> {42};
42 -> {43};
43 -> {44};
44 -> {45};
45 -> {27} [color=red];
45 -> {28} [color=green];
46 -> {47};
47 -> {48};
48 -> {49};
49 -> {50};
50 -> {51};
51 -> {52};
52 -> {27} [color=red];
52 -> {30} [color=green];
subgraph cluster_13 {
color=red
53 [label="Enter function test2" style="filled" fillcolor=red];
subgraph cluster_14 {
color=blue
54 [label="Enter block"];
55 [label="Access variable R|<local>/x|"];
56 [label="Variable declaration: lvar p: R|kotlin/Any?|"];
57 [label="Access variable R|<local>/p|"];
58 [label="Access variable <Unresolved name: length>#"];
59 [label="Postponed enter to lambda"];
subgraph cluster_15 {
color=blue
73 [label="Enter function anonymousFunction" style="filled" fillcolor=red];
subgraph cluster_16 {
color=blue
74 [label="Enter block"];
75 [label="Const: Null(null)"];
76 [label="Assignment: R|<local>/p|"];
77 [label="Function call: R|/n|<R|kotlin/Int?|>()"];
78 [label="Exit block"];
}
79 [label="Exit function anonymousFunction" style="filled" fillcolor=red];
}
61 [label="Postponed exit from lambda"];
62 [label="Postponed enter to lambda"];
subgraph cluster_17 {
color=blue
80 [label="Enter function anonymousFunction" style="filled" fillcolor=red];
subgraph cluster_18 {
color=blue
81 [label="Enter block"];
82 [label="Access variable R|<local>/p|"];
83 [label="Type operator: (R|<local>/p| as R|kotlin/String|)"];
84 [label="Const: Int(123)"];
85 [label="Exit block"];
}
86 [label="Exit function anonymousFunction" style="filled" fillcolor=red];
}
60 [label="Call arguments union" style="filled" fillcolor=yellow];
63 [label="Postponed exit from lambda"];
64 [label="Function call: R|/run2|<R|kotlin/Int?|>(...)"];
65 [label="Access variable R|<local>/p|"];
66 [label="Access variable <Inapplicable(UNSAFE_CALL): kotlin/String.length>#"];
67 [label="Access variable R|<local>/p|"];
68 [label="Enter safe call"];
69 [label="Access variable R|kotlin/String.length|"];
70 [label="Exit safe call"];
71 [label="Exit block"];
}
72 [label="Exit function test2" style="filled" fillcolor=red];
}
53 -> {54};
54 -> {55};
55 -> {56};
56 -> {57};
57 -> {58};
58 -> {59};
59 -> {73};
59 -> {61} [color=red];
59 -> {73} [style=dashed];
60 -> {64} [color=red];
61 -> {62};
62 -> {80};
62 -> {63} [color=red];
62 -> {80} [style=dashed];
63 -> {64} [color=green];
64 -> {65};
65 -> {66};
66 -> {67};
67 -> {68 70};
68 -> {69};
69 -> {70};
70 -> {71};
71 -> {72};
73 -> {74};
74 -> {75};
75 -> {76};
76 -> {77};
77 -> {78};
78 -> {79};
79 -> {60} [color=red];
79 -> {61} [color=green];
80 -> {81};
81 -> {82};
82 -> {83};
83 -> {84};
84 -> {85};
85 -> {86};
86 -> {60} [color=red];
86 -> {63} [color=green];
subgraph cluster_19 {
color=red
87 [label="Enter function test3" style="filled" fillcolor=red];
subgraph cluster_20 {
color=blue
88 [label="Enter block"];
89 [label="Access variable R|<local>/x|"];
90 [label="Variable declaration: lvar p: R|kotlin/Any?|"];
91 [label="Access variable R|<local>/p|"];
92 [label="Access variable <Unresolved name: length>#"];
93 [label="Postponed enter to lambda"];
subgraph cluster_21 {
color=blue
107 [label="Enter function anonymousFunction" style="filled" fillcolor=red];
subgraph cluster_22 {
color=blue
108 [label="Enter block"];
109 [label="Const: Null(null)"];
110 [label="Assignment: R|<local>/p|"];
111 [label="Function call: R|/n|<R|kotlin/Int?|>()"];
112 [label="Exit block"];
}
113 [label="Exit function anonymousFunction" style="filled" fillcolor=red];
}
95 [label="Postponed exit from lambda"];
96 [label="Postponed enter to lambda"];
subgraph cluster_23 {
color=blue
114 [label="Enter function anonymousFunction" style="filled" fillcolor=red];
subgraph cluster_24 {
color=blue
115 [label="Enter block"];
116 [label="Const: String()"];
117 [label="Assignment: R|<local>/p|"];
118 [label="Const: Int(123)"];
119 [label="Exit block"];
}
120 [label="Exit function anonymousFunction" style="filled" fillcolor=red];
}
94 [label="Call arguments union" style="filled" fillcolor=yellow];
97 [label="Postponed exit from lambda"];
98 [label="Function call: R|/run2|<R|kotlin/Int?|>(...)"];
99 [label="Access variable R|<local>/p|"];
100 [label="Access variable <Inapplicable(UNSAFE_CALL): kotlin/String.length>#"];
101 [label="Access variable R|<local>/p|"];
102 [label="Enter safe call"];
103 [label="Access variable R|kotlin/String.length|"];
104 [label="Exit safe call"];
105 [label="Exit block"];
}
106 [label="Exit function test3" style="filled" fillcolor=red];
}
87 -> {88};
88 -> {89};
89 -> {90};
90 -> {91};
91 -> {92};
92 -> {93};
93 -> {107};
93 -> {95} [color=red];
93 -> {107} [style=dashed];
94 -> {98} [color=red];
95 -> {96};
96 -> {114};
96 -> {97} [color=red];
96 -> {114} [style=dashed];
97 -> {98} [color=green];
98 -> {99};
99 -> {100};
100 -> {101};
101 -> {102 104};
102 -> {103};
103 -> {104};
104 -> {105};
105 -> {106};
107 -> {108};
108 -> {109};
109 -> {110};
110 -> {111};
111 -> {112};
112 -> {113};
113 -> {94} [color=red];
113 -> {95} [color=green];
114 -> {115};
115 -> {116};
116 -> {117};
117 -> {118};
118 -> {119};
119 -> {120};
120 -> {94} [color=red];
120 -> {97} [color=green];
}