Skip to content

Commit 92f5eee

Browse files
authored
Merge pull request swiftlang#72082 from hborla/isolation-default-arg
2 parents 8c0db3f + e653423 commit 92f5eee

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2427,6 +2427,8 @@ namespace {
24272427
}
24282428

24292429
void checkDefaultArgument(DefaultArgumentExpr *expr) {
2430+
getCurrentContextIsolation(expr);
2431+
24302432
// Check the context isolation against the required isolation for
24312433
// evaluating the default argument synchronously. If the default
24322434
// argument must be evaluated asynchronously, record that in the
@@ -3378,13 +3380,8 @@ namespace {
33783380

33793381
// FIXME: CurrentContextIsolationExpr does not have its actor set
33803382
// at this point.
3381-
if (auto *macro = dyn_cast<MacroExpansionExpr>(arg)) {
3382-
auto *expansion = macro->getRewritten();
3383-
if (auto *isolation = dyn_cast<CurrentContextIsolationExpr>(expansion)) {
3384-
recordCurrentContextIsolation(isolation);
3385-
arg = isolation->getActor();
3386-
}
3387-
}
3383+
if (auto isolation = getCurrentContextIsolation(arg))
3384+
arg = isolation;
33883385

33893386
argForIsolatedParam = arg;
33903387
unsatisfiedIsolation = std::nullopt;
@@ -3547,6 +3544,25 @@ namespace {
35473544
return false;
35483545
}
35493546

3547+
Expr *getCurrentContextIsolation(Expr *expr) {
3548+
// Look through caller-side default arguments for #isolation.
3549+
auto *defaultArg = dyn_cast<DefaultArgumentExpr>(expr);
3550+
if (defaultArg && defaultArg->isCallerSide()) {
3551+
expr = defaultArg->getCallerSideDefaultExpr();
3552+
}
3553+
3554+
if (auto *macro = dyn_cast<MacroExpansionExpr>(expr)) {
3555+
expr = macro->getRewritten();
3556+
}
3557+
3558+
if (auto *isolation = dyn_cast<CurrentContextIsolationExpr>(expr)) {
3559+
recordCurrentContextIsolation(isolation);
3560+
return isolation->getActor();
3561+
}
3562+
3563+
return nullptr;
3564+
}
3565+
35503566
void recordCurrentContextIsolation(
35513567
CurrentContextIsolationExpr *isolationExpr) {
35523568
// If an actor has already been assigned, we're done.

test/Concurrency/isolated_parameters.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,3 +477,16 @@ nonisolated func fromNonisolated(ns: NotSendable) async -> NotSendable {
477477
func invalidIsolatedClosureParam<A: AnyActor> (
478478
_: (isolated A) async throws -> Void // expected-error {{'isolated' parameter type 'A' does not conform to 'Actor' or 'DistributedActor'}}
479479
) {}
480+
481+
public func useDefaultIsolation(
482+
_ isolation: isolated (any Actor)? = #isolation
483+
) {}
484+
485+
public func useDefaultIsolationWithoutIsolatedParam(
486+
_ isolation: (any Actor)? = #isolation
487+
) {}
488+
489+
@MainActor func callUseDefaultIsolation() async {
490+
useDefaultIsolation()
491+
useDefaultIsolationWithoutIsolatedParam()
492+
}

0 commit comments

Comments
 (0)