Skip to content

Commit 419c477

Browse files
committed
Don't diagnose uses of @unchecked Sendable conformances
The `@unchecked` conformance is effectively the same as `@safe(unchecked)`, in that it asserts memory safety in a place where it cannot be automatically checked. But once that has been asserted, there is no reason to diagnose anywhere else. While here, drop the "unsafe declaration here" note, which isn't adding value but did add noise. Thanks, Alex!
1 parent 3ab5456 commit 419c477

File tree

6 files changed

+11
-45
lines changed

6 files changed

+11
-45
lines changed

include/swift/AST/DiagnosticsSema.def

+6-8
Original file line numberDiff line numberDiff line change
@@ -8090,9 +8090,9 @@ NOTE(note_reference_to_nonisolated_unsafe,none,
80908090
(const ValueDecl *))
80918091
NOTE(note_reference_unowned_unsafe,none,
80928092
"reference to unowned(unsafe) %kind0 is unsafe", (const ValueDecl *))
8093-
NOTE(note_use_of_unchecked_conformance_is_unsafe,none,
8094-
"%select{@unchecked|@unsafe}0 conformance of %1 to %kind2 involves unsafe code",
8095-
(bool, Type, const ValueDecl *))
8093+
NOTE(note_use_of_unsafe_conformance_is_unsafe,none,
8094+
"@unsafe conformance of %0 to %kind1 involves unsafe code",
8095+
(Type, const ValueDecl *))
80968096
GROUPED_WARNING(conformance_involves_unsafe,Unsafe,none,
80978097
"conformance of %0 to %kind1 involves unsafe code; use '@unsafe' to "
80988098
"indicate that the conformance is not memory-safe",
@@ -8106,9 +8106,9 @@ NOTE(note_type_witness_unsafe,none,
81068106

81078107
GROUPED_WARNING(override_safe_withunsafe,Unsafe,none,
81088108
"override of safe %0 with unsafe %0", (DescriptiveDeclKind))
8109-
GROUPED_WARNING(use_of_unchecked_conformance_is_unsafe,Unsafe,none,
8110-
"%select{@unchecked|@unsafe}0 conformance of %1 to %kind2 involves unsafe code",
8111-
(bool, Type, const ValueDecl *))
8109+
GROUPED_WARNING(use_of_unsafe_conformance_is_unsafe,Unsafe,none,
8110+
"@unsafe conformance of %0 to %kind1 involves unsafe code",
8111+
(Type, const ValueDecl *))
81128112
GROUPED_WARNING(reference_unowned_unsafe,Unsafe,none,
81138113
"reference to unowned(unsafe) %kind0 is unsafe", (const ValueDecl *))
81148114
GROUPED_WARNING(reference_to_nonisolated_unsafe,Unsafe,none,
@@ -8124,8 +8124,6 @@ GROUPED_WARNING(preconcurrency_import_unsafe,Unsafe,none,
81248124
"@preconcurrency import is not memory-safe because it can silently "
81258125
"introduce data races; use '@safe(unchecked)' to assert that the "
81268126
"code is memory-safe", ())
8127-
NOTE(unsafe_decl_here,none,
8128-
"unsafe %kindbase0 declared here", (const ValueDecl *))
81298127
NOTE(encapsulate_unsafe_in_enclosing_context,none,
81308128
"make %kindbase0 @safe(unchecked) to allow it to use unsafe constructs in its definition",
81318129
(const Decl *))

lib/Sema/TypeCheckAvailability.cpp

-12
Original file line numberDiff line numberDiff line change
@@ -4772,18 +4772,6 @@ swift::diagnoseConformanceAvailability(SourceLoc loc,
47724772
if (!where.getAvailability().allowsUnsafe() &&
47734773
ctx.LangOpts.hasFeature(Feature::WarnUnsafe)) {
47744774
if (auto normalConf = dyn_cast<NormalProtocolConformance>(rootConf)) {
4775-
// @unchecked Sendable conformances are considered unsafe when complete
4776-
// checking is enabled.
4777-
if (normalConf->isUnchecked() &&
4778-
normalConf->getProtocol()->isSpecificProtocol(KnownProtocolKind::Sendable) &&
4779-
normalConf->getProtocol()->getASTContext()
4780-
.LangOpts.StrictConcurrencyLevel == StrictConcurrency::Complete) {
4781-
diagnoseUnsafeUse(
4782-
UnsafeUse::forConformance(
4783-
concreteConf->getType(), conformance, loc,
4784-
where.getDeclContext()));
4785-
}
4786-
47874775
// @unsafe conformances are considered... unsafe.
47884776
if (normalConf->isUnsafe()) {
47894777
diagnoseUnsafeUse(

lib/Sema/TypeCheckType.cpp

-6
Original file line numberDiff line numberDiff line change
@@ -6699,12 +6699,6 @@ void swift::diagnoseUnsafeType(ASTContext &ctx, SourceLoc loc, Type type,
66996699
});
67006700

67016701
diagnose(specificType ? specificType : type);
6702-
6703-
if (specificType) {
6704-
if (auto specificTypeDecl = specificType->getAnyNominal()) {
6705-
specificTypeDecl->diagnose(diag::unsafe_decl_here, specificTypeDecl);
6706-
}
6707-
}
67086702
}
67096703

67106704
void swift::diagnoseUnsafeType(ASTContext &ctx, SourceLoc loc, Type type,

lib/Sema/TypeCheckUnsafe.cpp

+2-15
Original file line numberDiff line numberDiff line change
@@ -150,22 +150,10 @@ void swift::diagnoseUnsafeUse(const UnsafeUse &use, bool asNote) {
150150
case UnsafeUse::UnsafeConformance: {
151151
auto conformance = use.getConformance();
152152
ASTContext &ctx = conformance.getRequirement()->getASTContext();
153-
154-
// Figure out whether to diagnose @unchecked or @unsafe.
155-
bool isUnsafe = true;
156-
if (conformance.isConcrete()) {
157-
if (auto normal =
158-
conformance.getConcrete()->getRootNormalConformance()){
159-
if (normal->isUnchecked() && !normal->isUnsafe())
160-
isUnsafe = false;
161-
}
162-
}
163-
164153
ctx.Diags.diagnose(
165154
use.getLocation(),
166-
asNote ? diag::note_use_of_unchecked_conformance_is_unsafe
167-
: diag::use_of_unchecked_conformance_is_unsafe,
168-
isUnsafe,
155+
asNote ? diag::note_use_of_unsafe_conformance_is_unsafe
156+
: diag::use_of_unsafe_conformance_is_unsafe,
169157
use.getType(),
170158
conformance.getRequirement());
171159
return;
@@ -231,7 +219,6 @@ void swift::diagnoseUnsafeUse(const UnsafeUse &use, bool asNote) {
231219

232220
if (!asNote) {
233221
suggestUnsafeOnEnclosingDecl(loc, use.getDeclContext());
234-
decl->diagnose(diag::unsafe_decl_here, decl);
235222
}
236223

237224
return;

test/Unsafe/unsafe.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ struct SuperHolder {
8787
// -----------------------------------------------------------------------
8888
// Inheritance of @unsafe
8989
// -----------------------------------------------------------------------
90-
@unsafe class UnsafeSuper { // expected-note 2{{'UnsafeSuper' declared here}}
90+
@unsafe class UnsafeSuper {
9191
func f() { }
9292
};
9393

test/Unsafe/unsafe_concurrency.swift

+2-3
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,10 @@ func f() async { // expected-warning{{global function 'f' involves unsafe code;
3232
print(counter) // expected-note{{reference to nonisolated(unsafe) var 'counter' is unsafe in concurrently-executing code}}
3333
print(globalCounter) // expected-note{{reference to nonisolated(unsafe) var 'globalCounter' is unsafe in concurrently-executing code}}
3434

35-
acceptSendable(C()) // expected-note{{@unchecked conformance of 'C' to protocol 'Sendable' involves unsafe code}}
35+
acceptSendable(C()) // okay
3636
}
3737

38-
// expected-warning@+1{{type alias 'WeirdC' involves unsafe code; use '@unsafe' to indicate that its use is not memory-safe}}
39-
typealias WeirdC = RequiresSendable<C> // expected-note{{@unchecked conformance of 'C' to protocol 'Sendable' involves unsafe code}}
38+
typealias WeirdC = RequiresSendable<C> // okay
4039

4140

4241
@available(SwiftStdlib 5.9, *)

0 commit comments

Comments
 (0)