Skip to content

Commit 8dd5920

Browse files
committed
Warn about sending on Sendable type in function parameters (#74616)
When a function parameter is Sendable, adding 'sending' annotation does nothing, as the value is safe to be sent across isolation boundaries. SE-0430 seems to imply that 'sending' is intended to be used for non-Sendable types only.
1 parent b7c241d commit 8dd5920

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7993,6 +7993,9 @@ ERROR(sending_unsupported_param_specifier, none,
79937993
"'%0' cannot be applied to a 'sending' parameter", (StringRef))
79947994
ERROR(sending_only_on_parameters_and_results, none,
79957995
"'sending' may only be used on parameters and results", ())
7996+
WARNING(sending_applied_to_sendable, none,
7997+
"'sending' has no effect on Sendable parameter %0",
7998+
(Type))
79967999
ERROR(sending_cannot_be_applied_to_tuple_elt, none,
79978000
"'sending' cannot be applied to tuple elements", ())
79988001
ERROR(sending_function_wrong_sending,none,

lib/Sema/TypeCheckType.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4975,8 +4975,29 @@ TypeResolver::resolveSendingTypeRepr(SendingTypeRepr *repr,
49754975
return ErrorType::get(getASTContext());
49764976
}
49774977

4978+
NeverNullType resolvedType = resolveType(repr->getBase(), options);
4979+
4980+
// If resolved type is Sendable, warn about unnecessary 'sending' annotation.
4981+
if (options.is(TypeResolverContext::FunctionInput)) {
4982+
if(!resolvedType->hasTypeParameter()) {
4983+
if (resolvedType->isSendableType()) {
4984+
diagnose(repr->getSpecifierLoc(),
4985+
diag::sending_applied_to_sendable,
4986+
resolvedType.get())
4987+
.fixItRemove(repr->getSpecifierLoc());
4988+
}
4989+
} else {
4990+
if (resolvedType->getASTContext().getProtocol(KnownProtocolKind::Sendable)) {
4991+
diagnose(repr->getSpecifierLoc(),
4992+
diag::sending_applied_to_sendable,
4993+
resolvedType.get())
4994+
.fixItRemove(repr->getSpecifierLoc());
4995+
};
4996+
}
4997+
}
4998+
49784999
// Return the type.
4979-
return resolveType(repr->getBase(), options);
5000+
return resolvedType;
49805001
}
49815002

49825003
NeverNullType
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// RUN: %target-swift-frontend -module-name TestModule -typecheck -verify %s
2+
3+
struct SendableType {}
4+
class NonSendableType {}
5+
6+
func testSendingOnNonSendable(input: sending NonSendableType) {
7+
// okay
8+
}
9+
10+
func testSendingOnSendable(input: sending SendableType) {
11+
// expected-warning@-1{{'sending' has no effect}}{{35-43=}}
12+
}
13+
14+
func testSendingOnSendable(input: sending some Sendable) {
15+
// expected-warning@-1{{'sending' has no effect}}{{35-43=}}
16+
}
17+
18+
func testSendingOnSendable(input: sending any Sendable) {
19+
// expected-warning@-1{{'sending' has no effect}}{{35-43=}}
20+
}

0 commit comments

Comments
 (0)