Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions lib/Sema/TypeCheckConcurrency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8155,16 +8155,28 @@ ActorIsolation swift::inferConformanceIsolation(
return nominalIsolation;
}

bool anyIsolatedWitness = false;
auto protocol = conformance->getProtocol();
for (auto requirement : protocol->getMembers()) {
if (isa<TypeDecl>(requirement))

// Also check the value witnesses of each implied conformance to every
// inherited protocol, recursively.
Comment on lines +8160 to +8161
Copy link
Member

@DougGregor DougGregor Jun 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment nit: this isn't checking the value witnesses specifically, it's considering the isolation of the inherited conformances.

for (auto req : protocol->getRequirementSignature().getRequirements()) {
if (req.getKind() != RequirementKind::Conformance ||
!req.getFirstType()->isEqual(ctx.TheSelfType))
continue;

auto valueReq = dyn_cast<ValueDecl>(requirement);
if (!valueReq)
auto *assocConf = conformance->getAssociatedConformance(
req.getFirstType(), req.getProtocolDecl()).getConcrete();
auto isolation = assocConf->getIsolation();
if (isolation.isGlobalActor())
return isolation;
Comment on lines +8170 to +8171
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we infer isolation for from multiple sources elsewhere (for example, inferring type isolation from the protocols it conforms to), we refuse to infer isolation when there are conflicting inferences.. for example, @MainActor from one source and @SomeOtherGlobalActor from another source. Should we do the same here, rather than eagerly returning the first global actor isolation we find?

}

bool anyIsolatedWitness = false;
for (auto requirement : protocol->getProtocolRequirements()) {
if (isa<TypeDecl>(requirement))
continue;

auto valueReq = cast<ValueDecl>(requirement);
auto witness = conformance->getWitnessDecl(valueReq);
if (!witness)
continue;
Expand Down
5 changes: 5 additions & 0 deletions test/Concurrency/Inputs/isolated_conformance_other.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public protocol P {
func f()
}

public protocol PDerived: P {}
4 changes: 4 additions & 0 deletions test/Concurrency/isolated_conformance_inference.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,7 @@ class InferMeDefaults {
var someGlobalActorState: any P2.Type = DifferingConformances.self // expected-error{{global actor 'SomeGlobalActor'-isolated default value in a main actor-isolated context}}
var bothState: any (P & P2).Type = DifferingConformances.self // expected-error{{default argument cannot be both main actor-isolated and global actor 'SomeGlobalActor'-isolated}}
}

protocol PDerived: P {}

@MainActor struct ImpliedConformanceInference: PDerived { func f() {} }
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -emit-module -emit-module-path %t/isolated_conformance_other.swiftmodule %S/Inputs/isolated_conformance_other.swift -swift-version 6
// RUN: %target-typecheck-verify-swift -I %t -swift-version 6 -enable-upcoming-feature InferIsolatedConformances -default-isolation MainActor -swift-version 6
// REQUIRES: swift_feature_InferIsolatedConformances

import isolated_conformance_other

struct S1: P { func f() {} }
struct S2: PDerived { func f() {} }