-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Clang importer] Allow noncopyable C structs to define "destroy" operation #83364
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
[Clang importer] Allow noncopyable C structs to define "destroy" operation #83364
Conversation
In C interoperability mode, respect the ~Copyable annotation on C structs to import them as ~Copyable types. Fixes rdar://156877772.
…ation A C struct can be imported as noncopyable, but C doesn't have destructors, so there is no way to provide user-defined logic to perform the destruction. Introduce a new swift_attr that applies to imported noncopyable types and which provides such a "destroy" operation. It can be used like this: typedef struct __attribute__((swift_attr("~Copyable"))) __attribute__((swift_attr("destroy:wgpuAdapterInfoFreeMembers"))) WGPUAdapterInfo { /*...*/ } WGPUAdapterInfo; void wgpuAdapterInfoFreeMembers(WGPUAdapterInfo adapterInfo); This will bring the WGPUAdapterInfo struct in as a noncopyable type that will be cleaned up by calling wgpuAdapterInfoFreeMembers once it is no longer in use. Implements rdar://156889370.
@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.
This is great! Some nits inline.
lib/ClangImporter/ImportDecl.cpp
Outdated
if (!attributeName.starts_with("destroy:")) | ||
continue; | ||
|
||
auto destroyFuncName = attributeName.drop_front(strlen("destroy:")); |
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.
if (!attributeName.starts_with("destroy:")) | |
continue; | |
auto destroyFuncName = attributeName.drop_front(strlen("destroy:")); | |
if (!attributeName.consume_front("destroy:")) | |
continue; |
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 didn't know about consume_front
, thanks!
lib/ClangImporter/ImportDecl.cpp
Outdated
/// Function body synthesizer for a deinit of a noncopyable type, which | ||
/// passes "self" to the given "destroy" function. | ||
static std::pair<BraceStmt *, bool> | ||
synthesizeDeinitBodyForCustomDestroy( |
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 wonder if this (and co) should go to SwiftDeclSynthesizer.cpp
.
|
||
module NoncopyableStructs { | ||
header "noncopyable-struct.h" | ||
} |
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.
Nit: missing new line.
float x, y; | ||
} NonCopyable; | ||
|
||
typedef struct __attribute__((swift_attr("~Copyable"))) __attribute__((swift_attr("destroy:freeNonCopyableWithDeinit"))) NonCopyableWithDeinit { |
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.
Do we plan to support destroy:
with copyable types? If not, should we diagnose when people try to add it to a copyable type?
Maybe we should have a diagnostic when people try to add it to a C++ type that already has a non-trivial dtor.
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.
Copyable value types in Swift don't support user-defined deinit
, so we shouldn't try to support it for C structs, either.
A diagnostic when there's a destroy:
on a copyable type does make sense, though.
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 don't know what to do about C++ types a non-trivial destructor. Warn about and ignore the destroy:
operation, perhaps?
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.
Warn about and ignore the destroy: operation, perhaps?
I think that makes sense.
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've done that in the latest commit
When we cannot respect the "destroy:" annotation, mark the type as deprecated with a message thst says why there is a problem. There are various potential problems: * Multiple conflicting destroy functions * Destroy functions that don't meet the pattern * Type isn't imported as a move-only type * Type has a non-trivial destructor (in C++)
@swift-ci please smoke test |
This packages up the ~Copyable and "destroy" attributes in a macro.
@swift-ci please smoke test |
@swift-ci please smoke test windows |
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.
Thank you, this looks awesome! I did some nitpicking inline.
@@ -45,7 +45,7 @@ | |||
#define _CXX_INTEROP_CONCAT(...) \ | |||
_CXX_INTEROP_CONCAT_(__VA_ARGS__,,,,,,,,,,,,,,,,,) | |||
|
|||
/// Specifies that a C++ `class` or `struct` is reference-counted using | |||
/// Specifies that a C `class` or `struct` is reference-counted using |
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.
/// Specifies that a C `class` or `struct` is reference-counted using | |
/// Specifies that a `class` or `struct` is reference-counted using |
#define SWIFT_NONCOPYABLE \ | ||
__attribute__((swift_attr("~Copyable"))) | ||
|
||
/// Specifies that a class or struct should be imported as a non-copyable |
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.
/// Specifies that a class or struct should be imported as a non-copyable | |
/// Specifies that a `class` or `struct` should be imported as a non-copyable |
#define SWIFT_NONCOPYABLE \ | ||
__attribute__((swift_attr("~Copyable"))) | ||
|
||
/// Specifies that a class or struct should be imported as a non-copyable | ||
/// Swift value type that calls the given _destroy function when a value is no |
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.
/// Swift value type that calls the given _destroy function when a value is no | |
/// Swift value type that calls the given `_destroy` function when a value is no |
/// // once mt2 is unused, Swift will call mytypeFreeMembers(mt2) | ||
#define SWIFT_NONCOPYABLE_WITH_DESTROY(_destroy) \ |
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.
/// // once mt2 is unused, Swift will call mytypeFreeMembers(mt2) | |
#define SWIFT_NONCOPYABLE_WITH_DESTROY(_destroy) \ | |
/// // once mt2 is unused, Swift will call mytypeFreeMembers(mt2) | |
/// ``` | |
#define SWIFT_NONCOPYABLE_WITH_DESTROY(_destroy) \ |
@swift-ci please smoke test |
I also did a pass through |
@swift-ci please smoke test macOS |
A C struct can be imported as noncopyable, but C doesn't have
destructors, so there is no way to provide user-defined logic to
perform the destruction. Introduce a new swift_attr that applies to
imported noncopyable types and which provides such a "destroy"
operation. It can be used like this:
This will bring the WGPUAdapterInfo struct in as a noncopyable type
that will be cleaned up by calling wgpuAdapterInfoFreeMembers once it
is no longer in use.
Implements rdar://156889370.