Skip to content

Commit c9bfccb

Browse files
kavonktoso
authored andcommitted
[distributed] a number of ctor infra cleanups
1. We're no longer synthesizing the local init. All designated inits of a dist actor must have exactly one ActorTransport parameter. This parameter is used in the init's prologue to initialize some of its members. 2. We're synthesizing a factory function for the resolve initialization, instead of an actor-member named `init` to serve that purpose. As such, much of the earlier infrastructure is no longer needed, etc.
1 parent d5b7bb3 commit c9bfccb

File tree

9 files changed

+74
-124
lines changed

9 files changed

+74
-124
lines changed

include/swift/AST/Decl.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7003,18 +7003,6 @@ class ConstructorDecl : public AbstractFunctionDecl {
70037003
/// \endcode
70047004
bool isObjCZeroParameterWithLongSelector() const;
70057005

7006-
/// Checks if the initializer is a distributed actor's 'local' initializer:
7007-
/// ```
7008-
/// init(transport: ActorTransport)
7009-
/// ```
7010-
bool isDistributedActorLocalInit() const;
7011-
7012-
/// Checks if the initializer is a distributed actor's 'resolve' initializer:
7013-
/// ```
7014-
/// init(resolve address: ActorAddress, using transport: ActorTransport)
7015-
/// ```
7016-
bool isDistributedActorResolveInit() const;
7017-
70187006
static bool classof(const Decl *D) {
70197007
return D->getKind() == DeclKind::Constructor;
70207008
}

include/swift/AST/TypeCheckRequests.h

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1955,24 +1955,6 @@ class HasDefaultInitRequest
19551955
bool isCached() const { return true; }
19561956
};
19571957

