Skip to content

Commit e39a31a

Browse files
authored
Merge pull request #85319 from xedin/rdar-162394810
[AST/Sema] Add a diagnostic group `ExplicitSendable` to replace `-require-explicit-sendable`
2 parents 3e0ea3a + 123198b commit e39a31a

File tree

8 files changed

+48
-9
lines changed

8 files changed

+48
-9
lines changed

include/swift/AST/DiagnosticGroups.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ GROUP(TemporaryPointers,none,"temporary-pointers")
8787
GROUP(TrailingClosureMatching,none,"trailing-closure-matching")
8888
GROUP(UnknownWarningGroup,none,"unknown-warning-group")
8989
GROUP(WeakMutability,none,"weak-mutability")
90+
GROUP(ExplicitSendable,DefaultIgnoreWarnings,"explicit-sendable-annotations")
9091

9192
GROUP_LINK(PerformanceHints,ExistentialType)
9293
GROUP_LINK(PerformanceHints,ReturnTypeImplicitCopy)

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2857,9 +2857,9 @@ WARNING(remove_public_import,none,
28572857
WARNING(remove_package_import,none,
28582858
"package import of %0 was not used in package declarations",
28592859
(Identifier))
2860-
WARNING(public_decl_needs_sendable,none,
2861-
"public %kind0 does not specify whether it is 'Sendable' or not",
2862-
(const ValueDecl *))
2860+
GROUPED_WARNING(public_decl_needs_sendable,ExplicitSendable,none,
2861+
"public %kind0 does not specify whether it is 'Sendable' or not",
2862+
(const ValueDecl *))
28632863
NOTE(explicit_disable_sendable,none,
28642864
"make %kind0 explicitly non-Sendable to suppress this warning",
28652865
(const ValueDecl *))

include/swift/Basic/LangOptions.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,6 @@ namespace swift {
245245
// Availability macros definitions to be expanded at parsing.
246246
SmallVector<std::string, 4> AvailabilityMacros;
247247

248-
/// Require public declarations to declare that they are Sendable (or not).
249-
bool RequireExplicitSendable = false;
250-
251248
/// Detect and automatically import modules' cross-import overlays.
252249
bool EnableCrossImportOverlays = false;
253250

lib/Frontend/CompilerInvocation.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1272,7 +1272,6 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
12721272
if (Args.getLastArg(OPT_debug_cycles))
12731273
Opts.DebugDumpCycles = true;
12741274

1275-
Opts.RequireExplicitSendable |= Args.hasArg(OPT_require_explicit_sendable);
12761275
for (const Arg *A : Args.filtered(OPT_define_availability)) {
12771276
Opts.AvailabilityMacros.push_back(A->getValue());
12781277
}
@@ -2724,6 +2723,14 @@ static bool ParseDiagnosticArgs(DiagnosticOptions &Opts, ArgList &Args,
27242723
}
27252724
}());
27262725
}
2726+
2727+
// `-require-explicit-sendable` is an alias to `-Wwarning ExplicitSendable`.
2728+
if (Args.hasArg(OPT_require_explicit_sendable) &&
2729+
!Args.hasArg(OPT_suppress_warnings)) {
2730+
Opts.WarningsAsErrorsRules.push_back(WarningAsErrorRule(
2731+
WarningAsErrorRule::Action::Disable, "ExplicitSendable"));
2732+
}
2733+
27272734
if (Args.hasArg(OPT_debug_diagnostic_names)) {
27282735
Opts.PrintDiagnosticNames = PrintDiagnosticNamesMode::Identifier;
27292736
}

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "swift/AST/ASTWalker.h"
2626
#include "swift/AST/Concurrency.h"
2727
#include "swift/AST/ConformanceLookup.h"
28+
#include "swift/AST/DiagnosticGroups.h"
2829
#include "swift/AST/DistributedDecl.h"
2930
#include "swift/AST/ExistentialLayout.h"
3031
#include "swift/AST/GenericEnvironment.h"
@@ -1376,7 +1377,7 @@ static bool checkSendableInstanceStorage(
13761377
void swift::diagnoseMissingExplicitSendable(NominalTypeDecl *nominal) {
13771378
// Only diagnose when explicitly requested.
13781379
ASTContext &ctx = nominal->getASTContext();
1379-
if (!ctx.LangOpts.RequireExplicitSendable)
1380+
if (ctx.Diags.isIgnoredDiagnosticGroupTree(DiagGroupID::ExplicitSendable))
13801381
return;
13811382

13821383
if (nominal->getLoc().isInvalid())

test/Concurrency/objc_require_explicit_sendable.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -emit-sil -o /dev/null -I %S/Inputs/custom-modules %s -verify -parse-as-library -Wwarning ExplicitSendable
2+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -emit-sil -o /dev/null -I %S/Inputs/custom-modules %s -verify -parse-as-library -Wwarning ExplicitSendable -strict-concurrency=targeted
3+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -emit-sil -o /dev/null -I %S/Inputs/custom-modules %s -verify -parse-as-library -Wwarning ExplicitSendable -strict-concurrency=complete
14
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -emit-sil -o /dev/null -I %S/Inputs/custom-modules %s -verify -parse-as-library -require-explicit-sendable
25
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -emit-sil -o /dev/null -I %S/Inputs/custom-modules %s -verify -parse-as-library -require-explicit-sendable -strict-concurrency=targeted
36
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -emit-sil -o /dev/null -I %S/Inputs/custom-modules %s -verify -parse-as-library -require-explicit-sendable -strict-concurrency=complete

test/Concurrency/require-explicit-sendable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
// RUN: %target-swift-frontend -Wwarning ExplicitSendable -strict-concurrency=complete %s -emit-sil -o /dev/null -verify
12
// RUN: %target-swift-frontend -require-explicit-sendable -strict-concurrency=complete %s -emit-sil -o /dev/null -verify
23

3-
44
public protocol P { }
55

66
// expected-note@+2{{consider making struct 'S1' conform to the 'Sendable' protocol}}{{18-18=: Sendable}}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Explicit Sendable annotations on public type declarations
2+
3+
If a public type doesn't have an explicit Sendable or non-Sendable annotation it is sometimes hard to discern whether that is intentional or not, especially if a type could be Sendable.
4+
5+
## Overview
6+
7+
The Swift compiler would emit a warning if a public type has none of the following:
8+
9+
- A conformance to `Sendable` protocol;
10+
- An unavailable conformance to `Sendable` protocol;
11+
- `~Sendable` conformance to suppress the inference.
12+
13+
Let's consider a simple public type without any Senable annotations:
14+
15+
```
16+
public struct S {
17+
let x: Int
18+
}
19+
```
20+
21+
When compiling with `-Wwarning ExplicitSendable` the following warning is going to be produced by the Swift compiler:
22+
23+
```
24+
1 | public struct S {
25+
| |- warning: public struct 'S' does not specify whether it is 'Sendable' or not [#ExplicitSendable]
26+
| |- note: consider making struct 'S' conform to the 'Sendable' protocol
27+
| `- note: make struct 'S' explicitly non-Sendable to suppress this warning
28+
2 | let x: Int
29+
3 | }
30+
```

0 commit comments

Comments
 (0)