Commit 45d6c85
[K/N] Don't generate generic safe casts for Objective-C types
With `genericSafeCasts=true` enabled by default, the Kotlin/Native
compiler now generates additional type checks to make sure that generic
type erasure doesn't allow objects of unexpected types to slip through.
As a justified side effect, this also checks types of objects passed
from Objective-C libraries.
Objective-C is tricky, though.
For example, it is common to use "duck typing" in Objective-C:
if an Obj-C function has an Obj-C class or protocol as the return
type, the return value doesn't need to actually be of that type: it is
enough to respond to the same method calls.
Such mismatches happen even in Apple's own frameworks, which
caused KT-85508.
So, it is justified to not generate such type checks for Objective-C
types.
This commit does exactly that (and also removes handling of special
cases of Objective-C types there, as they are now covered with the
general Obj-C type exclusion).
It is also safe enough: calls to Objective-C methods mostly use dynamic
dispatch, which will fail with a readable error if a method is not
implemented.
As a side effect, this commit also fixes KT-85399: in that case, due to
a bug in cinterop (KT-56860), Kotlin translated the imported Objective-C
type wrong. So, the value was of the correct type, but Kotlin didn't
know it.
Improving handling of Obj-C types in generic type checks is tracked in
KT-85681.
^KT-85358 Fixed
^KT-85399 Fixed
^KT-85508 Fixed
^RCA: the regression reached the release because it occurs in
interaction with tricky Objective-C code that wasn't present in our
tests but is found in the wild.
Basically, sometimes the declared Obj-C type or its Kotlin translation
doesn't match the actual object type when getting values from Obj-C, and
it was unexpected.
This commit adds regression tests and effectively disables
`genericSafeCasts` for Obj-C types to make sure these and similar
problems won't occur further.
The problem was triggered by enabling `genericSafeCasts` in 21b204a.
(cherry picked from commit dd46827)1 parent 9261a6f commit 45d6c85
4 files changed
Lines changed: 44 additions & 21 deletions
File tree
- kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower
- native/native.tests/testData/codegen/cinterop
- objc
Lines changed: 28 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
26 | | - | |
27 | | - | |
28 | 26 | | |
29 | 27 | | |
30 | 28 | | |
| |||
182 | 180 | | |
183 | 181 | | |
184 | 182 | | |
185 | | - | |
186 | | - | |
187 | 183 | | |
188 | 184 | | |
189 | 185 | | |
| |||
205 | 201 | | |
206 | 202 | | |
207 | 203 | | |
208 | | - | |
209 | | - | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
210 | 232 | | |
211 | 233 | | |
212 | 234 | | |
| |||
Lines changed: 16 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
75 | 75 | | |
76 | 76 | | |
77 | 77 | | |
78 | | - | |
79 | | - | |
80 | | - | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
81 | 84 | | |
82 | 85 | | |
83 | 86 | | |
| |||
87 | 90 | | |
88 | 91 | | |
89 | 92 | | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
95 | 103 | | |
Lines changed: 0 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
5 | | - | |
6 | | - | |
7 | | - | |
8 | 4 | | |
9 | 5 | | |
10 | 6 | | |
| |||
Lines changed: 0 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
5 | | - | |
6 | | - | |
7 | 4 | | |
8 | 5 | | |
9 | 6 | | |
| |||
0 commit comments