Skip to content

Commit b0d9b9d

Browse files
authored
Merge pull request #4354 from swiftwasm/main
2 parents 95d2bac + abc8aa6 commit b0d9b9d

File tree

145 files changed

+1994
-480
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

145 files changed

+1994
-480
lines changed

CHANGELOG.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,36 @@ _**Note:** This is in reverse chronological order, so newer entries are added to
55

66
## Swift 5.7
77

8+
* [SE-0336][]:
9+
10+
It is now possible to declare `distributed actor` and `distributed func`s inside of them.
11+
12+
Distributed actors provide stronger isolation guarantees than "local" actors, and enable additional checks to be made on return types and parameters of distributed methods, e.g. checking if they conform to `Codable`. Distributed methods can be called on "remote" references of distributed actors, turning those invocations into remote procedure calls, by means of pluggable and user extensible distributed actor system implementations.
13+
14+
Swift does not provide any specific distributed actor system by itself, however, packages in the ecosystem fulfil the role of providing those implementations.
15+
16+
```swift
17+
distributed actor Greeter {
18+
var greetingsSent = 0
19+
20+
distributed func greet(name: String) -> String {
21+
greetingsSent += 1
22+
return "Hello, \(name)!"
23+
}
24+
}
25+
26+
func talkTo(greeter: Greeter) async throws {
27+
// isolation of distributed actors is stronger, it is impossible to refer to
28+
// any stored properties of distributed actors from outside of them:
29+
greeter.greetingsSent // distributed actor-isolated property 'name' can not be accessed from a non-isolated context
30+
31+
// remote calls are implicitly throwing and async,
32+
// to account for the potential networking involved:
33+
let greeting = try await greeter.greet(name: "Alice")
34+
print(greeting) // Hello, Alice!
35+
}
36+
```
37+
838
* The compiler now emits a warning when a non-final class conforms to a protocol that imposes a same-type requirement between `Self` and an associated type. This is because such a requirement makes the conformance unsound for subclasses.
939

1040
For example, Swift 5.6 would allow the following code, which at runtime would construct an instanec of `C` and not `SubC` as expected:
@@ -9034,6 +9064,7 @@ Swift 1.0
90349064
[SE-0337]: <https://github.com/apple/swift-evolution/blob/main/proposals/0337-support-incremental-migration-to-concurrency-checking.md>
90359065
[SE-0335]: <https://github.com/apple/swift-evolution/blob/main/proposals/0335-existential-any.md>
90369066
[SE-0341]: <https://github.com/apple/swift-evolution/blob/main/proposals/0341-opaque-parameters.md>
9067+
[SE-0336]: <https://github.com/apple/swift-evolution/blob/main/proposals/0336-distributed-actor-isolation.md>
90379068

90389069
[SR-75]: <https://bugs.swift.org/browse/SR-75>
90399070
[SR-106]: <https://bugs.swift.org/browse/SR-106>

include/swift/AST/Decl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6315,7 +6315,8 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
63156315
/// diagnosed errors during type checking.
63166316
FuncDecl *getDistributedThunk() const;
63176317

6318-
/// Returns 'true' if the function has the @c @_backDeploy attribute.
6318+
/// Returns 'true' if the function has (or inherits) the @c @_backDeploy
6319+
/// attribute.
63196320
bool isBackDeployed() const;
63206321

63216322
PolymorphicEffectKind getPolymorphicEffectKind(EffectKind kind) const;

include/swift/AST/DiagnosticsParse.def

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1887,11 +1887,6 @@ ERROR(attr_requires_concurrency, none,
18871887
"concurrency is enabled",
18881888
(StringRef, bool))
18891889

