Skip to content

[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

Merged
merged 4 commits into from
Jun 5, 2025

Conversation

hnrklssn
Copy link
Contributor

[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.

@hnrklssn
Copy link
Contributor Author

This PR is stacked on top of #81550, #81579 and #81585 to avoid merge conflicts.

@hnrklssn
Copy link
Contributor Author

@swift-ci please smoke test

@hnrklssn hnrklssn force-pushed the swiftify-sized-sizedby branch from 5b5f5dd to f3ff249 Compare May 23, 2025 19:37
@hnrklssn
Copy link
Contributor Author

@swift-ci please smoke test

hnrklssn added 2 commits May 29, 2025 13:32
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.
@hnrklssn hnrklssn force-pushed the swiftify-sized-sizedby branch from f3ff249 to 4a72c1e Compare May 29, 2025 20:33
@hnrklssn
Copy link
Contributor Author

@swift-ci please smoke test

@hnrklssn hnrklssn requested a review from atrick May 29, 2025 20:44
@hnrklssn
Copy link
Contributor Author

@swift-ci please smoke test

Copy link
Contributor

@Xazax-hun Xazax-hun left a 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.

@hnrklssn
Copy link
Contributor Author

hnrklssn commented Jun 4, 2025

@swift-ci please smoke test

Copy link
Contributor

@delcypher delcypher left a 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) {
Copy link
Contributor

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?

Copy link
Contributor Author

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 an Unsafe[Mutable]Pointer<T>. If it's not, getPointeeType will just return nil

@hnrklssn hnrklssn merged commit 89b09a6 into swiftlang:main Jun 5, 2025
3 checks passed
hnrklssn added a commit to hnrklssn/swift that referenced this pull request Jun 10, 2025
[Swiftify] Support __sized_by on byte-sized pointee types

(cherry picked from commit 89b09a6)
hnrklssn added a commit to hnrklssn/swift that referenced this pull request Jun 10, 2025
[Swiftify] Support __sized_by on byte-sized pointee types

(cherry picked from commit 89b09a6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants