Skip to content

Commit cb8f0a2

Browse files
committed
[Distributed] distributed func checks now work in protocols & dont crash
1 parent e681ae4 commit cb8f0a2

7 files changed

+47
-5
lines changed

lib/Sema/CodeSynthesisDistributedActor.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,6 @@ FuncDecl *GetDistributedThunkRequest::evaluate(
758758
return nullptr;
759759

760760
auto &C = distributedTarget->getASTContext();
761-
auto DC = distributedTarget->getDeclContext();
762761

763762
if (!getConcreteReplacementForProtocolActorSystemType(distributedTarget)) {
764763
// Don't synthesize thunks, unless there is a *concrete* ActorSystem.
@@ -782,9 +781,6 @@ FuncDecl *GetDistributedThunkRequest::evaluate(
782781
if (!C.getLoadedModule(C.Id_Distributed))
783782
return nullptr;
784783

785-
auto nominal = DC->getSelfNominalTypeDecl(); // NOTE: Always from DC
786-
assert(nominal);
787-
788784
// --- Prepare the "distributed thunk" which does the "maybe remote" dance:
789785
return createDistributedThunkFunction(func);
790786
}

lib/Sema/TypeCheckAttr.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5783,7 +5783,6 @@ void AttributeChecker::visitDistributedActorAttr(DistributedActorAttr *attr) {
57835783

57845784
// distributed func must be declared inside an distributed actor
57855785
auto selfTy = dc->getSelfTypeInContext();
5786-
57875786
if (!selfTy->isDistributedActor()) {
57885787
auto diagnostic = diagnoseAndRemoveAttr(
57895788
attr, diag::distributed_actor_func_not_in_distributed_actor);

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2880,6 +2880,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
28802880
}
28812881

28822882
TypeChecker::checkDeclAttributes(FD);
2883+
TypeChecker::checkDistributedFunc(FD);
28832884

28842885
if (!checkOverrides(FD)) {
28852886
// If a method has an 'override' keyword but does not

lib/Sema/TypeCheckDistributed.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,13 @@ void TypeChecker::checkDistributedActor(SourceFile *SF, NominalTypeDecl *nominal
702702
(void)nominal->getDistributedActorIDProperty();
703703
}
704704

705+
void TypeChecker::checkDistributedFunc(FuncDecl *func) {
706+
if (!func->isDistributed())
707+
return;
708+
709+
swift::checkDistributedFunction(func);
710+
}
711+
705712
llvm::SmallPtrSet<ProtocolDecl *, 2>
706713
swift::getDistributedSerializationRequirementProtocols(
707714
NominalTypeDecl *nominal, ProtocolDecl *protocol) {

lib/Sema/TypeChecker.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,6 +1104,9 @@ diagnosePotentialOpaqueTypeUnavailability(SourceRange ReferenceRange,
11041104
/// Type check a 'distributed actor' declaration.
11051105
void checkDistributedActor(SourceFile *SF, NominalTypeDecl *decl);
11061106

1107+
/// Type check a single 'distributed func' declaration.
1108+
void checkDistributedFunc(FuncDecl *func);
1109+
11071110
void checkConcurrencyAvailability(SourceRange ReferenceRange,
11081111
const DeclContext *ReferenceDC);
11091112

test/Distributed/distributed_actor_system_missing.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ distributed actor DA {
1818
// Note to add the typealias is diagnosed on the protocol:
1919
// _Distributed.DistributedActor:3:20: note: diagnostic produced elsewhere: protocol requires nested type 'ActorSystem'; do you want to add it?
2020
}
21+
2122
// When an actor declares a typealias for a system, but that system does not exist,
2223
// we need to fail gracefully, rather than crash trying to use the non-existing type.
2324
//

test/Distributed/distributed_protocols_distributed_func_serialization_requirements.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,41 @@ distributed actor SpecifyRequirement: NoSerializationRequirementYet {
2929
distributed func testAT() async throws -> NotCodable {
3030
.init()
3131
}
32+
33+
}
34+
35+
protocol ProtocolWithChecksSystem: DistributedActor
36+
where ActorSystem == FakeActorSystem {
37+
// expected-error@+1{{result type 'NotCodable' of distributed instance method 'testAT' does not conform to serialization requirement 'Codable'}}
38+
distributed func testAT() async throws -> NotCodable
39+
}
40+
distributed actor ProtocolWithChecksSystemDA: ProtocolWithChecksSystem {
41+
// expected-error@+1{{result type 'NotCodable' of distributed instance method 'testAT' does not conform to serialization requirement 'Codable'}}
42+
distributed func testAT() async throws -> NotCodable { .init() }
43+
}
44+
45+
protocol ProtocolWithChecksSeqReq: DistributedActor
46+
where Self.SerializationRequirement == Codable {
47+
// expected-error@+1{{result type 'NotCodable' of distributed instance method 'testAT' does not conform to serialization requirement 'Codable'}}
48+
distributed func testAT() async throws -> NotCodable
49+
}
50+
distributed actor ProtocolWithChecksSeqReqDA_MissingSystem: ProtocolWithChecksSeqReq {
51+
// expected-error@-1{{distributed actor 'ProtocolWithChecksSeqReqDA_MissingSystem' does not declare ActorSystem it can be used with.}}
52+
// expected-note@-2{{you can provide a module-wide default actor system by declaring:}}
53+
// expected-error@-3{{type 'ProtocolWithChecksSeqReqDA_MissingSystem' does not conform to protocol 'ProtocolWithChecksSeqReq'}}
54+
//
55+
// expected-error@-5{{distributed actor 'ProtocolWithChecksSeqReqDA_MissingSystem' does not declare ActorSystem it can be used with.}}
56+
57+
// Entire conformance is doomed, so we didn't proceed to checking the functions; that's fine
58+
distributed func testAT() async throws -> NotCodable { .init() }
59+
}
60+
61+
distributed actor ProtocolWithChecksSeqReqDA: ProtocolWithChecksSeqReq {
62+
typealias ActorSystem = FakeActorSystem
63+
// ok, since FakeActorSystem.SerializationRequirement == ProtocolWithChecksSeqReq.SerializationRequirement
64+
65+
// expected-error@+1{{result type 'NotCodable' of distributed instance method 'testAT' does not conform to serialization requirement 'Codable'}}
66+
distributed func testAT() async throws -> NotCodable { .init() }
3267
}
3368

3469
extension NoSerializationRequirementYet {

0 commit comments

Comments
 (0)