Skip to content

Commit 5b61828

Browse files
committed
[Concurrency] Don't infer actor isolation from inherited conformances.
If a conformance is inherited from a superclass, the isolation of the subclass should be inferred directly from the superclass. If the superclass has opted out of global actor inference from a protocol, such as by conforming to the protocol in an extension, then the subclass should not infer isolation from the protocol. (cherry picked from commit 86f3fe1)
1 parent 97ef673 commit 5b61828

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4882,8 +4882,19 @@ getIsolationFromConformances(NominalTypeDecl *nominal) {
48824882
return std::nullopt;
48834883

48844884
std::optional<ActorIsolation> foundIsolation;
4885-
for (auto proto :
4886-
nominal->getLocalProtocols(ConformanceLookupKind::NonStructural)) {
4885+
for (auto conformance :
4886+
nominal->getLocalConformances(ConformanceLookupKind::NonStructural)) {
4887+
4888+
// Don't include inherited conformances. If a conformance is inherited
4889+
// from a superclass, the isolation of the subclass should be inferred
4890+
// from the superclass, which is done directly in ActorIsolationRequest.
4891+
// If the superclass has opted out of global actor inference, such as
4892+
// by conforming to the protocol in an extension, then the subclass should
4893+
// not infer isolation from the protocol.
4894+
if (conformance->getKind() == ProtocolConformanceKind::Inherited)
4895+
continue;
4896+
4897+
auto *proto = conformance->getProtocol();
48874898
switch (auto protoIsolation = getActorIsolation(proto)) {
48884899
case ActorIsolation::ActorInstance:
48894900
case ActorIsolation::Unspecified:

test/Concurrency/global_actor_inference_swift6.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,13 @@ class C2: MainActorSuperclass, InferenceConflictWithSuperclass {
213213
}
214214

215215

216+
class ConformInExtension {}
217+
extension ConformInExtension: InferMainActor {}
218+
219+
class InheritConformance: ConformInExtension {
220+
func f() {}
221+
}
222+
223+
func testInheritedMainActorConformance() {
224+
InheritConformance().f() // okay; this is not main actor isolated
225+
}

0 commit comments

Comments
 (0)