Skip to content

Commit 4e3d4d2

Browse files
authored
Merge pull request #41233 from ktoso/wip-distributed-get-only-properties
2 parents 2049216 + f7c4674 commit 4e3d4d2

File tree

36 files changed

+874
-144
lines changed

36 files changed

+874
-144
lines changed

include/swift/AST/Attr.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ SIMPLE_DECL_ATTR(_inheritActorContext, InheritActorContext,
651651
// 117 was 'spawn' and is now unused
652652

653653
CONTEXTUAL_SIMPLE_DECL_ATTR(distributed, DistributedActor,
654-
DeclModifier | OnClass | OnFunc |
654+
DeclModifier | OnClass | OnFunc | OnVar |
655655
DistributedOnly |
656656
ABIBreakingToAdd | ABIBreakingToRemove |
657657
APIBreakingToAdd | APIBreakingToRemove,

include/swift/AST/Decl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ enum class DescriptiveDeclKind : uint8_t {
144144
Property,
145145
StaticProperty,
146146
ClassProperty,
147+
DistributedProperty,
147148
InfixOperator,
148149
PrefixOperator,
149150
PostfixOperator,
@@ -5240,6 +5241,9 @@ class VarDecl : public AbstractStorageDecl {
52405241
/// Is this an "async let" property?
52415242
bool isAsyncLet() const;
52425243

5244+
/// Does this have a 'distributed' modifier?
5245+
bool isDistributed() const;
5246+
52435247
/// Is this a stored property that will _not_ trigger any user-defined code
52445248
/// upon any kind of access?
52455249
bool isOrdinaryStoredProperty() const;

include/swift/AST/DiagnosticsSema.def

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4496,8 +4496,8 @@ ERROR(actor_isolated_non_self_reference,none,
44964496
"from the main actor|from a non-isolated context}3",
44974497
(DescriptiveDeclKind, DeclName, unsigned, unsigned, Type))
44984498
ERROR(distributed_actor_isolated_non_self_reference,none,
4499-
"distributed actor-isolated %0 %1 can only be referenced inside the "
4500-
"distributed actor",
4499+
"distributed actor-isolated %0 %1 can not be accessed from a "
4500+
"non-isolated context",
45014501
(DescriptiveDeclKind, DeclName))
45024502
ERROR(distributed_actor_needs_explicit_distributed_import,none,
45034503
"'_Distributed' module not imported, required for 'distributed actor'",
@@ -4569,11 +4569,11 @@ ERROR(distributed_actor_isolated_method,none,
45694569
"only 'distributed' instance methods can be called on a potentially remote distributed actor",
45704570
())
45714571
ERROR(distributed_actor_func_param_not_codable,none,
4572-
"parameter '%0' of type %1 in %2 does not conform to '%3'",
4572+
"parameter '%0' of type %1 in %2 does not conform to serialization requirement '%3'",
45734573
(StringRef, Type, DescriptiveDeclKind, StringRef))
4574-
ERROR(distributed_actor_func_result_not_codable,none,
4575-
"result type %0 of %1 does not conform to '%2'",
4576-
(Type, DescriptiveDeclKind, StringRef))
4574+
ERROR(distributed_actor_target_result_not_codable,none,
4575+
"result type %0 of %1 %2 does not conform to serialization requirement '%3'",
4576+
(Type, DescriptiveDeclKind, Identifier, StringRef))
45774577
ERROR(distributed_actor_remote_func_implemented_manually,none,
45784578
"distributed instance method's %0 remote counterpart %1 cannot not be implemented manually.",
45794579
(Identifier, Identifier))
@@ -4764,9 +4764,18 @@ ERROR(distributed_actor_user_defined_special_property,none,
47644764
"property %0 cannot be defined explicitly, as it conflicts with "
47654765
"distributed actor synthesized stored property",
47664766
(DeclName))
4767+
ERROR(distributed_property_can_only_be_computed_get_only,none,
4768+
"'distributed' computed property %0 cannot have setter",
4769+
(DeclName))
4770+
ERROR(distributed_property_cannot_be_static,none,
4771+
"'distributed' property %0 cannot be 'static'",
4772+
(DeclName))
4773+
ERROR(distributed_property_can_only_be_computed,none,
4774+
"%0 %1 cannot be 'distributed', only computed properties can",
4775+
(DescriptiveDeclKind, DeclName))
47674776
NOTE(distributed_actor_isolated_property,none,
4768-
"distributed actor state is only available within the actor instance", // TODO: reword in terms of isolation
4769-
())
4777+
"access to %0 %1 is only permitted within distributed actor %2",
4778+
(DescriptiveDeclKind, DeclName, DeclName))
47704779

47714780
ERROR(concurrency_lib_missing,none,
47724781
"missing '%0' declaration, probably because the '_Concurrency' "

lib/AST/Decl.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,13 @@ DescriptiveDeclKind Decl::getDescriptiveKind() const {
191191
auto var = cast<VarDecl>(this);
192192
switch (var->getCorrectStaticSpelling()) {
193193
case StaticSpellingKind::None:
194-
if (var->getDeclContext()->isTypeContext())
194+
if (var->getDeclContext()->isTypeContext()) {
195+
if (var->isDistributed() && !var->isLet()) {
196+
return DescriptiveDeclKind::DistributedProperty;
197+
}
198+
195199
return DescriptiveDeclKind::Property;
200+
}
196201
return var->isLet() ? DescriptiveDeclKind::Let
197202
: DescriptiveDeclKind::Var;
198203
case StaticSpellingKind::KeywordStatic:
@@ -299,6 +304,7 @@ StringRef Decl::getDescriptiveKindName(DescriptiveDeclKind K) {
299304
ENTRY(Property, "property");
300305
ENTRY(StaticProperty, "static property");
301306
ENTRY(ClassProperty, "class property");
307+
ENTRY(DistributedProperty, "distributed property");
302308
ENTRY(PrecedenceGroup, "precedence group");
303309
ENTRY(InfixOperator, "infix operator");
304310
ENTRY(PrefixOperator, "prefix operator");
@@ -6304,6 +6310,10 @@ bool VarDecl::isAsyncLet() const {
63046310
return getAttrs().hasAttribute<AsyncAttr>();
63056311
}
63066312

6313+
bool VarDecl::isDistributed() const {
6314+
return getAttrs().hasAttribute<DistributedActorAttr>();
6315+
}
6316+
63076317
bool VarDecl::isOrdinaryStoredProperty() const {
63086318
// we assume if it hasAttachedPropertyWrapper, it has no storage.
63096319
//

lib/SILGen/SILGenDistributed.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,15 +1154,17 @@ void SILGenFunction::emitDistributedThunk(SILDeclRef thunk) {
11541154
subTypes.push_back(paramTy);
11551155

11561156
// --- Codable: Decodable
1157-
auto decodableRequirementTy =
1158-
ctx.getProtocol(KnownProtocolKind::Decodable); // FIXME: actually use SerializatioNRequirement
1157+
auto decodableRequirementTy = ctx.getProtocol(
1158+
KnownProtocolKind::Decodable); // FIXME(distributed): actually use
1159+
// SerializationRequirement
11591160
auto paramDecodableTypeConfRef = module->lookupConformance(
11601161
paramTy, decodableRequirementTy);
11611162
subConformances.push_back(paramDecodableTypeConfRef);
11621163

11631164
// --- Codable: Encodable
11641165
auto encodableRequirementTy = ctx.getProtocol(
1165-
KnownProtocolKind::Encodable); // FIXME: actually use SerializatioNRequirement
1166+
KnownProtocolKind::Encodable); // FIXME(distributed): actually use
1167+
// SerializationRequirement
11661168
auto paramEncodableTypeConfRef = module->lookupConformance(
11671169
paramTy, encodableRequirementTy);
11681170
subConformances.push_back(paramEncodableTypeConfRef);

lib/Sema/TypeCheckAttr.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5548,9 +5548,14 @@ void AttributeChecker::visitDistributedActorAttr(DistributedActorAttr *attr) {
55485548

55495549
// distributed can be applied to actor definitions and their methods
55505550
if (auto varDecl = dyn_cast<VarDecl>(D)) {
5551-
// distributed can not be applied to stored properties
5552-
diagnoseAndRemoveAttr(attr, diag::distributed_actor_property);
5553-
return;
5551+
if (varDecl->isDistributed()) {
5552+
if (checkDistributedActorProperty(varDecl, /*diagnose=*/true))
5553+
return;
5554+
} else {
5555+
// distributed can not be applied to stored properties
5556+
diagnoseAndRemoveAttr(attr, diag::distributed_actor_property);
5557+
return;
5558+
}
55545559
}
55555560

55565561
// distributed can only be declared on an `actor`

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1953,16 +1953,18 @@ namespace {
19531953
void noteIsolatedActorMember(ValueDecl *decl, Expr *context) {
19541954
// detect if it is a distributed actor, to provide better isolation notes
19551955

1956-
auto isDistributedActor = false;
1957-
if (auto nominal = decl->getDeclContext()->getSelfNominalTypeDecl())
1958-
isDistributedActor = nominal->isDistributedActor();
1956+
auto nominal = decl->getDeclContext()->getSelfNominalTypeDecl();
1957+
bool isDistributedActor = false;
1958+
if (nominal) isDistributedActor = nominal->isDistributedActor();
19591959

19601960
// FIXME: Make this diagnostic more sensitive to the isolation context of
19611961
// the declaration.
19621962
if (isDistributedActor) {
1963-
if (dyn_cast<VarDecl>(decl)) {
1963+
if (isa<VarDecl>(decl)) {
19641964
// Distributed actor properties are never accessible externally.
1965-
decl->diagnose(diag::distributed_actor_isolated_property);
1965+
decl->diagnose(diag::distributed_actor_isolated_property,
1966+
decl->getDescriptiveKind(), decl->getName(),
1967+
nominal->getName());
19661968
} else {
19671969
// it's a function or subscript
19681970
decl->diagnose(diag::note_distributed_actor_isolated_method,
@@ -4705,9 +4707,9 @@ bool swift::isPotentiallyIsolatedActor(
47054707

47064708
if (var->getName().str().equals("__secretlyKnownToBeLocal")) {
47074709
// FIXME(distributed): we did a dynamic check and know that this actor is
4708-
// local,
4709-
// but we can't express that to the type system; the real implementation
4710-
// will have to mark 'self' as "known to be local" after an is-local check.
4710+
// local, but we can't express that to the type system; the real
4711+
// implementation will have to mark 'self' as "known to be local" after
4712+
// an is-local check.
47114713
return true;
47124714
}
47134715

0 commit comments

Comments
 (0)