You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: documentation/cxx-interop/safe-interop/index.md
+28-23Lines changed: 28 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -49,8 +49,9 @@ to safely interface with them. This document describes how such code needs to be
49
49
Types imported from C++ are considered foreign to Swift. Normally, most C++ types are imported into Swift
50
50
without any restriction. However, a small set of C++ APIs e.g. pointers/references and methods returning
51
51
pointers will be imported as unsafe (section [Working with C++ references and view types in Swift](https://www.swift.org/documentation/cxx-interop/#working-with-c-references-and-view-types-in-swift)
52
-
explains this in more detail.) Under the strict memory safe mode, the compiler will flip the polarity and
53
-
treat all types that are not known to be safe as unsafe, and will diagnose uses of them. In this section,
52
+
explains this in more detail.) Under the strict memory safety mode, the compiler will flip the polarity of its safety assumptions.
53
+
It will treat all types that are not known to be safe as unsafe, and emit diagnostics for usage of unsafe types without the `unsafe` keyword.
54
+
In this section,
54
55
we will show how to annotate unsafe C++ types so that they can be accessed safely and correctly from Swift.
55
56
Note that the features here are agnostic to whether strict safety mode is on or off. When the strict safety
56
57
mode is on, the compiler diagnostics can serve as a guide on how to properly annotate C++ types, and also help ensure
@@ -60,7 +61,9 @@ dependent on the lifetimes of other objects.
60
61
61
62
Under the strict safety mode, built-in numeric types like `int`, some standard library types like `std::string`,
62
63
and aggregate types built from other safe types are considered safe. All other unannotated types
63
-
are considered unsafe. Let's see what happens when we are trying to use an unannotated type in
64
+
are considered unsafe.
65
+
66
+
Let's see what happens when we are trying to use an unannotated type in
64
67
strict safety mode. Consider the following C++ type and APIs:
65
68
66
69
```c++
@@ -108,7 +111,7 @@ emits a warning about using an unsafe type.
108
111
109
112
Building the code again will emit a new diagnostic for the `fileName` function about
110
113
missing lifetime annotations. C and C++ functions that return non-escapable types need annotations
111
-
to describe their lifetime contracts via [lifetimebound](https://clang.llvm.org/docs/AttributeReference.html#id11)
114
+
to describe their lifetime contracts via [lifetimebound](https://clang.llvm.org/docs/AttributeReference.html#id8)
112
115
and [lifetime_capture_by](https://clang.llvm.org/docs/AttributeReference.html#lifetime-capture-by) annotations.
Notably, the default constructor of a type is always assumed to create an independent value.
304
308
305
-
We can also annotate`lifetimebound` APIs via APINotes. The `-1` index represents the `this` position.
309
+
We can also attach`lifetimebound`annotations to C and C++ APIs using [API Notes](https://clang.llvm.org/docs/APINotes.html). The `-1` index represents the `this` position.
306
310
307
311
```
308
312
Tags:
@@ -321,7 +325,7 @@ Tags:
321
325
Note that APINotes have some limitations around C++, they do not support overloaded functions.
322
326
323
327
While `lifetimebound` always describes the lifetime dependencies of the return value (or
324
-
the constructed object in case of constructors), we can use can use `lifetime_capture_by`
328
+
the constructed object in case of constructors), we can use can use the [`lifetime_capture_by`](https://clang.llvm.org/docs/AttributeReference.html#lifetime-capture-by)
325
329
annotation to describe the lifetime of other output values, like output/inout arguments
0 commit comments