[FIR] Resolve elvis call as special synthetic call

Before that commit we desugared `a ?: b` as

when (val elvis = a) {
    null -> b
    else -> elvis
}

It was incorrect, because `a` should be resolved in dependent mode,
  but when it was `elvis` initializer it was resolved in independent
  mode, so we can't infer type for `a` in some complex cases
This commit is contained in:
Dmitriy Novozhilov
2020-06-30 15:31:24 +03:00
parent b49b3245af
commit 102c9c08d0
48 changed files with 1560 additions and 1828 deletions
@@ -36,122 +36,102 @@ digraph elvis_kt {
subgraph cluster_6 {
color=blue
9 [label="Enter when branch condition "];
subgraph cluster_7 {
color=blue
10 [label="Enter when"];
11 [label="Access variable R|<local>/x|"];
12 [label="Enter safe call"];
13 [label="Access variable R|/A.b|"];
14 [label="Exit safe call"];
15 [label="Variable declaration: lval <elvis>: R|kotlin/Boolean?|"];
subgraph cluster_8 {
color=blue
16 [label="Enter when branch condition "];
17 [label="Const: Null(null)"];
18 [label="Operator =="];
19 [label="Exit when branch condition"];
}
subgraph cluster_9 {
color=blue
20 [label="Enter when branch condition else"];
21 [label="Exit when branch condition"];
}
22 [label="Enter when branch result"];
subgraph cluster_10 {
color=blue
23 [label="Enter block"];
24 [label="Access variable R|<local>/<elvis>|"];
25 [label="Exit block"];
}
26 [label="Exit when branch result"];
27 [label="Enter when branch result"];
subgraph cluster_11 {
color=blue
28 [label="Enter block"];
29 [label="Jump: ^test_1 Unit"];
30 [label="Stub" style="filled" fillcolor=gray];
31 [label="Exit block" style="filled" fillcolor=gray];
}
32 [label="Exit when branch result" style="filled" fillcolor=gray];
33 [label="Exit when"];
}
34 [label="Exit when branch condition"];
10 [label="Access variable R|<local>/x|"];
11 [label="Enter safe call"];
12 [label="Access variable R|/A.b|"];
13 [label="Exit safe call"];
14 [label="Exit lhs of ?:"];
15 [label="Enter rhs of ?:"];
16 [label="Jump: ^test_1 Unit"];
17 [label="Stub" style="filled" fillcolor=gray];
18 [label="Lhs of ?: is not null"];
19 [label="Exit ?:"];
20 [label="Exit when branch condition"];
}
35 [label="Synthetic else branch"];
36 [label="Enter when branch result"];
subgraph cluster_12 {
21 [label="Synthetic else branch"];
22 [label="Enter when branch result"];
subgraph cluster_7 {
color=blue
37 [label="Enter block"];
38 [label="Access variable R|<local>/x|"];
39 [label="Function call: R|<local>/x|.R|/A.foo|()"];
40 [label="Exit block"];
23 [label="Enter block"];
24 [label="Access variable R|<local>/x|"];
25 [label="Function call: R|<local>/x|.R|/A.foo|()"];
26 [label="Exit block"];
}
41 [label="Exit when branch result"];
42 [label="Exit when"];
27 [label="Exit when branch result"];
28 [label="Exit when"];
}
43 [label="Exit block"];
29 [label="Exit block"];
}
44 [label="Exit function test_1" style="filled" fillcolor=red];
30 [label="Exit function test_1" style="filled" fillcolor=red];
}
6 -> {7};
7 -> {8};
8 -> {9};
9 -> {10};
10 -> {11};
11 -> {12 14};
10 -> {11 13};
11 -> {12};
12 -> {13};
13 -> {14};
14 -> {15};
14 -> {18 15};
15 -> {16};
16 -> {17};
17 -> {18};
16 -> {30};
16 -> {17} [style=dotted];
17 -> {19} [style=dotted];
18 -> {19};
19 -> {27 20};
20 -> {21};
21 -> {22};
19 -> {20};
20 -> {22 21};
21 -> {28};
22 -> {23};
23 -> {24};
24 -> {25};
25 -> {26};
26 -> {33};
26 -> {27};
27 -> {28};
28 -> {29};
29 -> {44};
29 -> {30} [style=dotted];
30 -> {31} [style=dotted];
31 -> {32} [style=dotted];
32 -> {33} [style=dotted];
33 -> {34};
34 -> {36 35};
35 -> {42};
36 -> {37};
37 -> {38};
38 -> {39};
39 -> {40};
40 -> {41};
41 -> {42};
42 -> {43};
43 -> {44};
29 -> {30};
subgraph cluster_13 {
subgraph cluster_8 {
color=red
45 [label="Enter function test2" style="filled" fillcolor=red];
subgraph cluster_14 {
31 [label="Enter function test2" style="filled" fillcolor=red];
subgraph cluster_9 {
color=blue
46 [label="Enter block"];
subgraph cluster_15 {
32 [label="Enter block"];
subgraph cluster_10 {
color=blue
33 [label="Enter when"];
subgraph cluster_11 {
color=blue
34 [label="Enter when branch condition "];
35 [label="Access variable R|<local>/b|"];
36 [label="Type operator: (R|<local>/b| !is R|kotlin/String|)"];
37 [label="Exit when branch condition"];
}
38 [label="Synthetic else branch"];
39 [label="Enter when branch result"];
subgraph cluster_12 {
color=blue
40 [label="Enter block"];
41 [label="Const: String()"];
42 [label="Jump: ^test2 String()"];
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="Exit when"];
}
subgraph cluster_13 {
color=blue
47 [label="Enter when"];
subgraph cluster_16 {
subgraph cluster_14 {
color=blue
48 [label="Enter when branch condition "];
49 [label="Access variable R|<local>/b|"];
50 [label="Type operator: (R|<local>/b| !is R|kotlin/String|)"];
49 [label="Access variable R|<local>/a|"];
50 [label="Type operator: (R|<local>/a| !is R|kotlin/String?|)"];
51 [label="Exit when branch condition"];
}
52 [label="Synthetic else branch"];
53 [label="Enter when branch result"];
subgraph cluster_17 {
subgraph cluster_15 {
color=blue
54 [label="Enter block"];
55 [label="Const: String()"];
@@ -162,79 +142,34 @@ digraph elvis_kt {
59 [label="Exit when branch result" style="filled" fillcolor=gray];
60 [label="Exit when"];
}
subgraph cluster_18 {
color=blue
61 [label="Enter when"];
subgraph cluster_19 {
color=blue
62 [label="Enter when branch condition "];
63 [label="Access variable R|<local>/a|"];
64 [label="Type operator: (R|<local>/a| !is R|kotlin/String?|)"];
65 [label="Exit when branch condition"];
}
66 [label="Synthetic else branch"];
67 [label="Enter when branch result"];
subgraph cluster_20 {
color=blue
68 [label="Enter block"];
69 [label="Const: String()"];
70 [label="Jump: ^test2 String()"];
71 [label="Stub" style="filled" fillcolor=gray];
72 [label="Exit block" style="filled" fillcolor=gray];
}
73 [label="Exit when branch result" style="filled" fillcolor=gray];
74 [label="Exit when"];
}
subgraph cluster_21 {
color=blue
75 [label="Enter when"];
76 [label="Access variable R|<local>/a|"];
77 [label="Variable declaration: lval <elvis>: R|kotlin/String?|"];
subgraph cluster_22 {
color=blue
78 [label="Enter when branch condition "];
79 [label="Const: Null(null)"];
80 [label="Operator =="];
81 [label="Exit when branch condition"];
}
subgraph cluster_23 {
color=blue
82 [label="Enter when branch condition else"];
83 [label="Exit when branch condition"];
}
84 [label="Enter when branch result"];
subgraph cluster_24 {
color=blue
85 [label="Enter block"];
86 [label="Access variable R|<local>/<elvis>|"];
87 [label="Exit block"];
}
88 [label="Exit when branch result"];
89 [label="Enter when branch result"];
subgraph cluster_25 {
color=blue
90 [label="Enter block"];
91 [label="Access variable R|<local>/b|"];
92 [label="Exit block"];
}
93 [label="Exit when branch result"];
94 [label="Exit when"];
}
95 [label="Jump: ^test2 when (lval <elvis>: R|kotlin/String?| = R|<local>/a|) {
==($subj$, Null(null)) -> {
R|<local>/b|
}
else -> {
R|<local>/<elvis>|
}
}
"];
96 [label="Stub" style="filled" fillcolor=gray];
97 [label="Exit block" style="filled" fillcolor=gray];
61 [label="Access variable R|<local>/a|"];
62 [label="Exit lhs of ?:"];
63 [label="Enter rhs of ?:"];
64 [label="Access variable R|<local>/b|"];
65 [label="Lhs of ?: is not null"];
66 [label="Exit ?:"];
67 [label="Jump: ^test2 R|<local>/a| ?: R|<local>/b|"];
68 [label="Stub" style="filled" fillcolor=gray];
69 [label="Exit block" style="filled" fillcolor=gray];
}
98 [label="Exit function test2" style="filled" fillcolor=red];
70 [label="Exit function test2" style="filled" fillcolor=red];
}
45 -> {46};
31 -> {32};
32 -> {33};
33 -> {34};
34 -> {35};
35 -> {36};
36 -> {37};
37 -> {39 38};
38 -> {46};
39 -> {40};
40 -> {41};
41 -> {42};
42 -> {70};
42 -> {43} [style=dotted];
43 -> {44} [style=dotted];
44 -> {45} [style=dotted];
45 -> {46} [style=dotted];
46 -> {47};
47 -> {48};
48 -> {49};
@@ -245,50 +180,21 @@ digraph elvis_kt {
53 -> {54};
54 -> {55};
55 -> {56};
56 -> {98};
56 -> {70};
56 -> {57} [style=dotted];
57 -> {58} [style=dotted];
58 -> {59} [style=dotted];
59 -> {60} [style=dotted];
60 -> {61};
61 -> {62};
62 -> {63};
62 -> {65 63};
63 -> {64};
64 -> {65};
65 -> {67 66};
66 -> {74};
67 -> {68};
68 -> {69};
69 -> {70};
70 -> {98};
70 -> {71} [style=dotted];
71 -> {72} [style=dotted];
72 -> {73} [style=dotted];
73 -> {74} [style=dotted];
74 -> {75};
75 -> {76};
76 -> {77};
77 -> {78};
78 -> {79};
79 -> {80};
80 -> {81};
81 -> {89 82};
82 -> {83};
83 -> {84};
84 -> {85};
85 -> {86};
86 -> {87};
87 -> {88};
88 -> {94};
89 -> {90};
90 -> {91};
91 -> {92};
92 -> {93};
93 -> {94};
94 -> {95};
95 -> {98};
95 -> {96} [style=dotted];
96 -> {97} [style=dotted];
97 -> {98} [style=dotted];
64 -> {66};
65 -> {66};
66 -> {67};
67 -> {70};
67 -> {68} [style=dotted];
68 -> {69} [style=dotted];
69 -> {70} [style=dotted];
}
@@ -8,15 +8,7 @@ FILE: elvis.kt
}
public final fun test_1(x: R|A?|): R|kotlin/Unit| {
when () {
when (lval <elvis>: R|kotlin/Boolean?| = R|<local>/x|?.{ $subj$.R|/A.b| }) {
==($subj$, Null(null)) -> {
^test_1 Unit
}
else -> {
R|<local>/<elvis>|
}
}
-> {
R|<local>/x|?.{ $subj$.R|/A.b| } ?: ^test_1 Unit -> {
R|<local>/x|.R|/A.foo|()
}
}
@@ -35,13 +27,5 @@ FILE: elvis.kt
}
}
^test2 when (lval <elvis>: R|kotlin/String?| = R|<local>/a|) {
==($subj$, Null(null)) -> {
R|<local>/b|
}
else -> {
R|<local>/<elvis>|
}
}
^test2 R|<local>/a| ?: R|<local>/b|
}
@@ -434,104 +434,61 @@ digraph returns_kt {
151 [label="Access variable R|<local>/a|"];
152 [label="Type operator: (R|<local>/a| as? R|kotlin/String|)"];
153 [label="Variable declaration: lval s: R|kotlin/String?|"];
154 [label="Access variable R|<local>/s|"];
155 [label="Enter safe call"];
156 [label="Access variable R|/ext|"];
157 [label="Exit safe call"];
158 [label="Exit lhs of ?:"];
159 [label="Enter rhs of ?:"];
160 [label="Jump: ^test_4 Unit"];
161 [label="Stub" style="filled" fillcolor=gray];
162 [label="Lhs of ?: is not null"];
163 [label="Exit ?:"];
164 [label="Variable declaration: lval length: R|kotlin/Int|"];
165 [label="Postponed enter to lambda"];
subgraph cluster_42 {
color=blue
154 [label="Enter when"];
155 [label="Access variable R|<local>/s|"];
156 [label="Enter safe call"];
157 [label="Access variable R|/ext|"];
158 [label="Exit safe call"];
159 [label="Variable declaration: lval <elvis>: R|kotlin/Int?|"];
170 [label="Enter function anonymousFunction" style="filled" fillcolor=red];
subgraph cluster_43 {
color=blue
160 [label="Enter when branch condition "];
161 [label="Const: Null(null)"];
162 [label="Operator =="];
163 [label="Exit when branch condition"];
171 [label="Enter block"];
172 [label="Access variable R|<local>/s|"];
173 [label="Access variable R|kotlin/String.length|"];
174 [label="Exit block"];
}
subgraph cluster_44 {
color=blue
164 [label="Enter when branch condition else"];
165 [label="Exit when branch condition"];
}
166 [label="Enter when branch result"];
subgraph cluster_45 {
color=blue
167 [label="Enter block"];
168 [label="Access variable R|<local>/<elvis>|"];
169 [label="Exit block"];
}
170 [label="Exit when branch result"];
171 [label="Enter when branch result"];
subgraph cluster_46 {
color=blue
172 [label="Enter block"];
173 [label="Jump: ^test_4 Unit"];
174 [label="Stub" style="filled" fillcolor=gray];
175 [label="Exit block" style="filled" fillcolor=gray];
}
176 [label="Exit when branch result" style="filled" fillcolor=gray];
177 [label="Exit when"];
175 [label="Exit function anonymousFunction" style="filled" fillcolor=red];
}
178 [label="Variable declaration: lval length: R|kotlin/Int|"];
179 [label="Postponed enter to lambda"];
subgraph cluster_47 {
color=blue
184 [label="Enter function anonymousFunction" style="filled" fillcolor=red];
subgraph cluster_48 {
color=blue
185 [label="Enter block"];
186 [label="Access variable R|<local>/s|"];
187 [label="Access variable R|kotlin/String.length|"];
188 [label="Exit block"];
}
189 [label="Exit function anonymousFunction" style="filled" fillcolor=red];
}
180 [label="Postponed exit from lambda"];
181 [label="Function call: R|/runHigherOrder|<R|kotlin/Int|>(...)"];
182 [label="Exit block"];
166 [label="Postponed exit from lambda"];
167 [label="Function call: R|/runHigherOrder|<R|kotlin/Int|>(...)"];
168 [label="Exit block"];
}
183 [label="Exit function test_4" style="filled" fillcolor=red];
169 [label="Exit function test_4" style="filled" fillcolor=red];
}
149 -> {150};
150 -> {151};
151 -> {152};
152 -> {153};
153 -> {154};
154 -> {155};
155 -> {156 158};
154 -> {155 157};
155 -> {156};
156 -> {157};
157 -> {158};
158 -> {159};
158 -> {162 159};
159 -> {160};
160 -> {161};
161 -> {162};
160 -> {169};
160 -> {161} [style=dotted];
161 -> {163} [style=dotted];
162 -> {163};
163 -> {171 164};
163 -> {164};
164 -> {165};
165 -> {166};
165 -> {166 170};
166 -> {167};
167 -> {168};
168 -> {169};
169 -> {170};
170 -> {177};
170 -> {171};
171 -> {172};
172 -> {173};
173 -> {183};
173 -> {174} [style=dotted];
174 -> {175} [style=dotted];
175 -> {176} [style=dotted];
176 -> {177} [style=dotted];
177 -> {178};
178 -> {179};
179 -> {180 184};
180 -> {181};
181 -> {182};
182 -> {183};
184 -> {185};
185 -> {186};
186 -> {187};
187 -> {188};
188 -> {189};
173 -> {174};
174 -> {175};
}
@@ -74,15 +74,7 @@ FILE: returns.kt
}
public final fun test_4(a: R|kotlin/Any?|): R|kotlin/Unit| {
lval s: R|kotlin/String?| = (R|<local>/a| as? R|kotlin/String|)
lval length: R|kotlin/Int| = when (lval <elvis>: R|kotlin/Int?| = R|<local>/s|?.{ $subj$.R|/ext| }) {
==($subj$, Null(null)) -> {
^test_4 Unit
}
else -> {
R|<local>/<elvis>|
}
}
lval length: R|kotlin/Int| = R|<local>/s|?.{ $subj$.R|/ext| } ?: ^test_4 Unit
R|/runHigherOrder|<R|kotlin/Int|>(<L> = runHigherOrder@fun <anonymous>(): R|kotlin/Int| {
^ R|<local>/s|.R|kotlin/String.length|
}
@@ -50,61 +50,32 @@ digraph smartcastFromArgument_kt {
subgraph cluster_7 {
color=blue
14 [label="Enter when branch condition "];
subgraph cluster_8 {
color=blue
15 [label="Enter when"];
16 [label="Access variable R|<local>/a|"];
17 [label="Type operator: (R|<local>/a| as? R|A|)"];
18 [label="Variable declaration: lval <elvis>: R|A?|"];
subgraph cluster_9 {
color=blue
19 [label="Enter when branch condition "];
20 [label="Const: Null(null)"];
21 [label="Operator =="];
22 [label="Exit when branch condition"];
}
subgraph cluster_10 {
color=blue
23 [label="Enter when branch condition else"];
24 [label="Exit when branch condition"];
}
25 [label="Enter when branch result"];
subgraph cluster_11 {
color=blue
26 [label="Enter block"];
27 [label="Access variable R|<local>/<elvis>|"];
28 [label="Exit block"];
}
29 [label="Exit when branch result"];
30 [label="Enter when branch result"];
subgraph cluster_12 {
color=blue
31 [label="Enter block"];
32 [label="Jump: ^test Unit"];
33 [label="Stub" style="filled" fillcolor=gray];
34 [label="Exit block" style="filled" fillcolor=gray];
}
35 [label="Exit when branch result" style="filled" fillcolor=gray];
36 [label="Exit when"];
}
37 [label="Function call: R|/takeA|(...)"];
38 [label="Exit when branch condition"];
15 [label="Access variable R|<local>/a|"];
16 [label="Type operator: (R|<local>/a| as? R|A|)"];
17 [label="Exit lhs of ?:"];
18 [label="Enter rhs of ?:"];
19 [label="Jump: ^test Unit"];
20 [label="Stub" style="filled" fillcolor=gray];
21 [label="Lhs of ?: is not null"];
22 [label="Exit ?:"];
23 [label="Function call: R|/takeA|(...)"];
24 [label="Exit when branch condition"];
}
39 [label="Synthetic else branch"];
40 [label="Enter when branch result"];
subgraph cluster_13 {
25 [label="Synthetic else branch"];
26 [label="Enter when branch result"];
subgraph cluster_8 {
color=blue
41 [label="Enter block"];
42 [label="Access variable R|<local>/a|"];
43 [label="Function call: R|<local>/a|.R|/A.foo|()"];
44 [label="Exit block"];
27 [label="Enter block"];
28 [label="Access variable R|<local>/a|"];
29 [label="Function call: R|<local>/a|.R|/A.foo|()"];
30 [label="Exit block"];
}
45 [label="Exit when branch result"];
46 [label="Exit when"];
31 [label="Exit when branch result"];
32 [label="Exit when"];
}
47 [label="Exit block"];
33 [label="Exit block"];
}
48 [label="Exit function test" style="filled" fillcolor=red];
34 [label="Exit function test" style="filled" fillcolor=red];
}
11 -> {12};
12 -> {13};
@@ -112,37 +83,23 @@ digraph smartcastFromArgument_kt {
14 -> {15};
15 -> {16};
16 -> {17};
17 -> {18};
17 -> {21 18};
18 -> {19};
19 -> {20};
20 -> {21};
19 -> {34};
19 -> {20} [style=dotted];
20 -> {22} [style=dotted];
21 -> {22};
22 -> {30 23};
22 -> {23};
23 -> {24};
24 -> {25};
25 -> {26};
24 -> {26 25};
25 -> {32};
26 -> {27};
27 -> {28};
28 -> {29};
29 -> {36};
29 -> {30};
30 -> {31};
31 -> {32};
32 -> {48};
32 -> {33} [style=dotted];
33 -> {34} [style=dotted];
34 -> {35} [style=dotted];
35 -> {36} [style=dotted];
36 -> {37};
37 -> {38};
38 -> {40 39};
39 -> {46};
40 -> {41};
41 -> {42};
42 -> {43};
43 -> {44};
44 -> {45};
45 -> {46};
46 -> {47};
47 -> {48};
32 -> {33};
33 -> {34};
}
@@ -8,15 +8,7 @@ FILE: smartcastFromArgument.kt
}
public final fun test(a: R|kotlin/Any|): R|kotlin/Unit| {
when () {
R|/takeA|(when (lval <elvis>: R|A?| = (R|<local>/a| as? R|A|)) {
==($subj$, Null(null)) -> {
^test Unit
}
else -> {
R|<local>/<elvis>|
}
}
) -> {
R|/takeA|((R|<local>/a| as? R|A|) ?: ^test Unit) -> {
R|<local>/a|.R|/A.foo|()
}
}