3737#include " swift/AST/PrettyStackTrace.h"
3838#include " swift/AST/ProtocolConformance.h"
3939#include " swift/AST/Stmt.h"
40+ #include " swift/AST/Type.h"
4041#include " swift/AST/TypeCheckRequests.h"
4142#include " swift/AST/Types.h"
4243#include " swift/Basic/Assertions.h"
@@ -8154,6 +8155,21 @@ bool swift::importer::isMutabilityAttr(const clang::SwiftAttrAttr *swiftAttr) {
81548155 swiftAttr->getAttribute () == " nonmutating" ;
81558156}
81568157
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+
81578173void
81588174ClangImporter::Implementation::importSwiftAttrAttributes (Decl *MappedDecl) {
81598175 auto ClangDecl =
@@ -8178,6 +8194,7 @@ ClangImporter::Implementation::importSwiftAttrAttributes(Decl *MappedDecl) {
81788194 //
81798195 // __attribute__((swift_attr("attribute")))
81808196 //
8197+ bool seenUnsafe = false ;
81818198 for (auto swiftAttr : ClangDecl->specific_attrs <clang::SwiftAttrAttr>()) {
81828199 // FIXME: Hard-code @MainActor and @UIActor, because we don't have a
81838200 // point at which to do name lookup for imported entities.
@@ -8287,8 +8304,7 @@ ClangImporter::Implementation::importSwiftAttrAttributes(Decl *MappedDecl) {
82878304 if (swiftAttr->getAttribute () == " unsafe" ) {
82888305 if (!SwiftContext.LangOpts .hasFeature (Feature::AllowUnsafeAttribute))
82898306 continue ;
8290- auto attr = new (SwiftContext) UnsafeAttr (/* implicit=*/ false );
8291- MappedDecl->getAttrs ().add (attr);
8307+ seenUnsafe = true ;
82928308 continue ;
82938309 }
82948310
@@ -8332,6 +8348,13 @@ ClangImporter::Implementation::importSwiftAttrAttributes(Decl *MappedDecl) {
83328348 swiftAttr->getAttribute ());
83338349 }
83348350 }
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+ }
83358358 };
83368359 importAttrsFromDecl (ClangDecl);
83378360
0 commit comments