Skip to content

Commit 86f3fe1

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.
1 parent c4f6793 commit 86f3fe1

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

Diff for: lib/Sema/TypeCheckConcurrency.cpp

+13-2
Original file line numberDiff line numberDiff line change
@@ -4648,8 +4648,19 @@ getIsolationFromConformances(NominalTypeDecl *nominal) {
46484648
return std::nullopt;
46494649

46504650
std::optional<ActorIsolation> foundIsolation;
4651-
for (auto proto :
4652-
nominal->getLocalProtocols(ConformanceLookupKind::NonStructural)) {
4651+
for (auto conformance :
4652+
nominal->getLocalConformances(ConformanceLookupKind::NonStructural)) {
4653+
4654+
// Don't include inherited conformances. If a conformance is inherited
4655+
// from a superclass, the isolation of the subclass should be inferred
4656+
// from the superclass, which is done directly in ActorIsolationRequest.
4657+
// If the superclass has opted out of global actor inference, such as
4658+
// by conforming to the protocol in an extension, then the subclass should
4659+
// not infer isolation from the protocol.
4660+
if (conformance->getKind() == ProtocolConformanceKind::Inherited)
4661+
continue;
4662+
4663+
auto *proto = conformance->getProtocol();
46534664
switch (auto protoIsolation = getActorIsolation(proto)) {
46544665
case ActorIsolation::ActorInstance:
46554666
case ActorIsolation::Unspecified:

Diff for: test/Concurrency/global_actor_inference_swift6.swift

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