-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Swiftify] Support __sized_by on byte-sized pointee types #81752
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
Conversation
@swift-ci please smoke test |
5b5f5dd
to
f3ff249
Compare
@swift-ci please smoke test |
Previously we would emit a macro that would error on expansion when trying to add a safe wrapper to a function with __sized_by on a type that mapped to UnsafePointer<T> instead of UnsafeRawPointer or OpaquePointer. __sized_by is acceptable when used on byte-sized pointee types, so this adds machinery in the macro expansion to support that. Meanwhile on the ClangImporter side, we add a check so that __sized_by on pointee types with a size is ignored if that size is larger than 1 byte. When _SwiftifyImport applies .sizedBy to a pointer of type UnsafePointer<T> it will still map it to a RawSpan/UnsafeRawBufferPointer in the safe overload. The assumption is that any API using __sized_by is dealing with raw bytes, so raw pointers are a better Swift abstraction than UnsafePointer<CChar> etc. It also lets the user avoid doing a scary pointer cast from some potentially larger-than-byte-sized pointer to a byte-sized pointer. Casts to RawPointers are generally safer and more ergonomic. rdar://150966684 rdar://150966021
This enables -strict-memory-safety -warnings-as-errors on the Swift side to verify that the macro expansions don't cause any warnings and that they use `unsafe` correctly. On the clang side it enables -Xcc -Werror. To reduce noise in the test output and pass -Werror cleanly it also enables -Xcc -Wno-nullability-completeness. This will make it easier to detect mistakes when writing tests, because warnings stand out whereas previously they've been drowned out in the noise.
f3ff249
to
4a72c1e
Compare
@swift-ci please smoke test |
@swift-ci please smoke test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am a bit concerned about type aliases but in case that works the changes look good to me.
@swift-ci please smoke test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not too familiar with this code but at a glance it looks reasonable. I also have a question about a potential failure path.
let type = peelOptionalType(getParam(signature, index).type) | ||
if type.canRepresentBasicType(type: OpaquePointer.self) { | ||
return ExprSyntax("OpaquePointer(\(baseAddress))") | ||
} | ||
if isSizedBy { | ||
if let pointeeType = getPointeeType(type) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are there any cases where isSizedBy
is true but getPointeeType
fails?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only cases I'm aware of are:
- if it's applied to an
OpaquePointer
, which has an early exit above - if clang would stop stripping typedef'd pointer types when applying
__sized_by
- if it's already applied to an
UnsafeRawPointer
Otherwise it should always be anUnsafe[Mutable]Pointer<T>
. If it's not,getPointeeType
will just return nil
[Swiftify] Support __sized_by on byte-sized pointee types (cherry picked from commit 89b09a6)
[Swiftify] Support __sized_by on byte-sized pointee types (cherry picked from commit 89b09a6)
[Swiftify] Support __sized_by on byte-sized pointee types
Previously we would emit a macro that would error on expansion when
trying to add a safe wrapper to a function with __sized_by on a type
that mapped to UnsafePointer instead of UnsafeRawPointer or
OpaquePointer. __sized_by is acceptable when used on byte-sized pointee
types, so this adds machinery in the macro expansion to support that.
Meanwhile on the ClangImporter side, we add a check so that __sized_by
on pointee types with a size is ignored if that size is larger than 1
byte.
When _SwiftifyImport applies .sizedBy to a pointer of type
UnsafePointer it will still map it to a
RawSpan/UnsafeRawBufferPointer in the safe overload. The assumption is
that any API using __sized_by is dealing with raw bytes, so raw pointers
are a better Swift abstraction than UnsafePointer etc. It also
lets the user avoid doing a scary pointer cast from some potentially
larger-than-byte-sized pointer to a byte-sized pointer. Casts to
RawPointers are generally safer and more ergonomic.
rdar://150966684
rdar://150966021
[Swiftify] Enable warnings-as-errors in interop test cases (NFC)
This enables -strict-memory-safety -warnings-as-errors on the Swift side
to verify that the macro expansions don't cause any warnings and that
they use
unsafe
correctly. On the clang side it enables -Xcc -Werror.To reduce noise in the test output and pass -Werror cleanly it also
enables -Xcc -Wno-nullability-completeness. This will make it easier to
detect mistakes when writing tests, because warnings stand out whereas
previously they've been drowned out in the noise.