Skip to content

[Sema] nonisolated(unsafe) for local variables #70135

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
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
2 changes: 2 additions & 0 deletions lib/AST/TypeCheckRequests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1699,6 +1699,7 @@ bool ActorIsolation::requiresSubstitution() const {
switch (kind) {
case ActorInstance:
case Nonisolated:
case NonisolatedUnsafe:
case Unspecified:
return false;

Expand All @@ -1713,6 +1714,7 @@ ActorIsolation ActorIsolation::subst(SubstitutionMap subs) const {
switch (kind) {
case ActorInstance:
case Nonisolated:
case NonisolatedUnsafe:
case Unspecified:
return *this;

Expand Down
15 changes: 9 additions & 6 deletions lib/Sema/TypeCheckAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6816,12 +6816,14 @@ void AttributeChecker::visitNonisolatedAttr(NonisolatedAttr *attr) {
auto dc = D->getDeclContext();

if (auto var = dyn_cast<VarDecl>(D)) {
const bool isUnsafe =
attr->isUnsafe() && Ctx.LangOpts.hasFeature(Feature::GlobalConcurrency);

// stored properties have limitations as to when they can be nonisolated.
if (var->hasStorage()) {
const bool isUnsafeGlobal = attr->isUnsafe() && var->isGlobalStorage();

// 'nonisolated' can not be applied to mutable stored properties.
if (var->supportsMutation() && !isUnsafeGlobal) {
// 'nonisolated' can not be applied to mutable stored properties unless
// qualified as 'unsafe'.
if (var->supportsMutation() && !isUnsafe) {
diagnoseAndRemoveAttr(attr, diag::nonisolated_mutable_storage);
return;
}
Expand Down Expand Up @@ -6863,8 +6865,9 @@ void AttributeChecker::visitNonisolatedAttr(NonisolatedAttr *attr) {
return;
}

// nonisolated can not be applied to local properties.
if (dc->isLocalContext()) {
// nonisolated can not be applied to local properties unless qualified as
// 'unsafe'.
if (dc->isLocalContext() && !isUnsafe) {
diagnoseAndRemoveAttr(attr, diag::nonisolated_local_var);
return;
}
Expand Down
12 changes: 12 additions & 0 deletions lib/Sema/TypeCheckConcurrency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2813,6 +2813,12 @@ namespace {
if (getActorIsolation(value).isActorIsolated())
return false;

if (auto attr = value->getAttrs().getAttribute<NonisolatedAttr>();
ctx.LangOpts.hasFeature(Feature::GlobalConcurrency) && attr &&
attr->isUnsafe()) {
return false;
}

ctx.Diags.diagnose(loc, diag::shared_mutable_state_access, value);
value->diagnose(diag::kind_declared_here, value->getDescriptiveKind());
return true;
Expand Down Expand Up @@ -3279,6 +3285,12 @@ namespace {
}
}

if (auto attr = var->getAttrs().getAttribute<NonisolatedAttr>();
ctx.LangOpts.hasFeature(Feature::GlobalConcurrency) && attr &&
attr->isUnsafe()) {
return false;
}

// Otherwise, we have concurrent access. Complain.
bool preconcurrencyContext =
getActorIsolationOfContext(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,12 @@ func f() {
print(TestStatics.immutableInferredSendable)
print(TestStatics.mutable) // expected-warning{{reference to static property 'mutable' is not concurrency-safe because it involves shared mutable state}}
}

func testLocalNonisolatedUnsafe() async {
nonisolated(unsafe) var value = 1
let task = Task {
value = 2
return value
}
print(await task.value)
}