Skip to content

Commit add2681

Browse files
authored
Merge pull request #59874 from ktoso/pick-pick-fix-witnesses-distributed-squashed
🍒 [5.7][Distributed] Remove dist requirement explicit async throws limitation
2 parents 13b3ac9 + 9140bf3 commit add2681

23 files changed

+515
-221
lines changed

include/swift/AST/ActorIsolation.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ ActorIsolation getActorIsolation(ValueDecl *value);
193193
/// Determine how the given declaration context is isolated.
194194
ActorIsolation getActorIsolationOfContext(DeclContext *dc);
195195

196+
/// Check if both the value, and context are isolated to the same actor.
197+
bool isSameActorIsolated(ValueDecl *value, DeclContext *dc);
198+
196199
/// Determines whether this function's body uses flow-sensitive isolation.
197200
bool usesFlowSensitiveIsolation(AbstractFunctionDecl const *fn);
198201

include/swift/AST/DiagnosticsSema.def

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4820,9 +4820,6 @@ ERROR(distributed_actor_func_static,none,
48204820
ERROR(distributed_actor_func_not_in_distributed_actor,none,
48214821
"'distributed' method can only be declared within 'distributed actor'",
48224822
())
4823-
ERROR(distributed_method_requirement_must_be_async_throws,none, // FIXME(distributed): this is an implementation limitation we should lift
4824-
"'distributed' protocol requirement %0 must currently be declared explicitly 'async throws'",
4825-
(DeclName))
48264823
ERROR(distributed_actor_user_defined_special_property,none,
48274824
"property %0 cannot be defined explicitly, as it conflicts with "
48284825
"distributed actor synthesized stored property",

include/swift/AST/DistributedDecl.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
//
1616
//===----------------------------------------------------------------------===//
1717

18-
#ifndef SWIFT_DECL_TYPECHECKDISTRIBUTED_H
19-
#define SWIFT_DECL_TYPECHECKDISTRIBUTED_H
18+
#ifndef SWIFT_DECL_DISTRIBUTEDDECL_H
19+
#define SWIFT_DECL_DISTRIBUTEDDECL_H
2020

2121
#include "swift/AST/ConcreteDeclRef.h"
2222
#include "swift/AST/DiagnosticEngine.h"
@@ -132,4 +132,6 @@ extractDistributedSerializationRequirements(
132132

133133
}
134134

135-
#endif /* SWIFT_DECL_TYPECHECKDISTRIBUTED_H */
135+
// ==== ------------------------------------------------------------------------
136+
137+
#endif /* SWIFT_DECL_DISTRIBUTEDDECL_H */

include/swift/SIL/SILWitnessVisitor.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include "swift/AST/ASTVisitor.h"
2323
#include "swift/AST/Decl.h"
24+
#include "swift/AST/DistributedDecl.h"
2425
#include "swift/AST/ProtocolAssociations.h"
2526
#include "swift/AST/Types.h"
2627
#include "swift/SIL/TypeLowering.h"
@@ -145,6 +146,7 @@ template <class T> class SILWitnessVisitor : public ASTVisitor<T> {
145146
if (SILDeclRef::requiresNewWitnessTableEntry(func)) {
146147
asDerived().addMethod(SILDeclRef(func, SILDeclRef::Kind::Func));
147148
addAutoDiffDerivativeMethodsIfRequired(func, SILDeclRef::Kind::Func);
149+
addDistributedWitnessMethodsIfRequired(func, SILDeclRef::Kind::Func);
148150
}
149151
}
150152

@@ -192,6 +194,16 @@ template <class T> class SILWitnessVisitor : public ASTVisitor<T> {
192194
AFD->getASTContext())));
193195
}
194196
}
197+
198+
void addDistributedWitnessMethodsIfRequired(AbstractFunctionDecl *AFD,
199+
SILDeclRef::Kind kind) {
200+
if (!AFD->isDistributed())
201+
return;
202+
203+
// Add another which will be witnessed by the 'distributed thunk'
204+
SILDeclRef declRef(AFD, kind);
205+
asDerived().addMethod(declRef.asDistributed());
206+
}
195207
};
196208

197209
} // end namespace swift

lib/AST/ASTMangler.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ std::string ASTMangler::mangleWitnessThunk(
243243
appendOperator("TW");
244244
}
245245
}
246+
246247
return finalize();
247248
}
248249

lib/AST/Decl.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9184,6 +9184,13 @@ ActorIsolation swift::getActorIsolationOfContext(DeclContext *dc) {
91849184
return ActorIsolation::forUnspecified();
91859185
}
91869186

