37
37
#include " swift/AST/PrettyStackTrace.h"
38
38
#include " swift/AST/ProtocolConformance.h"
39
39
#include " swift/AST/Stmt.h"
40
+ #include " swift/AST/Type.h"
40
41
#include " swift/AST/TypeCheckRequests.h"
41
42
#include " swift/AST/Types.h"
42
43
#include " swift/Basic/Assertions.h"
@@ -8154,6 +8155,21 @@ bool swift::importer::isMutabilityAttr(const clang::SwiftAttrAttr *swiftAttr) {
8154
8155
swiftAttr->getAttribute () == " nonmutating" ;
8155
8156
}
8156
8157
8158
+ static bool importAsUnsafe (const ASTContext &context,
8159
+ const clang::RecordDecl *decl,
8160
+ const Decl *MappedDecl) {
8161
+ if (!context.LangOpts .hasFeature (Feature::SafeInterop) ||
8162
+ !context.LangOpts .hasFeature (Feature::AllowUnsafeAttribute) || !decl)
8163
+ return false ;
8164
+
8165
+ if (isa<ClassDecl>(MappedDecl))
8166
+ return false ;
8167
+
8168
+ // TODO: Add logic to cover structural rules.
8169
+ return !importer::hasNonEscapableAttr (decl) &&
8170
+ !importer::hasEscapableAttr (decl);
8171
+ }
8172
+
8157
8173
void
8158
8174
ClangImporter::Implementation::importSwiftAttrAttributes (Decl *MappedDecl) {
8159
8175
auto ClangDecl =
@@ -8178,6 +8194,7 @@ ClangImporter::Implementation::importSwiftAttrAttributes(Decl *MappedDecl) {
8178
8194
//
8179
8195
// __attribute__((swift_attr("attribute")))
8180
8196
//
8197
+ bool seenUnsafe = false ;
8181
8198
for (auto swiftAttr : ClangDecl->specific_attrs <clang::SwiftAttrAttr>()) {
8182
8199
// FIXME: Hard-code @MainActor and @UIActor, because we don't have a
8183
8200
// point at which to do name lookup for imported entities.
@@ -8287,8 +8304,7 @@ ClangImporter::Implementation::importSwiftAttrAttributes(Decl *MappedDecl) {
8287
8304
if (swiftAttr->getAttribute () == " unsafe" ) {
8288
8305
if (!SwiftContext.LangOpts .hasFeature (Feature::AllowUnsafeAttribute))
8289
8306
continue ;
8290
- auto attr = new (SwiftContext) UnsafeAttr (/* implicit=*/ false );
8291
- MappedDecl->getAttrs ().add (attr);
8307
+ seenUnsafe = true ;
8292
8308
continue ;
8293
8309
}
8294
8310
@@ -8332,6 +8348,13 @@ ClangImporter::Implementation::importSwiftAttrAttributes(Decl *MappedDecl) {
8332
8348
swiftAttr->getAttribute ());
8333
8349
}
8334
8350
}
8351
+
8352
+ if (seenUnsafe ||
8353
+ importAsUnsafe (SwiftContext, dyn_cast<clang::RecordDecl>(ClangDecl),
8354
+ MappedDecl)) {
8355
+ auto attr = new (SwiftContext) UnsafeAttr (/* implicit=*/ !seenUnsafe);
8356
+ MappedDecl->getAttrs ().add (attr);
8357
+ }
8335
8358
};
8336
8359
importAttrsFromDecl (ClangDecl);
8337
8360
0 commit comments