Skip to content

[cxx-interop] Members of private base classes should not be exposed #62831

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
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5007,6 +5007,9 @@ TinyPtrVector<ValueDecl *> ClangRecordMemberLookup::evaluate(
if (auto cxxRecord =
dyn_cast<clang::CXXRecordDecl>(recordDecl->getClangDecl())) {
for (auto base : cxxRecord->bases()) {
if (base.getAccessSpecifier() != clang::AccessSpecifier::AS_public)
continue;

clang::QualType baseType = base.getType();
if (auto spectType = dyn_cast<clang::TemplateSpecializationType>(baseType))
baseType = spectType->desugar();
Expand Down
3 changes: 3 additions & 0 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8442,6 +8442,9 @@ void ClangImporter::Implementation::loadAllMembersOfRecordDecl(
// If this is a C++ record, look through the base classes too.
if (auto cxxRecord = dyn_cast<clang::CXXRecordDecl>(clangRecord)) {
for (auto base : cxxRecord->bases()) {
if (base.getAccessSpecifier() != clang::AccessSpecifier::AS_public)
continue;

clang::QualType baseType = base.getType();
if (auto spectType = dyn_cast<clang::TemplateSpecializationType>(baseType))
baseType = spectType->desugar();
Expand Down
6 changes: 6 additions & 0 deletions test/Interop/Cxx/class/inheritance/Inputs/functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ struct DerivedFromDerived : Derived {
struct __attribute__((swift_attr("import_unsafe"))) DerivedFromNonTrivial
: NonTrivial {};

struct PrivatelyInherited : private Base {
};

struct ProtectedInherited : protected Base {
};

struct EmptyBaseClass {
const char *inBase() const __attribute__((swift_attr("import_unsafe"))) {
return "EmptyBaseClass::inBase";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,11 @@
// CHECK-NEXT: @discardableResult
// CHECK-NEXT: func inNonTrivialWithArgs(_ a: Int32, _ b: Int32) -> UnsafePointer<CChar>?
// CHECK-NEXT: }

// CHECK-NEXT: struct PrivatelyInherited {
// CHECK-NEXT: init()
// CHECK-NEXT: }

// CHECK-NEXT: struct ProtectedInherited {
// CHECK-NEXT: init()
// CHECK-NEXT: }
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import Functions

PrivatelyInherited().constInBase() // expected-error {{value of type 'PrivatelyInherited' has no member 'constInBase'}}
ProtectedInherited().constInBase() // expected-error {{value of type 'ProtectedInherited' has no member 'constInBase'}}


extension Base {
public func swiftFunc() {}
}
Expand Down