Skip to content

Commit 11d3547

Browse files
authored
Merge pull request #82444 from DougGregor/preconcurrency-unsafe-silence-6.2
[6.2] Allow '@unsafe' on import declarations to silence '@preconcurrency' warning
2 parents 96d6eb8 + c251897 commit 11d3547

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
@@ -833,7 +833,7 @@ SIMPLE_DECL_ATTR(sensitive, Sensitive,
833833
159)
834834

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

include/swift/AST/DiagnosticsSema.def

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

84018401
GROUPED_WARNING(preconcurrency_import_unsafe,StrictMemorySafety,none,
84028402
"'@preconcurrency' import is not memory-safe because it can silently "
8403-
"introduce data races", ())
8403+
"introduce data races; add '@unsafe' to indicate that this is unsafe", ())
84048404
GROUPED_WARNING(unsafe_without_unsafe,StrictMemorySafety,none,
84058405
"expression uses unsafe constructs but is not marked with 'unsafe'", ())
84068406
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
@@ -2504,7 +2504,8 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
25042504
// concurrency checking enabled.
25052505
if (ID->preconcurrency() &&
25062506
Ctx.LangOpts.StrictConcurrencyLevel == StrictConcurrency::Complete &&
2507-
Ctx.LangOpts.hasFeature(Feature::StrictMemorySafety)) {
2507+
Ctx.LangOpts.hasFeature(Feature::StrictMemorySafety) &&
2508+
ID->getExplicitSafety() != ExplicitSafety::Unsafe) {
25082509
diagnoseUnsafeUse(UnsafeUse::forPreconcurrencyImport(ID));
25092510
}
25102511
}

lib/Sema/TypeCheckUnsafe.cpp

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

199199
case UnsafeUse::PreconcurrencyImport: {
200200
auto importDecl = cast<ImportDecl>(use.getDecl());
201-
importDecl->diagnose(diag::preconcurrency_import_unsafe);
201+
importDecl->diagnose(diag::preconcurrency_import_unsafe)
202+
.fixItInsert(importDecl->getAttributeInsertionLoc(false), "@unsafe ");
203+
202204
return;
203205
}
204206
}
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)