Skip to content

Commit bb48d32

Browse files
committed
Sema: Implied 'Sendable' conformance should be unconditional
A conditional conformance to a protocol does not usually imply a conformance to the protocol's inherited protocols, because we have no way to guess what the conditional requirements should be. A carveout was added for 'Sendable', so that protocols could inherit from 'Sendable' retroactively. However, the 'Sendable' conformance would become conditional, which causes us to reject a _second_ conditional conformance to such a protocol: struct G<T> {} protocol P: Sendable {} protocol Q: Sendable {} extension G: P where T: P {} extension G: Q where T: Q {} To make this work, tweak the code so that an implied conformance has the same generic signature as the conforming type, that is, we force it to be unconditional. Fixes rdar://122754849 Fixes swiftlang#71544
1 parent c8acccf commit bb48d32

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

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

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))

0 commit comments

Comments
 (0)