Skip to content

Commit c859557

Browse files
author
Gabor Horvath
committed
[cxx-interop] Remove duplicated logic to import types as unsafe
After PR #79424 was merged the compiler proper is doing inference on what C++ types should be considered unsafe. Remove the duplicated (and slightly divergent) logic from the importer as we no longer need it and we should have a consistent view of what is considered unsafe. The only divergence left is the old logic that renames some methods to have "Unsafe" in their names. In the future, we want to get rid of this behavior (potentially under a new interop version).
1 parent 9ffdbbf commit c859557

File tree

2 files changed

+6
-31
lines changed

2 files changed

+6
-31
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8501,36 +8501,6 @@ bool swift::importer::isMutabilityAttr(const clang::SwiftAttrAttr *swiftAttr) {
85018501
swiftAttr->getAttribute() == "nonmutating";
85028502
}
85038503

8504-
static bool importAsUnsafe(ClangImporter::Implementation &impl,
8505-
const clang::NamedDecl *decl,
8506-
const Decl *MappedDecl) {
8507-
auto &context = impl.SwiftContext;
8508-
if (!context.LangOpts.hasFeature(Feature::AllowUnsafeAttribute))
8509-
return false;
8510-
8511-
if (isa<clang::CXXMethodDecl>(decl) &&
8512-
!evaluateOrDefault(context.evaluator, IsSafeUseOfCxxDecl({decl}), {}))
8513-
return true;
8514-
8515-
if (isa<ClassDecl>(MappedDecl))
8516-
return false;
8517-
8518-
// Most STL containers have std::allocator as their default allocator. We need
8519-
// to consider std::allocator safe for the STL containers to be ever
8520-
// considered safe.
8521-
if (decl->isInStdNamespace() && decl->getIdentifier() &&
8522-
decl->getName() == "allocator")
8523-
return false;
8524-
8525-
if (const auto *record = dyn_cast<clang::RecordDecl>(decl))
8526-
return evaluateOrDefault(
8527-
context.evaluator,
8528-
ClangTypeEscapability({record->getTypeForDecl(), &impl, false}),
8529-
CxxEscapability::Unknown) == CxxEscapability::Unknown;
8530-
8531-
return false;
8532-
}
8533-
85348504
void ClangImporter::Implementation::importNontrivialAttribute(
85358505
Decl *MappedDecl, llvm::StringRef AttrString) {
85368506
bool cached = true;
@@ -8717,7 +8687,11 @@ ClangImporter::Implementation::importSwiftAttrAttributes(Decl *MappedDecl) {
87178687
importNontrivialAttribute(MappedDecl, swiftAttr->getAttribute());
87188688
}
87198689

8720-
if (seenUnsafe || importAsUnsafe(*this, ClangDecl, MappedDecl)) {
8690+
bool importUnsafeHeuristic =
8691+
isa<clang::CXXMethodDecl>(ClangDecl) &&
8692+
!evaluateOrDefault(SwiftContext.evaluator,
8693+
IsSafeUseOfCxxDecl({ClangDecl}), {});
8694+
if (seenUnsafe || importUnsafeHeuristic) {
87218695
auto attr = new (SwiftContext) UnsafeAttr(/*implicit=*/!seenUnsafe);
87228696
MappedDecl->getAttrs().add(attr);
87238697
}

test/Interop/Cxx/class/safe-interop-mode.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ struct SWIFT_ESCAPABLE Owner {};
3434

3535
struct Unannotated {
3636
Unannotated();
37+
int *pointer;
3738
};
3839

3940
struct SWIFT_UNSAFE_REFERENCE UnsafeReference {};

0 commit comments

Comments
 (0)