Skip to content

Commit 2b3e26d

Browse files
committed
Warn about sending on Sendable type in function parameters (swiftlang#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 bd313de commit 2b3e26d

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

include/swift/AST/DiagnosticsSema.def

+3
Original file line numberDiff line numberDiff line change
@@ -7952,6 +7952,9 @@ ERROR(sending_unsupported_param_specifier, none,
79527952
"'%0' cannot be applied to a 'sending' parameter", (StringRef))
79537953
ERROR(sending_only_on_parameters_and_results, none,
79547954
"'sending' may only be used on parameters and results", ())
7955+
WARNING(sending_applied_to_sendable, none,
7956+
"'sending' has no effect on Sendable parameter %0",
7957+
(Type))
79557958
ERROR(sending_cannot_be_applied_to_tuple_elt, none,
79567959
"'sending' cannot be applied to tuple elements", ())
79577960
ERROR(sending_function_wrong_sending,none,

lib/Sema/TypeCheckType.cpp

+22-1
Original file line numberDiff line numberDiff line change
@@ -4976,8 +4976,29 @@ TypeResolver::resolveSendingTypeRepr(SendingTypeRepr *repr,
49764976
return ErrorType::get(getASTContext());
49774977
}
49784978

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

49835004
NeverNullType
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)