Skip to content

Conversation

susmonteiro
Copy link
Contributor

@susmonteiro susmonteiro commented Sep 23, 2025

SWIFT_COPYABLE_IF is similar to SWIFT_ESCAPABLE_IF, allowing us to import a C++ type as copyable/non-copyable depending on some other types.

rdar://158852663

@susmonteiro
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.

This looks great! I only had some couple nits inline.

if (result.has_value())
return result.value();

if (importerImpl)
Copy link
Contributor

Choose a reason for hiding this comment

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

This was a bug in the original code, but in case we figure out early that a type is non-copyable or non-escapable based on checking the first template argument, we will never diagnose that the annotation is not correct for the second template argument. I think this is probably not too bad, but maybe we want to have a rdar or a github issue so we can address this later.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixing the annotation would never change the way we import the type right? i.e. if the first argument determines how we import the type, we will never look at the second one. Since this is an annotation, would it make sense to emit an error on a template argument that will never be used?

/// Specifies that a C++ `class` or `struct` should be imported as a copyable
/// Swift value if all of the specified template arguments are copyable.
#define SWIFT_COPYABLE_IF(...) \
__attribute__((swift_attr("copyable_if:" _CXX_INTEROP_CONCAT(__VA_ARGS__))))
Copy link
Contributor

Choose a reason for hiding this comment

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

I know you're just following along the beaten path, but I believe if we named these attributes such that they follow the actual Swift attribute grammar, we wouldn't have to write custom parsers for each of them. E.g. if this was something like swift_attr("@Copyable(if: " _CXX_INTEROP_CONCAT(__VA_ARGS__) ")" ) the standard Swift attribute parser would be able to parse it. I think we should move over to that approach, to reduce complexity in ClangImporter. Even for existing attributes: since users are using the macro definitions, we can swap the underlying attribute without affecting anyone.

Thoughts?
@susmonteiro @Xazax-hun @egorzhdan @j-hui @fahadnayyar

Copy link
Contributor

@Xazax-hun Xazax-hun Sep 23, 2025

Choose a reason for hiding this comment

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

I believe we do not actually need to include the @ in swift_attr. I think it is not a bad idea to make it a valid attribute but we should do that in a separate PR that could be reverted independently (just in case).

Copy link
Contributor Author

@susmonteiro susmonteiro Sep 24, 2025

Choose a reason for hiding this comment

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

Makes sense to me! I agree that it should be a separate PR. Opened an issue: #84486

Refactor `ClangTypeEscapability::evaluate` so that we can reuse part of it for `CxxValueSemantics::evaluate`
@susmonteiro susmonteiro force-pushed the susmonteiro/copyable-if-annotation branch from ca8d386 to f4cf914 Compare September 24, 2025 11:00
@susmonteiro
Copy link
Contributor Author

@swift-ci please smoke test

@susmonteiro susmonteiro merged commit 00da70f into swiftlang:main Sep 25, 2025
3 checks passed
@susmonteiro susmonteiro deleted the susmonteiro/copyable-if-annotation branch September 30, 2025 11:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ interop Feature: Interoperability with C++

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants