Skip to content

Commit e845af8

Browse files
authored
Merge pull request #39550 from DougGregor/require-explicit-sendable
Add `-require-explicit-sendable` to warn about non-Sendable public types
2 parents a5e6800 + 62edd18 commit e845af8

11 files changed

+494
-75
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1954,6 +1954,15 @@ NOTE(non_sendable_nominal,none,
19541954
NOTE(add_nominal_sendable_conformance,none,
19551955
"consider making %0 %1 conform to the 'Sendable' protocol",
19561956
(DescriptiveDeclKind, DeclName))
1957+
WARNING(public_decl_needs_sendable,none,
1958+
"public %0 %1 does not specify whether it is 'Sendable' or not",
1959+
(DescriptiveDeclKind, DeclName))
1960+
NOTE(explicit_unchecked_sendable,none,
1961+
"add '@unchecked Sendable' conformance to %0 %1 if this type manually implements concurrency safety",
1962+
(DescriptiveDeclKind, DeclName))
1963+
NOTE(explicit_disable_sendable,none,
1964+
"make %0 %1 explicitly non-Sendable to suppress this warning",
1965+
(DescriptiveDeclKind, DeclName))
19571966

19581967
NOTE(required_by_opaque_return,none,
19591968
"required by opaque return type of %0 %1", (DescriptiveDeclKind, DeclName))

include/swift/Basic/LangOptions.h

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

182+
/// Require public declarations to declare that they are Sendable (or not).
183+
bool RequireExplicitSendable = false;
184+
182185
/// If false, '#file' evaluates to the full path rather than a
183186
/// human-readable string.
184187
bool EnableConcisePoundFile = false;

include/swift/Option/Options.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,10 @@ def require_explicit_availability_target : Separate<["-"], "require-explicit-ava
413413
HelpText<"Suggest fix-its adding @available(<target>, *) to public declarations without availability">,
414414
MetaVarName<"<target>">;
415415

416+
def require_explicit_sendable : Flag<["-"], "require-explicit-sendable">,
417+
Flags<[FrontendOption, NoInteractiveOption]>,
418+
HelpText<"Require explicit Sendable annotations on public declarations">;
419+
416420
def define_availability : Separate<["-"], "define-availability">,
417421
Flags<[FrontendOption, NoInteractiveOption]>,
418422
HelpText<"Define an availability macro in the format 'macroName : iOS 13.0, macOS 10.15'">,

lib/Driver/ToolChains.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ void ToolChain::addCommonFrontendArgs(const OutputInfo &OI,
233233
inputArgs.AddLastArg(arguments, options::OPT_enable_library_evolution);
234234
inputArgs.AddLastArg(arguments, options::OPT_require_explicit_availability);
235235
inputArgs.AddLastArg(arguments, options::OPT_require_explicit_availability_target);
236+
inputArgs.AddLastArg(arguments, options::OPT_require_explicit_sendable);
236237
inputArgs.AddLastArg(arguments, options::OPT_enable_testing);
237238
inputArgs.AddLastArg(arguments, options::OPT_enable_private_imports);
238239
inputArgs.AddLastArg(arguments, options::OPT_g_Group);

lib/Frontend/CompilerInvocation.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,7 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
575575
}
576576
}
577577

578+
Opts.RequireExplicitSendable |= Args.hasArg(OPT_require_explicit_sendable);
578579
for (const Arg *A : Args.filtered(OPT_define_availability)) {
579580
Opts.AvailabilityMacros.push_back(A->getValue());
580581
}

lib/Sema/TypeCheckAvailability.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2446,6 +2446,7 @@ bool swift::diagnoseExplicitUnavailability(SourceLoc loc,
24462446
auto proto = rootConf->getProtocol()->getDeclaredInterfaceType();
24472447

24482448
StringRef platform;
2449+
auto behavior = DiagnosticBehavior::Unspecified;
24492450
switch (attr->getPlatformAgnosticAvailability()) {
24502451
case PlatformAgnosticAvailabilityKind::Deprecated:
24512452
llvm_unreachable("shouldn't see deprecations in explicit unavailability");
@@ -2456,6 +2457,12 @@ bool swift::diagnoseExplicitUnavailability(SourceLoc loc,
24562457
// This was platform-specific; indicate the platform.
24572458
platform = attr->prettyPlatformString();
24582459
break;
2460+
} else if (rootConf->getProtocol()->isSpecificProtocol(
2461+
KnownProtocolKind::Sendable) &&
2462+
!ctx.LangOpts.isSwiftVersionAtLeast(6)) {
2463+
// Downgrade unavailable Sendable conformances to warnings prior to
2464+
// Swift 6.
2465+
behavior = DiagnosticBehavior::Warning;
24592466
}
24602467
LLVM_FALLTHROUGH;
24612468

@@ -2474,7 +2481,8 @@ bool swift::diagnoseExplicitUnavailability(SourceLoc loc,
24742481
EncodedDiagnosticMessage EncodedMessage(attr->Message);
24752482
diags.diagnose(loc, diag::conformance_availability_unavailable,
24762483
type, proto,
2477-
platform.empty(), platform, EncodedMessage.Message);
2484+
platform.empty(), platform, EncodedMessage.Message)
2485+
.limitBehavior(behavior);
24782486

24792487
switch (attr->getVersionAvailability(ctx)) {
24802488
case AvailableVersionComparison::Available:

0 commit comments

Comments
 (0)