Skip to content

[cxx-interop] Import non-public inherited members #79348

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 32 commits into from
Feb 25, 2025

Conversation

j-hui
Copy link
Contributor

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

This patch is follow-up work from #78942 and concurrent with #79093.
It imports non-public members, which were previously not being imported.

The access level of each inherited base member is computed according to
the inheritance rules in C++, and accounts for public/protected/private
inheritance. Base members that are inaccessible from the derived class
(either because they are private, or due to nested private inheritance)
are also imported into Swift but marked as unavailable, to ensure that
(1) trying to access these members produces a reasonable error message
rather than claiming that the inaccessible member doesn't exist, and
(2) ambiguous member lookups aren't erroneously disambiguated by virtue
of one being public and the other private.

Once #79093 (SWIFT_PRIVATE_FILEID) is merged in, these inherited members
will be available within Swift extensions that have been blessed by the
SWIFT_PRIVATE_FILEID annotation.

rdar://137764620

@j-hui
Copy link
Contributor Author

j-hui commented Feb 13, 2025

@swift-ci Please 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.

Are there any tests where a field with the same name appears in multiple bases? We can disambiguate accessing those in C++ by using qualified lookup but it is not possible in Swift where in case of structs we put everything in the same type. So I think we might want to skip fields (and methods?) that can cause ambiguities.

(This is different from the case where the members in the derived class shadows the members in the base.)

@j-hui
Copy link
Contributor Author

j-hui commented Feb 13, 2025

Ambiguous lookups need follow-up work. Last time I tried it out, it gave me a very unhelpful error message, because the way the diagnostics are implemented don't seem to consider that kind of ambiguity.

That said, I still think we should import those ambiguous members, because it lets us report the same error from Swift as in C++ (even if that isn't quite implemented right now). This is the same reason why I am importing inaccessible private base members; without doing so, we get "missing field" errors that don't actually point to the underlying problem.

To work around an ambiguous member lookup, users can add a shim method in C++ where they perform the fully qualified lookup there.

@j-hui
Copy link
Contributor Author

j-hui commented Feb 13, 2025

I will add a test case that has ambiguous members and make sure it does not affect unambiguous member lookup.

@j-hui
Copy link
Contributor Author

j-hui commented Feb 14, 2025

@Xazax-hun I added a test case that checks ambiguous lookups, and it misbehaves somewhat (specifically, lookups that should be ambiguous do not seem to be).

There is also a pretty big discrepancy in how ClangImporter.cpp::ClangRecordMemberLookup and ImportDecl.cpp::loadAllMembersOfRecordDecl() deal with ambiguities, which causes the module interface suggest a different behavior that what we observe when type-checking various lookups.

To avoid blowing up the size of this PR, I would like to revisit this issue in follow-up work.

rdar://144843878

@j-hui
Copy link
Contributor Author

j-hui commented Feb 14, 2025

@swift-ci Please test

@j-hui
Copy link
Contributor Author

j-hui commented Feb 21, 2025

@swift-ci Please test

@j-hui
Copy link
Contributor Author

j-hui commented Feb 21, 2025

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

Thanks!

@j-hui
Copy link
Contributor Author

j-hui commented Feb 21, 2025

@swift-ci Please test

@j-hui
Copy link
Contributor Author

j-hui commented Feb 22, 2025

@swift-ci please test

@j-hui
Copy link
Contributor Author

j-hui commented Feb 22, 2025

@swift-ci please smoke test windows platform

@j-hui
Copy link
Contributor Author

j-hui commented Feb 25, 2025

@swift-ci please test

@j-hui
Copy link
Contributor Author

j-hui commented Feb 25, 2025

@swift-ci Please test

@j-hui j-hui enabled auto-merge (squash) February 25, 2025 04:12
@j-hui j-hui merged commit 66c2e2c into swiftlang:main Feb 25, 2025
4 of 5 checks passed
@j-hui j-hui added c++ interop Feature: Interoperability with C++ 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
c++ interop Feature: Interoperability with C++ c++ to swift Feature → c++ interop: c++ to swift
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants