Skip to content

[5.5][Concurrency] Actor cannot conform to global actor isolated protocol #37413

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 1 commit into from
May 14, 2021
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
5 changes: 5 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -4459,6 +4459,11 @@ ERROR(global_actor_isolated_requirement_witness_conflict,none,
"%0 %1 isolated to global actor %2 can not satisfy corresponding "
"requirement from protocol %3 isolated to global actor %4",
(DescriptiveDeclKind, DeclName, Type, Identifier, Type))
ERROR(actor_cannot_conform_to_global_actor_protocol,none,
"actor %0 cannot conform to global actor isolated protocol %1",
(Type, Type))
NOTE(protocol_isolated_to_global_actor_here,none,
"%0 is isolated to global actor %1 here", (Type, Type))

WARNING(non_concurrent_param_type,none,
"cannot pass argument of non-sendable type %0 across actors",
Expand Down
20 changes: 20 additions & 0 deletions lib/Sema/TypeCheckProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1899,6 +1899,26 @@ checkIndividualConformance(NormalProtocolConformance *conformance,
return conformance;
}

if (T->isActorType()) {
if (auto globalActor = Proto->getGlobalActorAttr()) {
C.Diags.diagnose(ComplainLoc,
diag::actor_cannot_conform_to_global_actor_protocol, T,
ProtoType);

CustomAttr *attr;
NominalTypeDecl *actor;

std::tie(attr, actor) = *globalActor;

C.Diags.diagnose(attr->getLocation(),
diag::protocol_isolated_to_global_actor_here, ProtoType,
actor->getDeclaredInterfaceType());

conformance->setInvalid();
return conformance;
}
}

if (Proto->isObjC()) {
// Foreign classes cannot conform to objc protocols.
if (auto clas = canT->getClassOrBoundGenericClass()) {
Expand Down
12 changes: 12 additions & 0 deletions test/Concurrency/actor_isolation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -818,3 +818,15 @@ extension MyActor {
}
}
}

@available(SwiftStdlib 5.5, *)
@MainActor // expected-note {{'GloballyIsolatedProto' is isolated to global actor 'MainActor' here}}
protocol GloballyIsolatedProto {
}

// rdar://75849035 - trying to conform an actor to a global-actor isolated protocol should result in an error
func test_conforming_actor_to_global_actor_protocol() {
@available(SwiftStdlib 5.5, *)
actor MyValue : GloballyIsolatedProto {}
// expected-error@-1 {{actor 'MyValue' cannot conform to global actor isolated protocol 'GloballyIsolatedProto'}}
}
10 changes: 10 additions & 0 deletions test/Concurrency/actor_isolation_unsafe.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,13 @@ class C7: C2 {
globalSome() // okay
}
}

@MainActor(unsafe) // expected-note {{'GloballyIsolatedProto' is isolated to global actor 'MainActor' here}}
protocol GloballyIsolatedProto {
}

// rdar://75849035 - trying to conform an actor to a global-actor isolated protocol should result in an error
func test_conforming_actor_to_global_actor_protocol() {
actor MyValue : GloballyIsolatedProto {}
// expected-error@-1 {{actor 'MyValue' cannot conform to global actor isolated protocol 'GloballyIsolatedProto'}}
}