-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Improve and collate diagnostics for uses of unsafe constructs in declarations #78307
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@swift-ci please smoke test |
For now, just use this as an intermediate state to provide a single place to render diagnostics.
Instead of producing a warning for each use of an unsafe entity, collect all of the uses of unsafe constructs within a given function and batch them together in a single diagnostic at the function level that tells you what you can do (add `@unsafe` or `@safe(unchecked)`, depending on whether all unsafe uses were in the definition), plus notes identifying every unsafe use within that declaration. The new diagnostic renderer nicely collects together in a single snippet, so it's easier to reason about. Here's an example from the embedded runtime that previously would have been 6 separate warnings, each with 1-2 notes: ``` swift/stdlib/public/core/EmbeddedRuntime.swift:397:13: warning: global function 'swift_retainCount' involves unsafe code; use '@safe(unchecked)' to assert that the code is memory-safe 395 | 396 | @_cdecl("swift_retainCount") 397 | public func swift_retainCount(object: Builtin.RawPointer) -> Int { | `- warning: global function 'swift_retainCount' involves unsafe code; use '@safe(unchecked)' to assert that the code is memory-safe 398 | if !isValidPointerForNativeRetain(object: object) { return 0 } 399 | let o = UnsafeMutablePointer<HeapObject>(object) | | `- note: call to unsafe initializer 'init(_:)' | `- note: reference to unsafe generic struct 'UnsafeMutablePointer' 400 | let refcount = refcountPointer(for: o) | | `- note: reference to let 'o' involves unsafe type 'UnsafeMutablePointer<HeapObject>' | `- note: call to global function 'refcountPointer(for:)' involves unsafe type 'UnsafeMutablePointer<Int>' 401 | return loadAcquire(refcount) & HeapObject.refcountMask | | `- note: reference to let 'refcount' involves unsafe type 'UnsafeMutablePointer<Int>' | `- note: call to global function 'loadAcquire' involves unsafe type 'UnsafeMutablePointer<Int>' 402 | } 403 | ``` Note that we have lost a little bit of information, because we no longer produce "unsafe declaration was here" notes pointing back at things like `UnsafeMutablePointer` or `recountPointer(for:)`. However, strict memory safety tends to be noisy to turn on, so it's worth losing a little bit of easily-recovered information to gain some brevity.
…n handle it Drive the strict-safety diagnostics for a particular declaration from primary type checking for declarations, so any memory-safety-related diagnostics will only be emitted for the primary files. This also brings them together as notes under a single warning for each declaration.
…currency code A nonisolated(unsafe) declaration clearly indicates that the declaration itself is unsafe, so it doesn't need to be diagnosted. Instead, diagnose any reference to such a declaration that occurs when strict concurrency is enabled. Make this a collatable unsafe use.
ea0f9ee
to
70adcad
Compare
@swift-ci please smoke test |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Instead of producing a warning for each use of an unsafe entity, collect all of the uses of unsafe constructs within a given function and batch them together in a single diagnostic at the enclosing declaration that tells you what you can do (add
@unsafe
or@safe(unchecked)
, depending on whether all unsafe uses were in the definition), plus notes identifying every unsafe use within that declaration. The new diagnostic renderer nicely collects together in a single snippet, so it's easier to reason about.Here's an example from the embedded runtime that previously would have been 6 separate warnings, each with 1-2 notes:
Note that we have lost a little bit of information, because we no longer produce "unsafe declaration was here" notes pointing back at things like
UnsafeMutablePointer
orrecountPointer(for:)
. However, strict memory safety tends to be noisy to turn on, so it's worth losing a little bit of easily-recovered information to gain some brevity.While here, also fix a few issues with the checking:
nonisolated(unsafe)
declarations, not the declaration itselfunowned(unsafe)
declarations, not the declaration itself