Skip to content

Commit e899ff2

Browse files
authored
Merge pull request #74908 from slavapestov/fix-implied-sendable-conformance
Sema: Change behavior of implied 'Sendable' conformance
2 parents 80e0e02 + 144b9fa commit e899ff2

7 files changed

+75
-2
lines changed

lib/AST/ASTContext.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2714,6 +2714,9 @@ ProtocolConformance *
27142714
ASTContext::getSpecializedConformance(Type type,
27152715
NormalProtocolConformance *generic,
27162716
SubstitutionMap substitutions) {
2717+
CONDITIONAL_ASSERT(substitutions.getGenericSignature().getCanonicalSignature()
2718+
== generic->getGenericSignature().getCanonicalSignature());
2719+
27172720
// If the specialization is a no-op, use the root conformance instead.
27182721
if (collapseSpecializedConformance(type, generic, substitutions)) {
27192722
++NumCollapsedSpecializedProtocolConformances;

lib/AST/ConformanceLookup.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,12 @@ LookupConformanceInModuleRequest::evaluate(
718718
// specialized type.
719719
auto *normalConf = cast<NormalProtocolConformance>(conformance);
720720
auto *conformanceDC = normalConf->getDeclContext();
721+
722+
if (normalConf->getSourceKind() == ConformanceEntryKind::Implied &&
723+
normalConf->getProtocol()->isSpecificProtocol(KnownProtocolKind::Sendable)) {
724+
conformanceDC = conformanceDC->getSelfNominalTypeDecl();
725+
}
726+
721727
auto subMap = type->getContextSubstitutionMap(mod, conformanceDC);
722728
return ProtocolConformanceRef(
723729
ctx.getSpecializedConformance(type, normalConf, subMap));

lib/AST/ProtocolConformance.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,10 @@ GenericSignature ProtocolConformance::getGenericSignature() const {
231231
case ProtocolConformanceKind::Self:
232232
// If we have a normal or inherited protocol conformance, look for its
233233
// generic signature.
234+
if (getSourceKind() == ConformanceEntryKind::Implied &&
235+
getProtocol()->isSpecificProtocol(KnownProtocolKind::Sendable)) {
236+
return getDeclContext()->getSelfNominalTypeDecl()->getGenericSignature();
237+
}
234238
return getDeclContext()->getGenericSignatureOfContext();
235239

236240
case ProtocolConformanceKind::Builtin:
@@ -406,7 +410,7 @@ ConditionalRequirementsRequest::evaluate(Evaluator &evaluator,
406410
return {};
407411
}
408412

409-
const auto extensionSig = ext->getGenericSignature();
413+
const auto extensionSig = NPC->getGenericSignature();
410414

411415
// The extension signature should be a superset of the type signature, meaning
412416
// every thing in the type signature either is included too or is implied by

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6055,6 +6055,8 @@ bool swift::checkSendableConformance(
60556055
}
60566056
}
60576057

6058+
if (conformance->getSourceKind() == ConformanceEntryKind::Implied)
6059+
conformanceDC = nominal;
60586060
return checkSendableInstanceStorage(nominal, conformanceDC, check);
60596061
}
60606062

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2415,8 +2415,16 @@ checkIndividualConformance(NormalProtocolConformance *conformance) {
24152415
ComplainLoc, diag::unchecked_conformance_not_special, ProtoType);
24162416
}
24172417

2418+
bool allowImpliedConditionalConformance = false;
2419+
if (Proto->isSpecificProtocol(KnownProtocolKind::Sendable)) {
2420+
if (Context.LangOpts.StrictConcurrencyLevel != StrictConcurrency::Complete)
2421+
allowImpliedConditionalConformance = true;
2422+
} else if (Proto->isMarkerProtocol()) {
2423+
allowImpliedConditionalConformance = true;
2424+
}
2425+
24182426
if (conformance->getSourceKind() == ConformanceEntryKind::Implied &&
2419-
!Proto->isMarkerProtocol()) {
2427+
!allowImpliedConditionalConformance) {
24202428
// We've got something like:
24212429
//
24222430
// protocol Foo : Proto {}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %target-typecheck-verify-swift -swift-version 5
2+
// RUN: %target-swift-emit-silgen %s -swift-version 5
3+
4+
protocol P: Sendable {}
5+
protocol Q: Sendable {}
6+
7+
struct One<T> { // expected-note {{consider making generic parameter 'T' conform to the 'Sendable' protocol}}
8+
var t: T // expected-warning {{stored property 't' of 'Sendable'-conforming generic struct 'One' has non-sendable type 'T'; this is an error in the Swift 6 language mode}}
9+
}
10+
11+
extension One: P where T: P {}
12+
13+
struct Both<T> { // expected-note {{consider making generic parameter 'T' conform to the 'Sendable' protocol}}
14+
var t: T // expected-warning {{stored property 't' of 'Sendable'-conforming generic struct 'Both' has non-sendable type 'T'; this is an error in the Swift 6 language mode}}
15+
}
16+
17+
extension Both: P where T: P {}
18+
extension Both: Q where T: Q {}
19+
20+
func takesSendable<T: Sendable>(_: T) {}
21+
22+
takesSendable(One<Int>(t: 3))
23+
takesSendable(Both<Int>(t: 3))
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// RUN: %target-typecheck-verify-swift -swift-version 6
2+
3+
protocol P: Sendable {}
4+
protocol Q: Sendable {}
5+
6+
struct One<T> { // expected-note {{consider making generic parameter 'T' conform to the 'Sendable' protocol}}
7+
var t: T // expected-error {{stored property 't' of 'Sendable'-conforming generic struct 'One' has non-sendable type 'T'}}
8+
}
9+
10+
extension One: P where T: P {}
11+
// expected-error@-1 {{conditional conformance of type 'One<T>' to protocol 'P' does not imply conformance to inherited protocol 'Sendable'}}
12+
// expected-note@-2 {{did you mean to explicitly state the conformance like 'extension One: Sendable where ...'}}
13+
14+
struct Both<T> { // expected-note {{consider making generic parameter 'T' conform to the 'Sendable' protocol}}
15+
var t: T // expected-error {{stored property 't' of 'Sendable'-conforming generic struct 'Both' has non-sendable type 'T'}}
16+
}
17+
18+
extension Both: P where T: P {}
19+
// expected-error@-1 {{conditional conformance of type 'Both<T>' to protocol 'P' does not imply conformance to inherited protocol 'Sendable'}}
20+
// expected-note@-2 {{did you mean to explicitly state the conformance like 'extension Both: Sendable where ...'}}
21+
22+
extension Both: Q where T: Q {}
23+
24+
func takesSendable<T: Sendable>(_: T) {}
25+
26+
takesSendable(One<Int>(t: 3))
27+
takesSendable(Both<Int>(t: 3))

0 commit comments

Comments
 (0)