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: proposals/nnnn-strict-memory-safety.md
+51-1Lines changed: 51 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -27,7 +27,7 @@ While there are a number of potential definitions for memory safety, the one pro
27
27
***Lifetime safety** : all accesses to a value are guaranteed to occur during its lifetime. Violations of this property, such as accessing a value after its lifetime has ended, are often called use-after-free errors.
28
28
***Bounds safety**: all accesses to memory are within the intended bounds of the memory allocation, such as accessing elements in an array. Violations of this property are called out-of-bounds accesses.
29
29
***Type safety** : all accesses to a value use the type to which it was initialized, or a type that is compatible with that type. For example, one cannot access a `String` value as if it were an `Array`. Violations of this property are called type confusions.
30
-
***Initialization safety** : all values are initialized properly to being used, so they cannot contain unexpected data. Violations of this property often lead to information disclosures (where data that should be invisible becomes available) or even other memory-safety issues like use-after-frees or type confusions.
30
+
***Initialization safety** : all values are initialized properly prior to being used, so they cannot contain unexpected data. Violations of this property often lead to information disclosures (where data that should be invisible becomes available) or even other memory-safety issues like use-after-frees or type confusions.
31
31
***Thread safety:** all values are accessed concurrently in a manner that is synchronized sufficiently to maintain their invariants. Violations of this property are typically called data races, and can lead to any of the other memory safety problems.
32
32
33
33
### Memory safety in Swift
@@ -241,6 +241,56 @@ All of these APIs will be marked `@unsafe`. For all of the types that are `@unsa
241
241
242
242
The `-Ounchecked` compiler flag disables some checking in the standard library, including (for example) bounds checking on array accesses. It is generally discouraged in all Swift code, but is particularly problematic in conjunction with strict memory safety because it removes the checking that makes certain standard library APIs safe. Therefore, the compiler will produce a diagnostic when the two features are combined.
243
243
244
+
### Unsafe overrides
245
+
246
+
Overriding a safe method within an `@unsafe` one could introduce unsafety, so it will produce a diagnostic in the strict safety mode:
247
+
248
+
```swift
249
+
classSuper {
250
+
funcf() { }
251
+
}
252
+
253
+
classSub: Super {
254
+
@unsafefuncf() { ... } // warning: override of safe instance method with unsafe instance method
255
+
}
256
+
```
257
+
258
+
to suppress this warning, the `Sub` class itself can be marked as `@unsafe`, e.g.,
259
+
260
+
```swift
261
+
@unsafe
262
+
classSub: Super {
263
+
funcf() { ... } // warning: override of safe instance method with unsafe instance method
264
+
}
265
+
```
266
+
267
+
The `@unsafe` annotation is at the class level because any use of the `Sub` type can now introduce unsafe behavior, and any indication of that unsafe behavior will be lost once that `Sub` is converted to a `Super` instance.
268
+
269
+
### Unsafe conformances
270
+
271
+
Implementing a protocol requirement that is not `@unsafe` (and not part of an `@unsafe` protocol) within an `@unsafe` declaration introduces unsafety, so it will produce a diagnostic in the strict safety mode:
To suppress this warning, one can place `@unsafe` on the extension (or type) where the conformance to `P` is supplied. This notes that the conformance itself is unsafe:
286
+
287
+
```swift
288
+
@unsafe
289
+
extensionConformsToP: P {
290
+
@unsafefuncf() { }
291
+
}
292
+
```
293
+
244
294
### Strict safety mode and escalatable warnings
245
295
246
296
The strict memory safety mode can be enabled with the new compiler flag `-strict-memory-safety`.
0 commit comments