1890-
ERROR(attr_requires_distributed, none,
1891-
"'%0' %select{attribute|modifier}1 is only valid when experimental "
1892-
"distributed support is enabled",
1893-
(StringRef, bool))
1894-
18951890
//------------------------------------------------------------------------------
18961891
// MARK: syntax parsing diagnostics
18971892
//------------------------------------------------------------------------------

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6320,5 +6320,9 @@ ERROR(attr_incompatible_with_back_deploy,none,
63206320
"'%0' cannot be applied to a back deployed %1",
63216321
(DeclAttribute, DescriptiveDeclKind))
63226322

6323+
ERROR(back_deploy_not_on_coroutine,none,
6324+
"'%0' is not supported on coroutine %1",
6325+
(DeclAttribute, DescriptiveDeclKind))
6326+
63236327
#define UNDEFINE_DIAGNOSTIC_MACROS
63246328
#include "DefineDiagnosticMacros.h"

include/swift/AST/DistributedDecl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ getDistributedSerializationRequirements(
106106
llvm::SmallPtrSet<ProtocolDecl *, 2>
107107
extractDistributedSerializationRequirements(
108108
ASTContext &C, ArrayRef<Requirement> allRequirements);
109+
109110
}
110111

111112
#endif /* SWIFT_DECL_TYPECHECKDISTRIBUTED_H */

include/swift/AST/KnownIdentifiers.def

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ IDENTIFIER(keyPath)
109109
IDENTIFIER(makeIterator)
110110
IDENTIFIER(makeAsyncIterator)
111111
IDENTIFIER(nestedContainer)
112+
IDENTIFIER(isEmpty)
112113
IDENTIFIER(Iterator)
113114
IDENTIFIER(AsyncIterator)
114115
IDENTIFIER(load)
@@ -124,6 +125,7 @@ IDENTIFIER(oldValue)
124125
IDENTIFIER(Optional)
125126
IDENTIFIER_(OptionalNilComparisonType)
126127
IDENTIFIER(parameter)
128+
IDENTIFIER(popFirst)
127129
IDENTIFIER(projected)
128130
IDENTIFIER(projectedValue)
129131
IDENTIFIER(Protocol)
@@ -149,7 +151,6 @@ IDENTIFIER(Type)
149151
IDENTIFIER(type)
150152
IDENTIFIER(typeMismatch)
151153
IDENTIFIER(underlyingError)
152-
IDENTIFIER(unsafelyUnwrapped)
153154
IDENTIFIER(Value)
154155
IDENTIFIER(value)
155156
IDENTIFIER_WITH_NAME(value_, "_value")

include/swift/AST/KnownStdlibTypes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ KNOWN_STDLIB_TYPE_DECL(String, NominalTypeDecl, 0)
4949
KNOWN_STDLIB_TYPE_DECL(StaticString, NominalTypeDecl, 0)
5050
KNOWN_STDLIB_TYPE_DECL(Substring, NominalTypeDecl, 0)
5151
KNOWN_STDLIB_TYPE_DECL(Array, NominalTypeDecl, 1)
52+
KNOWN_STDLIB_TYPE_DECL(ArraySlice, NominalTypeDecl, 1)
5253
KNOWN_STDLIB_TYPE_DECL(_ContiguousArrayStorage, ClassDecl, 1)
5354
KNOWN_STDLIB_TYPE_DECL(Set, NominalTypeDecl, 1)
5455
KNOWN_STDLIB_TYPE_DECL(Sequence, NominalTypeDecl, 1)

include/swift/AST/SwiftNameTranslation.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ namespace objc_translation {
5454
bool checkParent = true);
5555

5656
} // end namespace objc_translation
57+
58+
namespace cxx_translation {
59+
60+
/// Returns true if the given value decl D is visible to C++ of its
61+
/// own accord (i.e. without considering its context)
62+
bool isVisibleToCxx(const ValueDecl *VD, AccessLevel minRequiredAccess,
63+
bool checkParent = true);
64+
65+
} // end namespace cxx_translation
66+
5767
} // end namespace swift
5868

5969
#endif

