Skip to content

Commit f200dae

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 2b3e26d commit f200dae

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
@@ -7952,9 +7952,12 @@ 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,
7955+
WARNING(sending_applied_to_sendable_parameter, none,
79567956
"'sending' has no effect on Sendable parameter %0",
79577957
(Type))
7958+
WARNING(sending_applied_to_sendable_result, none,
7959+
"'sending' has no effect on Sendable function result type %0",
7960+
(Type))
79587961
ERROR(sending_cannot_be_applied_to_tuple_elt, none,
79597962
"'sending' cannot be applied to tuple elements", ())
79607963
ERROR(sending_function_wrong_sending,none,

lib/Sema/TypeCheckType.cpp

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

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

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)