Skip to content

Commit 227aa2a

Browse files
committed
[Distributed] diagnose missing module
1 parent 8c27e49 commit 227aa2a

13 files changed

+132
-85
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4573,6 +4573,10 @@ ERROR(distributed_actor_designated_ctor_missing_transport_param,none,
45734573
"designated distributed actor initializer %0 is missing required "
45744574
"ActorTransport parameter",
45754575
(DeclName))
4576+
ERROR(distributed_actor_user_defined_special_property,none,
4577+
"property %0 cannot be defined explicitly, as it conflicts with "
4578+
"distributed actor synthesized stored property",
4579+
(DeclName))
45764580
ERROR(distributed_actor_independent_property_must_be_let,none,
45774581
"_distributedActorIndependent can be applied to properties, however they must be 'let'",
45784582
())

include/swift/AST/TypeCheckRequests.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2250,6 +2250,24 @@ class HasCircularRawValueRequest
22502250
bool isCached() const { return true; }
22512251
};
22522252

2253+
/// Checks if the _Distributed module is available.
2254+
class DistributedModuleIsAvailableRequest
2255+
: public SimpleRequest<DistributedModuleIsAvailableRequest, bool(Decl *),
2256+
RequestFlags::Cached> {
2257+
public:
2258+
using SimpleRequest::SimpleRequest;
2259+
2260+
private:
2261+
friend SimpleRequest;
2262+
2263+
// Evaluation.
2264+
bool evaluate(Evaluator &evaluator, Decl *decl) const;
2265+
2266+
public:
2267+
// Cached.
2268+
bool isCached() const { return true; }
2269+
};
2270+
22532271
/// Computes an initializer context for a parameter with a default argument.
22542272
class DefaultArgumentInitContextRequest
22552273
: public SimpleRequest<DefaultArgumentInitContextRequest,

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ SWIFT_REQUEST(TypeChecker, InferredGenericSignatureRequest,
140140
SmallVector<Requirement, 2>,
141141
SmallVector<TypeLoc, 2>, bool),
142142
Cached, NoLocationInfo)
143+
SWIFT_REQUEST(TypeChecker, DistributedModuleIsAvailableRequest,
144+
bool(ModuleDecl *), Cached, NoLocationInfo)
143145
SWIFT_REQUEST(TypeChecker, InheritedTypeRequest,
144146
Type(llvm::PointerUnion<const TypeDecl *, const ExtensionDecl *>,
145147
unsigned, TypeResolutionStage),

lib/SILGen/SILGenDistributed.cpp

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ getActorTransportArgument(ASTContext& C, SILFunction& F, ConstructorDecl *ctor)
5252
// TODO(distributed): also be able to locate a generic transport
5353
Type argTy = arg->getType().getASTType();
5454
auto argDecl = arg->getDecl();
55-
argTy->dump();
56-
argDecl->dump();
5755

5856
auto conformsToTransport = module->lookupConformance(
5957
argDecl->getInterfaceType(), transportProto);
@@ -108,9 +106,6 @@ emitDistributedActorTransportInit(
108106
auto loc = SILLocation(ctor);
109107
loc.markAutoGenerated();
110108

111-
fprintf(stderr, "[%s:%d] (%s) ----------------------------------------------------------------------------\n", __FILE__, __LINE__, __FUNCTION__);
112-
SGF.F.dump();
113-
114109
// ==== Prepare assignment: get the self.transport address
115110
SILValue transportArgValue = getActorTransportArgument(C, F, ctor);
116111
ManagedValue transportArgManaged = ManagedValue::forUnmanaged(transportArgValue);
@@ -151,9 +146,6 @@ emitDistributedActorIdentityInit(
151146
auto loc = SILLocation(ctor);
152147
loc.markAutoGenerated();
153148

154-
fprintf(stderr, "[%s:%d] (%s) ----------------------------------------------------------------------------\n", __FILE__, __LINE__, __FUNCTION__);
155-
SGF.F.dump();
156-
157149
// === prepare the transport.assignIdentity(_:) function
158150

159151
AbstractFunctionDecl *assignIdentityFuncDecl = lookupAssignIdentityFunc(C);
@@ -187,9 +179,6 @@ emitDistributedActorIdentityInit(
187179
// selfVarDecl.getType() // TODO: do this; then dont need the self type decl
188180
SILValue selfMetatypeValue = B.createMetatype(loc, selfMetatype);
189181

190-
fprintf(stderr, "[%s:%d] (%s) ----------------------------------------------------------------------------\n", __FILE__, __LINE__, __FUNCTION__);
191-
SGF.F.dump();
192-
193182
// === Make the transport.assignIdentity call
194183
// --- prepare the witness_method
195184
// Note: it does not matter on what module we perform the lookup,
@@ -208,7 +197,6 @@ emitDistributedActorIdentityInit(
208197
selfTy,
209198
distributedActorProto);
210199
assert(!distributedActorConfRef.isInvalid() && "Missing conformance to `DistributedActor`");
211-
distributedActorConfRef.dump();
212200

213201
// auto anyActorIdentityDecl = C.getAnyActorIdentityDecl();
214202

@@ -228,8 +216,6 @@ emitDistributedActorIdentityInit(
228216

229217
// --- prepare conformance subs
230218
auto genericSig = assignIdentityMethod->getGenericSignature();
231-
fprintf(stderr, "[%s:%d] (%s) generic sig\n", __FILE__, __LINE__, __FUNCTION__);
232-
genericSig->dump();
233219

234220
SubstitutionMap subs =
235221
SubstitutionMap::get(genericSig,
@@ -246,9 +232,6 @@ emitDistributedActorIdentityInit(
246232
loc, assignWitnessMethod, subs,
247233
{ temp, selfMetatypeValue, transportArchetypeValue});
248234

249-
fprintf(stderr, "[%s:%d] (%s) ----------------------------------------------------------------------------\n", __FILE__, __LINE__, __FUNCTION__);
250-
SGF.F.dump();
251-
252235
// ==== Assign the identity to stored property
253236

254237
// --- Prepare address of self.id
@@ -259,9 +242,6 @@ emitDistributedActorIdentityInit(
259242
// --- assign to the property
260243
B.createCopyAddr(loc, /*src*/temp, /*dest*/idFieldAddr,
261244
IsTake, IsInitialization);
262-
263-
fprintf(stderr, "[%s:%d] (%s) DONE(ID) ----------------------------------------------------------------------------\n", __FILE__, __LINE__, __FUNCTION__);
264-
SGF.F.dump();
265245
}
266246

267247
void SILGenFunction::initializeDistributedActorImplicitStorageInit(
@@ -297,9 +277,6 @@ void SILGenFunction::initializeDistributedActorImplicitStorageInit(
297277
if (!pbd) continue;
298278
if (pbd->isStatic()) continue;
299279

300-
fprintf(stderr, "[%s:%d] (%s) MEMBER\n", __FILE__, __LINE__, __FUNCTION__);
301-
member->dump();
302-
303280
Pattern *pattern = pbd->getPattern(0);
304281
VarDecl *var = pbd->getSingleVar();
305282
if (!var) continue;
@@ -321,20 +298,23 @@ void SILGenFunction::initializeDistributedActorImplicitStorageInit(
321298

322299
assert(transportMember && "Missing DistributedActor.actorTransport member");
323300
assert(idMember && "Missing DistributedActor.id member");
324-
325-
fprintf(stderr, "[%s:%d] (%s) --- DONE DONE DONE DONE DONE DONE -----------------------------------------------------------\n", __FILE__, __LINE__, __FUNCTION__);
326-
fprintf(stderr, "[%s:%d] (%s) ----------------------------------------------------------------------------\n", __FILE__, __LINE__, __FUNCTION__);
327-
F.dump();
328301
}
329302

330-
331303
void SILGenFunction::emitDistributedActorReady(
332304
ConstructorDecl *ctor, ManagedValue selfArg) {
333305
// TODO(distributed): implement actorReady call
334306
}
335307

336308
/******************************************************************************/
337-
/***************************** DISTRIBUTED THUNKS ****************************/
309+
/******************* DISTRIBUTED DEINIT: resignAddress ************************/
310+
/******************************************************************************/
311+
312+
// TODO: implement calling resignAddress via a defer in deinit
313+
314+
315+
316+
/******************************************************************************/
317+
/***************************** DISTRIBUTED THUNKS *****************************/
338318
/******************************************************************************/
339319

340320
void SILGenFunction::emitDistributedThunk(SILDeclRef thunk) {

lib/Sema/TypeCheckDistributed.cpp

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,23 @@ using namespace swift;
3232
// ==== ------------------------------------------------------------------------
3333

3434
bool swift::ensureDistributedModuleLoaded(Decl *decl) {
35-
fprintf(stderr, "[%s:%d] (%s) ensureDistributedModuleLoaded\n", __FILE__, __LINE__, __FUNCTION__);
3635
auto &C = decl->getASTContext();
37-
if (!C.getLoadedModule(C.Id_Distributed)) {
38-
// seems we're missing the _Distributed module, ask to import it explicitly
39-
decl->diagnose(diag::distributed_actor_needs_explicit_distributed_import);
40-
return false;
41-
}
36+
auto moduleAvailable = evaluateOrDefault(
37+
C.evaluator, DistributedModuleIsAvailableRequest{decl}, false);
38+
return moduleAvailable;
39+
}
40+
41+
bool
42+
DistributedModuleIsAvailableRequest::evaluate(Evaluator &evaluator,
43+
Decl *decl) const {
44+
auto &C = decl->getASTContext();
45+
46+
if (C.getLoadedModule(C.Id_Distributed))
47+
return true;
4248

43-
return true;
49+
// seems we're missing the _Distributed module, ask to import it explicitly
50+
decl->diagnose(diag::distributed_actor_needs_explicit_distributed_import);
51+
return false;
4452
}
4553

4654
// ==== ------------------------------------------------------------------------
@@ -50,7 +58,7 @@ bool IsDistributedActorRequest::evaluate(
5058
// Protocols are actors if they inherit from `DistributedActor`.
5159
if (auto protocol = dyn_cast<ProtocolDecl>(nominal)) {
5260
auto &ctx = protocol->getASTContext();
53-
auto *distributedActorProtocol = ctx.getDistributedActorDecl(); // getProtocol(KnownProtocolKind::DistributedActor);
61+
auto *distributedActorProtocol = ctx.getDistributedActorDecl();
5462
return (protocol == distributedActorProtocol ||
5563
protocol->inheritsFrom(distributedActorProtocol));
5664
}
@@ -141,8 +149,22 @@ bool swift::checkDistributedFunction(FuncDecl *func, bool diagnose) {
141149
return false;
142150
}
143151

152+
void swift::checkDistributedActorProperties(const ClassDecl *decl) {
153+
auto &C = decl->getASTContext();
154+
155+
for (auto member : decl->getMembers()) {
156+
if (auto prop = dyn_cast<VarDecl>(member)) {
157+
auto id = prop->getName();
158+
if (id == C.Id_actorTransport ||
159+
id == C.Id_id) {
160+
prop->diagnose(diag::distributed_actor_user_defined_special_property,
161+
id);
162+
}
163+
}
164+
}
165+
}
166+
144167
void swift::checkDistributedActorConstructor(const ClassDecl *decl, ConstructorDecl *ctor) {
145-
fprintf(stderr, "[%s:%d] (%s) checkDistributedActorConstructor\n", __FILE__, __LINE__, __FUNCTION__);
146168
// bail out unless distributed actor, only those have special rules to check here
147169
if (!decl->isDistributedActor())
148170
return;
@@ -151,6 +173,9 @@ void swift::checkDistributedActorConstructor(const ClassDecl *decl, ConstructorD
151173
if (!ctor->isDesignatedInit())
152174
return;
153175

176+
if (!swift::ensureDistributedModuleLoaded(ctor))
177+
return;
178+
154179
// === Designated initializers must accept exactly one ActorTransport
155180
auto &C = ctor->getASTContext();
156181
auto module = ctor->getParentModule();
@@ -163,19 +188,12 @@ void swift::checkDistributedActorConstructor(const ClassDecl *decl, ConstructorD
163188
for (auto param : *ctor->getParameters()) {
164189
auto paramTy = ctor->mapTypeIntoContext(param->getInterfaceType());
165190
auto conformance = TypeChecker::conformsToProtocol(paramTy, protocolDecl, module);
166-
fprintf(stderr, "[%s:%d] (%s) CHECKED CONFORMANCE\n", __FILE__, __LINE__, __FUNCTION__);
167-
paramTy->dump();
168-
protocolDecl->dump();
169-
fprintf(stderr, "[%s:%d] (%s) IT IS VALID: %d\n", __FILE__, __LINE__, __FUNCTION__, !conformance.isInvalid());
170191

171192
if (paramTy->isEqual(protocolTy) || !conformance.isInvalid()) {
172193
transportParamsCount += 1;
173194
transportParams.push_back(param);
174195
}
175196
}
176-
// ok! We found exactly one transport parameter
177-
if (transportParamsCount == 1)
178-
return;
179197

180198
// missing transport parameter
181199
if (transportParamsCount == 0) {
@@ -185,32 +203,40 @@ void swift::checkDistributedActorConstructor(const ClassDecl *decl, ConstructorD
185203
return;
186204
}
187205

206+
// ok! We found exactly one transport parameter
207+
if (transportParamsCount == 1)
208+
return;
209+
188210
// TODO(distributed): could list exact parameters
189211
ctor->diagnose(diag::distributed_actor_designated_ctor_must_have_one_transport_param,
190212
ctor->getName(), transportParamsCount);
191213
}
192214

193215
// ==== ------------------------------------------------------------------------
194216

195-
void TypeChecker::checkDistributedActor(const ClassDecl *decl) {
196-
fprintf(stderr, "[%s:%d] (%s) CHECK DIST\n", __FILE__, __LINE__, __FUNCTION__);
197-
198-
auto mutableDecl = const_cast<ClassDecl*>(decl);
217+
void TypeChecker::checkDistributedActor(ClassDecl *decl) {
218+
// ==== Ensure the _Distributed module is available,
219+
// without it there's no reason to check the decl in more detail anyway.
220+
if (!swift::ensureDistributedModuleLoaded(decl))
221+
return;
199222

200223
// ==== Constructors
201224
// --- Get the default initializer
202225
// If applicable, this will the default 'init(transport:)' initializer
203-
(void)mutableDecl->getDefaultInitializer();
226+
(void)decl->getDefaultInitializer();
204227

205228
// --- Check all constructors
206229
for (auto member : decl->getMembers())
207230
if (auto ctor = dyn_cast<ConstructorDecl>(member))
208231
checkDistributedActorConstructor(decl, ctor);
209232

210233
// ==== Properties
211-
// Synthesize properties
234+
// --- Check for any illegal re-definitions
235+
checkDistributedActorProperties(decl);
236+
237+
// --- Synthesize properties
212238
// TODO: those could technically move to DerivedConformance style
213-
swift::addImplicitDistributedActorMembersToClass(mutableDecl);
239+
swift::addImplicitDistributedActorMembersToClass(decl);
214240

215241
// ==== Functions
216242
}

lib/Sema/TypeCheckDistributed.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#include "swift/AST/ConcreteDeclRef.h"
2121
#include "swift/AST/DiagnosticEngine.h"
2222
#include "swift/AST/Type.h"
23-
#include <cassert>
2423

2524
namespace swift {
2625

@@ -37,10 +36,16 @@ class PatternBindingDecl;
3736
class ProtocolConformance;
3837
class ValueDecl;
3938

39+
40+
/******************************************************************************/
41+
/********************* Distributed Actor Type Checking ************************/
42+
/******************************************************************************/
43+
4044
// Diagnose an error if the _Distributed module is not loaded.
4145
bool ensureDistributedModuleLoaded(Decl *decl);
4246

43-
/// Check distributed actor isolation rules.
47+
/// Check for illegal property declarations (e.g. re-declaring transport or id)
48+
void checkDistributedActorProperties(const ClassDecl *decl);
4449

4550
/// The local and resolve distributed actor constructors have special rules to check.
4651
void checkDistributedActorConstructor(const ClassDecl *decl, ConstructorDecl *ctor);

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4626,8 +4626,6 @@ void ConformanceChecker::ensureRequirementsAreSatisfied() {
46264626
proto->getASTContext().Diags.diagnose(Loc, diag::type_does_not_conform,
46274627
Adoptee,
46284628
Proto->getDeclaredInterfaceType());
4629-
Adoptee->dump();
4630-
assert(false && "diag::type_does_not_conform");
46314629
Conformance->setInvalid();
46324630
}
46334631
return;
@@ -5090,8 +5088,6 @@ void swift::diagnoseConformanceFailure(Type T,
50905088

50915089
diags.diagnose(ComplainLoc, diag::type_does_not_conform,
50925090
T, Proto->getDeclaredInterfaceType());
5093-
T->dump();
5094-
assert(false && "diag::type_does_not_conform");
50955091
}
50965092

50975093
void ConformanceChecker::diagnoseOrDefer(

lib/Sema/TypeCheckStorage.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,14 +168,6 @@ StoredPropertiesRequest::evaluate(Evaluator &evaluator,
168168
}
169169
}
170170

171-
172-
if (auto classDecl = dyn_cast<ClassDecl>(decl)) {
173-
if (classDecl->isDistributedActor()) {
174-
fprintf(stderr, "[%s:%d] (%s) StoredPropertiesRequest::evaluate == count:%d \n", __FILE__, __LINE__, __FUNCTION__, results.size());
175-
decl->dump();
176-
}
177-
}
178-
179171
return decl->getASTContext().AllocateCopy(results);
180172
}
181173

lib/Sema/TypeChecker.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1021,7 +1021,8 @@ diagnosePotentialOpaqueTypeUnavailability(SourceRange ReferenceRange,
10211021
const DeclContext *ReferenceDC,
10221022
const UnavailabilityReason &Reason);
10231023

1024-
void checkDistributedActor(const ClassDecl *decl);
1024+
/// Type check a 'distributed actor' declaration.
1025+
void checkDistributedActor(ClassDecl *decl);
10251026

10261027
void checkConcurrencyAvailability(SourceRange ReferenceRange,
10271028
const DeclContext *ReferenceDC);

stdlib/public/Distributed/DistributedActor.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import _Concurrency
2020
///
2121
/// FIXME: !!! We'd need Actor to also conform to this, but don't want to add that conformance in _Concurrency yet.
2222
@_marker
23-
public protocol AnyActor {}
23+
public protocol AnyActor: AnyObject {}
2424

2525
// ==== Distributed Actor -----------------------------------------------------
2626

@@ -35,11 +35,8 @@ public protocol AnyActor {}
3535
@available(SwiftStdlib 5.5, *)
3636
public protocol DistributedActor:
3737
AnyActor,
38-
AnyObject,
3938
Identifiable, Hashable, Codable {
4039

41-
// init(transport: ActorTransport) // FIXME(distributed): must remove this
42-
4340
/// Resolves the passed in `identity` against the `transport`, returning
4441
/// either a local or remote actor reference.
4542
///

0 commit comments

Comments
 (0)