9187+
bool swift::isSameActorIsolated(ValueDecl *value, DeclContext *dc) {
9188+
auto valueIsolation = getActorIsolation(value);
9189+
auto dcIsolation = getActorIsolationOfContext(dc);
9190+
return valueIsolation.isActorIsolated() && dcIsolation.isActorIsolated() &&
9191+
valueIsolation.getActor() == dcIsolation.getActor();
9192+
}
9193+
91879194
ClangNode Decl::getClangNodeImpl() const {
91889195
assert(Bits.Decl.FromClang);
91899196
void * const *ptr = nullptr;

lib/AST/TypeCheckRequests.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1575,7 +1575,11 @@ void swift::simple_display(
15751575
llvm::raw_ostream &out, const ActorIsolation &state) {
15761576
switch (state) {
15771577
case ActorIsolation::ActorInstance:
1578-
out << "actor-isolated to instance of " << state.getActor()->getName();
1578+
out << "actor-isolated to instance of ";
1579+
if (state.isDistributedActor()) {
1580+
out << "distributed ";
1581+
}
1582+
out << "actor " << state.getActor()->getName();
15791583
break;
15801584

15811585
case ActorIsolation::Independent:

lib/Parse/ParseDecl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7163,7 +7163,7 @@ Parser::parseDeclVar(ParseDeclOptions Flags,
71637163
///
71647164
/// \verbatim
71657165
/// decl-func:
7166-
/// attribute-list? ('static' | 'class')? 'mutating'? 'func'
7166+
/// attribute-list? ('static' | 'class' | 'distributed')? 'mutating'? 'func'
71677167
/// any-identifier generic-params? func-signature where-clause?
71687168
/// stmt-brace?
71697169
/// \endverbatim
@@ -7247,7 +7247,7 @@ ParserResult<FuncDecl> Parser::parseDeclFunc(SourceLoc StaticLoc,
72477247
}
72487248

72497249
DebuggerContextChange DCC(*this, SimpleName, DeclKind::Func);
7250-
7250+
72517251
// Parse the generic-params, if present.
72527252
GenericParamList *GenericParams;
72537253
auto GenericParamResult = maybeParseGenericParams();

lib/SIL/IR/SILFunctionType.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2112,7 +2112,7 @@ static CanSILFunctionType getSILFunctionType(
21122112
.withConcurrent(isSendable)
21132113
.withAsync(isAsync)
21142114
.build();
2115-
2115+
21162116
return SILFunctionType::get(genericSig, silExtInfo, coroutineKind,
21172117
calleeConvention, inputs, yields,
21182118
results, errorResult,

lib/SILGen/SILGenApply.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "swift/AST/ForeignAsyncConvention.h"
3030
#include "swift/AST/ForeignErrorConvention.h"
3131
#include "swift/AST/GenericEnvironment.h"
32+
#include "swift/AST/DistributedDecl.h"
3233
#include "swift/AST/GenericSignature.h"
3334
#include "swift/AST/Module.h"
3435
#include "swift/AST/ModuleLoader.h"
@@ -627,6 +628,16 @@ class Callee {
627628
return fn;
628629
}
629630
case Kind::WitnessMethod: {
631+
if (auto func = constant->getFuncDecl()) {
632+
if (func->isDistributed() && isa<ProtocolDecl>(func->getDeclContext())) {
633+
// If we're calling cross-actor, we must always use a distributed thunk
634+
if (!isSameActorIsolated(func, SGF.FunctionDC)) {
635+
// We must adjust the constant to use a distributed thunk.
636+
constant = constant->asDistributed();
637+
}
638+
}
639+
}
640+
630641
auto constantInfo =
631642
SGF.getConstantInfo(SGF.getTypeExpansionContext(), *constant);
632643

@@ -700,6 +711,16 @@ class Callee {
700711
return createCalleeTypeInfo(SGF, constant, constantInfo.getSILType());
701712
}
702713
case Kind::WitnessMethod: {
714+
if (auto func = constant->getFuncDecl()) {
715+
if (func->isDistributed() && isa<ProtocolDecl>(func->getDeclContext())) {
716+
// If we're calling cross-actor, we must always use a distributed thunk
717+
if (!isSameActorIsolated(func, SGF.FunctionDC)) {
718+
/// We must adjust the constant to use a distributed thunk.
719+
constant = constant->asDistributed();
720+
}
721+
}
722+
}
723+
703724
auto constantInfo =
704725
SGF.getConstantInfo(SGF.getTypeExpansionContext(), *constant);
705726
return createCalleeTypeInfo(SGF, constant, constantInfo.getSILType());

0 commit comments

Comments
 (0)