Skip to content

Commit c5b91d1

Browse files
authored
Merge pull request #59718 from ktoso/wip-protocols-and-das-improved
[2/3][Distributed][Typecheck] Fix distributed func typechecks in protocols
2 parents 0219611 + fa077c1 commit c5b91d1

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
@@ -793,7 +793,6 @@ FuncDecl *GetDistributedThunkRequest::evaluate(
793793
return nullptr;
794794

795795
auto &C = distributedTarget->getASTContext();
796-
auto DC = distributedTarget->getDeclContext();
797796

798797
if (!getConcreteReplacementForProtocolActorSystemType(distributedTarget)) {
799798
// Don't synthesize thunks, unless there is a *concrete* ActorSystem.
@@ -817,9 +816,6 @@ FuncDecl *GetDistributedThunkRequest::evaluate(
817816
if (!C.getLoadedModule(C.Id_Distributed))
818817
return nullptr;
819818

820-
auto nominal = DC->getSelfNominalTypeDecl(); // NOTE: Always from DC
821-
assert(nominal);
822-
823819
// --- Prepare the "distributed thunk" which does the "maybe remote" dance:
824820
return createDistributedThunkFunction(func);
825821
}

lib/Sema/TypeCheckAttr.cpp

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

59015901
// distributed func must be declared inside an distributed actor
59025902
auto selfTy = dc->getSelfTypeInContext();
5903-
59045903
if (!selfTy->isDistributedActor()) {
59055904
auto diagnostic = diagnoseAndRemoveAttr(
59065905
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
@@ -2875,6 +2875,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
28752875
}
28762876

28772877
TypeChecker::checkDeclAttributes(FD);
2878+
TypeChecker::checkDistributedFunc(FD);
28782879

28792880
if (!checkOverrides(FD)) {
28802881
// 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
@@ -703,6 +703,13 @@ void TypeChecker::checkDistributedActor(SourceFile *SF, NominalTypeDecl *nominal
703703
(void)nominal->getDistributedActorIDProperty();
704704
}
705705

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

lib/Sema/TypeChecker.h

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

1104+
/// Type check a single 'distributed func' declaration.
1105+
void checkDistributedFunc(FuncDecl *func);
1106+
11041107
void checkConcurrencyAvailability(SourceRange ReferenceRange,
11051108
const DeclContext *ReferenceDC);
11061109

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)