Skip to content

[Concurrency] Look through caller-side default arguments for #isolation when computing the isolated actor expression for a call. #72082

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions lib/Sema/TypeCheckConcurrency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2427,6 +2427,8 @@ namespace {
}

void checkDefaultArgument(DefaultArgumentExpr *expr) {
getCurrentContextIsolation(expr);

// Check the context isolation against the required isolation for
// evaluating the default argument synchronously. If the default
// argument must be evaluated asynchronously, record that in the
Expand Down Expand Up @@ -3055,7 +3057,7 @@ namespace {
ctx.Diags.diagnose(loc, diag::shared_mutable_state_access, value)
.limitBehaviorUntilSwiftVersion(limit, 6)
// Preconcurrency global variables are warnings even in Swift 6
.limitBehaviorIf(isPreconcurrencyImport, DiagnosticBehavior::Warning);
.limitBehaviorIf(isPreconcurrencyImport, limit);
value->diagnose(diag::kind_declared_here, value->getDescriptiveKind());
if (const auto sourceFile = getDeclContext()->getParentSourceFile();
sourceFile && isPreconcurrencyImport) {
Expand Down Expand Up @@ -3378,13 +3380,8 @@ namespace {

// FIXME: CurrentContextIsolationExpr does not have its actor set
// at this point.
if (auto *macro = dyn_cast<MacroExpansionExpr>(arg)) {
auto *expansion = macro->getRewritten();
if (auto *isolation = dyn_cast<CurrentContextIsolationExpr>(expansion)) {
recordCurrentContextIsolation(isolation);
arg = isolation->getActor();
}
}
if (auto isolation = getCurrentContextIsolation(arg))
arg = isolation;

argForIsolatedParam = arg;
unsatisfiedIsolation = std::nullopt;
Expand Down Expand Up @@ -3547,6 +3544,25 @@ namespace {
return false;
}

Expr *getCurrentContextIsolation(Expr *expr) {
// Look through caller-side default arguments for #isolation.
auto *defaultArg = dyn_cast<DefaultArgumentExpr>(expr);
if (defaultArg && defaultArg->isCallerSide()) {
expr = defaultArg->getCallerSideDefaultExpr();
}

if (auto *macro = dyn_cast<MacroExpansionExpr>(expr)) {
expr = macro->getRewritten();
}

if (auto *isolation = dyn_cast<CurrentContextIsolationExpr>(expr)) {
recordCurrentContextIsolation(isolation);
return isolation->getActor();
}

return nullptr;
}

void recordCurrentContextIsolation(
CurrentContextIsolationExpr *isolationExpr) {
// If an actor has already been assigned, we're done.
Expand Down
4 changes: 2 additions & 2 deletions test/Concurrency/global_variables.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -emit-module -emit-module-path %t/GlobalVariables.swiftmodule -module-name GlobalVariables -parse-as-library -strict-concurrency=minimal -swift-version 5 %S/Inputs/GlobalVariables.swift
// RUN: %target-swift-frontend -disable-availability-checking -parse-as-library -swift-version 6 -I %t %s -emit-sil -o /dev/null -verify %s
// RUN: %target-swift-frontend -disable-availability-checking -parse-as-library -swift-version 6 -I %t -emit-sil -o /dev/null -verify %s

// REQUIRES: concurrency

Expand Down Expand Up @@ -71,7 +71,7 @@ func testLocalNonisolatedUnsafe() async {

func testImportedGlobals() { // expected-note{{add '@MainActor' to make global function 'testImportedGlobals()' part of global actor 'MainActor'}}
let _ = Globals.integerConstant
let _ = Globals.integerMutable // expected-warning{{reference to static property 'integerMutable' is not concurrency-safe because it involves shared mutable state}}
let _ = Globals.integerMutable
let _ = Globals.nonisolatedUnsafeIntegerConstant
let _ = Globals.nonisolatedUnsafeIntegerMutable
let _ = Globals.actorInteger // expected-error{{main actor-isolated static property 'actorInteger' can not be referenced from a non-isolated context}}
Expand Down
13 changes: 13 additions & 0 deletions test/Concurrency/isolated_parameters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -477,3 +477,16 @@ nonisolated func fromNonisolated(ns: NotSendable) async -> NotSendable {
func invalidIsolatedClosureParam<A: AnyActor> (
_: (isolated A) async throws -> Void // expected-error {{'isolated' parameter type 'A' does not conform to 'Actor' or 'DistributedActor'}}
) {}

public func useDefaultIsolation(
_ isolation: isolated (any Actor)? = #isolation
) {}

public func useDefaultIsolationWithoutIsolatedParam(
_ isolation: (any Actor)? = #isolation
) {}

@MainActor func callUseDefaultIsolation() async {
useDefaultIsolation()
useDefaultIsolationWithoutIsolatedParam()
}