Skip to content

Commit a162a37

Browse files
committed
Add a Future Directions section on unsafe cases in pattern matching
1 parent b7ad562 commit a162a37

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

proposals/nnnn-strict-memory-safety.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,33 @@ The first two options are the most straightforward, but the fact that actors hav
437437

438438
It is unclear whether `SerialExecutor` is or will be the only protocol of this nature. If there are others, it could be worth providing a special form of the `@unsafe` attribute on the protocol itself, such as `@unsafe(conforms)`, that is only considered unsafe for conforming types.
439439

440+
### Handling of `@unsafe` cases
441+
442+
When an enum case is explicitly marked `@unsafe`, but involves no associated data that is unsafe, this proposal doesn't have a way to suppress safety diagnostics when pattern matching that case. For example:
443+
444+
```swift
445+
enum WeirdAddress {
446+
@unsafe case rawOffsetIntoGlobalArray(Int)
447+
}
448+
449+
func example(_ address: WeirdAddress) {
450+
if case .rawOffsetIntoGlobalArray(let offset) = weirdAddress { // reference to @unsafe case rawOffsetIntoGlobalArray that can't be suppressed
451+
}
452+
}
453+
454+
```
455+
456+
We have several options here:
457+
458+
* We could suppress the diagnostic for this use of an `@unsafe case`. One would still get diagnostics when constructing such a case.
459+
460+
* We could reject `@unsafe` on case declarations that don't involve any unsafe types.
461+
462+
* We could extend the pattern grammar with an `unsafe` pattern to suppress this diagnostic, e.g.,
463+
```swift
464+
if case unsafe .rawOffsetIntoGlobalArray(let offset) = weirdAddress { ... }
465+
```
466+
440467
### Overloading to stage in safe APIs
441468

442469
When adopting the strict memory safety mode, it's likely that a Swift module will want to replace existing APIs that traffic in unsafe types (such as `UnsafeMutablePointer`) with safer equivalents (such as `Span`). To retain compatibility for older clients, the existing APIs will need to be left in place. Unfortunately, this might mean that the best name for the API is already taken. For example, perhaps we have a data packet that exposes its bytes via a property:

0 commit comments

Comments
 (0)