Skip to content

[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

Merged

Conversation

DougGregor
Copy link
Member

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.

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.
@DougGregor
Copy link
Member 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.

This is great! Some nits inline.

Comment on lines 2714 to 2717
if (!attributeName.starts_with("destroy:"))
continue;

auto destroyFuncName = attributeName.drop_front(strlen("destroy:"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!attributeName.starts_with("destroy:"))
continue;
auto destroyFuncName = attributeName.drop_front(strlen("destroy:"));
if (!attributeName.consume_front("destroy:"))
continue;

Copy link
Member Author

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!

/// Function body synthesizer for a deinit of a noncopyable type, which
/// passes "self" to the given "destroy" function.
static std::pair<BraceStmt *, bool>
synthesizeDeinitBodyForCustomDestroy(
Copy link
Contributor

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"
}
Copy link
Contributor

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 {
Copy link
Contributor

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.

Copy link
Member Author

@DougGregor DougGregor Jul 28, 2025

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.

Copy link
Member Author

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?

Copy link
Contributor

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.

Copy link
Member Author

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++)
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor DougGregor enabled auto-merge July 28, 2025 20:56
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

@swift-ci please smoke test windows

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.

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// 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

Comment on lines 187 to 188
/// // once mt2 is unused, Swift will call mytypeFreeMembers(mt2)
#define SWIFT_NONCOPYABLE_WITH_DESTROY(_destroy) \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// // 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) \

@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

I also did a pass through <swift/bridging> to remove unnecessary mentions of C++ throughout. Only a few things there are actually C++-specific.

@DougGregor
Copy link
Member Author

@swift-ci please smoke test macOS

@DougGregor DougGregor merged commit 9194fa0 into swiftlang:main Jul 30, 2025
3 checks passed
@DougGregor DougGregor deleted the noncopyable-imported-c-struct-destroy branch July 30, 2025 04:02
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.

2 participants