Skip to content

Commit ddf2fc4

Browse files
committed
[Concurrency] Allow #isolation to have a more specific contextual type.
1 parent ea7d077 commit ddf2fc4

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

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

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

test/Concurrency/isolation_macro.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
// RUN: %empty-directory(%t)
12
// RUN: %target-swift-frontend -dump-ast %s -enable-experimental-feature OptionalIsolatedParameters | %FileCheck %s
23

4+
// Diagnostics testing
5+
// RUN: not %target-swift-frontend -swift-version 5 -typecheck -enable-experimental-feature OptionalIsolatedParameters -DTEST_DIAGNOSTICS %s > %t/diagnostics.txt 2>&1
6+
// RUN: %FileCheck %s --check-prefix CHECK-DIAGS < %t/diagnostics.txt
7+
38
// REQUIRES: concurrency
49
// REQUIRES: asserts
510
// REQUIRES: swift_swift_parser
@@ -94,3 +99,18 @@ extension A {
9499

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

0 commit comments

Comments
 (0)