1958-
/// Checks whether this type has a distributed actor "local" initializer.
1959-
class HasDistributedActorLocalInitRequest
1960-
: public SimpleRequest<HasDistributedActorLocalInitRequest, bool(NominalTypeDecl *),
1961-
RequestFlags::Cached> {
1962-
public:
1963-
using SimpleRequest::SimpleRequest;
1964-
1965-
private:
1966-
friend SimpleRequest;
1967-
1968-
// Evaluation.
1969-
bool evaluate(Evaluator &evaluator, NominalTypeDecl *decl) const;
1970-
1971-
public:
1972-
// Caching.
1973-
bool isCached() const { return true; }
1974-
};
1975-
19761958
/// Synthesizes a default initializer for a given type.
19771959
class SynthesizeDefaultInitRequest
19781960
: public SimpleRequest<SynthesizeDefaultInitRequest,

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,6 @@ SWIFT_REQUEST(TypeChecker, IsDefaultActorRequest,
100100
Cached, NoLocationInfo)
101101
SWIFT_REQUEST(TypeChecker, IsDistributedActorRequest, bool(NominalTypeDecl *),
102102
Cached, NoLocationInfo)
103-
SWIFT_REQUEST(TypeChecker, HasDistributedActorLocalInitRequest,
104-
bool(NominalTypeDecl *), Cached, NoLocationInfo)
105103
SWIFT_REQUEST(TypeChecker, IsDistributedFuncRequest, bool(FuncDecl *),
106104
Cached, NoLocationInfo)
107105
SWIFT_REQUEST(TypeChecker, GlobalActorInstanceRequest,

lib/AST/Decl.cpp

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8060,45 +8060,6 @@ bool ConstructorDecl::isObjCZeroParameterWithLongSelector() const {
80608060
return params->get(0)->getInterfaceType()->isVoid();
80618061
}
80628062

8063-
bool ConstructorDecl::isDistributedActorLocalInit() const {
8064-
auto name = getName();
8065-
auto argumentNames = name.getArgumentNames();
8066-
8067-
if (argumentNames.size() != 1)
8068-
return false;
8069-
8070-
auto &C = getASTContext();
8071-
if (argumentNames[0] != C.Id_transport)
8072-
return false;
8073-
8074-
auto *params = getParameters();
8075-
assert(params->size() == 1);
8076-
8077-
auto transportType = C.getActorTransportDecl()->getDeclaredInterfaceType();
8078-
return params->get(0)->getInterfaceType()->isEqual(transportType);
8079-
}
8080-
8081-
// TODO: remove resolve init in favor of resolve function?
8082-
bool ConstructorDecl::isDistributedActorResolveInit() const {
8083-
auto name = getName();
8084-
auto argumentNames = name.getArgumentNames();
8085-
8086-
if (argumentNames.size() != 2)
8087-
return false;
8088-
8089-
auto &C = getASTContext();
8090-
if (argumentNames[0] != C.Id_resolve ||
8091-
argumentNames[1] != C.Id_using)
8092-
return false;
8093-
8094-
auto *params = getParameters();
8095-
auto identityType = C.getAnyActorIdentityDecl()->getDeclaredInterfaceType();
8096-
auto transportType = C.getActorTransportDecl()->getDeclaredInterfaceType();
8097-
8098-
return params->get(0)->getInterfaceType()->isEqual(identityType) &&
8099-
params->get(1)->getInterfaceType()->isEqual(transportType);
8100-
}
8101-
81028063

81038064
DestructorDecl::DestructorDecl(SourceLoc DestructorLoc, DeclContext *Parent)
81048065
: AbstractFunctionDecl(DeclKind::Destructor, Parent,

lib/Sema/CodeSynthesis.cpp

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1464,24 +1464,6 @@ HasDefaultInitRequest::evaluate(Evaluator &evaluator,
14641464
return areAllStoredPropertiesDefaultInitializable(evaluator, decl);
14651465
}
14661466

1467-
bool
1468-
HasDistributedActorLocalInitRequest::evaluate(Evaluator &evaluator,
1469-
NominalTypeDecl *nominal) const {
1470-
if (auto *decl = dyn_cast<ClassDecl>(nominal)) {
1471-
if (!decl->isDistributedActor())
1472-
return false;
1473-
1474-
for (auto *member : decl->getMembers())
1475-
if (auto ctor = dyn_cast<ConstructorDecl>(member))
1476-
if (ctor->isDistributedActorLocalInit())
1477-
return true;
1478-
}
1479-
1480-
// areAllStoredPropertiesDefaultInitializable(evaluator, decl);
1481-
1482-
return false;
1483-
}
1484-
14851467
/// Synthesizer callback for a function body consisting of "return".
14861468
static std::pair<BraceStmt *, bool>
14871469
synthesizeSingleReturnFunctionBody(AbstractFunctionDecl *afd, void *) {

lib/Sema/CodeSynthesisDistributedActor.cpp

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
//===--- CodeSynthesis.cpp - Type Checking for Declarations ---------------===//
1+
//===--- CodeSynthesisDistributedActor.cpp --------------------------------===//
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -12,21 +12,15 @@
1212

1313
#include "TypeCheckDistributed.h"
1414

15-
#include "CodeSynthesis.h"
1615

1716
#include "TypeChecker.h"
18-
#include "TypeCheckDecl.h"
19-
#include "TypeCheckObjC.h"
2017
#include "TypeCheckType.h"
2118
#include "swift/AST/ASTPrinter.h"
2219
#include "swift/AST/Availability.h"
2320
#include "swift/AST/Expr.h"
2421
#include "swift/AST/GenericEnvironment.h"
2522
#include "swift/AST/Initializer.h"
2623
#include "swift/AST/ParameterList.h"
27-
#include "swift/AST/PrettyStackTrace.h"
28-
#include "swift/AST/ProtocolConformance.h"
29-
#include "swift/AST/SourceFile.h"
3024
#include "swift/AST/TypeCheckRequests.h"
3125
#include "swift/Basic/Defer.h"
3226
#include "swift/ClangImporter/ClangModule.h"
@@ -40,6 +34,76 @@ using namespace swift;
4034
/******************************* INITIALIZERS *********************************/
4135
/******************************************************************************/
4236

37+
/// Synthesizes the
38+
///
39+
/// \verbatim
40+
/// actor A {
41+
/// static resolve(_ address: ActorAddress,
42+
/// using transport: ActorTransport) throws -> Self
43+
/// }
44+
/// \endverbatim
45+
///
46+
/// factory function in the AST, with an empty body. It's body is
47+
/// expected to be filled-in during SILGen.
48+
static void addFactoryResolveFunction(ClassDecl *decl) {
49+
assert(decl->isDistributedActor());
50+
auto &ctx = decl->getASTContext();
51+
52+
{
53+
auto &C = ctx;
54+
auto conformanceDC = decl;
55+
56+
// Expected type: (Self) -> (ActorAddress, ActorTransport) -> (Self)
57+
//
58+
// Param: (resolve address: AnyActorAddress)
59+
auto addressType = C.getAnyActorIdentityDecl()->getDeclaredInterfaceType();
60+
auto *idParamDecl = new (C) ParamDecl(
61+
SourceLoc(), SourceLoc(), C.Id_resolve,
62+
SourceLoc(), C.Id_id, conformanceDC);
63+
idParamDecl->setImplicit();
64+
idParamDecl->setSpecifier(ParamSpecifier::Default);
65+
idParamDecl->setInterfaceType(addressType);
66+
67+
// Param: (using transport: ActorTransport)
68+
auto transportType = C.getActorTransportDecl()->getDeclaredInterfaceType();
69+
auto *transportParamDecl = new (C) ParamDecl(
70+
SourceLoc(), SourceLoc(), C.Id_using,
71+
SourceLoc(), C.Id_transport, conformanceDC);
72+
transportParamDecl->setImplicit();
73+
transportParamDecl->setSpecifier(ParamSpecifier::Default);
74+
transportParamDecl->setInterfaceType(transportType);
75+
76+
auto *paramList = ParameterList::create(
77+
C,
78+
/*LParenLoc=*/SourceLoc(),
79+
/*params=*/{idParamDecl, transportParamDecl},
80+
/*RParenLoc=*/SourceLoc()
81+
);
82+
83+
// Func name: init(resolve:using:)
84+
DeclName name(C, DeclBaseName::createConstructor(), paramList);
85+
86+
auto *initDecl =
87+
new (C) ConstructorDecl(name, SourceLoc(),
88+
/*Failable=*/false, SourceLoc(),
89+
/*Async=*/false, SourceLoc(),
90+
/*Throws=*/true, SourceLoc(),
91+
paramList,
92+
/*GenericParams=*/nullptr, conformanceDC);
93+
initDecl->setImplicit();
94+
// TODO: determine how to mark this as being synthesized by SILGen.
95+
// initDecl->setSynthesized();
96+
// initDecl->setBodySynthesizer(&createDistributedActor_init_resolve_body);
97+
98+
auto *nonIsoAttr = new (C) NonisolatedAttr(/*IsImplicit*/true);
99+
initDecl->getAttrs().add(nonIsoAttr);
100+
101+
initDecl->copyFormalAccessFrom(decl, /*sourceIsParentContext=*/true);
102+
103+
decl->addMember(initDecl);
104+
}
105+
}
106+
43107
/// Synthesizes an empty body of the `init(transport:)` initializer as:
44108
///
45109
/// ```
@@ -449,6 +513,7 @@ void swift::addImplicitDistributedActorMembersToClass(ClassDecl *decl) {
449513
if (!swift::ensureDistributedModuleLoaded(decl))
450514
return;
451515

516+
addFactoryResolveFunction(decl);
452517
addImplicitDistributedActorStoredProperties(decl);
453518
addImplicitRemoteActorFunctions(decl);
454519
// addImplicitResignIdentity(decl);

lib/Sema/TypeCheckDecl.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -445,14 +445,6 @@ InitKindRequest::evaluate(Evaluator &evaluator, ConstructorDecl *decl) const {
445445
}
446446
}
447447

448-
// the init(transport:) initializer of a distributed actor is special, as
449-
// it ties the actors lifecycle with the transport. As such, it must always
450-
// be invoked by any other initializer, just like a designated initializer.
451-
if (auto clazz = dyn_cast<ClassDecl>(decl->getDeclContext())) {
452-
if (clazz->isDistributedActor() && decl->isDistributedActorLocalInit())
453-
return CtorInitializerKind::Designated;
454-
}
455-
456448
if (decl->getDeclContext()->getExtendedProtocolDecl()) {
457449
return CtorInitializerKind::Convenience;
458450
}

lib/Sema/TypeCheckStmt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "TypeChecker.h"
1818
#include "TypeCheckAvailability.h"
1919
#include "TypeCheckConcurrency.h"
20+
#include "TypeCheckDistributed.h"
2021
#include "TypeCheckType.h"
2122
#include "MiscDiagnostics.h"
2223
#include "swift/Subsystems.h"
@@ -2038,7 +2039,6 @@ TypeCheckFunctionBodyRequest::evaluate(Evaluator &evaluator,
20382039
// Class constructor checking.
20392040
if (auto *ctor = dyn_cast<ConstructorDecl>(AFD)) {
20402041
if (auto classDecl = ctor->getDeclContext()->getSelfClassDecl()) {
2041-
// checkActorConstructor(classDecl, ctor);
20422042
checkClassConstructorBody(classDecl, ctor, body);
20432043
}
20442044
}

stdlib/public/Distributed/DistributedActor.swift

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ public protocol AnyActor: AnyObject {}
3636
public protocol DistributedActor:
3737
AnyActor,
3838
Identifiable, Hashable, Codable {
39-
4039
/// Resolves the passed in `identity` against the `transport`, returning
4140
/// either a local or remote actor reference.
4241
///
@@ -74,23 +73,6 @@ public protocol DistributedActor:
7473
nonisolated var id: AnyActorIdentity { get }
7574
}
7675

77-
@available(SwiftStdlib 5.5, *)
78-
extension DistributedActor {
79-
80-
public static func resolve<Identity>(_ identity: Identity, using transport: ActorTransport)
81-
throws -> Self where Identity: ActorIdentity {
82-
switch try transport.resolve(AnyActorIdentity(identity), as: Self.self) {
83-
case .resolved(let instance):
84-
return instance
85-
86-
case .makeProxy:
87-
// FIXME: this needs actual implementation of distributedActorRemoteCreate
88-
let remote: Any = distributedActorRemoteCreate(identity: identity, transport: transport)
89-
return remote as! Self
90-
}
91-
}
92-
}
93-
9476
// ==== Hashable conformance ---------------------------------------------------
9577

9678
@available(SwiftStdlib 5.5, *)

0 commit comments

Comments
 (0)