Skip to content

Commit d2a03b4

Browse files
authored
Merge pull request #84119 from ktoso/wip-allow-any-serialization-requirement
2 parents 59e8fde + 644aa87 commit d2a03b4

File tree

4 files changed

+116
-13
lines changed

4 files changed

+116
-13
lines changed

lib/SILGen/SILGenDistributed.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,7 @@ void InitializeDistActorIdentity::dump(SILGenFunction &) const {
281281

282282
bool SILGenFunction::shouldReplaceConstantForApplyWithDistributedThunk(
283283
FuncDecl *func) const {
284-
auto isDistributedFuncOrAccessor =
285-
func->isDistributed();
284+
auto isDistributedFuncOrAccessor = func->isDistributed();
286285
if (auto acc = dyn_cast<AccessorDecl>(func)) {
287286
isDistributedFuncOrAccessor =
288287
acc->getStorage()->isDistributed();

lib/Sema/CSDiagnostics.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9264,9 +9264,10 @@ bool NoopCheckedCast::diagnoseForcedCastExpr() const {
92649264
}
92659265

92669266
if (fromType->isEqual(toType)) {
9267-
auto castTypeRepr = expr->getCastTypeRepr();
9268-
emitDiagnostic(diag::forced_downcast_noop, toType)
9269-
.fixItRemove(SourceRange(diagLoc, castTypeRepr->getSourceRange().End));
9267+
auto diag = emitDiagnostic(diag::forced_downcast_noop, toType);
9268+
if (auto castTypeRepr = expr->getCastTypeRepr()) {
9269+
diag.fixItRemove(SourceRange(diagLoc, castTypeRepr->getSourceRange().End));
9270+
}
92709271

92719272
} else {
92729273
emitDiagnostic(diag::forced_downcast_coercion, fromType, toType)

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+
serializationRequirementMetaTypeTy->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)