Skip to content

[6.2] Sema: Expand isolated conformance inference to consider conformances to inherited protocols #82384

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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 @@ -8239,16 +8239,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.

Choose a reason for hiding this comment

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

Didn't want this to get lost; see Doug Gregor's feedback on this code comment here: #82383 (comment)

for (auto req : protocol->getRequirementSignature().getRequirements()) {
if (req.getKind() != RequirementKind::Conformance ||
!req.getFirstType()->isEqual(protocol->getSelfInterfaceType()))
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;
}

Choose a reason for hiding this comment

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

Also a question raised by Doug Gregor here regarding the above if: #82383 (comment)


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() {} }