include/swift/AST/TypeCheckRequests.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,6 +1039,30 @@ class IsDistributedActorRequest :
10391039
bool isCached() const { return true; }
10401040
};
10411041

1042+
/// Retrieve the implicit conformance for the given distributed actor type to
1043+
/// the Codable protocol protocol.
1044+
///
1045+
/// Similar to 'GetImplicitSendableRequest'.
1046+
class GetDistributedActorImplicitCodableRequest :
1047+
public SimpleRequest<GetDistributedActorImplicitCodableRequest,
1048+
NormalProtocolConformance *(NominalTypeDecl *, KnownProtocolKind),
1049+
RequestFlags::Cached> {
1050+
public:
1051+
using SimpleRequest::SimpleRequest;
1052+
1053+
private:
1054+
friend SimpleRequest;
1055+
1056+
NormalProtocolConformance *evaluate(
1057+
Evaluator &evaluator,
1058+
NominalTypeDecl *nominal,
1059+
KnownProtocolKind protoKind) const;
1060+
1061+
public:
1062+
// Caching
1063+
bool isCached() const { return true; }
1064+
};
1065+
10421066
/// Obtain the 'remoteCall' function of a 'DistributedActorSystem'.
10431067
class GetDistributedActorSystemRemoteCallFunctionRequest :
10441068
public SimpleRequest<GetDistributedActorSystemRemoteCallFunctionRequest,

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ SWIFT_REQUEST(TypeChecker, IsDefaultActorRequest,
107107
Cached, NoLocationInfo)
108108
SWIFT_REQUEST(TypeChecker, IsDistributedActorRequest, bool(NominalTypeDecl *),
109109
Cached, NoLocationInfo)
110+
SWIFT_REQUEST(TypeChecker, GetDistributedActorImplicitCodableRequest,
111+
NormalProtocolConformance *(NominalTypeDecl *, KnownProtocolKind),
112+
Cached, NoLocationInfo)
110113
SWIFT_REQUEST(TypeChecker, GetDistributedActorSystemRemoteCallFunctionRequest,
111114
AbstractFunctionDecl *(NominalTypeDecl *, bool),
112115
Cached, NoLocationInfo)

