Skip to content

Commit d1b1b1c

Browse files
committed
Describe checking for unsafe overrides and unsafe conformances
And fix the grammaro finagolfin pointed out, properly
1 parent 989a414 commit d1b1b1c

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

proposals/nnnn-strict-memory-safety.md

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ While there are a number of potential definitions for memory safety, the one pro
2727
* **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.
2828
* **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.
2929
* **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.
3131
* **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.
3232

3333
### Memory safety in Swift
@@ -241,6 +241,56 @@ All of these APIs will be marked `@unsafe`. For all of the types that are `@unsa
241241

242242
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.
243243

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+
class Super {
250+
func f() { }
251+
}
252+
253+
class Sub: Super {
254+
@unsafe func f() { ... } // 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+
class Sub: Super {
263+
func f() { ... } // 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:
272+
273+
```swift
274+
protocol P {
275+
func f()
276+
}
277+
278+
struct ConformsToP { }
279+
280+
extension ConformsToP: P {
281+
@unsafe func f() { } // warning: unsafe instance method 'f()' cannot satisfy safe requirement
282+
}
283+
```
284+
285+
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+
extension ConformsToP: P {
290+
@unsafe func f() { }
291+
}
292+
```
293+
244294
### Strict safety mode and escalatable warnings
245295

246296
The strict memory safety mode can be enabled with the new compiler flag `-strict-memory-safety`.

0 commit comments

Comments
 (0)