Skip to content

Commit 765f8ac

Browse files
committed
Check sending in the process of diagnosing NonSendable types
1 parent f8abb1b commit 765f8ac

File tree

2 files changed

+38
-17
lines changed

2 files changed

+38
-17
lines changed

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,11 +1184,8 @@ bool swift::diagnoseNonSendableTypesInReference(
11841184
// Check params of this function or subscript override for sendability
11851185
for (auto param : *paramList) {
11861186
Type paramType = param->getInterfaceType().subst(subs);
1187-
if (param->isSending() && !paramType->hasError()) {
1188-
continue;
1189-
}
1190-
if (diagnoseNonSendableTypes(
1191-
paramType, fromDC, derivedConformanceType, refLoc,
1187+
if (diagnoseNonSendableTypesWithSendingCheck(
1188+
param, paramType, fromDC, derivedConformanceType, refLoc,
11921189
diagnoseLoc.isInvalid() ? refLoc : diagnoseLoc,
11931190
getSendableParamDiag(refKind), decl, getActorIsolation()))
11941191
return true;
@@ -1198,27 +1195,19 @@ bool swift::diagnoseNonSendableTypesInReference(
11981195
// Check the result type of a function or subscript.
11991196
if (funcCheckOptions.contains(FunctionCheckKind::Results)) {
12001197
Type resultType;
1201-
bool hasSendingResult;
12021198
if (auto func = dyn_cast<FuncDecl>(decl)) {
12031199
resultType = func->getResultInterfaceType().subst(subs);
1204-
hasSendingResult = func->hasSendingResult();
12051200
decl = func;
12061201
} else if (auto subscript = dyn_cast<SubscriptDecl>(decl)) {
12071202
resultType = subscript->getElementInterfaceType().subst(subs);
1208-
hasSendingResult = isa<SendingTypeRepr>(subscript->getResultTypeRepr());
12091203
}
12101204
if (!resultType) {
12111205
return false;
12121206
}
1213-
auto diag = getSendableResultDiag(refKind);
1214-
if (diag.ID == diag::non_sendable_result_in_witness.ID &&
1215-
hasSendingResult && !resultType->hasError()) {
1216-
return false;
1217-
}
1218-
if (diagnoseNonSendableTypes(
1219-
resultType, fromDC, derivedConformanceType, refLoc,
1220-
diagnoseLoc.isInvalid() ? refLoc : diagnoseLoc, diag, decl,
1221-
getActorIsolation()))
1207+
if (diagnoseNonSendableTypesWithSendingCheck(
1208+
decl, resultType, fromDC, derivedConformanceType, refLoc,
1209+
diagnoseLoc.isInvalid() ? refLoc : diagnoseLoc,
1210+
getSendableResultDiag(refKind), decl, getActorIsolation()))
12221211
return true;
12231212
}
12241213

lib/Sema/TypeCheckConcurrency.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "swift/AST/Expr.h"
2424
#include "swift/AST/Module.h"
2525
#include "swift/AST/Type.h"
26+
#include "swift/AST/TypeRepr.h"
2627
#include "swift/Sema/Concurrency.h"
2728

2829
#include <cassert>
@@ -436,6 +437,37 @@ namespace detail {
436437
};
437438
}
438439

440+
/// Diagnose any non-Sendable types that occur within the given type, using
441+
/// the given diagnostic.
442+
///
443+
/// \returns \c true if any errors were produced, \c false if no diagnostics or
444+
/// only warnings and notes were produced or if a decl contains a sending
445+
/// parameter or result
446+
template <typename... DiagArgs>
447+
bool diagnoseNonSendableTypesWithSendingCheck(
448+
ValueDecl *decl, Type type, SendableCheckContext fromContext,
449+
Type derivedConformance, SourceLoc typeLoc, SourceLoc diagnoseLoc,
450+
Diag<Type, DiagArgs...> diag,
451+
typename detail::Identity<DiagArgs>::type... diagArgs) {
452+
if (auto param = dyn_cast<ParamDecl>(decl)) {
453+
if (param->isSending()) {
454+
return false;
455+
}
456+
}
457+
if (auto *func = dyn_cast<FuncDecl>(decl)) {
458+
if (func->hasSendingResult())
459+
return false;
460+
}
461+
if (auto *subscript = dyn_cast<SubscriptDecl>(decl)) {
462+
if (isa<SendingTypeRepr>(subscript->getResultTypeRepr()))
463+
return false;
464+
}
465+
466+
return diagnoseNonSendableTypes(
467+
type, fromContext, derivedConformance, typeLoc, diagnoseLoc, diag,
468+
std::forward<decltype(diagArgs)>(diagArgs)...);
469+
}
470+
439471
/// Diagnose any non-Sendable types that occur within the given type, using
440472
/// the given diagnostic.
441473
///

0 commit comments

Comments
 (0)