Skip to content

Commit 0e3da0e

Browse files
committed
Allow '@unsafe' on import declarations to silence '@preconcurrency' warning
'@preconcurrency' imports open up memory safety holes with respect to Sendable, which are diagnosed under strict memory safety + strict concurrency checking. Allow one to write '@unsafe' on those imports to silence the diagnostic about it.
1 parent c34f81e commit 0e3da0e

File tree

5 files changed

+16
-4
lines changed

5 files changed

+16
-4
lines changed

include/swift/AST/DeclAttr.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,7 @@ SIMPLE_DECL_ATTR(sensitive, Sensitive,
834834
159)
835835

836836
SIMPLE_DECL_ATTR(unsafe, Unsafe,
837-
OnAbstractFunction | OnSubscript | OnVar | OnMacro | OnNominalType | OnExtension | OnTypeAlias | OnEnumElement,
837+
OnAbstractFunction | OnSubscript | OnVar | OnMacro | OnNominalType | OnExtension | OnTypeAlias | OnEnumElement | OnImport,
838838
UserInaccessible | ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIStableToRemove | ForbiddenInABIAttr,
839839
160)
840840

include/swift/AST/DiagnosticsSema.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8481,7 +8481,7 @@ GROUPED_WARNING(override_safe_with_unsafe,StrictMemorySafety,none,
84818481

84828482
GROUPED_WARNING(preconcurrency_import_unsafe,StrictMemorySafety,none,
84838483
"'@preconcurrency' import is not memory-safe because it can silently "
8484-
"introduce data races", ())
8484+
"introduce data races; add '@unsafe' to indicate that this is unsafe", ())
84858485
GROUPED_WARNING(unsafe_without_unsafe,StrictMemorySafety,none,
84868486
"expression uses unsafe constructs but is not marked with 'unsafe'", ())
84878487
GROUPED_WARNING(for_unsafe_without_unsafe,StrictMemorySafety,none,

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2486,7 +2486,8 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
24862486
// concurrency checking enabled.
24872487
if (ID->preconcurrency() &&
24882488
Ctx.LangOpts.StrictConcurrencyLevel == StrictConcurrency::Complete &&
2489-
Ctx.LangOpts.hasFeature(Feature::StrictMemorySafety)) {
2489+
Ctx.LangOpts.hasFeature(Feature::StrictMemorySafety) &&
2490+
ID->getExplicitSafety() != ExplicitSafety::Unsafe) {
24902491
diagnoseUnsafeUse(UnsafeUse::forPreconcurrencyImport(ID));
24912492
}
24922493
}

lib/Sema/TypeCheckUnsafe.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,9 @@ void swift::diagnoseUnsafeUse(const UnsafeUse &use) {
195195

196196
case UnsafeUse::PreconcurrencyImport: {
197197
auto importDecl = cast<ImportDecl>(use.getDecl());
198-
importDecl->diagnose(diag::preconcurrency_import_unsafe);
198+
importDecl->diagnose(diag::preconcurrency_import_unsafe)
199+
.fixItInsert(importDecl->getAttributeInsertionLoc(false), "@unsafe ");
200+
199201
return;
200202
}
201203
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend -emit-module-path %t/unsafe_swift_decls.swiftmodule %S/Inputs/unsafe_swift_decls.swift
3+
4+
// RUN: %target-typecheck-verify-swift -strict-memory-safety -enable-experimental-feature StrictConcurrency -I %t
5+
6+
// REQUIRES: concurrency
7+
// REQUIRES: swift_feature_StrictConcurrency
8+
9+
@preconcurrency @unsafe import unsafe_swift_decls

0 commit comments

Comments
 (0)