Skip to content

Commit 9c0c70b

Browse files
committed
[Distributed] Allow disabling SerializationRequirement by using Any
It should be possible to disable SerializationRequirement by assigning Any to it. This effectively disables the checking, since any type conforms to that. Previously we would crash trying to check for this conformance; or rather, emitting diagnostics about un-necessary casts then encountering incomplete types in CSDiagnostics. Avoiding the cast entirely sounds like a good solution here. resolves rdar://159285863
1 parent 5926420 commit 9c0c70b

File tree

2 files changed

+111
-8
lines changed

2 files changed

+111
-8
lines changed

lib/Sema/DerivedConformance/DerivedConformanceDistributedActor.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -332,17 +332,24 @@ deriveBodyDistributed_invokeHandlerOnReturn(AbstractFunctionDecl *afd,
332332
metatypeVar->setImplicit();
333333
metatypeVar->setSynthesized();
334334

335+
// If the SerializationRequirement requires it, we need to emit a cast:
335336
// metatype as! <<concrete SerializationRequirement.Type>>
337+
bool serializationRequirementIsAny =
338+
metatypeParam->getInterfaceType()->getMetatypeInstanceType()->isEqual(
339+
C.getAnyExistentialType());
340+
336341
auto metatypeRef =
337342
new (C) DeclRefExpr(ConcreteDeclRef(metatypeParam), dloc, implicit);
338-
auto metatypeSRCastExpr = ForcedCheckedCastExpr::createImplicit(
339-
C, metatypeRef, serializationRequirementMetaTypeTy);
340-
341343
auto metatypePattern = NamedPattern::createImplicit(C, metatypeVar);
342-
auto metatypePB = PatternBindingDecl::createImplicit(
343-
C, swift::StaticSpellingKind::None, metatypePattern,
344-
/*expr=*/metatypeSRCastExpr, func);
345344

345+
PatternBindingDecl *metatypePB = serializationRequirementIsAny ?
346+
PatternBindingDecl::createImplicit(
347+
C, swift::StaticSpellingKind::None, metatypePattern,
348+
/*expr=*/metatypeRef, func) :
349+
PatternBindingDecl::createImplicit(
350+
C, swift::StaticSpellingKind::None, metatypePattern,
351+
/*expr=*/ForcedCheckedCastExpr::createImplicit(
352+
C, metatypeRef, serializationRequirementMetaTypeTy), func);
346353
stmts.push_back(metatypePB);
347354
stmts.push_back(metatypeVar);
348355
}
@@ -399,7 +406,6 @@ static FuncDecl *deriveDistributedActorSystem_invokeHandlerOnReturn(
399406
auto system = derived.Nominal;
400407
auto &C = system->getASTContext();
401408

402-
// auto serializationRequirementType = getDistributedActorSystemType(decl);
403409
auto resultHandlerType = getDistributedActorSystemResultHandlerType(system);
404410
auto unsafeRawPointerType = C.getUnsafeRawPointerType();
405411
auto anyTypeType = ExistentialMetatypeType::get(C.TheAnyType); // Any.Type
@@ -436,7 +442,7 @@ static FuncDecl *deriveDistributedActorSystem_invokeHandlerOnReturn(
436442
/*throws=*/true,
437443
/*ThrownType=*/Type(),
438444
/*genericParams=*/nullptr, params,
439-
/*returnType*/ TupleType::getEmpty(C), system);
445+
/*returnType=*/TupleType::getEmpty(C), system);
440446
funcDecl->setSynthesized(true);
441447
funcDecl->copyFormalAccessFrom(system, /*sourceIsParentContext=*/true);
442448
funcDecl->setBodySynthesizer(deriveBodyDistributed_invokeHandlerOnReturn);
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// RUN: %target-swift-frontend -typecheck -verify -target %target-swift-5.7-abi-triple -I %t 2>&1 %s
2+
3+
// UNSUPPORTED: back_deploy_concurrency
4+
// REQUIRES: concurrency
5+
// REQUIRES: distributed
6+
7+
import Distributed
8+
9+
final class CrashActorSystem: DistributedActorSystem {
10+
typealias ActorID = CrashActorID
11+
typealias InvocationEncoder = CrashInvocationEncoder
12+
typealias InvocationDecoder = CrashInvocationDecoder
13+
typealias ResultHandler = CrashResultHandler
14+
15+
typealias SerializationRequirement = Any
16+
17+
func assignID<Actor: DistributedActor>(_: Actor.Type) -> ActorID where Actor.ID == ActorID {
18+
fatalError()
19+
}
20+
21+
func resignID(_: ActorID) {
22+
// nothing
23+
}
24+
25+
func actorReady<Actor: DistributedActor>(_: Actor) where Actor.ID == ActorID { }
26+
27+
func resolve<Actor: DistributedActor>(id _: ActorID, as _: Actor.Type) throws -> Actor? where Actor.ID == ActorID {
28+
return nil
29+
}
30+
31+
func makeInvocationEncoder() -> InvocationEncoder {
32+
return InvocationEncoder()
33+
}
34+
35+
func remoteCallVoid<Actor: DistributedActor, Failure: Error>(
36+
on _: Actor,
37+
target _: RemoteCallTarget,
38+
invocation _: inout InvocationEncoder,
39+
throwing _: Failure.Type
40+
) async throws where Actor.ID == ActorID {
41+
return
42+
}
43+
44+
func remoteCall<Actor: DistributedActor, Failure: Error, Success: SerializationRequirement>(
45+
on _: Actor,
46+
target _: RemoteCallTarget,
47+
invocation _: inout InvocationEncoder,
48+
throwing _: Failure.Type,
49+
returning _: Success.Type
50+
) async throws -> Success where Actor.ID == ActorID {
51+
fatalError()
52+
}
53+
54+
}
55+
56+
struct CrashActorID: Hashable {
57+
func hash(into hasher: inout Hasher) { }
58+
}
59+
60+
struct CrashInvocationEncoder: DistributedTargetInvocationEncoder {
61+
typealias SerializationRequirement = CrashActorSystem.SerializationRequirement
62+
63+
func recordGenericSubstitution<GenericSubstitution>(_: GenericSubstitution.Type) throws { }
64+
mutating func recordArgument<Argument: SerializationRequirement>(_: RemoteCallArgument<Argument>) throws { }
65+
mutating func recordReturnType<Success: SerializationRequirement>(_: Success.Type) throws { }
66+
func recordErrorType<Failure: Error>(_: Failure.Type) throws { }
67+
func doneRecording() throws { }
68+
}
69+
70+
struct CrashInvocationDecoder: DistributedTargetInvocationDecoder {
71+
typealias SerializationRequirement = CrashActorSystem.SerializationRequirement
72+
73+
func decodeGenericSubstitutions() throws -> [Any.Type] {
74+
return []
75+
}
76+
77+
mutating func decodeNextArgument<Argument: SerializationRequirement>() throws -> Argument {
78+
fatalError()
79+
}
80+
81+
func decodeReturnType() throws -> Any.Type? {
82+
return nil
83+
}
84+
85+
func decodeErrorType() throws -> Any.Type? {
86+
return nil
87+
}
88+
89+
}
90+
91+
struct CrashResultHandler: DistributedTargetInvocationResultHandler {
92+
typealias SerializationRequirement = CrashActorSystem.SerializationRequirement
93+
94+
func onReturn<Success: SerializationRequirement>(value _: Success) async throws { }
95+
func onReturnVoid() async throws { }
96+
func onThrow<Failure: Error>(error _: Failure) async throws { }
97+
}

0 commit comments

Comments
 (0)