Skip to content

Commit fbd66e2

Browse files
committed
[Distributed] Only parse distributed when experimentla mode enabled
1 parent 86c7245 commit fbd66e2

16 files changed

+131
-22
lines changed

include/swift/AST/ASTContext.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,9 @@ class ASTContext final {
753753
/// Get the runtime availability of support for concurrency.
754754
AvailabilityContext getConcurrencyAvailability();
755755

756+
/// Get the runtime availability of support for distributed actors.
757+
AvailabilityContext getDistributedAvailability();
758+
756759
/// Get the runtime availability of support for differentiation.
757760
AvailabilityContext getDifferentiationAvailability();
758761

include/swift/AST/DiagnosticsParse.def

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1839,6 +1839,11 @@ ERROR(attr_requires_concurrency, none,
18391839
"concurrency is enabled",
18401840
(StringRef, bool))
18411841

1842+
ERROR(attr_requires_distributed, none,
1843+
"'%0' %select{attribute|modifier}1 is only valid when experimental "
1844+
"distributed support is enabled",
1845+
(StringRef, bool))
1846+
18421847
//------------------------------------------------------------------------------
18431848
// MARK: syntax parsing diagnostics
18441849
//------------------------------------------------------------------------------

include/swift/Frontend/Frontend.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,9 +354,6 @@ class CompilerInvocation {
354354
/// imported.
355355
bool shouldImportSwiftConcurrency() const;
356356

357-
/// Whether the Distributed support library should be implicitly imported.
358-
bool shouldImportSwiftDistributed() const;
359-
360357
/// Performs input setup common to these tools:
361358
/// sil-opt, sil-func-extractor, sil-llvm-gen, and sil-nm.
362359
/// Return value includes the buffer so caller can keep it alive.
@@ -536,6 +533,10 @@ class CompilerInstance {
536533
/// i.e. if it can be found.
537534
bool canImportSwiftConcurrency() const;
538535

536+
/// Whether the Distributed actors support library can be imported
537+
/// i.e. if it can be found.
538+
bool canImportSwiftDistributed() const;
539+
539540
/// Gets the SourceFile which is the primary input for this CompilerInstance.
540541
/// \returns the primary SourceFile, or nullptr if there is no primary input;
541542
/// if there are _multiple_ primary inputs, fails with an assertion.

include/swift/Runtime/RuntimeFunctions.def

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1692,15 +1692,15 @@ FUNCTION(DefaultActorDeallocateResilient,
16921692
// );
16931693
FUNCTION(DistributedActorInitializeRemote,
16941694
swift_distributedActor_remote_initialize, SwiftCC,
1695-
ConcurrencyAvailability,
1695+
DistributedAvailability,
16961696
RETURNS(OpaquePtrTy),
16971697
ARGS(TypeMetadataPtrTy),
16981698
ATTRS(NoUnwind))
16991699

1700-
// void swift_distributedActor_destroy(DefaultActor *actor); // TODO: ProxyActor *proxy?
1700+
// void swift_distributedActor_destroy(DefaultActor *actor);
17011701
FUNCTION(DistributedActorDestroy,
17021702
swift_distributedActor_destroy, SwiftCC,
1703-
ConcurrencyAvailability,
1703+
DistributedAvailability,
17041704
RETURNS(VoidTy),
17051705
ARGS(RefCountedPtrTy),
17061706
ATTRS(NoUnwind))

include/swift/Strings.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ constexpr static const StringLiteral STDLIB_NAME = "Swift";
2424
constexpr static const StringLiteral SWIFT_ONONE_SUPPORT = "SwiftOnoneSupport";
2525
/// The name of the Concurrency module, which supports that extension.
2626
constexpr static const StringLiteral SWIFT_CONCURRENCY_NAME = "_Concurrency";
27+
/// The name of the Distributed module, which supports that extension.
28+
constexpr static const StringLiteral SWIFT_DISTRIBUTED_NAME = "_Distributed";
2729
/// The name of the SwiftShims module, which contains private stdlib decls.
2830
constexpr static const StringLiteral SWIFT_SHIMS_NAME = "SwiftShims";
2931
/// The name of the Builtin module, which contains Builtin functions.

lib/AST/Availability.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,10 @@ AvailabilityContext ASTContext::getConcurrencyAvailability() {
330330
return getSwift55Availability();
331331
}
332332

333+
AvailabilityContext ASTContext::getDistributedAvailability() {
334+
return getSwift55Availability(); // FIXME(distributed): getSwiftFutureAvailability perhaps? The module is not shipping in any release so far so it does not matter for real though.
335+
}
336+
333337
AvailabilityContext ASTContext::getDifferentiationAvailability() {
334338
return getSwiftFutureAvailability();
335339
}

lib/Frontend/Frontend.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -779,12 +779,6 @@ bool CompilerInvocation::shouldImportSwiftConcurrency() const {
779779
FrontendOptions::ParseInputMode::SwiftModuleInterface;
780780
}
781781

782-
bool CompilerInvocation::shouldImportSwiftDistributed() const {
783-
return getLangOptions().EnableExperimentalDistributed &&
784-
getFrontendOptions().InputMode !=
785-
FrontendOptions::ParseInputMode::SwiftModuleInterface;
786-
}
787-
788782
/// Implicitly import the SwiftOnoneSupport module in non-optimized
789783
/// builds. This allows for use of popular specialized functions
790784
/// from the standard library, which makes the non-optimized builds
@@ -825,6 +819,11 @@ bool CompilerInstance::canImportSwiftConcurrency() const {
825819
{getASTContext().getIdentifier(SWIFT_CONCURRENCY_NAME), SourceLoc()});
826820
}
827821

822+
bool CompilerInstance::canImportSwiftDistributed() const {
823+
return getASTContext().canImportModule(
824+
{getASTContext().getIdentifier(SWIFT_DISTRIBUTED_NAME), SourceLoc()});
825+
}
826+
828827
ImplicitImportInfo CompilerInstance::getImplicitImportInfo() const {
829828
auto &frontendOpts = Invocation.getFrontendOptions();
830829

lib/IRGen/IRGenModule.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,14 @@ namespace RuntimeConstants {
786786
return RuntimeAvailability::AlwaysAvailable;
787787
}
788788

789+
RuntimeAvailability DistributedAvailability(ASTContext &context) {
790+
auto featureAvailability = context.getDistributedAvailability();
791+
if (!isDeploymentAvailabilityContainedIn(context, featureAvailability)) {
792+
return RuntimeAvailability::ConditionallyAvailable;
793+
}
794+
return RuntimeAvailability::AlwaysAvailable;
795+
}
796+
789797
RuntimeAvailability DifferentiationAvailability(ASTContext &context) {
790798
auto featureAvailability = context.getDifferentiationAvailability();
791799
if (!isDeploymentAvailabilityContainedIn(context, featureAvailability)) {

lib/Parse/ParseDecl.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1611,6 +1611,14 @@ bool Parser::parseNewDeclAttribute(DeclAttributes &Attributes, SourceLoc AtLoc,
16111611
DiscardAttribute = true;
16121612
}
16131613

1614+
// If this attribute is only permitted when distributed is enabled, reject it.
1615+
if (DeclAttribute::isDistributedOnly(DK) &&
1616+
!shouldParseExperimentalDistributed()) {
1617+
diagnose(Loc, diag::attr_requires_distributed, AttrName,
1618+
DeclAttribute::isDeclModifier(DK));
1619+
DiscardAttribute = true;
1620+
}
1621+
16141622
if (Context.LangOpts.Target.isOSBinFormatCOFF()) {
16151623
if (DK == DAK_WeakLinked) {
16161624
diagnose(Loc, diag::attr_unsupported_on_target, AttrName,

lib/Sema/TypeCheckAvailability.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,6 +1702,24 @@ void TypeChecker::checkConcurrencyAvailability(SourceRange ReferenceRange,
17021702
}
17031703
}
17041704

1705+
void TypeChecker::checkDistributedAvailability(SourceRange ReferenceRange,
1706+
const DeclContext *ReferenceDC) {
1707+
// Check the availability of concurrency runtime support.
1708+
ASTContext &ctx = ReferenceDC->getASTContext();
1709+
if (ctx.LangOpts.DisableAvailabilityChecking)
1710+
return;
1711+
1712+
auto runningOS =
1713+
TypeChecker::overApproximateAvailabilityAtLocation(
1714+
ReferenceRange.Start, ReferenceDC);
1715+
auto availability = ctx.getDistributedAvailability();
1716+
if (!runningOS.isContainedIn(availability)) {
1717+
diagnosePotentialConcurrencyUnavailability(
1718+
ReferenceRange, ReferenceDC,
1719+
UnavailabilityReason::requiresVersionRange(availability.getOSVersion()));
1720+
}
1721+
}
1722+
17051723
void TypeChecker::diagnosePotentialUnavailability(
17061724
const ValueDecl *D, SourceRange ReferenceRange,
17071725
const DeclContext *ReferenceDC,

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2418,9 +2418,11 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
24182418

24192419
TypeChecker::checkDeclAttributes(CD);
24202420

2421-
if (CD->isActor())
2421+
if (CD->isActor()) {
24222422
TypeChecker::checkConcurrencyAvailability(CD->getLoc(), CD);
2423-
2423+
if (CD->isDistributedActor())
2424+
TypeChecker::checkDistributedAvailability(CD->getLoc(), CD);
2425+
}
24242426
for (Decl *Member : CD->getABIMembers())
24252427
visit(Member);
24262428

lib/Sema/TypeChecker.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,6 +1026,9 @@ void checkDistributedActor(ClassDecl *decl);
10261026
void checkConcurrencyAvailability(SourceRange ReferenceRange,
10271027
const DeclContext *ReferenceDC);
10281028

1029+
void checkDistributedAvailability(SourceRange ReferenceRange,
1030+
const DeclContext *ReferenceDC);
1031+
10291032
/// Emits a diagnostic for a reference to a storage accessor that is
10301033
/// potentially unavailable.
10311034
void diagnosePotentialAccessorUnavailability(

test/Distributed/distributed_actor_is_experimental.swift

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,37 @@ actor SomeActor {}
77

88
@available(SwiftStdlib 5.5, *)
99
distributed actor DA {}
10-
// expected-error@-1{{'_Distributed' module not imported, required for 'distributed actor'}}
10+
// expected-error@-1{{'distributed' modifier is only valid when experimental distributed support is enabled}}
1111

1212
@available(SwiftStdlib 5.5, *)
13-
distributed actor class DAC {} // expected-error{{distributed' can only be applied to 'actor' definitions, and distributed actor-isolated async functions}}
14-
// expected-error@-1{{keyword 'class' cannot be used as an identifier here}}
13+
distributed actor class DAC {}
14+
// expected-error@-1{{'distributed' modifier is only valid when experimental distributed support is enabled}}
15+
// expected-error@-2{{keyword 'class' cannot be used as an identifier here}}
1516

1617
actor A {
1718
func normal() async {}
18-
distributed func dist() {} // expected-error{{'distributed' function can only be declared within 'distributed actor'}}
19-
distributed func distAsync() async {} // expected-error{{'distributed' function can only be declared within 'distributed actor'}}
19+
distributed func dist() {}
20+
// expected-error@-1{{'distributed' modifier is only valid when experimental distributed support is enabled}}
21+
distributed func distAsync() async {}
22+
// expected-error@-1{{'distributed' modifier is only valid when experimental distributed support is enabled}}
2023

21-
distributed var neverOk: String { // expected-error{{'distributed' modifier cannot be applied to this declaration}}
24+
distributed var neverOk: String {
25+
// expected-error@-1{{'distributed' modifier is only valid when experimental distributed support is enabled}}
2226
"vars are not allowed to be distributed *ever* anyway"
2327
}
2428
}
2529

2630
@available(SwiftStdlib 5.5, *)
2731
distributed actor DA2 {
28-
// expected-error@-1{{'_Distributed' module not imported, required for 'distributed actor'}}
32+
// expected-error@-1{{'distributed' modifier is only valid when experimental distributed support is enabled}}
2933
func normal() async {}
3034
distributed func dist() {}
35+
// expected-error@-1{{'distributed' modifier is only valid when experimental distributed support is enabled}}
3136
distributed func distAsync() async {}
37+
// expected-error@-1{{'distributed' modifier is only valid when experimental distributed support is enabled}}
3238

33-
distributed var neverOk: String { // expected-error{{'distributed' modifier cannot be applied to this declaration}}
39+
distributed var neverOk: String {
40+
// expected-error@-1{{'distributed' modifier is only valid when experimental distributed support is enabled}}
3441
"vars are not allowed to be distributed *ever* anyway"
3542
}
3643
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// RUN: %target-typecheck-verify-swift -disable-availability-checking -enable-experimental-distributed
2+
// REQUIRES: concurrency
3+
// REQUIRES: distributed
4+
5+
actor SomeActor {}
6+
7+
@available(SwiftStdlib 5.5, *)
8+
distributed actor DA {}
9+
// expected-error@-1{{'_Distributed' module not imported, required for 'distributed actor'}}
10+
11+
@available(SwiftStdlib 5.5, *)
12+
distributed actor class DAC {}
13+
// expected-error@-1{{distributed' can only be applied to 'actor' definitions, and distributed actor-isolated async functions}}
14+
// expected-error@-2{{keyword 'class' cannot be used as an identifier here}}
15+
16+
actor A {
17+
func normal() async {}
18+
distributed func dist() {} // expected-error{{'distributed' function can only be declared within 'distributed actor'}}
19+
distributed func distAsync() async {} // expected-error{{'distributed' function can only be declared within 'distributed actor'}}
20+
21+
distributed var neverOk: String { // expected-error{{'distributed' modifier cannot be applied to this declaration}}
22+
"vars are not allowed to be distributed *ever* anyway"
23+
}
24+
}
25+
26+
@available(SwiftStdlib 5.5, *)
27+
distributed actor DA2 {
28+
// expected-error@-1{{'_Distributed' module not imported, required for 'distributed actor'}}
29+
func normal() async {}
30+
distributed func dist() {}
31+
distributed func distAsync() async {}
32+
33+
distributed var neverOk: String { // expected-error{{'distributed' modifier cannot be applied to this declaration}}
34+
"vars are not allowed to be distributed *ever* anyway"
35+
}
36+
}
37+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: %target-typecheck-verify-swift -disable-availability-checking
2+
// ^^^^ notice the, on purpose, missing '-enable-experimental-distributed'
3+
// REQUIRES: concurrency
4+
// REQUIRES: distributed
5+
6+
import _Distributed
7+
8+
@available(macOS 12.0, *)
9+
distributed actor A {}
10+
// expected-error@-1{{'distributed' modifier is only valid when experimental distributed support is enabled}}

tools/sil-opt/SILOpt.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,8 @@ int main(int argc, char **argv) {
415415
}
416416
Invocation.getLangOptions().EnableExperimentalConcurrency =
417417
EnableExperimentalConcurrency;
418+
Invocation.getLangOptions().EnableExperimentalDistributed =
419+
EnableExperimentalDistributed;
418420

419421
Invocation.getLangOptions().EnableObjCInterop =
420422
EnableObjCInterop ? true :

0 commit comments

Comments
 (0)