Skip to content

Commit 9c793a8

Browse files
committed
[SE-0466] Nested types of nonisolated types don't infer main-actor isolation
Under discussion as part of an amendment to SE-0466, limit default main actor inference so it doesn't apply to a nested type within a nonisolated type.
1 parent a28d065 commit 9c793a8

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6084,13 +6084,28 @@ computeDefaultInferredActorIsolation(ValueDecl *value) {
60846084
-> std::optional<std::tuple<InferredActorIsolation, ValueDecl *,
60856085
std::optional<ActorIsolation>>> {
60866086
// Default global actor isolation does not apply to any declarations
6087-
// within actors and distributed actors.
6088-
bool inActorContext = false;
6087+
// within actors and distributed actors, nor does it apply in a
6088+
// nonisolated type.
60896089
auto *dc = value->getInnermostDeclContext();
6090-
while (dc && !inActorContext) {
6090+
while (dc) {
60916091
if (auto *nominal = dc->getSelfNominalTypeDecl()) {
60926092
if (nominal->isAnyActor())
60936093
return {};
6094+
6095+
if (dc != dyn_cast<DeclContext>(value)) {
6096+
switch (getActorIsolation(nominal)) {
6097+
case ActorIsolation::Unspecified:
6098+
case ActorIsolation::ActorInstance:
6099+
case ActorIsolation::Nonisolated:
6100+
case ActorIsolation::NonisolatedUnsafe:
6101+
case ActorIsolation::Erased:
6102+
case ActorIsolation::CallerIsolationInheriting:
6103+
return {};
6104+
6105+
case ActorIsolation::GlobalActor:
6106+
break;
6107+
}
6108+
}
60946109
}
60956110
dc = dc->getParent();
60966111
}

test/Concurrency/assume_mainactor_typechecker_errors_sendablecheck.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@ enum CK: CodingKey {
99
case one
1010

1111
func f() { }
12+
13+
struct Nested {
14+
func g() { }
15+
}
1216
}
1317

14-
nonisolated func testCK(x: CK) {
18+
nonisolated func testCK(x: CK, y: CK.Nested) {
1519
x.f() // okay, because CK and CK.f are not @MainActor.
20+
y.g()
1621
}

0 commit comments

Comments
 (0)