Skip to content

Commit 9c01b64

Browse files
committed
Use generic signature instead of ASTContext to warn about sending on Sendable type
Follow PR review comments and extend the warnings about sending on sendable types to cover types with a type parameter, and use generic signature instead of ASTContext to check conformance to Sendable. Additionally, use different warning messages based on whether the sending on Sendable is used on a function parameter or function result.
1 parent 8dd5920 commit 9c01b64

File tree

3 files changed

+40
-26
lines changed

3 files changed

+40
-26
lines changed

include/swift/AST/DiagnosticsSema.def

+4-1
Original file line numberDiff line numberDiff line change
@@ -7993,9 +7993,12 @@ 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,
7996+
WARNING(sending_applied_to_sendable_parameter, none,
79977997
"'sending' has no effect on Sendable parameter %0",
79987998
(Type))
7999+
WARNING(sending_applied_to_sendable_result, none,
8000+
"'sending' has no effect on Sendable function result type %0",
8001+
(Type))
79998002
ERROR(sending_cannot_be_applied_to_tuple_elt, none,
80008003
"'sending' cannot be applied to tuple elements", ())
80018004
ERROR(sending_function_wrong_sending,none,

lib/Sema/TypeCheckType.cpp

+15-16
Original file line numberDiff line numberDiff line change
@@ -4976,23 +4976,22 @@ TypeResolver::resolveSendingTypeRepr(SendingTypeRepr *repr,
49764976
}
49774977

49784978
NeverNullType resolvedType = resolveType(repr->getBase(), options);
4979-
4979+
49804980
// 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-
};
4981+
bool isFunctionParam = options.is(TypeResolverContext::FunctionInput);
4982+
bool isFunctionResult = options.is(TypeResolverContext::FunctionResult);
4983+
if (inStage(TypeResolutionStage::Interface)
4984+
&& (isFunctionParam || isFunctionResult)) {
4985+
auto contextTy = GenericEnvironment::mapTypeIntoContext(
4986+
resolution.getGenericSignature().getGenericEnvironment(),
4987+
resolvedType);
4988+
if (contextTy->isSendableType()) {
4989+
diagnose(repr->getSpecifierLoc(),
4990+
isFunctionParam
4991+
? diag::sending_applied_to_sendable_parameter
4992+
: diag::sending_applied_to_sendable_result,
4993+
resolvedType.get())
4994+
.fixItRemove(repr->getSpecifierLoc());
49964995
}
49974996
}
49984997

Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
11
// RUN: %target-swift-frontend -module-name TestModule -typecheck -verify %s
22

33
struct SendableType {}
4-
class NonSendableType {}
4+
class NonSendableType {
5+
init() {}
6+
}
7+
struct SendableTypeWithTypeParam<SendableType> {}
58

6-
func testSendingOnNonSendable(input: sending NonSendableType) {
7-
// okay
9+
func testSendingOnNonSendable(input: sending NonSendableType) -> sending NonSendableType {
10+
return NonSendableType() // okay
811
}
912

10-
func testSendingOnSendable(input: sending SendableType) {
11-
// expected-warning@-1{{'sending' has no effect}}{{35-43=}}
13+
func testSendingOnSendableParam(input: sending SendableType) {
14+
// expected-warning@-1{{'sending' has no effect}}{{40-48=}}
1215
}
1316

14-
func testSendingOnSendable(input: sending some Sendable) {
17+
func testSendingOnSendable(input: sending SendableTypeWithTypeParam<SendableType>) {
1518
// expected-warning@-1{{'sending' has no effect}}{{35-43=}}
1619
}
1720

18-
func testSendingOnSendable(input: sending any Sendable) {
19-
// expected-warning@-1{{'sending' has no effect}}{{35-43=}}
20-
}
21+
func testSendingOnSendableParam(input: sending some Sendable) {
22+
// expected-warning@-1{{'sending' has no effect}}{{40-48=}}
23+
}
24+
25+
func testSendingOnSendableParam(input: sending any Sendable) {
26+
// expected-warning@-1{{'sending' has no effect}}{{40-48=}}
27+
}
28+
29+
func testSendingOnSendableResult() -> sending SendableType {
30+
// expected-warning@-1{{'sending' has no effect}}{{39-47=}}
31+
return SendableType()
32+
}

0 commit comments

Comments
 (0)