Skip to content

Add -require-explicit-sendable to warn about non-Sendable public types #39550

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -1949,6 +1949,15 @@ NOTE(non_sendable_nominal,none,
NOTE(add_nominal_sendable_conformance,none,
"consider making %0 %1 conform to the 'Sendable' protocol",
(DescriptiveDeclKind, DeclName))
WARNING(public_decl_needs_sendable,none,
"public %0 %1 does not specify whether it is 'Sendable' or not",
(DescriptiveDeclKind, DeclName))
NOTE(explicit_unchecked_sendable,none,
"add '@unchecked Sendable' conformance to %0 %1 if this type manually implements concurrency safety",
(DescriptiveDeclKind, DeclName))
NOTE(explicit_disable_sendable,none,
"make %0 %1 explicitly non-Sendable to suppress this warning",
(DescriptiveDeclKind, DeclName))

NOTE(required_by_opaque_return,none,
"required by opaque return type of %0 %1", (DescriptiveDeclKind, DeclName))
Expand Down
3 changes: 3 additions & 0 deletions include/swift/Basic/LangOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ namespace swift {
// Availability macros definitions to be expanded at parsing.
SmallVector<std::string, 4> AvailabilityMacros;

/// Require public declarations to declare that they are Sendable (or not).
bool RequireExplicitSendable = false;

/// If false, '#file' evaluates to the full path rather than a
/// human-readable string.
bool EnableConcisePoundFile = false;
Expand Down
4 changes: 4 additions & 0 deletions include/swift/Option/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,10 @@ def require_explicit_availability_target : Separate<["-"], "require-explicit-ava
HelpText<"Suggest fix-its adding @available(<target>, *) to public declarations without availability">,
MetaVarName<"<target>">;

def require_explicit_sendable : Flag<["-"], "require-explicit-sendable">,
Flags<[FrontendOption, NoInteractiveOption]>,
HelpText<"Require explicit Sendable annotations on public declarations">;

def define_availability : Separate<["-"], "define-availability">,
Flags<[FrontendOption, NoInteractiveOption]>,
HelpText<"Define an availability macro in the format 'macroName : iOS 13.0, macOS 10.15'">,
Expand Down
1 change: 1 addition & 0 deletions lib/Driver/ToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ void ToolChain::addCommonFrontendArgs(const OutputInfo &OI,
inputArgs.AddLastArg(arguments, options::OPT_enable_library_evolution);
inputArgs.AddLastArg(arguments, options::OPT_require_explicit_availability);
inputArgs.AddLastArg(arguments, options::OPT_require_explicit_availability_target);
inputArgs.AddLastArg(arguments, options::OPT_require_explicit_sendable);
inputArgs.AddLastArg(arguments, options::OPT_enable_testing);
inputArgs.AddLastArg(arguments, options::OPT_enable_private_imports);
inputArgs.AddLastArg(arguments, options::OPT_g_Group);
Expand Down
1 change: 1 addition & 0 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,7 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
}
}

Opts.RequireExplicitSendable |= Args.hasArg(OPT_require_explicit_sendable);
for (const Arg *A : Args.filtered(OPT_define_availability)) {
Opts.AvailabilityMacros.push_back(A->getValue());
}
Expand Down
10 changes: 9 additions & 1 deletion lib/Sema/TypeCheckAvailability.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2446,6 +2446,7 @@ bool swift::diagnoseExplicitUnavailability(SourceLoc loc,
auto proto = rootConf->getProtocol()->getDeclaredInterfaceType();

StringRef platform;
auto behavior = DiagnosticBehavior::Unspecified;
switch (attr->getPlatformAgnosticAvailability()) {
case PlatformAgnosticAvailabilityKind::Deprecated:
llvm_unreachable("shouldn't see deprecations in explicit unavailability");
Expand All @@ -2456,6 +2457,12 @@ bool swift::diagnoseExplicitUnavailability(SourceLoc loc,
// This was platform-specific; indicate the platform.
platform = attr->prettyPlatformString();
break;
} else if (rootConf->getProtocol()->isSpecificProtocol(
KnownProtocolKind::Sendable) &&
!ctx.LangOpts.isSwiftVersionAtLeast(6)) {
// Downgrade unavailable Sendable conformances to warnings prior to
// Swift 6.
behavior = DiagnosticBehavior::Warning;
}
LLVM_FALLTHROUGH;

Expand All @@ -2474,7 +2481,8 @@ bool swift::diagnoseExplicitUnavailability(SourceLoc loc,
EncodedDiagnosticMessage EncodedMessage(attr->Message);
diags.diagnose(loc, diag::conformance_availability_unavailable,
type, proto,
platform.empty(), platform, EncodedMessage.Message);
platform.empty(), platform, EncodedMessage.Message)
.limitBehavior(behavior);

switch (attr->getVersionAvailability(ctx)) {
case AvailableVersionComparison::Available:
Expand Down
Loading