Skip to content

[cxx-interop] Import some C++ methods as Unsafe #77342

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

static bool importAsUnsafe(ASTContext &context, const clang::RecordDecl *decl,
static bool importAsUnsafe(ASTContext &context, const clang::NamedDecl *decl,
const Decl *MappedDecl) {
if (!context.LangOpts.hasFeature(Feature::SafeInterop) ||
!context.LangOpts.hasFeature(Feature::AllowUnsafeAttribute) || !decl)
!context.LangOpts.hasFeature(Feature::AllowUnsafeAttribute))
return false;

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

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

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

return false;
}

void
Expand Down Expand Up @@ -8395,9 +8404,7 @@ ClangImporter::Implementation::importSwiftAttrAttributes(Decl *MappedDecl) {
}
}

if (seenUnsafe ||
importAsUnsafe(SwiftContext, dyn_cast<clang::RecordDecl>(ClangDecl),
MappedDecl)) {
if (seenUnsafe || importAsUnsafe(SwiftContext, ClangDecl, MappedDecl)) {
auto attr = new (SwiftContext) UnsafeAttr(/*implicit=*/!seenUnsafe);
MappedDecl->getAttrs().add(attr);
}
Expand Down
8 changes: 7 additions & 1 deletion test/Interop/Cxx/class/safe-interop-mode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ struct UnknownEscapabilityAggregate {
Unannotated unann;
};

struct MyContainer {
int begin() const { return 0; }
int end() const { return -1; }
};

//--- test.swift

import Test
Expand All @@ -57,7 +62,8 @@ func useUnsafeParam2(x: UnsafeReference) { // expected-warning{{reference to uns
func useUnsafeParam3(x: UnknownEscapabilityAggregate) { // expected-warning{{reference to unsafe struct 'UnknownEscapabilityAggregate'}}
}

func useSafeParams(x: Owner, y: View, z: SafeEscapableAggregate) {
func useSafeParams(x: Owner, y: View, z: SafeEscapableAggregate, c: MyContainer) {
let _ = c.__beginUnsafe() // expected-warning{{call to unsafe instance method '__beginUnsafe'}}
}

func useCfType(x: CFArray) {
Expand Down