Skip to content

Commit 72f49bd

Browse files
authored
Merge pull request #77342 from swiftlang/gaborh/import-as-unsafe-for-real
[cxx-interop] Import some C++ methods as Unsafe
2 parents a9efa84 + d885dec commit 72f49bd

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8226,18 +8226,27 @@ bool swift::importer::isMutabilityAttr(const clang::SwiftAttrAttr *swiftAttr) {
82268226
swiftAttr->getAttribute() == "nonmutating";
82278227
}
82288228

8229-
static bool importAsUnsafe(ASTContext &context, const clang::RecordDecl *decl,
8229+
static bool importAsUnsafe(ASTContext &context, const clang::NamedDecl *decl,
82308230
const Decl *MappedDecl) {
82318231
if (!context.LangOpts.hasFeature(Feature::SafeInterop) ||
8232-
!context.LangOpts.hasFeature(Feature::AllowUnsafeAttribute) || !decl)
8232+
!context.LangOpts.hasFeature(Feature::AllowUnsafeAttribute))
82338233
return false;
82348234

8235+
if (isa<clang::CXXMethodDecl>(decl) &&
8236+
!evaluateOrDefault(context.evaluator, IsSafeUseOfCxxDecl({decl, context}),
8237+
{}))
8238+
return true;
8239+
82358240
if (isa<ClassDecl>(MappedDecl))
82368241
return false;
82378242

8238-
return evaluateOrDefault(
8239-
context.evaluator, ClangTypeEscapability({decl->getTypeForDecl()}),
8240-
CxxEscapability::Unknown) == CxxEscapability::Unknown;
8243+
if (const auto *record = dyn_cast<clang::RecordDecl>(decl))
8244+
return evaluateOrDefault(context.evaluator,
8245+
ClangTypeEscapability({record->getTypeForDecl()}),
8246+
CxxEscapability::Unknown) ==
8247+
CxxEscapability::Unknown;
8248+
8249+
return false;
82418250
}
82428251

82438252
void
@@ -8419,9 +8428,7 @@ ClangImporter::Implementation::importSwiftAttrAttributes(Decl *MappedDecl) {
84198428
}
84208429
}
84218430

8422-
if (seenUnsafe ||
8423-
importAsUnsafe(SwiftContext, dyn_cast<clang::RecordDecl>(ClangDecl),
8424-
MappedDecl)) {
8431+
if (seenUnsafe || importAsUnsafe(SwiftContext, ClangDecl, MappedDecl)) {
84258432
auto attr = new (SwiftContext) UnsafeAttr(/*implicit=*/!seenUnsafe);
84268433
MappedDecl->getAttrs().add(attr);
84278434
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ struct UnknownEscapabilityAggregate {
4646
Unannotated unann;
4747
};
4848

49+
struct MyContainer {
50+
int begin() const { return 0; }
51+
int end() const { return -1; }
52+
};
53+
4954
//--- test.swift
5055

5156
import Test
@@ -61,7 +66,8 @@ func useUnsafeParam2(x: UnsafeReference) { // expected-warning{{reference to uns
6166
func useUnsafeParam3(x: UnknownEscapabilityAggregate) { // expected-warning{{reference to unsafe struct 'UnknownEscapabilityAggregate'}}
6267
}
6368

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

6773
func useCfType(x: CFArray) {

0 commit comments

Comments
 (0)