Skip to content

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

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

Closed
wants to merge 36 commits into from

Conversation

j-hui
Copy link
Contributor

@j-hui j-hui commented Nov 20, 2024

This patch begins to implement the feature pitched on Swift Forums: https://forums.swift.org/t/feature-proposal-accessing-non-public-c-members-from-swift/76116

rdar://137764620

This patch is only a proof of concept for now.

It only works for fields, and not any other non-public members (which
aren't being imported yet).

This patch also does not do any sanity checking for the swift_attr.
@j-hui
Copy link
Contributor Author

j-hui commented Nov 20, 2024

This is still a draft PR. It currently builds and successfully compiles a small Swift project that reads a private integer field in a C++ class.

Still missing:

  • import non-public methods into Swift
  • sanity checking and diagnostics for malformed __swift_attr__
  • import non-public static functions into Swift
  • import inherited non-public members into Swift
  • handle nested non-public inheritance
  • import non-public nested classes into Swift
  • import non-public nested enums into Swift
  • import non-public typedefs into Swift
  • tests, tests, tests! (does not block review, though helpful)
  • code formatting (does not block review)
  • write le documentation

Open questions:

  • how/whether to support nested annotations
  • bike-shedding on what the attribute and macro should be named

Side quests (out of scope, but might be nice to fix while I'm at it):

  • Handle importing multiply inherited members with overlapping names

@j-hui
Copy link
Contributor Author

j-hui commented Nov 20, 2024

@swift-ci please smoke test

@j-hui
Copy link
Contributor Author

j-hui commented Nov 20, 2024

@swift-ci smoke test

@j-hui
Copy link
Contributor Author

j-hui commented Nov 21, 2024

@swift-ci please smoke test

@j-hui
Copy link
Contributor Author

j-hui commented Nov 21, 2024

@swift-ci please smoke test

@j-hui
Copy link
Contributor Author

j-hui commented Nov 21, 2024

@swift-ci please smoke test

@j-hui
Copy link
Contributor Author

j-hui commented Nov 22, 2024

Marking this as ready for review, but it's still missing a few tests:

  • thoroughly test (possibly nested, possibly multiple) non-public inheritance
  • thoroughly test non-public nested class definitions
  • test out the sanity checks and diagnostics I wrote
  • test out non-public union members
  • some executable tests (just to make sure that things work past access checking, which happens in semantic analysis)

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.

I have not looked at the tests yet as there are some TODOs around there, but the code changes look good to me so far.

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.

Overall the approach looks great to me! I only have a couple minor comments.

Comment on lines 4 to 12
// Override this to test structs
#ifndef TEST_CLASS
#define TEST_CLASS class
#endif

// Override this to test protected
#ifndef TEST_PRIVATE
#define TEST_PRIVATE private
#endif
Copy link
Contributor

Choose a reason for hiding this comment

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

I think I would prefer having a few (mostly) copy-pasted types over these macros, since the macros make it harder to debug test failures and make small incremental changes to the 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.

I agree with the sentiment you express and inlined some of the other macros I was using.

However, I actually ended up keeping these specific overrides because the visibility of members here should actually behave exactly the same whether the record is a struct or a class, and whether the members are private or protected. In other words, these macros here only superficially quadruple our test coverage, because they are exercising the exact same code path in ClangImporter.

Copy link
Member

@DougGregor DougGregor 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 looking really good

@j-hui j-hui marked this pull request as draft December 13, 2024 00:49
@fahadnayyar fahadnayyar self-requested a review December 13, 2024 17:46
@j-hui
Copy link
Contributor Author

j-hui commented Feb 15, 2025

Superseded by #79093

@j-hui j-hui closed this Feb 15, 2025
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.

4 participants