swift
d4ce618e - Provide more Fix-It guidance for concurrency-unsafe global variables (SE-0412)

Commit
1 year ago
Provide more Fix-It guidance for concurrency-unsafe global variables (SE-0412) When diagnosing a concurrency-unsafe global or static variable, provide Fix-Its with specific guidance and advice. This is intended to aid the workflow for folks enabling strict concurrency checking or Swift 6. There are up to three Fix-Its attached to a diagnostic about concurrency-unsafe global/static variables: * convert 'global' to a 'let' constant to make the shared state immutable, which replaces `var` with `let` * restrict 'global' to the main actor if it will only be accessed from the main thread, which adds `@MainActor` * unsafely mark %0 as concurrency-safe if all accesses are protected by an external synchronization mechanism, which adds `nonisolated(unsafe)` I fretted over two things before deciding on this path: 1. For the second note, the reality is that any global actor will suffice, but `@MainActor` is orders of magnitude more common than any other global actor, so "common case convenience" wins over "precise but less useful. 2. For the third note, `nonisolated(unsafe)` should only be used sparingly, and surfacing it via Fix-It could cause overuse. However, developers need to know about it, and this is how we do that. It comes last in the list of notes (after the better options) and says "unsafe" in not one but two places.
Author
Committer
Parents
Loading