Skip to content

[Concurrency] Downgrade the isolated default argument error to a warning when the argument expression is @preconcurrency. #76795

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 1 commit into from
Oct 2, 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
80 changes: 34 additions & 46 deletions lib/Sema/TypeCheckConcurrency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3758,6 +3758,14 @@ namespace {
if (!unsatisfiedIsolation)
return false;

// Record whether the callee isolation or the context isolation
// is preconcurrency, which is used later to downgrade errors to
// warnings in minimal checking.
bool preconcurrency = getContextIsolation().preconcurrency() ||
(calleeDecl && getActorIsolation(calleeDecl).preconcurrency());
unsatisfiedIsolation =
unsatisfiedIsolation->withPreconcurrency(preconcurrency);

bool onlyArgsCrossIsolation = callOptions.contains(
ActorReferenceResult::Flags::OnlyArgsCrossIsolation);
if (!onlyArgsCrossIsolation &&
Expand All @@ -3775,63 +3783,41 @@ namespace {
// If we need to mark the call as implicitly asynchronous, make sure
// we're in an asynchronous context.
if (requiresAsync && !getDeclContext()->isAsyncContext()) {
auto diagnostic = calleeDecl ?
Diagnostic(
/*id*/diag::actor_isolated_call_decl,
/*args*/*unsatisfiedIsolation, calleeDecl, getContextIsolation()
) :
Diagnostic(
/*id*/diag::actor_isolated_call,
/*args*/*unsatisfiedIsolation, getContextIsolation()
);

if (ctx.LangOpts.hasFeature(Feature::GroupActorErrors)) {

IsolationError mismatch([calleeDecl, apply, unsatisfiedIsolation, getContextIsolation]() {
if (calleeDecl) {
auto preconcurrency = getContextIsolation().preconcurrency() ||
getActorIsolation(calleeDecl).preconcurrency();

return IsolationError(
apply->getLoc(),
preconcurrency,
Diagnostic(diag::actor_isolated_call_decl,
*unsatisfiedIsolation,
calleeDecl,
getContextIsolation()));
} else {
return IsolationError(
apply->getLoc(),
getContextIsolation().preconcurrency(),
Diagnostic(diag::actor_isolated_call,
*unsatisfiedIsolation,
getContextIsolation()));
}
}());

auto iter = applyErrors.find(std::make_pair(*unsatisfiedIsolation, getContextIsolation()));
if (iter != applyErrors.end()){
iter->second.push_back((mismatch));
} else {
DiagnosticList list;
list.push_back((mismatch));
auto keyPair = std::make_pair(*unsatisfiedIsolation, getContextIsolation());
applyErrors.insert(std::make_pair(keyPair, list));
IsolationError mismatch(apply->getLoc(), preconcurrency, diagnostic);
auto key = std::make_pair(
unsatisfiedIsolation->withPreconcurrency(false),
getContextIsolation());
if (applyErrors.find(key) == applyErrors.end()) {
applyErrors.insert(std::make_pair(key, DiagnosticList()));
}

applyErrors[key].push_back(mismatch);
} else {
ctx.Diags.diagnose(
apply->getLoc(),
diagnostic.getID(),
diagnostic.getArgs())
.warnUntilSwiftVersionIf(preconcurrency, 6);

if (calleeDecl) {
auto calleeIsolation = getInferredActorIsolation(calleeDecl);
auto preconcurrency = getContextIsolation().preconcurrency() ||
calleeIsolation.preconcurrency();

ctx.Diags.diagnose(
apply->getLoc(), diag::actor_isolated_call_decl,
*unsatisfiedIsolation,
calleeDecl,
getContextIsolation())
.warnUntilSwiftVersionIf(preconcurrency, 6);
calleeDecl->diagnose(diag::actor_isolated_sync_func, calleeDecl);
if (auto source = calleeIsolation.source.isInferred()) {
calleeDecl->diagnose(diag::actor_isolation_source,
calleeIsolation.isolation,
calleeIsolation.source);
}
} else {
ctx.Diags.diagnose(
apply->getLoc(), diag::actor_isolated_call, *unsatisfiedIsolation,
getContextIsolation())
.warnUntilSwiftVersionIf(getContextIsolation().preconcurrency(), 6);
}

if (unsatisfiedIsolation->isGlobalActor()) {
Expand Down Expand Up @@ -5962,10 +5948,12 @@ DefaultInitializerIsolation::evaluate(Evaluator &evaluator,
auto requiredIsolation = checker.computeRequiredIsolation(initExpr);
if (requiredIsolation.isActorIsolated()) {
if (enclosingIsolation != requiredIsolation) {
bool preconcurrency =
!isa<ParamDecl>(var) || requiredIsolation.preconcurrency();
var->diagnose(
diag::isolated_default_argument_context,
requiredIsolation, enclosingIsolation)
.warnUntilSwiftVersionIf(!isa<ParamDecl>(var), 6);
.warnUntilSwiftVersionIf(preconcurrency, 6);
return ActorIsolation::forUnspecified();
}
}
Expand Down
9 changes: 8 additions & 1 deletion test/Concurrency/isolated_default_arguments.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// RUN: %empty-directory(%t)

// RUN: %target-swift-frontend -I %t -disable-availability-checking -strict-concurrency=complete -parse-as-library -emit-sil -o /dev/null -verify -enable-upcoming-feature IsolatedDefaultValues -enable-upcoming-feature RegionBasedIsolation -enable-upcoming-feature InferSendableFromCaptures %s
// RUN: %target-swift-frontend -I %t -disable-availability-checking -strict-concurrency=complete -parse-as-library -emit-sil -o /dev/null -verify -enable-upcoming-feature InferSendableFromCaptures %s

// REQUIRES: concurrency
// REQUIRES: asserts
Expand Down Expand Up @@ -317,3 +317,10 @@ struct Values {
@MainActor var value: Int { 0 }
@SomeGlobalActor var otherValue: Int { 0 }
}

class PreconcurrencyInit {
@preconcurrency @MainActor init() {}
}

// expected-warning@+1 {{main actor-isolated default value in a nonisolated context; this is an error in the Swift 6 language mode}}
func downgrade(_: PreconcurrencyInit = .init()) {}