[FIR] Add equality constraint from expected type for some synthetic function calls

This fixes some cases where we infer some type variable inside one
of the branches to Nothing instead of the expected type because Nothing
appeared in some other branch.

Specifically, we add an equality instead of a subtype constraint during
completion of calls to synthetic functions for if/when, try and !!.
We don't do it when the call contains a (possibly nested) elvis or is
inside the RHS of an assignment.
Otherwise, we would prevent some smart-casts.

#KT-65882 Fixed
This commit is contained in:
Kirill Rakhman
2024-02-21 16:06:47 +01:00
committed by Space Team
parent eaef7122f6
commit 69a7bf7f68
106 changed files with 1441 additions and 578 deletions
@@ -7,7 +7,7 @@ class Qux<T1, T2: Baz<T2, <!UPPER_BOUND_VIOLATED!>Bar<Foo, T2><!>>>(var f: T2)
class Quux<T> {
fun test(): Unit {
val x: Qux<in T, <!UPPER_BOUND_VIOLATED!>Baz<T, <!UPPER_BOUND_VIOLATED!>Bar<Foo, <!CONFLICTING_PROJECTION!>in<!> T><!>><!>> = null!!
val x: Qux<in T, <!UPPER_BOUND_VIOLATED!>Baz<T, <!UPPER_BOUND_VIOLATED!>Bar<Foo, <!CONFLICTING_PROJECTION!>in<!> T><!>><!>> = <!INITIALIZER_TYPE_MISMATCH!>null!!<!>
x.f = null!!
}
}
@@ -112,173 +112,173 @@ digraph smartcastToNothing_kt {
35 [label="Enter block"];
36 [label="Const: Null(null)"];
37 [label="Check not null: Null(null)!!" style="filled" fillcolor=yellow];
38 [label="Stub" style="filled" fillcolor=gray];
39 [label="Jump: ^myListOf Null(null)!!" style="filled" fillcolor=gray];
40 [label="Stub" style="filled" fillcolor=gray];
41 [label="Exit block" style="filled" fillcolor=gray];
38 [label="Jump: ^myListOf Null(null)!!"];
39 [label="Stub" style="filled" fillcolor=gray];
40 [label="Exit block" style="filled" fillcolor=gray];
}
42 [label="Exit function myListOf" style="filled" fillcolor=gray];
41 [label="Exit function myListOf" style="filled" fillcolor=red];
}
34 -> {35};
35 -> {36};
36 -> {37};
37 -> {38} [style=dotted];
37 -> {38};
38 -> {41};
38 -> {39} [style=dotted];
39 -> {40 42} [style=dotted];
39 -> {40} [style=dotted];
40 -> {41} [style=dotted];
41 -> {42} [style=dotted];
subgraph cluster_12 {
color=red
43 [label="Enter class A" style="filled" fillcolor=red];
42 [label="Enter class A" style="filled" fillcolor=red];
subgraph cluster_13 {
color=blue
44 [label="Enter function <init>" style="filled" fillcolor=red];
45 [label="Delegated constructor call: super<R|kotlin/Any|>()" style="filled" fillcolor=yellow];
46 [label="Exit function <init>" style="filled" fillcolor=red];
43 [label="Enter function <init>" style="filled" fillcolor=red];
44 [label="Delegated constructor call: super<R|kotlin/Any|>()" style="filled" fillcolor=yellow];
45 [label="Exit function <init>" style="filled" fillcolor=red];
}
subgraph cluster_14 {
color=blue
47 [label="Enter property" style="filled" fillcolor=red];
48 [label="Const: Int(1)"];
49 [label="Exit property" style="filled" fillcolor=red];
46 [label="Enter property" style="filled" fillcolor=red];
47 [label="Const: Int(1)"];
48 [label="Exit property" style="filled" fillcolor=red];
}
subgraph cluster_15 {
color=blue
50 [label="Enter property" style="filled" fillcolor=red];
51 [label="Const: Boolean(true)"];
52 [label="Exit property" style="filled" fillcolor=red];
49 [label="Enter property" style="filled" fillcolor=red];
50 [label="Const: Boolean(true)"];
51 [label="Exit property" style="filled" fillcolor=red];
}
53 [label="Exit class A" style="filled" fillcolor=red];
52 [label="Exit class A" style="filled" fillcolor=red];
}
43 -> {44} [color=green];
43 -> {53} [style=dotted];
43 -> {44 47 50} [style=dashed];
42 -> {43} [color=green];
42 -> {52} [style=dotted];
42 -> {43 46 49} [style=dashed];
43 -> {44};
44 -> {45};
45 -> {46};
46 -> {47} [color=green];
45 -> {46} [color=green];
46 -> {47};
47 -> {48};
48 -> {49};
49 -> {50} [color=green];
48 -> {49} [color=green];
49 -> {50};
50 -> {51};
51 -> {52};
52 -> {53} [color=green];
51 -> {52} [color=green];
subgraph cluster_16 {
color=red
54 [label="Enter function test_0" style="filled" fillcolor=red];
53 [label="Enter function test_0" style="filled" fillcolor=red];
subgraph cluster_17 {
color=blue
55 [label="Enter block"];
56 [label="Const: Null(null)"];
57 [label="Variable declaration: lvar s: R|A?|"];
54 [label="Enter block"];
55 [label="Const: Null(null)"];
56 [label="Variable declaration: lvar s: R|A?|"];
subgraph cluster_18 {
color=blue
58 [label="Enter block"];
57 [label="Enter block"];
subgraph cluster_19 {
color=blue
59 [label="Function call arguments enter"];
60 [label="Access variable R|<local>/results|"];
61 [label="Function call arguments exit"];
58 [label="Function call arguments enter"];
59 [label="Access variable R|<local>/results|"];
60 [label="Function call arguments exit"];
}
62 [label="Function call: R|<local>/results|.R|SubstitutionOverride<kotlin/collections/List.iterator: R|kotlin/collections/Iterator<kotlin/Nothing>|>|()" style="filled" fillcolor=yellow];
63 [label="Variable declaration: lval <iterator>: R|kotlin/collections/Iterator<kotlin/Nothing>|"];
61 [label="Function call: R|<local>/results|.R|SubstitutionOverride<kotlin/collections/List.iterator: R|kotlin/collections/Iterator<kotlin/Nothing>|>|()" style="filled" fillcolor=yellow];
62 [label="Variable declaration: lval <iterator>: R|kotlin/collections/Iterator<kotlin/Nothing>|"];
subgraph cluster_20 {
color=blue
64 [label="Enter while loop"];
63 [label="Enter while loop"];
subgraph cluster_21 {
color=blue
65 [label="Enter loop condition"];
64 [label="Enter loop condition"];
subgraph cluster_22 {
color=blue
66 [label="Function call arguments enter"];
67 [label="Access variable R|<local>/<iterator>|"];
68 [label="Function call arguments exit"];
65 [label="Function call arguments enter"];
66 [label="Access variable R|<local>/<iterator>|"];
67 [label="Function call arguments exit"];
}
69 [label="Function call: R|<local>/<iterator>|.R|SubstitutionOverride<kotlin/collections/Iterator.hasNext: R|kotlin/Boolean|>|()" style="filled" fillcolor=yellow];
70 [label="Exit loop condition"];
68 [label="Function call: R|<local>/<iterator>|.R|SubstitutionOverride<kotlin/collections/Iterator.hasNext: R|kotlin/Boolean|>|()" style="filled" fillcolor=yellow];
69 [label="Exit loop condition"];
}
subgraph cluster_23 {
color=blue
71 [label="Enter loop block"];
70 [label="Enter loop block"];
subgraph cluster_24 {
color=blue
72 [label="Enter block"];
71 [label="Enter block"];
subgraph cluster_25 {
color=blue
73 [label="Function call arguments enter"];
74 [label="Access variable R|<local>/<iterator>|"];
75 [label="Function call arguments exit"];
72 [label="Function call arguments enter"];
73 [label="Access variable R|<local>/<iterator>|"];
74 [label="Function call arguments exit"];
}
76 [label="Function call: R|<local>/<iterator>|.R|SubstitutionOverride<kotlin/collections/Iterator.next: R|kotlin/Nothing|>|()" style="filled" fillcolor=yellow];
77 [label="Stub" style="filled" fillcolor=gray];
78 [label="Variable declaration: lval result: R|kotlin/Nothing|" style="filled" fillcolor=gray];
75 [label="Function call: R|<local>/<iterator>|.R|SubstitutionOverride<kotlin/collections/Iterator.next: R|kotlin/Nothing|>|()" style="filled" fillcolor=yellow];
76 [label="Stub" style="filled" fillcolor=gray];
77 [label="Variable declaration: lval result: R|kotlin/Nothing|" style="filled" fillcolor=gray];
subgraph cluster_26 {
color=blue
79 [label="Enter block" style="filled" fillcolor=gray];
80 [label="Access variable R|<local>/result|" style="filled" fillcolor=gray];
81 [label="Stub" style="filled" fillcolor=gray];
82 [label="Assignment: R|<local>/s|" style="filled" fillcolor=gray];
78 [label="Enter block" style="filled" fillcolor=gray];
79 [label="Access variable R|<local>/result|" style="filled" fillcolor=gray];
80 [label="Stub" style="filled" fillcolor=gray];
81 [label="Assignment: R|<local>/s|" style="filled" fillcolor=gray];
subgraph cluster_27 {
color=blue
83 [label="Enter when" style="filled" fillcolor=gray];
82 [label="Enter when" style="filled" fillcolor=gray];
subgraph cluster_28 {
color=blue
84 [label="Enter when branch condition " style="filled" fillcolor=gray];
85 [label="Access variable R|<local>/result|" style="filled" fillcolor=gray];
86 [label="Stub" style="filled" fillcolor=gray];
87 [label="Access variable <Unresolved name: b>#" style="filled" fillcolor=gray];
88 [label="Exit when branch condition" style="filled" fillcolor=gray];
83 [label="Enter when branch condition " style="filled" fillcolor=gray];
84 [label="Access variable R|<local>/result|" style="filled" fillcolor=gray];
85 [label="Stub" style="filled" fillcolor=gray];
86 [label="Access variable <Unresolved name: b>#" style="filled" fillcolor=gray];
87 [label="Exit when branch condition" style="filled" fillcolor=gray];
}
89 [label="Synthetic else branch" style="filled" fillcolor=gray];
90 [label="Enter when branch result" style="filled" fillcolor=gray];
88 [label="Synthetic else branch" style="filled" fillcolor=gray];
89 [label="Enter when branch result" style="filled" fillcolor=gray];
subgraph cluster_29 {
color=blue
91 [label="Enter block" style="filled" fillcolor=gray];
92 [label="Jump: break@@@[R|<local>/<iterator>|.R|SubstitutionOverride<kotlin/collections/Iterator.hasNext: R|kotlin/Boolean|>|()] " style="filled" fillcolor=gray];
93 [label="Stub" style="filled" fillcolor=gray];
94 [label="Exit block" style="filled" fillcolor=gray];
90 [label="Enter block" style="filled" fillcolor=gray];
91 [label="Jump: break@@@[R|<local>/<iterator>|.R|SubstitutionOverride<kotlin/collections/Iterator.hasNext: R|kotlin/Boolean|>|()] " style="filled" fillcolor=gray];
92 [label="Stub" style="filled" fillcolor=gray];
93 [label="Exit block" style="filled" fillcolor=gray];
}
95 [label="Exit when branch result" style="filled" fillcolor=gray];
96 [label="Exit when" style="filled" fillcolor=gray];
94 [label="Exit when branch result" style="filled" fillcolor=gray];
95 [label="Exit when" style="filled" fillcolor=gray];
}
97 [label="Exit block" style="filled" fillcolor=gray];
96 [label="Exit block" style="filled" fillcolor=gray];
}
98 [label="Exit block" style="filled" fillcolor=gray];
97 [label="Exit block" style="filled" fillcolor=gray];
}
99 [label="Exit loop block" style="filled" fillcolor=gray];
98 [label="Exit loop block" style="filled" fillcolor=gray];
}
100 [label="Exit while loop"];
99 [label="Exit while loop"];
}
101 [label="Exit block"];
100 [label="Exit block"];
}
102 [label="Access variable R|<local>/s|"];
103 [label="Enter safe call"];
101 [label="Access variable R|<local>/s|"];
102 [label="Enter safe call"];
subgraph cluster_30 {
color=blue
104 [label="Function call arguments enter"];
105 [label="Postponed enter to lambda"];
103 [label="Function call arguments enter"];
104 [label="Postponed enter to lambda"];
subgraph cluster_31 {
color=blue
106 [label="Enter function <anonymous>" style="filled" fillcolor=red];
105 [label="Enter function <anonymous>" style="filled" fillcolor=red];
subgraph cluster_32 {
color=blue
107 [label="Enter block"];
108 [label="Access variable R|<local>/it|"];
109 [label="Access variable R|/A.a|"];
110 [label="Exit block"];
106 [label="Enter block"];
107 [label="Access variable R|<local>/it|"];
108 [label="Access variable R|/A.a|"];
109 [label="Exit block"];
}
111 [label="Exit function <anonymous>" style="filled" fillcolor=red];
110 [label="Exit function <anonymous>" style="filled" fillcolor=red];
}
112 [label="Function call arguments exit"];
111 [label="Function call arguments exit"];
}
113 [label="Postponed exit from lambda"];
114 [label="Function call: $subj$.R|kotlin/let|<R|A|, R|kotlin/Int|>(...)" style="filled" fillcolor=yellow];
115 [label="Exit safe call"];
116 [label="Exit block"];
112 [label="Postponed exit from lambda"];
113 [label="Function call: $subj$.R|kotlin/let|<R|A|, R|kotlin/Int|>(...)" style="filled" fillcolor=yellow];
114 [label="Exit safe call"];
115 [label="Exit block"];
}
117 [label="Exit function test_0" style="filled" fillcolor=red];
116 [label="Exit function test_0" style="filled" fillcolor=red];
}
53 -> {54};
54 -> {55};
55 -> {56};
56 -> {57};
@@ -294,13 +294,13 @@ digraph smartcastToNothing_kt {
66 -> {67};
67 -> {68};
68 -> {69};
69 -> {70};
70 -> {71 100};
69 -> {70 99};
70 -> {71};
71 -> {72};
72 -> {73};
73 -> {74};
74 -> {75};
75 -> {76};
75 -> {76} [style=dotted];
76 -> {77} [style=dotted];
77 -> {78} [style=dotted];
78 -> {79} [style=dotted];
@@ -312,111 +312,111 @@ digraph smartcastToNothing_kt {
84 -> {85} [style=dotted];
85 -> {86} [style=dotted];
86 -> {87} [style=dotted];
87 -> {88} [style=dotted];
88 -> {89 90} [style=dotted];
89 -> {96} [style=dotted];
87 -> {88 89} [style=dotted];
88 -> {95} [style=dotted];
89 -> {90} [style=dotted];
90 -> {91} [style=dotted];
91 -> {92} [style=dotted];
92 -> {93 100} [style=dotted];
91 -> {92 99} [style=dotted];
92 -> {93} [style=dotted];
93 -> {94} [style=dotted];
94 -> {95} [style=dotted];
95 -> {96} [style=dotted];
96 -> {97} [style=dotted];
97 -> {98} [style=dotted];
98 -> {99} [style=dotted];
99 -> {65} [color=green style=dotted];
98 -> {64} [color=green style=dotted];
99 -> {100};
100 -> {101};
101 -> {102};
102 -> {103 115};
101 -> {102 114};
102 -> {103};
103 -> {104};
104 -> {105};
105 -> {106 112};
105 -> {113} [style=dotted];
105 -> {106} [style=dashed];
104 -> {105 111};
104 -> {112} [style=dotted];
104 -> {105} [style=dashed];
105 -> {106};
106 -> {107};
107 -> {108};
108 -> {109};
109 -> {110};
110 -> {111};
110 -> {112};
111 -> {113};
112 -> {114};
113 -> {114} [label="Postponed"];
112 -> {113} [label="Postponed"];
113 -> {114};
114 -> {115};
115 -> {116};
116 -> {117};
subgraph cluster_33 {
color=red
118 [label="Enter function test_1" style="filled" fillcolor=red];
117 [label="Enter function test_1" style="filled" fillcolor=red];
subgraph cluster_34 {
color=blue
119 [label="Enter block"];
118 [label="Enter block"];
subgraph cluster_35 {
color=blue
120 [label="Enter when"];
119 [label="Enter when"];
subgraph cluster_36 {
color=blue
121 [label="Enter when branch condition "];
122 [label="Access variable R|<local>/a|"];
123 [label="Type operator: (R|<local>/a| is R|kotlin/Nothing?|)"];
124 [label="Exit when branch condition"];
120 [label="Enter when branch condition "];
121 [label="Access variable R|<local>/a|"];
122 [label="Type operator: (R|<local>/a| is R|kotlin/Nothing?|)"];
123 [label="Exit when branch condition"];
}
125 [label="Synthetic else branch"];
126 [label="Enter when branch result"];
124 [label="Synthetic else branch"];
125 [label="Enter when branch result"];
subgraph cluster_37 {
color=blue
127 [label="Enter block"];
128 [label="Access variable R|<local>/a|"];
129 [label="Smart cast: R|<local>/a|"];
130 [label="Enter safe call"];
131 [label="Access variable R|kotlin/String.length|"];
132 [label="Exit safe call"];
133 [label="Variable declaration: lval b: R|kotlin/Int?|"];
134 [label="Exit block"];
126 [label="Enter block"];
127 [label="Access variable R|<local>/a|"];
128 [label="Smart cast: R|<local>/a|"];
129 [label="Enter safe call"];
130 [label="Access variable R|kotlin/String.length|"];
131 [label="Exit safe call"];
132 [label="Variable declaration: lval b: R|kotlin/Int?|"];
133 [label="Exit block"];
}
135 [label="Exit when branch result"];
136 [label="Exit when"];
134 [label="Exit when branch result"];
135 [label="Exit when"];
}
subgraph cluster_38 {
color=blue
137 [label="Enter when"];
136 [label="Enter when"];
subgraph cluster_39 {
color=blue
138 [label="Enter when branch condition "];
139 [label="Access variable R|<local>/a|"];
140 [label="Type operator: (R|<local>/a| is R|kotlin/Nothing|)"];
141 [label="Exit when branch condition"];
137 [label="Enter when branch condition "];
138 [label="Access variable R|<local>/a|"];
139 [label="Type operator: (R|<local>/a| is R|kotlin/Nothing|)"];
140 [label="Exit when branch condition"];
}
142 [label="Synthetic else branch"];
143 [label="Enter when branch result"];
141 [label="Synthetic else branch"];
142 [label="Enter when branch result"];
subgraph cluster_40 {
color=blue
144 [label="Enter block"];
145 [label="Access variable R|<local>/a|"];
146 [label="Smart cast: R|<local>/a|"];
147 [label="Access variable R|kotlin/String.length|"];
148 [label="Variable declaration: lval b: R|kotlin/Int|"];
149 [label="Exit block"];
143 [label="Enter block"];
144 [label="Access variable R|<local>/a|"];
145 [label="Smart cast: R|<local>/a|"];
146 [label="Access variable R|kotlin/String.length|"];
147 [label="Variable declaration: lval b: R|kotlin/Int|"];
148 [label="Exit block"];
}
150 [label="Exit when branch result"];
151 [label="Exit when"];
149 [label="Exit when branch result"];
150 [label="Exit when"];
}
152 [label="Exit block"];
151 [label="Exit block"];
}
153 [label="Exit function test_1" style="filled" fillcolor=red];
152 [label="Exit function test_1" style="filled" fillcolor=red];
}
117 -> {118};
118 -> {119};
119 -> {120};
120 -> {121};
121 -> {122};
122 -> {123};
123 -> {124};
124 -> {125 126};
125 -> {136};
123 -> {124 125};
124 -> {135};
125 -> {126};
126 -> {127};
127 -> {128};
128 -> {129};
129 -> {130 132};
128 -> {129 131};
129 -> {130};
130 -> {131};
131 -> {132};
132 -> {133};
@@ -427,9 +427,9 @@ digraph smartcastToNothing_kt {
137 -> {138};
138 -> {139};
139 -> {140};
140 -> {141};
141 -> {142 143};
142 -> {151};
140 -> {141 142};
141 -> {150};
142 -> {143};
143 -> {144};
144 -> {145};
145 -> {146};
@@ -439,6 +439,5 @@ digraph smartcastToNothing_kt {
149 -> {150};
150 -> {151};
151 -> {152};
152 -> {153};
}