Skip to content

[cxx-interop] Remove duplicated logic to import types as unsafe #79458

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
Feb 18, 2025
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
36 changes: 5 additions & 31 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8501,36 +8501,6 @@ bool swift::importer::isMutabilityAttr(const clang::SwiftAttrAttr *swiftAttr) {
swiftAttr->getAttribute() == "nonmutating";
}

static bool importAsUnsafe(ClangImporter::Implementation &impl,
const clang::NamedDecl *decl,
const Decl *MappedDecl) {
auto &context = impl.SwiftContext;
if (!context.LangOpts.hasFeature(Feature::AllowUnsafeAttribute))
return false;

if (isa<clang::CXXMethodDecl>(decl) &&
!evaluateOrDefault(context.evaluator, IsSafeUseOfCxxDecl({decl}), {}))
return true;

if (isa<ClassDecl>(MappedDecl))
return false;

// Most STL containers have std::allocator as their default allocator. We need
// to consider std::allocator safe for the STL containers to be ever
// considered safe.
if (decl->isInStdNamespace() && decl->getIdentifier() &&
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We no longer need this exception as unannotated types without pointer members are considered safe now.

decl->getName() == "allocator")
return false;

if (const auto *record = dyn_cast<clang::RecordDecl>(decl))
return evaluateOrDefault(
context.evaluator,
ClangTypeEscapability({record->getTypeForDecl(), &impl, false}),
CxxEscapability::Unknown) == CxxEscapability::Unknown;

return false;
}

void ClangImporter::Implementation::importNontrivialAttribute(
Decl *MappedDecl, llvm::StringRef AttrString) {
bool cached = true;
Expand Down Expand Up @@ -8717,7 +8687,11 @@ ClangImporter::Implementation::importSwiftAttrAttributes(Decl *MappedDecl) {
importNontrivialAttribute(MappedDecl, swiftAttr->getAttribute());
}

if (seenUnsafe || importAsUnsafe(*this, ClangDecl, MappedDecl)) {
bool importUnsafeHeuristic =
isa<clang::CXXMethodDecl>(ClangDecl) &&
!evaluateOrDefault(SwiftContext.evaluator,
IsSafeUseOfCxxDecl({ClangDecl}), {});
if (seenUnsafe || importUnsafeHeuristic) {
auto attr = new (SwiftContext) UnsafeAttr(/*implicit=*/!seenUnsafe);
MappedDecl->getAttrs().add(attr);
}
Expand Down
1 change: 1 addition & 0 deletions test/Interop/Cxx/class/safe-interop-mode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ struct SWIFT_ESCAPABLE Owner {};

struct Unannotated {
Unannotated();
int *pointer;
};

struct SWIFT_UNSAFE_REFERENCE UnsafeReference {};
Expand Down