Skip to content

Commit 23a0186

Browse files
authored
Merge pull request #71698 from hborla/enable-isolation-inheritance
[Concurrency] Enable `OptionalIsolatedParameters`.
2 parents 30984f5 + 26621b8 commit 23a0186

File tree

13 files changed

+54
-31
lines changed

13 files changed

+54
-31
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5558,8 +5558,6 @@ ERROR(isolated_attr_global_actor_type,none,
55585558
ERROR(isolated_attr_bad_convention,none,
55595559
"'@isolated(%0)' function type cannot have %1 convention",
55605560
(StringRef, StringRef))
5561-
ERROR(isolation_macro_experimental,none,
5562-
"#isolation macro is experimental", ())
55635561

55645562
NOTE(in_derived_conformance, none,
55655563
"in derived conformance to %0",

include/swift/Basic/Features.def

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ LANGUAGE_FEATURE(FreestandingMacros, 397, "freestanding declaration macros")
122122
SUPPRESSIBLE_LANGUAGE_FEATURE(RetroactiveAttribute, 364, "@retroactive")
123123
SUPPRESSIBLE_LANGUAGE_FEATURE(ExtensionMacroAttr, 0, "@attached(extension)")
124124
LANGUAGE_FEATURE(TypedThrows, 413, "Typed throws")
125+
LANGUAGE_FEATURE(OptionalIsolatedParameters, 420, "Optional isolated parameters")
125126
SUPPRESSIBLE_LANGUAGE_FEATURE(Extern, 0, "@_extern")
126127
LANGUAGE_FEATURE(ExpressionMacroDefaultArguments, 422, "Expression macro as caller-side default argument")
127128

@@ -261,9 +262,6 @@ EXPERIMENTAL_FEATURE(StructLetDestructuring, true)
261262
/// lifetime-dependent results.
262263
EXPERIMENTAL_FEATURE(NonescapableTypes, true)
263264

264-
// Allow optional isolated parameters.
265-
EXPERIMENTAL_FEATURE(OptionalIsolatedParameters, true)
266-
267265
/// Enable the `@_staticExclusiveOnly` attribute.
268266
EXPERIMENTAL_FEATURE(StaticExclusiveOnly, true)
269267

lib/Sema/CSGen.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3923,6 +3923,13 @@ namespace {
39233923
}
39243924

39253925
Type visitCurrentContextIsolationExpr(CurrentContextIsolationExpr *E) {
3926+
// If this was expanded from the builtin `#isolation` macro, it
3927+
// already has a type.
3928+
if (auto type = E->getType())
3929+
return type;
3930+
3931+
// Otherwise, this was created for a `for await` loop, where its
3932+
// type is always `(any Actor)?`.
39263933
auto actorProto = CS.getASTContext().getProtocol(
39273934
KnownProtocolKind::Actor);
39283935
return OptionalType::get(actorProto->getDeclaredExistentialType());

lib/Sema/ConstraintSystem.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "swift/AST/ExistentialLayout.h"
2626
#include "swift/AST/GenericEnvironment.h"
2727
#include "swift/AST/Initializer.h"
28+
#include "swift/AST/MacroDefinition.h"
2829
#include "swift/AST/ParameterList.h"
2930
#include "swift/AST/ProtocolConformance.h"
3031
#include "swift/AST/TypeCheckRequests.h"
@@ -3964,6 +3965,16 @@ void ConstraintSystem::resolveOverload(ConstraintLocator *locator,
39643965
// Record a fix here
39653966
(void)recordFix(MacroMissingPound::create(*this, macro, locator));
39663967
}
3968+
3969+
// The default type of the #isolation builtin macro is `(any Actor)?`
3970+
if (macro->getBuiltinKind() == BuiltinMacroKind::IsolationMacro) {
3971+
auto *fnType = openedType->getAs<FunctionType>();
3972+
auto actor = getASTContext().getProtocol(KnownProtocolKind::Actor);
3973+
addConstraint(
3974+
ConstraintKind::Defaultable, fnType->getResult(),
3975+
OptionalType::get(actor->getDeclaredExistentialType()),
3976+
locator);
3977+
}
39673978
}
39683979
}
39693980

lib/Sema/TypeCheckMacros.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,10 +1132,6 @@ evaluateFreestandingMacro(FreestandingMacroExpansion *expansion,
11321132
return nullptr;
11331133

11341134
case BuiltinMacroKind::IsolationMacro:
1135-
if (!ctx.LangOpts.hasFeature(Feature::OptionalIsolatedParameters)) {
1136-
ctx.Diags.diagnose(loc, diag::isolation_macro_experimental);
1137-
}
1138-
11391135
// Create a buffer full of scratch space; this will be populated
11401136
// much later.
11411137
std::string scratchSpace(128, ' ');

lib/Sema/TypeCheckType.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4944,13 +4944,10 @@ TypeResolver::resolveIsolatedTypeRepr(IsolatedTypeRepr *repr,
49444944
Type unwrappedType = type;
49454945

49464946
// Optional actor types are fine - `nil` represents `nonisolated`.
4947-
auto allowOptional = getASTContext().LangOpts
4948-
.hasFeature(Feature::OptionalIsolatedParameters);
4949-
if (allowOptional) {
4950-
if (auto wrappedOptionalType = unwrappedType->getOptionalObjectType()) {
4951-
unwrappedType = wrappedOptionalType;
4952-
}
4947+
if (auto wrappedOptionalType = unwrappedType->getOptionalObjectType()) {
4948+
unwrappedType = wrappedOptionalType;
49534949
}
4950+
49544951
if (auto dynamicSelfType = dyn_cast<DynamicSelfType>(unwrappedType)) {
49554952
unwrappedType = dynamicSelfType->getSelfType();
49564953
}

stdlib/public/Concurrency/Actor.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,12 @@ internal func _enqueueOnMain(_ job: UnownedJob)
7676
#if $Macros
7777
/// Produce a reference to the actor to which the enclosing code is
7878
/// isolated, or `nil` if the code is nonisolated.
79+
///
80+
/// If the type annotation provided for `#isolation` is not `(any Actor)?`,
81+
/// the type must match the enclosing actor type. If no type annotation is
82+
/// provided, the type defaults to `(any Actor)?`.
7983
@available(SwiftStdlib 5.1, *)
8084
@freestanding(expression)
81-
public macro isolation() -> (any Actor)? = Builtin.IsolationMacro
85+
public macro isolation<T>() -> T = Builtin.IsolationMacro
8286
#endif
8387

stdlib/public/Concurrency/CMakeLists.txt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,6 @@ else()
6161
endif()
6262

6363

64-
list(APPEND SWIFT_RUNTIME_CONCURRENCY_SWIFT_FLAGS
65-
"-enable-experimental-feature"
66-
"OptionalIsolatedParameters"
67-
)
68-
6964
list(APPEND SWIFT_RUNTIME_CONCURRENCY_C_FLAGS
7065
"-D__STDC_WANT_LIB_EXT1__=1")
7166

test/Concurrency/isolated_parameters.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
// RUN: %target-swift-frontend -disable-availability-checking -strict-concurrency=complete -enable-experimental-feature OptionalIsolatedParameters %s -emit-sil -o /dev/null -verify -verify-additional-prefix complete-
2-
// RUN: %target-swift-frontend -disable-availability-checking -strict-concurrency=complete -enable-experimental-feature OptionalIsolatedParameters %s -emit-sil -o /dev/null -verify -enable-experimental-feature RegionBasedIsolation
1+
// RUN: %target-swift-frontend -disable-availability-checking -strict-concurrency=complete %s -emit-sil -o /dev/null -verify -verify-additional-prefix complete-
2+
// RUN: %target-swift-frontend -disable-availability-checking -strict-concurrency=complete %s -emit-sil -o /dev/null -verify -enable-experimental-feature RegionBasedIsolation
33

44
// REQUIRES: concurrency
5-
// REQUIRES: asserts
65
// REQUIRES: swift_swift_parser
76

87
@available(SwiftStdlib 5.1, *)

test/Concurrency/isolation_macro.swift

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
// RUN: %target-swift-frontend -dump-ast %s -enable-experimental-feature OptionalIsolatedParameters | %FileCheck %s
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend -dump-ast %s | %FileCheck %s
3+
4+
// Diagnostics testing
5+
// RUN: not %target-swift-frontend -swift-version 5 -typecheck -DTEST_DIAGNOSTICS %s > %t/diagnostics.txt 2>&1
6+
// RUN: %FileCheck %s --check-prefix CHECK-DIAGS < %t/diagnostics.txt
27

38
// REQUIRES: concurrency
4-
// REQUIRES: asserts
59
// REQUIRES: swift_swift_parser
610

711
// CHECK-LABEL: nonisolatedFunc()
@@ -94,3 +98,18 @@ extension A {
9498

9599
func g() {}
96100
}
101+
102+
#if TEST_DIAGNOSTICS
103+
@available(SwiftStdlib 5.1, *)
104+
@MainActor
105+
func testContextualType() {
106+
let _: any Actor = #isolation
107+
let _: MainActor = #isolation
108+
let _: MainActor? = #isolation
109+
110+
// CHECK-DIAGS: error: cannot convert value of type 'MainActor' to expected argument type 'Int'
111+
// CHECK-DIAGS: note: in expansion of macro 'isolation' here
112+
// CHECK-DIAGS: let _: Int = #isolation
113+
let _: Int = #isolation
114+
}
115+
#endif

test/Concurrency/optional_isolated_parameters.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: %empty-directory(%t)
2-
// RUN: %target-build-swift -Xfrontend -disable-availability-checking -enable-experimental-feature OptionalIsolatedParameters %s -o %t/main
2+
// RUN: %target-build-swift -Xfrontend -disable-availability-checking %s -o %t/main
33
// RUN: %target-codesign %t/main
44
// RUN: %target-run %t/main | %FileCheck %s
55

test/Distributed/isolation_macro.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
// RUN: %target-swift-frontend -typecheck %s -enable-experimental-feature OptionalIsolatedParameters -verify
1+
// RUN: %target-swift-frontend -typecheck %s -verify
22

3-
// RUN: %target-swift-frontend -dump-ast %s -enable-experimental-feature OptionalIsolatedParameters | %FileCheck %s
3+
// RUN: %target-swift-frontend -dump-ast %s | %FileCheck %s
44

55
// REQUIRES: concurrency
6-
// REQUIRES: asserts
76
// REQUIRES: distributed
87
// REQUIRES: swift_swift_parser
98

test/SILGen/isolated_parameters.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-swift-frontend -emit-silgen -enable-experimental-feature OptionalIsolatedParameters %s -module-name test -swift-version 5 | %FileCheck %s
1+
// RUN: %target-swift-frontend -emit-silgen %s -module-name test -swift-version 5 | %FileCheck %s
22
// REQUIRES: concurrency
33

44
@available(SwiftStdlib 5.1, *)

0 commit comments

Comments
 (0)