[cxx-interop] Import private fields of C++ structs

While private and protected fields coming from C++ cannot be accessed from Swift, they can affect Swift typechecking.

For instance, the Swift typechecker mechanism that adds implicit `Sendable` conformances works by iterating over all of the struct's fields and checking whether all of them are `Sendable`. This logic was broken for C++ types with private fields, since they were never accounted for. This resulted in erroneous implicit `Sendable` confromances being added.

Same applies for `BitwiseCopyable`.

In addition to this, ClangImporter used to mistakenly mark all C++ structs that have private fields as types with unreferenceable storage, which hampered optimizations.

As a side effect of this change, we now also provide a better diagnostic when someone tries to access a private C++ field from Swift.

rdar://134430857
This commit is contained in:
Egor Zhdan
2024-10-07 18:52:19 +01:00
parent 4902e69ee1
commit 6943986c71
13 changed files with 128 additions and 20 deletions

View File

@@ -96,7 +96,7 @@ static AccessorDecl *makeFieldGetterDecl(ClangImporter::Implementation &Impl,
/*Throws=*/false,
/*ThrowsLoc=*/SourceLoc(), /*ThrownType=*/TypeLoc(),
params, getterType, importedDecl, clangNode);
getterDecl->setAccess(AccessLevel::Public);
getterDecl->setAccess(importedFieldDecl->getFormalAccess());
getterDecl->setIsObjC(false);
getterDecl->setIsDynamic(false);
@@ -128,7 +128,7 @@ static AccessorDecl *makeFieldSetterDecl(ClangImporter::Implementation &Impl,
setterDecl->setIsObjC(false);
setterDecl->setIsDynamic(false);
setterDecl->setSelfAccessKind(SelfAccessKind::Mutating);
setterDecl->setAccess(AccessLevel::Public);
setterDecl->setAccess(importedFieldDecl->getFormalAccess());
return setterDecl;
}