Skip to content

[cxx-interop] Allow Swift to access non-public C++ members #79093

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 9 commits into from
Feb 18, 2025

Conversation

j-hui
Copy link
Contributor

@j-hui j-hui commented Feb 1, 2025

This patch introduces an a C++ class annotation, SWIFT_PRIVATE_FILEID, which will specify where Swift extensions of that class will be allowed to access its non-public members, e.g.:

class SWIFT_PRIVATE_FILEID("MyModule/MyFile.swift") Foo { ... };

The goal of this feature is to help C++ developers incrementally migrate the implementation of their C++ classes to Swift, without breaking encapsulation and indiscriminately exposing those classes' private and protected fields.

rdar://137764620


This patch is a reimplementation of the feature work from #77726, which got too large to be properly tested and reviewed. I first proposed this feature on Swift Forums: https://forums.swift.org/t/feature-proposal-accessing-non-public-c-members-from-swift/76116.

For those reviewing: I'd like feedback one aspect of this feature/patch, which is the name of the annotation. Specifically, I'm worried about it being named "Swift private" because that that could be confused with the existing swift_private attribute (which tells ClangImporter not to import a field).

Several other pieces of follow-up/concurrent work are needed (though none block this patch):

This patch introduces an a C++ class annotation, SWIFT_PRIVATE_FILEID,
which will specify where Swift extensions of that class will be allowed
to access its non-public members, e.g.:

    class SWIFT_PRIVATE_FILEID("MyModule/MyFile.swift") Foo { ... };

The goal of this feature is to help C++ developers incrementally migrate
the implementation of their C++ classes to Swift, without breaking
encapsulation and indiscriminately exposing those classes' private and
protected fields.

rdar://137764620
@j-hui
Copy link
Contributor Author

j-hui commented Feb 1, 2025

@swift-ci please smoke test

j-hui added a commit that referenced this pull request Feb 2, 2025
This patch is follow-up work from #78942 and concurrent with #79093.
It imports non-public members, which were previously not being imported.
Once #79093 (SWIFT_PRIVATE_FILEID) is merged in, these inherited members
will be available within certain Swift extensions.
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.

Looks good!

-       Make StringRef("private_fileid:") a constexpr
-       Move fileID handling code into SourceFile::FileIDStr class
-       Document what is a valid fileID format
-       Diagnose multiple/invalid SWIFT_PRIVATE_FILEID annotations
-       Handle multiple SWIFT_PRIVATE_FILEID annotations, even though
        it should not be valid (makes it robust against feature changes
        and diagnostic implementation issues)
@j-hui
Copy link
Contributor Author

j-hui commented Feb 13, 2025

@swift-ci Please test

@j-hui
Copy link
Contributor Author

j-hui commented Feb 13, 2025

Thanks for the feedback @DougGregor and @Xazax-hun ! I think I addressed most of the points; I'll add the test case that Gabor suggested tomorrow.

I also think it would be nice to add a test case to ensure the diagnostics are behaving as expected.

Test cases:

  • check that this works for annotated class templates
  • check diagnostics for various invalid SWIFT_PRIVATE_FILEID attrs

@j-hui
Copy link
Contributor Author

j-hui commented Feb 13, 2025

@swift-ci Please test

@j-hui
Copy link
Contributor Author

j-hui commented Feb 13, 2025

@swift-ci Please test

@j-hui j-hui requested a review from DougGregor February 14, 2025 06:39
@j-hui
Copy link
Contributor Author

j-hui commented Feb 14, 2025

@swift-ci please test windows platform

@j-hui
Copy link
Contributor Author

j-hui commented Feb 15, 2025

@swift-ci please test windows platform

@j-hui j-hui requested a review from fahadnayyar as a code owner February 15, 2025 04:20
@j-hui
Copy link
Contributor Author

j-hui commented Feb 15, 2025

@swift-ci please test

@j-hui
Copy link
Contributor Author

j-hui commented Feb 17, 2025

@swift-ci please smoke test

@j-hui j-hui merged commit bdf2294 into swiftlang:main Feb 18, 2025
3 checks passed
@j-hui j-hui deleted the feat-SWIFT_PRIVATE_FILEID branch February 18, 2025 19:22
@j-hui j-hui added c++ interop Feature: Interoperability with C++ c++ to swift Feature → c++ interop: c++ to swift access control Feature → modifiers: Access control and access levels and removed c++ to swift Feature → c++ interop: c++ to swift labels Mar 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
access control Feature → modifiers: Access control and access levels c++ interop Feature: Interoperability with C++
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants