Skip to content

[cxx-interop] Allow more C++ decls in public Swift interfaces #78057

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 2 commits into from
Dec 10, 2024
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
23 changes: 19 additions & 4 deletions lib/Sema/TypeCheckAccess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1828,6 +1828,8 @@ class UsableFromInlineChecker : public AccessControlCheckerBase,
}
};

bool isFragileClangDecl(const clang::Decl *decl);

bool isFragileClangType(clang::QualType type) {
if (type.isNull())
return true;
Expand All @@ -1842,20 +1844,21 @@ bool isFragileClangType(clang::QualType type) {
// Pointers to non-fragile types are non-fragile.
if (underlyingTypePtr->isPointerType())
return isFragileClangType(underlyingTypePtr->getPointeeType());
if (auto tagDecl = underlyingTypePtr->getAsTagDecl())
return isFragileClangDecl(tagDecl);
return true;
}

bool isFragileClangNode(const ClangNode &node) {
auto *decl = node.getAsDecl();
if (!decl)
return false;
bool isFragileClangDecl(const clang::Decl *decl) {
// Namespaces by themselves don't impact ABI.
if (isa<clang::NamespaceDecl>(decl))
return false;
// Objective-C type declarations are compatible with library evolution.
if (isa<clang::ObjCContainerDecl>(decl))
return false;
if (auto *fd = dyn_cast<clang::FunctionDecl>(decl)) {
if (auto *ctorDecl = dyn_cast<clang::CXXConstructorDecl>(fd))
return isFragileClangDecl(ctorDecl->getParent());
if (!isa<clang::CXXMethodDecl>(decl) &&
!isFragileClangType(fd->getDeclaredReturnType())) {
for (const auto *param : fd->parameters()) {
Expand Down Expand Up @@ -1891,9 +1894,21 @@ bool isFragileClangNode(const ClangNode &node) {
return !cxxRecordDecl->isCLike() &&
!cxxRecordDecl->getDeclContext()->isExternCContext();
}
if (auto *varDecl = dyn_cast<clang::VarDecl>(decl))
return isFragileClangType(varDecl->getType());
if (auto *fieldDecl = dyn_cast<clang::FieldDecl>(decl))
return isFragileClangType(fieldDecl->getType()) ||
isFragileClangDecl(fieldDecl->getParent());
return true;
}

bool isFragileClangNode(const ClangNode &node) {
auto *decl = node.getAsDecl();
if (!decl)
return false;
return isFragileClangDecl(decl);
}

} // end anonymous namespace

/// Returns the kind of origin, implementation-only import or SPI declaration,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,24 @@ public func getMyCStruct() -> MyCStruct {
return MyCStruct()
}

extension MyCStruct {
@inlinable public var y: CInt {
get {
return self.x
}
}

@inlinable public var anotherInstanceOfSelf: MyCStruct {
get {
return MyCStruct(x: self.x + 1)
}
}
}

//--- main.swift

import UsesCLibrary

let _ = getMyCStruct()
let _ = getMyCStruct().y
let _ = getMyCStruct().anotherInstanceOfSelf