Commit b311c63
authored
[cxx-interop] Remove SWIFT_RETURNED_AS_RETAINED_BY_DEFAULT annotation (swiftlang#81329)
This patch removes the `SWIFT_RETURNED_AS_RETAINED_BY_DEFAULT`
annotation while maintaining the support for
`SWIFT_RETURNED_AS_UNRETAINED_BY_DEFAULT`. These type-level annotations
were initially introduced in
[PR-81093](swiftlang#81093) to reduce the
annotation burden in large C++ codebases where many C++ APIs returning
`SWIFT_SHARED_REFERENCE` types are exposed to Swift.
### Motivation
The original goal was to make C++ interop more ergonomic by allowing
type-level defaults for ownership conventions
for`SWIFT_SHARED_REFERENCE` types . However, defaulting to retained
return values (+1) seems to be problematic and poses memory safety
risks.
### Why we’re removing `SWIFT_RETURNED_AS_RETAINED_BY_DEFAULT`
- **Memory safety risks:** Defaulting to retained can potentially lead
to use-after-free bugs when the API implementation actually returns
`unowned` (`+0`). These errors are subtle and can be hard to debug or
discover, particularly in the absence of explicit API-level
`SWIFT_RETURNS_(UN)RETAINED` annotations.
- **Risky transitive behavior:** If a `SWIFT_SHARED_REFERENCE` type is
annotated with `SWIFT_RETURNED_AS_RETAINED_BY_DEFAULT`, any new C++ API
returning this type will inherit the retained behavior by default—even
if the API's actual return behavior is unretained. Unless explicitly
overridden with `SWIFT_RETURNS_UNRETAINED`, this can introduce a silent
mismatch in ownership expectations and lead to use-after-free bugs. This
is especially risky in large or evolving codebases where such defaults
may be overlooked.
- **Simpler multiple inheritance semantics:** With only one type-level
default (`SWIFT_RETURNED_AS_UNRETAINED_BY_DEFAULT`), we avoid
complications that can arise when multiple base classes specify
conflicting ownership defaults. This simplifies reasoning about behavior
in class hierarchies and avoids ambiguity when Swift determines the
ownership convention for inherited APIs.
### Why we’re keeping `SWIFT_RETURNED_AS_UNRETAINED_BY_DEFAULT`
- It still enables projects to suppress warnings for unannotated C++
APIs returning `SWIFT_SHARED_REFERENCE` types, helping to reduce noise
while maintaining clarity.
- It encourages explicitness for retained behavior. Developers must
annotate retained return values with `SWIFT_RETURNS_RETAINED`, making
ownership intent clearer and safer.
- The worst-case outcome of assuming unretained when the return is
actually retained is a memory leak, which is more tolerable and easier
to debug than a use-after-free.
- Having a single default mechanism improves clarity for documentation,
diagnostics, and long-term maintenance of Swift/C++ interop code.1 parent 93882f2 commit b311c63
File tree
6 files changed
+39
-94
lines changed- lib/ClangImporter
- SwiftBridging/swift
- test/Interop/Cxx/foreign-reference
- Inputs
6 files changed
+39
-94
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8809 | 8809 | | |
8810 | 8810 | | |
8811 | 8811 | | |
8812 | | - | |
8813 | | - | |
| 8812 | + | |
8814 | 8813 | | |
8815 | 8814 | | |
8816 | 8815 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3656 | 3656 | | |
3657 | 3657 | | |
3658 | 3658 | | |
3659 | | - | |
3660 | | - | |
| 3659 | + | |
3661 | 3660 | | |
3662 | 3661 | | |
3663 | 3662 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
191 | 191 | | |
192 | 192 | | |
193 | 193 | | |
194 | | - | |
195 | | - | |
196 | | - | |
197 | | - | |
198 | | - | |
199 | | - | |
200 | | - | |
201 | | - | |
202 | | - | |
203 | | - | |
204 | | - | |
205 | | - | |
206 | | - | |
207 | | - | |
208 | | - | |
209 | | - | |
210 | | - | |
211 | 194 | | |
212 | 195 | | |
213 | 196 | | |
| |||
Lines changed: 15 additions & 46 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
331 | 331 | | |
332 | 332 | | |
333 | 333 | | |
334 | | - | |
335 | | - | |
336 | | - | |
337 | | - | |
338 | | - | |
339 | | - | |
340 | | - | |
341 | 334 | | |
342 | 335 | | |
343 | 336 | | |
| |||
348 | 341 | | |
349 | 342 | | |
350 | 343 | | |
351 | | - | |
352 | | - | |
353 | | - | |
354 | | - | |
355 | | - | |
356 | 344 | | |
357 | 345 | | |
358 | 346 | | |
| |||
372 | 360 | | |
373 | 361 | | |
374 | 362 | | |
375 | | - | |
376 | | - | |
377 | | - | |
378 | | - | |
379 | | - | |
380 | | - | |
381 | | - | |
382 | | - | |
383 | | - | |
384 | | - | |
385 | | - | |
386 | 363 | | |
387 | 364 | | |
388 | 365 | | |
389 | 366 | | |
390 | 367 | | |
391 | | - | |
392 | | - | |
393 | | - | |
394 | 368 | | |
395 | 369 | | |
396 | 370 | | |
| |||
402 | 376 | | |
403 | 377 | | |
404 | 378 | | |
405 | | - | |
| 379 | + | |
406 | 380 | | |
407 | | - | |
| 381 | + | |
408 | 382 | | |
409 | 383 | | |
410 | 384 | | |
411 | 385 | | |
412 | 386 | | |
413 | 387 | | |
414 | 388 | | |
415 | | - | |
| 389 | + | |
416 | 390 | | |
417 | | - | |
| 391 | + | |
418 | 392 | | |
419 | 393 | | |
420 | 394 | | |
421 | 395 | | |
422 | 396 | | |
423 | 397 | | |
424 | | - | |
| 398 | + | |
425 | 399 | | |
426 | 400 | | |
427 | 401 | | |
| |||
433 | 407 | | |
434 | 408 | | |
435 | 409 | | |
436 | | - | |
437 | | - | |
438 | | - | |
439 | | - | |
440 | | - | |
441 | | - | |
442 | | - | |
443 | | - | |
444 | | - | |
445 | | - | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
446 | 413 | | |
447 | 414 | | |
448 | 415 | | |
| |||
453 | 420 | | |
454 | 421 | | |
455 | 422 | | |
456 | | - | |
| 423 | + | |
457 | 424 | | |
458 | 425 | | |
459 | | - | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
460 | 432 | | |
461 | 433 | | |
462 | 434 | | |
| |||
471 | 443 | | |
472 | 444 | | |
473 | 445 | | |
474 | | - | |
475 | | - | |
476 | | - | |
477 | 446 | | |
478 | 447 | | |
479 | 448 | | |
| |||
Lines changed: 10 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
35 | 38 | | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
Lines changed: 12 additions & 21 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
249 | 249 | | |
250 | 250 | | |
251 | 251 | | |
252 | | - | |
253 | | - | |
254 | | - | |
255 | 252 | | |
256 | 253 | | |
257 | 254 | | |
| |||
261 | 258 | | |
262 | 259 | | |
263 | 260 | | |
264 | | - | |
265 | | - | |
266 | | - | |
267 | | - | |
268 | | - | |
269 | | - | |
270 | | - | |
271 | | - | |
| 261 | + | |
| 262 | + | |
272 | 263 | | |
273 | | - | |
274 | | - | |
| 264 | + | |
| 265 | + | |
275 | 266 | | |
276 | | - | |
277 | | - | |
| 267 | + | |
| 268 | + | |
278 | 269 | | |
279 | | - | |
280 | | - | |
| 270 | + | |
| 271 | + | |
281 | 272 | | |
282 | | - | |
283 | | - | |
| 273 | + | |
| 274 | + | |
284 | 275 | | |
285 | | - | |
286 | | - | |
| 276 | + | |
| 277 | + | |
287 | 278 | | |
0 commit comments