Skip to content

Commit 252abb5

Browse files
committed
Sema: Key the implied Sendable conformance behavior off of -swift-version instead of -strict-concurrency
Also add some comments to explain what in the world is going on with the new checks.
1 parent 475d9b7 commit 252abb5

5 files changed

+23
-3
lines changed

lib/AST/ConformanceLookup.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,10 @@ LookupConformanceInModuleRequest::evaluate(
719719
auto *normalConf = cast<NormalProtocolConformance>(conformance);
720720
auto *conformanceDC = normalConf->getDeclContext();
721721

722+
// In -swift-version 5 mode, a conditional conformance to a protocol can imply
723+
// a Sendable conformance. The implied conformance is unconditional so it uses
724+
// the generic signature of the nominal type and not the generic signature of
725+
// the extension that declared the (implying) conditional conformance.
722726
if (normalConf->getSourceKind() == ConformanceEntryKind::Implied &&
723727
normalConf->getProtocol()->isSpecificProtocol(KnownProtocolKind::Sendable)) {
724728
conformanceDC = conformanceDC->getSelfNominalTypeDecl();

lib/AST/ProtocolConformance.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,16 @@ GenericSignature ProtocolConformance::getGenericSignature() const {
237237
case ProtocolConformanceKind::Self:
238238
// If we have a normal or inherited protocol conformance, look for its
239239
// generic signature.
240+
241+
// In -swift-version 5 mode, a conditional conformance to a protocol can imply
242+
// a Sendable conformance. The implied conformance is unconditional so it uses
243+
// the generic signature of the nominal type and not the generic signature of
244+
// the extension that declared the (implying) conditional conformance.
240245
if (getSourceKind() == ConformanceEntryKind::Implied &&
241246
getProtocol()->isSpecificProtocol(KnownProtocolKind::Sendable)) {
242247
return getDeclContext()->getSelfNominalTypeDecl()->getGenericSignature();
243248
}
249+
244250
return getDeclContext()->getGenericSignatureOfContext();
245251

246252
case ProtocolConformanceKind::Builtin:
@@ -416,6 +422,10 @@ ConditionalRequirementsRequest::evaluate(Evaluator &evaluator,
416422
return {};
417423
}
418424

425+
// In -swift-version 5 mode, a conditional conformance to a protocol can imply
426+
// a Sendable conformance. We ask the conformance for its generic signature,
427+
// which will always be the generic signature of `ext` except in this case,
428+
// where it's the generic signature of the extended nominal.
419429
const auto extensionSig = NPC->getGenericSignature();
420430

421431
// The extension signature should be a superset of the type signature, meaning

lib/Sema/TypeCheckConcurrency.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -6054,6 +6054,10 @@ bool swift::checkSendableConformance(
60546054
}
60556055
}
60566056

6057+
// In -swift-version 5 mode, a conditional conformance to a protocol can imply
6058+
// a Sendable conformance. The implied conformance is unconditional, so check
6059+
// the storage for sendability as if the conformance was declared on the nominal,
6060+
// and not some (possibly constrained) extension.
60576061
if (conformance->getSourceKind() == ConformanceEntryKind::Implied)
60586062
conformanceDC = nominal;
60596063
return checkSendableInstanceStorage(nominal, conformanceDC, check);

lib/Sema/TypeCheckProtocol.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -2417,7 +2417,9 @@ checkIndividualConformance(NormalProtocolConformance *conformance) {
24172417

24182418
bool allowImpliedConditionalConformance = false;
24192419
if (Proto->isSpecificProtocol(KnownProtocolKind::Sendable)) {
2420-
if (Context.LangOpts.StrictConcurrencyLevel != StrictConcurrency::Complete)
2420+
// In -swift-version 5 mode, a conditional conformance to a protocol can imply
2421+
// a Sendable conformance.
2422+
if (!Context.LangOpts.isSwiftVersionAtLeast(6))
24212423
allowImpliedConditionalConformance = true;
24222424
} else if (Proto->isMarkerProtocol()) {
24232425
allowImpliedConditionalConformance = true;

test/Concurrency/implied_sendable_conformance_swift5.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// RUN: %target-typecheck-verify-swift -swift-version 5
2-
// RUN: %target-swift-emit-silgen %s -swift-version 5
1+
// RUN: %target-typecheck-verify-swift -swift-version 5 -strict-concurrency=complete
2+
// RUN: %target-swift-emit-silgen %s -swift-version 5 -strict-concurrency=complete
33

44
protocol P: Sendable {}
55
protocol Q: Sendable {}

0 commit comments

Comments
 (0)