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
@@ -217,7 +217,7 @@ There are a few exemptions to the rule that any unsafe constructs within the sig
217
217
218
218
### `@safe` attribute
219
219
220
-
Like the `@unsafe` attribute, the `@safe` attribute ise used on declarations whose signatures involve unsafe types. However, the `@safe` attribute means that the declaration is consider safe to use even though its signature includes unsafe types. For example, marking `UnsafeBufferPointer` as `@unsafe` means that all operations involving an unsafe buffer pointer are implicitly considered `@unsafe`. The `@safe` attribute can be used to say that those particular operations are actually safe. For example, any operation involving buffer indices or count are safe, because they don't touch the memory itself. This can be indicated by marking these APIs `@safe`:
220
+
Like the `@unsafe` attribute, the `@safe` attribute is used on declarations whose signatures involve unsafe types. However, the `@safe` attribute means that the declaration is consider safe to use even though its signature includes unsafe types. For example, marking `UnsafeBufferPointer` as `@unsafe` means that all operations involving an unsafe buffer pointer are implicitly considered `@unsafe`. The `@safe` attribute can be used to say that those particular operations are actually safe. For example, any operation involving buffer indices or count are safe, because they don't touch the memory itself. This can be indicated by marking these APIs `@safe`:
221
221
222
222
```swift
223
223
extensionUnsafeBufferPointer {
@@ -249,6 +249,20 @@ extension Array<Int> {
249
249
}
250
250
```
251
251
252
+
The `@safe` annotation on a declaration takes responsibility for its direct arguments, so (for example) a variable of unsafe type used as an argument to a `@safe` function (or as the `self` for a property or subscript reference) will not be diagnosed as unsafe:
253
+
254
+
```swift
255
+
extensionArray<Int> {
256
+
funcsum() ->Int {
257
+
withUnsafeBufferPointer { buffer in
258
+
let count = buffer.count// count is `@safe`, no diagnostic even though 'buffer' has unsafe type
259
+
let address = buffer.baseAddress// warning: 'buffer' and 'baseAddress' are both unsafe
260
+
c_library_sum_function(address, count, 0) // warning: 'c_library_sum_function' and 'address' are both unsafe
261
+
}
262
+
}
263
+
}
264
+
```
265
+
252
266
### `unsafe` expression
253
267
254
268
When a declaration is marked `@unsafe`, it is free to use any other unsafe types as part of its interface. Any time there is executable code that makes use of unsafe constructs, that code must be within an `unsafe` expression or it will receive a diagnostic about uses of unsafe code. In the example from the previous section, `wrapper` can be marked as `@unsafe` to suppress diagnostics by explicitly propagating unsafety to their clients:
0 commit comments