Skip to content
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

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

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 manual cherry-picking 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):

  • Another patch to make non-public inherited members accessible in Swift. Those are not being imported yet. (This work was already done in [cxx-interop] Allow Swift extensions to access non-public C++ members #77726 so it should be pretty straightforward to port.)
  • The Swift forums pitch (specifically, the gist it links to) it a bit outdated; I need to update it to reflect the current implementation (specifically, the behavior for weird annotations etc.).
  • Update release notes/Swift documentation to talk about this feature.

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

1 participant