Skip to content

Commit acd9299

Browse files
committed
[stdlib] Distributed: Remove invokeOnReturn requirement and its synthesis
This is no longer necessary because `onReturn` is a protocol requirement now.
1 parent c6d1395 commit acd9299

File tree

3 files changed

+6
-346
lines changed

3 files changed

+6
-346
lines changed

lib/Sema/DerivedConformanceDistributedActor.cpp

Lines changed: 0 additions & 320 deletions
Original file line numberDiff line numberDiff line change
@@ -121,318 +121,6 @@ static FuncDecl *deriveDistributedActor_resolve(DerivedConformance &derived) {
121121
return factoryDecl;
122122
}
123123

124-
/******************************************************************************/
125-
/*************** INVOKE HANDLER ON-RETURN FUNCTION ****************************/
126-
/******************************************************************************/
127-
128-
namespace {
129-
struct DoInvokeOnReturnContext {
130-
ParamDecl *handlerParam;
131-
ParamDecl *resultBufferParam;
132-
};
133-
} // namespace
134-
135-
static std::pair<BraceStmt *, bool>
136-
deriveBodyDistributed_doInvokeOnReturn(AbstractFunctionDecl *afd, void *arg) {
137-
auto &C = afd->getASTContext();
138-
auto *context = static_cast<DoInvokeOnReturnContext *>(arg);
139-
140-
// mock locations, we're a thunk and don't really need detailed locations
141-
const SourceLoc sloc = SourceLoc();
142-
const DeclNameLoc dloc = DeclNameLoc();
143-
bool implicit = true;
144-
145-
auto returnTypeParam = afd->getParameters()->get(0);
146-
SmallVector<ASTNode, 8> stmts;
147-
148-
VarDecl *resultVar =
149-
new (C) VarDecl(/*isStatic=*/false, VarDecl::Introducer::Let, sloc,
150-
C.getIdentifier("result"), afd);
151-
{
152-
auto resultLoadCall = CallExpr::createImplicit(
153-
C,
154-
UnresolvedDotExpr::createImplicit(
155-
C,
156-
/*base=*/
157-
new (C) DeclRefExpr(ConcreteDeclRef(context->resultBufferParam),
158-
dloc, implicit),
159-
/*baseName=*/DeclBaseName(C.getIdentifier("load")),
160-
/*argLabels=*/
161-
{C.getIdentifier("fromByteOffset"), C.getIdentifier("as")}),
162-
ArgumentList::createImplicit(
163-
C, {Argument(sloc, C.getIdentifier("as"),
164-
new (C) DeclRefExpr(ConcreteDeclRef(returnTypeParam),
165-
dloc, implicit))}));
166-
167-
auto resultPattern = NamedPattern::createImplicit(C, resultVar);
168-
auto resultPB = PatternBindingDecl::createImplicit(
169-
C, swift::StaticSpellingKind::None, resultPattern,
170-
/*expr=*/resultLoadCall, afd);
171-
172-
stmts.push_back(resultPB);
173-
stmts.push_back(resultVar);
174-
}
175-
176-
// call the ad-hoc `handler.onReturn`
177-
{
178-
// Find the ad-hoc requirement ensured function on the concrete handler:
179-
auto onReturnFunc = C.getOnReturnOnDistributedTargetInvocationResultHandler(
180-
context->handlerParam->getInterfaceType()->getAnyNominal());
181-
assert(onReturnFunc && "did not find ad-hoc requirement witness!");
182-
183-
Expr *callExpr = CallExpr::createImplicit(
184-
C,
185-
UnresolvedDotExpr::createImplicit(
186-
C,
187-
/*base=*/
188-
new (C) DeclRefExpr(ConcreteDeclRef(context->handlerParam), dloc,
189-
implicit),
190-
/*baseName=*/onReturnFunc->getBaseName(),
191-
/*paramList=*/onReturnFunc->getParameters()),
192-
ArgumentList::forImplicitCallTo(
193-
DeclNameRef(onReturnFunc->getName()),
194-
{new (C) DeclRefExpr(ConcreteDeclRef(resultVar), dloc, implicit)},
195-
C));
196-
callExpr = TryExpr::createImplicit(C, sloc, callExpr);
197-
callExpr = AwaitExpr::createImplicit(C, sloc, callExpr);
198-
199-
stmts.push_back(callExpr);
200-
}
201-
202-
auto body = BraceStmt::create(C, sloc, {stmts}, sloc, implicit);
203-
return {body, /*isTypeChecked=*/false};
204-
}
205-
206-
// Create local function:
207-
// func invokeOnReturn<R: Self.SerializationRequirement>(
208-
// _ returnType: R.Type
209-
// ) async throws {
210-
// let value = resultBuffer.load(as: returnType)
211-
// try await handler.onReturn(value: value)
212-
// }
213-
static FuncDecl* createLocalFunc_doInvokeOnReturn(
214-
ASTContext& C, FuncDecl* parentFunc,
215-
NominalTypeDecl* systemNominal,
216-
ParamDecl* handlerParam,
217-
ParamDecl* resultBufParam) {
218-
auto DC = parentFunc;
219-
auto DAS = C.getDistributedActorSystemDecl();
220-
auto doInvokeLocalFuncIdent = C.getIdentifier("doInvokeOnReturn");
221-
222-
// mock locations, we're a synthesized func and don't need real locations
223-
const SourceLoc sloc = SourceLoc();
224-
225-
// <R: Self.SerializationRequirement>
226-
// We create the generic param at invalid depth, which means it'll be filled
227-
// by semantic analysis.
228-
auto *resultGenericParamDecl = GenericTypeParamDecl::createImplicit(
229-
parentFunc, C.getIdentifier("R"), /*depth*/ 0, /*index*/ 0);
230-
GenericParamList *doInvokeGenericParamList =
231-
GenericParamList::create(C, sloc, {resultGenericParamDecl}, sloc);
232-
233-
auto returnTypeIdent = C.getIdentifier("returnType");
234-
auto resultTyParamDecl =
235-
ParamDecl::createImplicit(C,
236-
/*argument=*/returnTypeIdent,
237-
/*parameter=*/returnTypeIdent,
238-
resultGenericParamDecl->getInterfaceType(), DC);
239-
ParameterList *doInvokeParamsList =
240-
ParameterList::create(C, {resultTyParamDecl});
241-
242-
SmallVector<Requirement, 2> requirements;
243-
for (auto p : getDistributedSerializationRequirementProtocols(systemNominal, DAS)) {
244-
auto requirement =
245-
Requirement(RequirementKind::Conformance,
246-
resultGenericParamDecl->getDeclaredInterfaceType(),
247-
p->getDeclaredInterfaceType());
248-
requirements.push_back(requirement);
249-
}
250-
GenericSignature doInvokeGenSig =
251-
buildGenericSignature(C, parentFunc->getGenericSignature(),
252-
{resultGenericParamDecl->getDeclaredInterfaceType()
253-
->castTo<GenericTypeParamType>()},
254-
std::move(requirements));
255-
256-
FuncDecl *doInvokeOnReturnFunc = FuncDecl::createImplicit(
257-
C, swift::StaticSpellingKind::None,
258-
DeclName(C, doInvokeLocalFuncIdent, doInvokeParamsList),
259-
sloc,
260-
/*async=*/true,
261-
/*throws=*/true,
262-
/*ThrownType=*/Type(),
263-
doInvokeGenericParamList, doInvokeParamsList,
264-
/*returnType=*/C.TheEmptyTupleType, parentFunc);
265-
doInvokeOnReturnFunc->setImplicit();
266-
doInvokeOnReturnFunc->setSynthesized();
267-
doInvokeOnReturnFunc->setGenericSignature(doInvokeGenSig);
268-
269-
auto *doInvokeContext = C.Allocate<DoInvokeOnReturnContext>();
270-
doInvokeContext->handlerParam = handlerParam;
271-
doInvokeContext->resultBufferParam = resultBufParam;
272-
doInvokeOnReturnFunc->setBodySynthesizer(
273-
deriveBodyDistributed_doInvokeOnReturn, doInvokeContext);
274-
275-
return doInvokeOnReturnFunc;
276-
}
277-
278-
static std::pair<BraceStmt *, bool>
279-
deriveBodyDistributed_invokeHandlerOnReturn(AbstractFunctionDecl *afd,
280-
void *context) {
281-
auto implicit = true;
282-
ASTContext &C = afd->getASTContext();
283-
auto DC = afd->getDeclContext();
284-
auto DAS = C.getDistributedActorSystemDecl();
285-
286-
// mock locations, we're a thunk and don't really need detailed locations
287-
const SourceLoc sloc = SourceLoc();
288-
const DeclNameLoc dloc = DeclNameLoc();
289-
290-
NominalTypeDecl *nominal = dyn_cast<NominalTypeDecl>(DC);
291-
assert(nominal);
292-
293-
auto func = dyn_cast<FuncDecl>(afd);
294-
assert(func);
295-
296-
// === parameters
297-
auto params = func->getParameters();
298-
assert(params->size() == 3);
299-
auto handlerParam = params->get(0);
300-
auto resultBufParam = params->get(1);
301-
auto metatypeParam = params->get(2);
302-
303-
auto serializationRequirementTypeTy =
304-
getDistributedSerializationRequirementType(nominal, DAS);
305-
306-
auto serializationRequirementMetaTypeTy =
307-
ExistentialMetatypeType::get(serializationRequirementTypeTy);
308-
309-
// Statements
310-
SmallVector<ASTNode, 8> stmts;
311-
312-
// --- `let m = metatype as! SerializationRequirement.Type`
313-
VarDecl *metatypeVar =
314-
new (C) VarDecl(/*isStatic=*/false, VarDecl::Introducer::Let, sloc,
315-
C.getIdentifier("m"), func);
316-
{
317-
metatypeVar->setImplicit();
318-
metatypeVar->setSynthesized();
319-
320-
// metatype as! <<concrete SerializationRequirement.Type>>
321-
auto metatypeRef =
322-
new (C) DeclRefExpr(ConcreteDeclRef(metatypeParam), dloc, implicit);
323-
auto metatypeSRCastExpr = ForcedCheckedCastExpr::createImplicit(
324-
C, metatypeRef, serializationRequirementMetaTypeTy);
325-
326-
auto metatypePattern = NamedPattern::createImplicit(C, metatypeVar);
327-
auto metatypePB = PatternBindingDecl::createImplicit(
328-
C, swift::StaticSpellingKind::None, metatypePattern,
329-
/*expr=*/metatypeSRCastExpr, func);
330-
331-
stmts.push_back(metatypePB);
332-
stmts.push_back(metatypeVar);
333-
}
334-
335-
// --- Declare the local function `doInvokeOnReturn`...
336-
FuncDecl *doInvokeOnReturnFunc = createLocalFunc_doInvokeOnReturn(
337-
C, func,
338-
nominal, handlerParam, resultBufParam);
339-
stmts.push_back(doInvokeOnReturnFunc);
340-
341-
// --- try await _openExistential(metatypeVar, do: <<doInvokeLocalFunc>>)
342-
{
343-
auto openExistentialBaseIdent = C.getIdentifier("_openExistential");
344-
auto doIdent = C.getIdentifier("do");
345-
346-
auto openExArgs = ArgumentList::createImplicit(
347-
C, {
348-
Argument(sloc, Identifier(),
349-
new (C) DeclRefExpr(ConcreteDeclRef(metatypeVar), dloc,
350-
implicit)),
351-
Argument(sloc, doIdent,
352-
new (C) DeclRefExpr(ConcreteDeclRef(doInvokeOnReturnFunc),
353-
dloc, implicit)),
354-
});
355-
Expr *tryAwaitDoOpenExistential =
356-
CallExpr::createImplicit(C,
357-
UnresolvedDeclRefExpr::createImplicit(
358-
C, openExistentialBaseIdent),
359-
openExArgs);
360-
361-
tryAwaitDoOpenExistential =
362-
AwaitExpr::createImplicit(C, sloc, tryAwaitDoOpenExistential);
363-
tryAwaitDoOpenExistential =
364-
TryExpr::createImplicit(C, sloc, tryAwaitDoOpenExistential);
365-
366-
stmts.push_back(tryAwaitDoOpenExistential);
367-
}
368-
369-
auto body = BraceStmt::create(C, sloc, {stmts}, sloc, implicit);
370-
return {body, /*isTypeChecked=*/false};
371-
}
372-
373-
/// Synthesizes the
374-
///
375-
/// \verbatim
376-
/// static func invokeHandlerOnReturn(
377-
//// handler: ResultHandler,
378-
//// resultBuffer: UnsafeRawPointer,
379-
//// metatype _metatype: Any.Type
380-
//// ) async throws
381-
/// \endverbatim
382-
static FuncDecl *deriveDistributedActorSystem_invokeHandlerOnReturn(
383-
DerivedConformance &derived) {
384-
auto system = derived.Nominal;
385-
auto &C = system->getASTContext();
386-
387-
// auto serializationRequirementType = getDistributedActorSystemType(decl);
388-
auto resultHandlerType = getDistributedActorSystemResultHandlerType(system);
389-
auto unsafeRawPointerType = C.getUnsafeRawPointerType();
390-
auto anyTypeType = ExistentialMetatypeType::get(C.TheAnyType); // Any.Type
391-
392-
// auto serializationRequirementType =
393-
// getDistributedSerializationRequirementType(system, DAS);
394-
395-
// params:
396-
// - handler: Self.ResultHandler
397-
// - resultBuffer:
398-
// - metatype _metatype: Any.Type
399-
auto *params = ParameterList::create(
400-
C,
401-
/*LParenLoc=*/SourceLoc(),
402-
/*params=*/
403-
{
404-
ParamDecl::createImplicit(
405-
C, C.Id_handler, C.Id_handler,
406-
system->mapTypeIntoContext(resultHandlerType), system),
407-
ParamDecl::createImplicit(
408-
C, C.Id_resultBuffer, C.Id_resultBuffer,
409-
unsafeRawPointerType, system),
410-
ParamDecl::createImplicit(
411-
C, C.Id_metatype, C.Id_metatype,
412-
anyTypeType, system)
413-
},
414-
/*RParenLoc=*/SourceLoc());
415-
416-
// Func name: invokeHandlerOnReturn(handler:resultBuffer:metatype)
417-
DeclName name(C, C.Id_invokeHandlerOnReturn, params);
418-
419-
// Expected type: (Self.ResultHandler, UnsafeRawPointer, any Any.Type) async
420-
// throws -> ()
421-
auto *funcDecl =
422-
FuncDecl::createImplicit(C, StaticSpellingKind::None, name, SourceLoc(),
423-
/*async=*/true,
424-
/*throws=*/true,
425-
/*ThrownType=*/Type(),
426-
/*genericParams=*/nullptr, params,
427-
/*returnType*/ TupleType::getEmpty(C), system);
428-
funcDecl->setSynthesized(true);
429-
funcDecl->copyFormalAccessFrom(system, /*sourceIsParentContext=*/true);
430-
funcDecl->setBodySynthesizer(deriveBodyDistributed_invokeHandlerOnReturn);
431-
432-
derived.addMembersToConformanceContext({funcDecl});
433-
return funcDecl;
434-
}
435-
436124
/******************************************************************************/
437125
/******************************* PROPERTIES ***********************************/
438126
/******************************************************************************/
@@ -935,14 +623,6 @@ std::pair<Type, TypeDecl *> DerivedConformance::deriveDistributedActor(
935623

936624
ValueDecl *
937625
DerivedConformance::deriveDistributedActorSystem(ValueDecl *requirement) {
938-
if (auto func = dyn_cast<FuncDecl>(requirement)) {
939-
// just a simple name check is enough here,
940-
// if we are invoked here we know for sure it is for the "right" function
941-
if (func->getName().getBaseName() == Context.Id_invokeHandlerOnReturn) {
942-
return deriveDistributedActorSystem_invokeHandlerOnReturn(*this);
943-
}
944-
}
945-
946626
return nullptr;
947627
}
948628

lib/Sema/DerivedConformances.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -400,11 +400,6 @@ ValueDecl *DerivedConformance::getDerivableRequirement(NominalTypeDecl *nominal,
400400
}
401401
}
402402

