Skip to content

[cxx-interop] Support importing static factories as initializers #79288

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 1 commit into from
Mar 3, 2025

Conversation

Xazax-hun
Copy link
Contributor

This PR adds a feature to import static factory functions returning foreign reference types to be imported as initializers using the SWIFT_NAME annotation.

@Xazax-hun Xazax-hun added the c++ interop Feature: Interoperability with C++ label Feb 11, 2025
Copy link
Contributor

@egorzhdan egorzhdan left a comment

Choose a reason for hiding this comment

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

Looks great! I only have a few questions about testing.

int value = 0;

__attribute__((swift_name("init(_:)")))
__attribute__((swift_attr("returns_retained")))
Copy link
Contributor

Choose a reason for hiding this comment

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

Would using returns_unretained be reasonable here in some cases?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm. Do you mean we should assume returns_retained? Or the fact that a user could pass in 0? I change the test to not pass in the initial refcount.

Copy link
Contributor

@egorzhdan egorzhdan Feb 11, 2025

Choose a reason for hiding this comment

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

Not necessarily, I just wanted to check if we should test the other possible annotation, returns_unretained

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, I see. It feels weird for a ctor to do returns_unretained. I think we should warn on that. I can do that in a follow-up PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

Should we allow importing static factories as both returns_retained and returns_unretained for now? Once we have support for importing actual C++ constructors as Swift initializers then we can maybe think about a default behaviour for constructors and static factory methods. Seems like the default should be returns_retained but I am not aware if people are currently using returns_unretained for static factories.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will add a test for the returns_unretained case. I think it might be a good idea to add diagnostics in a separate PR in this case as those can be reverted independently of the feature itself if we encounter an idiom we did not think about.

@Xazax-hun Xazax-hun force-pushed the gaborh/import-static-factory-as-initializer branch 3 times, most recently from c893fac to 28ac70b Compare February 12, 2025 12:24
@Xazax-hun Xazax-hun requested a review from j-hui February 13, 2025 10:28
@@ -3907,6 +3907,11 @@ namespace {
// FIXME: Poor location info.
auto nameLoc = Impl.importSourceLoc(decl->getLocation());

if (auto method = dyn_cast<clang::CXXMethodDecl>(decl);
Copy link
Contributor

Choose a reason for hiding this comment

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

@Xazax-hun why did you choose this location in the importFunctionDecl function to implement this logic?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the importFunctionDecl function that has most of the function importing logic. We usually calculate the Swift Name for an imported declaration relatively early on. So this logic belongs here, and relatively early in this function (but after we have the SwiftName). There is another call site of importGlobalAsInitializer in the same function to handle the non-C++ method case, which is a good indication that this is the right place.

The easiest way to find where to put some code is to step through the import process of a simple example in the debugger and see what is the earliest point where you have all the information you need and check where would the code logically belong after that point.

Copy link
Contributor Author

@Xazax-hun Xazax-hun Mar 3, 2025

Choose a reason for hiding this comment

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

Another consideration is that we want to do this before we did any actual importingn (creating Swift AST) because we want to avoid importing the same function twice in two different ways.

Copy link
Contributor

Choose a reason for hiding this comment

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

Makes sense, thanks!

@@ -0,0 +1,36 @@
#pragma once

struct __attribute__((swift_attr("import_as_ref")))
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we use "import_refernece" or "import_as_ref" for these tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point! We actually should use import_reference.

This PR adds a feature to import static factory functions returning
foreign reference types to be imported as initializers using the
SWIFT_NAME annotation.
@Xazax-hun Xazax-hun force-pushed the gaborh/import-static-factory-as-initializer branch from 28ac70b to 2f46d55 Compare March 3, 2025 11:25
@Xazax-hun
Copy link
Contributor Author

@swift-ci please smoke test

@Xazax-hun Xazax-hun enabled auto-merge March 3, 2025 16:14
@Xazax-hun Xazax-hun merged commit f79b73a into main Mar 3, 2025
3 checks passed
@Xazax-hun Xazax-hun deleted the gaborh/import-static-factory-as-initializer branch March 3, 2025 16:17
fahadnayyar added a commit that referenced this pull request Apr 1, 2025
…Swift Initializer (#79986)

Building on top of PR #79288, this update synthesizes a static factory method using the default new operator, with a call to the default constructor expression for C++ foreign reference types, and imports them as Swift initializers.

rdar://147529406
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.

3 participants