Skip to content

Commit 68d72cd

Browse files
authored
Merge pull request #38993 from ktoso/wip-revert-38974
2 parents fe4ba18 + beaf8a3 commit 68d72cd

18 files changed

+84
-198
lines changed

include/swift/AST/Decl.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5986,10 +5986,6 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
59865986
/// Returns 'true' if the function is distributed.
59875987
bool isDistributed() const;
59885988

5989-
/// Get (or synthesize) the associated remote function for this one.
5990-
/// For example, for `distributed func hi()` get `func _remote_hi()`.
5991-
AbstractFunctionDecl *getDistributedActorRemoteFuncDecl() const;
5992-
59935989
PolymorphicEffectKind getPolymorphicEffectKind(EffectKind kind) const;
59945990

59955991
// FIXME: Hack that provides names with keyword arguments for accessors.

include/swift/AST/TypeCheckRequests.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -933,18 +933,18 @@ class IsDistributedActorRequest :
933933
bool isCached() const { return true; }
934934
};
935935

936-
/// Obtain the 'remote' counterpart of a 'distributed func'.
937-
class GetDistributedRemoteFuncRequest :
938-
public SimpleRequest<GetDistributedRemoteFuncRequest,
939-
AbstractFunctionDecl *(AbstractFunctionDecl *),
936+
/// Determine whether the given func is distributed.
937+
class IsDistributedFuncRequest :
938+
public SimpleRequest<IsDistributedFuncRequest,
939+
bool(FuncDecl *),
940940
RequestFlags::Cached> {
941941
public:
942942
using SimpleRequest::SimpleRequest;
943943

944944
private:
945945
friend SimpleRequest;
946946

947-
AbstractFunctionDecl *evaluate(Evaluator &evaluator, AbstractFunctionDecl *func) const;
947+
bool evaluate(Evaluator &evaluator, FuncDecl *func) const;
948948

949949
public:
950950
// Caching
@@ -2046,6 +2046,8 @@ enum class ImplicitMemberAction : uint8_t {
20462046
ResolveEncodable,
20472047
ResolveDecodable,
20482048
ResolveDistributedActor,
2049+
ResolveDistributedActorIdentity,
2050+
ResolveDistributedActorTransport,
20492051
};
20502052

20512053
class ResolveImplicitMemberRequest

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ SWIFT_REQUEST(TypeChecker, IsDefaultActorRequest,
100100
Cached, NoLocationInfo)
101101
SWIFT_REQUEST(TypeChecker, IsDistributedActorRequest, bool(NominalTypeDecl *),
102102
Cached, NoLocationInfo)
103-
SWIFT_REQUEST(TypeChecker, GetDistributedRemoteFuncRequest, AbstractFunctionDecl *(AbstractFunctionDecl *),
103+
SWIFT_REQUEST(TypeChecker, IsDistributedFuncRequest, bool(FuncDecl *),
104104
Cached, NoLocationInfo)
105105
SWIFT_REQUEST(TypeChecker, GlobalActorInstanceRequest,
106106
VarDecl *(NominalTypeDecl *),

lib/AST/Decl.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4222,12 +4222,21 @@ void NominalTypeDecl::synthesizeSemanticMembersIfNeeded(DeclName member) {
42224222
if (baseName == DeclBaseName::createConstructor()) {
42234223
if ((member.isSimpleName() || argumentNames.front() == Context.Id_from)) {
42244224
action.emplace(ImplicitMemberAction::ResolveDecodable);
4225+
} else if (argumentNames.front() == Context.Id_transport) {
4226+
action.emplace(ImplicitMemberAction::ResolveDistributedActorTransport);
42254227
}
42264228
} else if (!baseName.isSpecial() &&
42274229
baseName.getIdentifier() == Context.Id_encode &&
42284230
(member.isSimpleName() || argumentNames.front() == Context.Id_to)) {
42294231
action.emplace(ImplicitMemberAction::ResolveEncodable);
42304232
}
4233+
} else if (member.isSimpleName() || argumentNames.size() == 2) {
4234+
if (baseName == DeclBaseName::createConstructor()) {
4235+
if (argumentNames[0] == Context.Id_resolve &&
4236+
argumentNames[1] == Context.Id_using) {
4237+
action.emplace(ImplicitMemberAction::ResolveDistributedActor);
4238+
}
4239+
}
42314240
}
42324241
}
42334242

@@ -7208,19 +7217,14 @@ bool AbstractFunctionDecl::isSendable() const {
72087217
}
72097218

72107219
bool AbstractFunctionDecl::isDistributed() const {
7211-
return this->getAttrs().hasAttribute<DistributedActorAttr>();
7212-
}
7213-
7214-
AbstractFunctionDecl*
7215-
AbstractFunctionDecl::getDistributedActorRemoteFuncDecl() const {
7216-
if (!this->isDistributed())
7217-
return nullptr;
7220+
auto func = dyn_cast<FuncDecl>(this);
7221+
if (!func)
7222+
return false;
72187223

7219-
auto mutableThis = const_cast<AbstractFunctionDecl *>(this);
7220-
return evaluateOrDefault(
7221-
getASTContext().evaluator,
7222-
GetDistributedRemoteFuncRequest{mutableThis},
7223-
nullptr);
7224+
auto mutableFunc = const_cast<FuncDecl *>(func);
7225+
return evaluateOrDefault(getASTContext().evaluator,
7226+
IsDistributedFuncRequest{mutableFunc},
7227+
false);
72247228
}
72257229

72267230
BraceStmt *AbstractFunctionDecl::getBody(bool canSynthesize) const {

lib/AST/TypeCheckRequests.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,12 @@ void swift::simple_display(llvm::raw_ostream &out,
10351035
case ImplicitMemberAction::ResolveDistributedActor:
10361036
out << "resolve DistributedActor";
10371037
break;
1038+
case ImplicitMemberAction::ResolveDistributedActorIdentity:
1039+
out << "resolve DistributedActor.id";
1040+
break;
1041+
case ImplicitMemberAction::ResolveDistributedActorTransport:
1042+
out << "resolve DistributedActor.actorTransport";
1043+
break;
10381044
}
10391045
}
10401046

lib/SILGen/SILGenDistributed.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,7 @@ void SILGenFunction::emitDistributedActor_resignAddress(
814814
void SILGenFunction::emitDistributedActorClassMemberDestruction(
815815
SILLocation cleanupLoc, ManagedValue selfValue, ClassDecl *cd,
816816
SILBasicBlock *normalMemberDestroyBB, SILBasicBlock *finishBB) {
817+
ASTContext &ctx = getASTContext();
817818
auto selfTy = cd->getDeclaredInterfaceType();
818819

819820
Scope scope(Cleanups, CleanupLocation(cleanupLoc));

lib/Sema/CodeSynthesis.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,10 +1255,9 @@ ResolveImplicitMemberRequest::evaluate(Evaluator &evaluator,
12551255

12561256
auto &Context = target->getASTContext();
12571257
switch (action) {
1258-
case ImplicitMemberAction::ResolveImplicitInit: {
1258+
case ImplicitMemberAction::ResolveImplicitInit:
12591259
TypeChecker::addImplicitConstructors(target);
12601260
break;
1261-
}
12621261
case ImplicitMemberAction::ResolveCodingKeys: {
12631262
// CodingKeys is a special type which may be synthesized as part of
12641263
// Encodable/Decodable conformance. If the target conforms to either
@@ -1275,17 +1274,17 @@ ResolveImplicitMemberRequest::evaluate(Evaluator &evaluator,
12751274
if (!evaluateTargetConformanceTo(decodableProto)) {
12761275
(void)evaluateTargetConformanceTo(encodableProto);
12771276
}
1278-
break;
12791277
}
1278+
break;
12801279
case ImplicitMemberAction::ResolveEncodable: {
12811280
// encode(to:) may be synthesized as part of derived conformance to the
12821281
// Encodable protocol.
12831282
// If the target should conform to the Encodable protocol, check the
12841283
// conformance here to attempt synthesis.
12851284
auto *encodableProto = Context.getProtocol(KnownProtocolKind::Encodable);
12861285
(void)evaluateTargetConformanceTo(encodableProto);
1287-
break;
12881286
}
1287+
break;
12891288
case ImplicitMemberAction::ResolveDecodable: {
12901289
// init(from:) may be synthesized as part of derived conformance to the
12911290
// Decodable protocol.
@@ -1294,11 +1293,17 @@ ResolveImplicitMemberRequest::evaluate(Evaluator &evaluator,
12941293
TypeChecker::addImplicitConstructors(target);
12951294
auto *decodableProto = Context.getProtocol(KnownProtocolKind::Decodable);
12961295
(void)evaluateTargetConformanceTo(decodableProto);
1297-
break;
12981296
}
1299-
case ImplicitMemberAction::ResolveDistributedActor: {
1300-
if (auto classDecl = dyn_cast<ClassDecl>(target))
1301-
swift::addImplicitDistributedActorMembers(classDecl);
1297+
break;
1298+
case ImplicitMemberAction::ResolveDistributedActor:
1299+
case ImplicitMemberAction::ResolveDistributedActorTransport:
1300+
case ImplicitMemberAction::ResolveDistributedActorIdentity: {
1301+
// init(transport:) and init(resolve:using:) may be synthesized as part of
1302+
// derived conformance to the DistributedActor protocol.
1303+
// If the target should conform to the DistributedActor protocol, check the
1304+
// conformance here to attempt synthesis.
1305+
// FIXME(distributed): invoke the requirement adding explicitly here
1306+
TypeChecker::addImplicitConstructors(target);
13021307
auto *distributedActorProto =
13031308
Context.getProtocol(KnownProtocolKind::DistributedActor);
13041309
(void)evaluateTargetConformanceTo(distributedActorProto);

lib/Sema/CodeSynthesisDistributedActor.cpp

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,7 @@ synthesizeRemoteFuncStubBody(AbstractFunctionDecl *func, void *context) {
205205
auto uintInit = ctx.getIntBuiltinInitDecl(uintDecl);
206206

207207
auto missingTransportDecl = ctx.getMissingDistributedActorTransport();
208-
assert(missingTransportDecl &&
209-
"Could not locate '_missingDistributedActorTransport' function");
208+
assert(missingTransportDecl && "Could not locate '_missingDistributedActorTransport' function");
210209

211210
// Create a call to _Distributed._missingDistributedActorTransport
212211
auto loc = func->getLoc();
@@ -244,10 +243,16 @@ synthesizeRemoteFuncStubBody(AbstractFunctionDecl *func, void *context) {
244243
file->setBuiltinInitializer(staticStringInit);
245244

246245
auto startLineAndCol = SM.getPresumedLineAndColumnForLoc(distributedFunc->getStartLoc());
246+
// auto *line = new (ctx) MagicIdentifierLiteralExpr(
247+
// MagicIdentifierLiteralExpr::Line, loc, /*Implicit=*/true);
248+
// auto *line = new (ctx) IntegerLiteralExpr(startLineAndCol.first, loc,
249+
// /*implicit*/ true);
247250
auto *line = IntegerLiteralExpr::createFromUnsigned(ctx, startLineAndCol.first);
248251
line->setType(uintType);
249252
line->setBuiltinInitializer(uintInit);
250253

254+
// auto *column = new (ctx) MagicIdentifierLiteralExpr(
255+
// MagicIdentifierLiteralExpr::Column, loc, /*Implicit=*/true);
251256
auto *column = IntegerLiteralExpr::createFromUnsigned(ctx, startLineAndCol.second);
252257
column->setType(uintType);
253258
column->setBuiltinInitializer(uintInit);
@@ -259,6 +264,7 @@ synthesizeRemoteFuncStubBody(AbstractFunctionDecl *func, void *context) {
259264

260265
SmallVector<ASTNode, 2> stmts;
261266
stmts.push_back(call); // something() -> Never
267+
// stmts.push_back(new (ctx) ReturnStmt(SourceLoc(), /*Result=*/nullptr)); // FIXME: this causes 'different types for return type: String vs. ()'
262268
auto body = BraceStmt::create(ctx, SourceLoc(), stmts, SourceLoc(),
263269
/*implicit=*/true);
264270
return { body, /*isTypeChecked=*/true };
@@ -284,8 +290,7 @@ static Identifier makeRemoteFuncIdentifier(FuncDecl* func) {
284290
///
285291
/// and is intended to be replaced by a transport library by providing an
286292
/// appropriate @_dynamicReplacement function.
287-
static FuncDecl*
288-
addImplicitRemoteFunction(ClassDecl *decl, FuncDecl *func) {
293+
static void addImplicitRemoteActorFunction(ClassDecl *decl, FuncDecl *func) {
289294
auto &C = decl->getASTContext();
290295
auto parentDC = decl;
291296

@@ -310,10 +315,6 @@ addImplicitRemoteFunction(ClassDecl *decl, FuncDecl *func) {
310315
remoteFuncDecl->getAttrs().add(
311316
new (C) DistributedActorIndependentAttr(/*IsImplicit=*/true));
312317

313-
// nonisolated
314-
remoteFuncDecl->getAttrs().add(
315-
new (C) NonisolatedAttr(/*IsImplicit=*/true));
316-
317318
// users should never have to access this function directly;
318319
// it is only invoked from our distributed function thunk if the actor is remote.
319320
remoteFuncDecl->setUserAccessible(false);
@@ -325,16 +326,16 @@ addImplicitRemoteFunction(ClassDecl *decl, FuncDecl *func) {
325326
remoteFuncDecl->copyFormalAccessFrom(func, /*sourceIsParentContext=*/false);
326327

327328
decl->addMember(remoteFuncDecl);
328-
return remoteFuncDecl;
329329
}
330330

331331
/// Synthesize dynamic _remote stub functions for each encountered distributed function.
332-
static void addImplicitRemoteFunctions(ClassDecl *decl) {
332+
static void addImplicitRemoteActorFunctions(ClassDecl *decl) {
333333
assert(decl->isDistributedActor());
334+
334335
for (auto member : decl->getMembers()) {
335-
auto func = dyn_cast<AbstractFunctionDecl>(member);
336+
auto func = dyn_cast<FuncDecl>(member);
336337
if (func && func->isDistributed()) {
337-
(void) func->getDistributedActorRemoteFuncDecl();
338+
addImplicitRemoteActorFunction(decl, func);
338339
}
339340
}
340341
}
@@ -343,33 +344,8 @@ static void addImplicitRemoteFunctions(ClassDecl *decl) {
343344
/************************ SYNTHESIS ENTRY POINT *******************************/
344345
/******************************************************************************/
345346

346-
AbstractFunctionDecl *TypeChecker::addImplicitDistributedActorRemoteFunction(
347-
ClassDecl *decl, AbstractFunctionDecl *AFD) {
348-
if (!decl->isDistributedActor())
349-
return nullptr;
350-
351-
if (auto func = dyn_cast<FuncDecl>(AFD))
352-
return addImplicitRemoteFunction(decl, func);
353-
354-
return nullptr;
355-
}
356-
357-
void TypeChecker::addImplicitDistributedActorRemoteFunctions(NominalTypeDecl *decl) {
358-
// Bail out if not a distributed actor definition.
359-
if (!decl->isDistributedActor())
360-
return;
361-
362-
// If the _Distributed module is missing we cannot synthesize anything.
363-
if (!swift::ensureDistributedModuleLoaded(decl))
364-
return;
365-
366-
if (auto clazz = dyn_cast<ClassDecl>(decl))
367-
addImplicitRemoteFunctions(clazz);
368-
}
369-
370347
/// Entry point for adding all computed members to a distributed actor decl.
371-
// TODO(distributed): move the synthesis of protocol requirements to DerivedConformance style
372-
void swift::addImplicitDistributedActorMembers(ClassDecl *decl) {
348+
void swift::addImplicitDistributedActorMembersToClass(ClassDecl *decl) {
373349
// Bail out if not a distributed actor definition.
374350
if (!decl->isDistributedActor())
375351
return;
@@ -380,4 +356,5 @@ void swift::addImplicitDistributedActorMembers(ClassDecl *decl) {
380356

381357
addFactoryResolveFunction(decl);
382358
addImplicitDistributedActorStoredProperties(decl);
359+
addImplicitRemoteActorFunctions(decl);
383360
}

lib/Sema/TypeCheckAttr.cpp

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2365,7 +2365,7 @@ void AttributeChecker::visitDiscardableResultAttr(DiscardableResultAttr *attr) {
23652365
}
23662366
}
23672367

2368-
/// Lookup the replaced decl in the replacements scope.
2368+
/// Lookup the replaced decl in the replacments scope.
23692369
static void lookupReplacedDecl(DeclNameRef replacedDeclName,
23702370
const DeclAttribute *attr,
23712371
const ValueDecl *replacement,
@@ -2400,10 +2400,9 @@ static void lookupReplacedDecl(DeclNameRef replacedDeclName,
24002400
if (declCtxt->isInSpecializeExtensionContext())
24012401
options |= NL_IncludeUsableFromInline;
24022402

2403-
if (typeCtx) {
2403+
if (typeCtx)
24042404
moduleScopeCtxt->lookupQualified({typeCtx}, replacedDeclName, options,
24052405
results);
2406-
}
24072406
}
24082407

24092408
/// Remove any argument labels from the interface type of the given value that
@@ -3499,19 +3498,6 @@ DynamicallyReplacedDeclRequest::evaluate(Evaluator &evaluator,
34993498
if (attr->isInvalid())
35003499
return nullptr;
35013500

3502-
auto *clazz = VD->getDeclContext()->getSelfClassDecl();
3503-
if (clazz && clazz->isDistributedActor()) {
3504-
// Since distributed actors synthesize their `_remote`
3505-
// counterparts to any `distributed func` declared on them, and such remote
3506-
// function is a popular target for dynamic function replacement - we must
3507-
// force the synthesis has happened here already, such that we can lookup
3508-
// the func for the replacement we're handling right now.
3509-
//
3510-
// This request is cached, so it won't cause wasted/duplicate work.
3511-
TypeChecker::addImplicitDistributedActorRemoteFunctions(clazz);
3512-
}
3513-
3514-
35153501
// If we can lazily resolve the function, do so now.
35163502
if (auto *LazyResolver = attr->Resolver) {
35173503
auto decl = LazyResolver->loadDynamicallyReplacedFunctionDecl(

lib/Sema/TypeCheckDecl.cpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2580,7 +2580,6 @@ static ArrayRef<Decl *> evaluateMembersRequest(
25802580
TypeChecker::addImplicitConstructors(nominal);
25812581
}
25822582

2583-
25842583
// Force any conformances that may introduce more members.
25852584
for (auto conformance : idc->getLocalConformances()) {
25862585
auto proto = conformance->getProtocol();
@@ -2612,15 +2611,15 @@ static ArrayRef<Decl *> evaluateMembersRequest(
26122611
TypeChecker::checkConformance(conformance->getRootNormalConformance());
26132612
}
26142613

2614+
// If the type conforms to Encodable or Decodable, even via an extension,
2615+
// the CodingKeys enum is synthesized as a member of the type itself.
2616+
// Force it into existence.
26152617
if (nominal) {
2616-
// If the type conforms to Encodable or Decodable, even via an extension,
2617-
// the CodingKeys enum is synthesized as a member of the type itself.
2618-
// Force it into existence.
26192618
(void) evaluateOrDefault(
26202619
ctx.evaluator,
2621-
ResolveImplicitMemberRequest{nominal,
2622-
ImplicitMemberAction::ResolveCodingKeys},
2623-
{});
2620+
ResolveImplicitMemberRequest{nominal,
2621+
ImplicitMemberAction::ResolveCodingKeys},
2622+
{});
26242623
}
26252624

26262625
// If the decl has a @main attribute, we need to force synthesis of the
@@ -2639,10 +2638,6 @@ static ArrayRef<Decl *> evaluateMembersRequest(
26392638
(void) var->getPropertyWrapperInitializerInfo();
26402639
}
26412640
}
2642-
2643-
if (auto *func = dyn_cast<FuncDecl>(member)) {
2644-
(void) func->getDistributedActorRemoteFuncDecl();
2645-
}
26462641
}
26472642

26482643
SortedDeclList synthesizedMembers;

0 commit comments

Comments
 (0)