403-
// DistributedActor.actorSystem
404-
if (name.isCompoundName() &&
405-
name.getBaseName() == ctx.Id_invokeHandlerOnReturn)
406-
return getRequirement(KnownProtocolKind::DistributedActorSystem);
407-
408403
return nullptr;
409404
}
410405

stdlib/public/Distributed/DistributedActorSystem.swift

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -416,22 +416,6 @@ public protocol DistributedActorSystem<SerializationRequirement>: Sendable {
416416
where Act: DistributedActor,
417417
Act.ID == ActorID,
418418
Err: Error
419-
420-
// Implementation notes:
421-
// The `metatype` must be the type of `Value`, and it must conform to
422-
// `SerializationRequirement`. If it does not, the method will crash at
423-
// runtime. This is because we cannot express
424-
// `Value: SerializationRequirement`, however the generic `Value` is still
425-
// useful since it allows us to avoid boxing the value into an existential,
426-
// before we'd right away unbox it as first thing in the implementation of
427-
// this function.
428-
/// Implementation synthesized by the compiler.
429-
/// Not intended to be invoked explicitly from user code!
430-
func invokeHandlerOnReturn(
431-
handler: ResultHandler,
432-
resultBuffer: UnsafeRawPointer,
433-
metatype: Any.Type
434-
) async throws
435419
}
436420

437421
// ==== ----------------------------------------------------------------------------------------------------------------
@@ -619,11 +603,12 @@ extension DistributedActorSystem {
619603
if returnType == Void.self {
620604
try await handler.onReturnVoid()
621605
} else {
622-
try await self.invokeHandlerOnReturn(
623-
handler: handler,
624-
resultBuffer: resultBuffer,
625-
metatype: returnType
626-
)
606+
func invokeOnReturn<R>(_ returnType: R.Type) async throws {
607+
let value = resultBuffer.load(as: returnType)
608+
try await handler.onReturn(value: value)
609+
}
610+
611+
try await _openExistential(returnType, do: invokeOnReturn)
627612
}
628613
} catch {
629614
try await handler.onThrow(error: error)

0 commit comments

Comments
 (0)