include/swift/Basic/LangOptions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ namespace swift {
344344
bool EnableInferPublicSendable = false;
345345

346346
/// Enable experimental 'distributed' actors and functions.
347-
bool EnableExperimentalDistributed = false;
347+
bool EnableExperimentalDistributed = true;
348348

349349
/// Enable experimental 'move only' features.
350350
bool EnableExperimentalMoveOnly = false;

include/swift/IDE/CompletionLookup.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,6 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
541541

542542
static bool canUseAttributeOnDecl(DeclAttrKind DAK, bool IsInSil,
543543
bool IsConcurrencyEnabled,
544-
bool IsDistributedEnabled,
545544
Optional<DeclKind> DK);
546545

547546
void getAttributeDeclCompletions(bool IsInSil, Optional<DeclKind> DK);

include/swift/Parse/Parser.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -748,14 +748,6 @@ class Parser {
748748
Context.LangOpts.ParseForSyntaxTreeOnly;
749749
}
750750

751-
/// Returns true to indicate that experimental 'distributed actor' syntax
752-
/// should be parsed if the parser is only a syntax tree or if the user has
753-
/// passed the `-enable-experimental-distributed' flag to the frontend.
754-
bool shouldParseExperimentalDistributed() const {
755-
return Context.LangOpts.EnableExperimentalDistributed ||
756-
Context.LangOpts.ParseForSyntaxTreeOnly;
757-
}
758-
759751
public:
760752
InFlightDiagnostic diagnose(SourceLoc Loc, Diagnostic Diag) {
761753
if (Diags.isDiagnosticPointsToFirstBadToken(Diag.getID()) &&

lib/AST/ASTContext.cpp

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3028,23 +3028,21 @@ AnyFunctionType::Param swift::computeSelfParam(AbstractFunctionDecl *AFD,
30283028
// FIXME(distributed): pending swift-evolution, allow `self =` in class
30293029
// inits in general.
30303030
// See also: https://github.com/apple/swift/pull/19151 general impl
3031-
if (Ctx.LangOpts.EnableExperimentalDistributed) {
3032-
auto ext = dyn_cast<ExtensionDecl>(AFD->getDeclContext());
3033-
auto distProto =
3034-
Ctx.getProtocol(KnownProtocolKind::DistributedActor);
3035-
if (distProto && ext && ext->getExtendedNominal() &&
3036-
ext->getExtendedNominal()->getInterfaceType()
3037-
->isEqual(distProto->getInterfaceType())) {
3038-
auto name = CD->getName();
3039-
auto params = name.getArgumentNames();
3040-
if (params.size() == 1 && params[0] == Ctx.Id_from) {
3041-
// FIXME(distributed): this is a workaround to allow init(from:) to
3042-
// be implemented in AST by allowing the self to be mutable in the
3043-
// decoding initializer. This should become a general Swift
3044-
// feature, allowing this in all classes:
3045-
// https://forums.swift.org/t/allow-self-x-in-class-convenience-initializers/15924
3046-
selfAccess = SelfAccessKind::Mutating;
3047-
}
3031+
auto ext = dyn_cast<ExtensionDecl>(AFD->getDeclContext());
3032+
auto distProto =
3033+
Ctx.getProtocol(KnownProtocolKind::DistributedActor);
3034+
if (distProto && ext && ext->getExtendedNominal() &&
3035+
ext->getExtendedNominal()->getInterfaceType()
3036+
->isEqual(distProto->getInterfaceType())) {
3037+
auto name = CD->getName();
3038+
auto params = name.getArgumentNames();
3039+
if (params.size() == 1 && params[0] == Ctx.Id_from) {
3040+
// FIXME(distributed): this is a workaround to allow init(from:) to
3041+
// be implemented in AST by allowing the self to be mutable in the
3042+
// decoding initializer. This should become a general Swift
3043+
// feature, allowing this in all classes:
3044+
// https://forums.swift.org/t/allow-self-x-in-class-convenience-initializers/15924
3045+
selfAccess = SelfAccessKind::Mutating;
30483046
}
30493047
}
30503048
} else {

lib/AST/ASTMangler.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1323,10 +1323,16 @@ void ASTMangler::appendType(Type type, GenericSignature sig,
13231323

13241324
// type ::= archetype
13251325
case TypeKind::PrimaryArchetype:
1326-
case TypeKind::OpenedArchetype:
13271326
case TypeKind::SequenceArchetype:
13281327
llvm_unreachable("Cannot mangle free-standing archetypes");
13291328

1329+
case TypeKind::OpenedArchetype: {
1330+
// Opened archetypes have always been mangled via their interface type,
1331+
// although those manglings aren't used in any stable manner.
1332+
auto openedType = cast<OpenedArchetypeType>(tybase);
1333+
return appendType(openedType->getInterfaceType(), sig, forDecl);
1334+
}
1335+
13301336
case TypeKind::OpaqueTypeArchetype: {
13311337
auto opaqueType = cast<OpaqueTypeArchetypeType>(tybase);
13321338
auto opaqueDecl = opaqueType->getDecl();

lib/AST/ASTPrinter.cpp

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4917,20 +4917,22 @@ void PrintAST::visitRepeatWhileStmt(RepeatWhileStmt *stmt) {
49174917
visit(stmt->getCond());
49184918
}
49194919

4920-
void PrintAST::printStmtCondition(StmtCondition stmt) {
4921-
for (auto elt : stmt) {
4922-
if (auto pattern = elt.getPatternOrNull()) {
4923-
printPattern(pattern);
4924-
auto initializer = elt.getInitializer();
4925-
if (initializer) {
4926-
Printer << " = ";
4927-
visit(initializer);
4928-
}
4929-
}
4930-
else if (auto boolean = elt.getBooleanOrNull()) {
4931-
visit(boolean);
4932-
}
4933-
}
4920+
void PrintAST::printStmtCondition(StmtCondition condition) {
4921+
interleave(
4922+
condition,
4923+
[&](StmtConditionElement &elt) {
4924+
if (auto pattern = elt.getPatternOrNull()) {
4925+
printPattern(pattern);
4926+
auto initializer = elt.getInitializer();
4927+
if (initializer) {
4928+
Printer << " = ";
4929+
visit(initializer);
4930+
}
4931+
} else if (auto boolean = elt.getBooleanOrNull()) {
4932+
visit(boolean);
4933+
}
4934+
},
4935+
[&] { Printer << ", "; });
49344936
}
49354937

49364938
void PrintAST::visitDoStmt(DoStmt *stmt) {

lib/AST/Decl.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,12 @@ Decl::getBackDeployBeforeOSVersion(PlatformKind Kind) const {
386386
}
387387
}
388388
}
389+
390+
// Accessors may inherit `@_backDeploy`.
391+
if (getKind() == DeclKind::Accessor) {
392+
return cast<AccessorDecl>(this)->getStorage()->getBackDeployBeforeOSVersion(Kind);
393+
}
394+
389395
return None;
390396
}
391397

@@ -7649,7 +7655,16 @@ bool AbstractFunctionDecl::isSendable() const {
76497655
}
76507656

76517657
bool AbstractFunctionDecl::isBackDeployed() const {
7652-
return getAttrs().hasAttribute<BackDeployAttr>();
7658+
if (getAttrs().hasAttribute<BackDeployAttr>())
7659+
return true;
7660+
7661+
// Property and subscript accessors inherit the attribute.
7662+
if (auto *AD = dyn_cast<AccessorDecl>(this)) {
7663+
if (AD->getStorage()->getAttrs().hasAttribute<BackDeployAttr>())
7664+
return true;
7665+
}
7666+
7667+
return false;
76537668
}
76547669

76557670
BraceStmt *AbstractFunctionDecl::getBody(bool canSynthesize) const {
@@ -9060,7 +9075,9 @@ ActorIsolation swift::getActorIsolationOfContext(DeclContext *dc) {
90609075
}
90619076

90629077
if (auto *tld = dyn_cast<TopLevelCodeDecl>(dc)) {
9063-
if (dc->isAsyncContext()) {
9078+
if (dc->isAsyncContext() ||
9079+
(dc->getASTContext().LangOpts.WarnConcurrency &&
9080+
dc->getASTContext().LangOpts.EnableExperimentalAsyncTopLevel)) {
90649081
if (Type mainActor = dc->getASTContext().getMainActorType())
90659082
return ActorIsolation::forGlobalActor(mainActor, /*unsafe=*/false);
90669083
}

lib/AST/DistributedDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ Type swift::getDistributedActorSystemType(NominalTypeDecl *actor) {
102102

103103
auto DA = C.getDistributedActorDecl();
104104
if (!DA)
105-
return ErrorType::get(C);
105+
return ErrorType::get(C); // FIXME(distributed): just use Type()
106106

107107
// Dig out the actor system type.
108108
auto module = actor->getParentModule();

lib/AST/GenericEnvironment.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,11 @@ Type MapTypeOutOfContext::operator()(SubstitutableType *type) const {
242242
auto archetype = cast<ArchetypeType>(type);
243243
if (isa<OpaqueTypeArchetypeType>(archetype->getRoot()))
244244
return Type();
245-
245+
246+
// Leave opened archetypes alone; they're handled contextually.
247+
if (isa<OpenedArchetypeType>(archetype))
248+
return Type(type);
249+
246250
return archetype->getInterfaceType();
247251
}
248252

0 commit comments

Comments
 (0)