Skip to content

[Concurrency] Two isolation inference changes for default isolation mode. #79044

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
Jan 30, 2025
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
47 changes: 20 additions & 27 deletions lib/Sema/TypeCheckConcurrency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5636,7 +5636,26 @@ InferredActorIsolation ActorIsolationRequest::evaluate(
// within our module, make our default isolation main actor.
if (ctx.LangOpts.hasFeature(Feature::UnspecifiedMeansMainActorIsolated) &&
value->getModuleContext() == ctx.MainModule) {
defaultIsolation = ActorIsolation::forMainActor(ctx);

// Default global actor isolation does not apply to any declarations
// within actors and distributed actors.
bool inActorContext = false;
auto *dc = value->getInnermostDeclContext();
while (dc && !inActorContext) {
if (auto *nominal = dc->getSelfNominalTypeDecl()) {
inActorContext = nominal->isAnyActor();
}
dc = dc->getParent();
}

if (!inActorContext) {
// FIXME: deinit should be implicitly MainActor too.
if (isa<TypeDecl>(value) || isa<ExtensionDecl>(value) ||
isa<AbstractStorageDecl>(value) || isa<FuncDecl>(value) ||
isa<ConstructorDecl>(value)) {
defaultIsolation = ActorIsolation::forMainActor(ctx);
}
}
}

// If we have an async function... by default we inherit isolation.
Expand Down Expand Up @@ -5782,32 +5801,6 @@ InferredActorIsolation ActorIsolationRequest::evaluate(
}
}

// If this is an actor, use the actor isolation of the actor.
if (ctx.LangOpts.hasFeature(Feature::UnspecifiedMeansMainActorIsolated)) {
// non-async inits and deinits need to be always nonisolated since we can
// run the deinit anywhere.
//
// TODO: We should add a check for if they are marked with global actor
// isolation.
if (auto *func = dyn_cast<AbstractFunctionDecl>(value)) {
if (isa<DestructorDecl>(func) && !func->isAsyncContext())
return {ActorIsolation::forNonisolated(false /*unsafe*/),
IsolationSource(func, IsolationSource::LexicalContext)};

if (isa<ConstructorDecl>(func) && !func->isAsyncContext())
return {ActorIsolation::forNonisolated(false /*unsafe*/),
IsolationSource(func, IsolationSource::LexicalContext)};
}

if (auto nominal = dyn_cast<NominalTypeDecl>(value)) {
if (nominal->isActor() && !nominal->isGlobalActor()) {
auto isolation = ActorIsolation::forActorInstanceSelf(value);
return {inferredIsolation(isolation),
IsolationSource(nominal, IsolationSource::LexicalContext)};
}
}
}

// If this is an accessor, use the actor isolation of its storage
// declaration.
if (auto accessor = dyn_cast<AccessorDecl>(value)) {
Expand Down
6 changes: 4 additions & 2 deletions test/Concurrency/Runtime/unspecified_is_main_actor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,17 @@ tests.test("checkIfOnMainQueue crashes off the main queue 2") { @CustomActor ()
class Klass {}

struct MainActorIsolated {
init() {}
init() {
checkIfOnMainQueue()
}

func test() async {
checkIfOnMainQueue()
}
};

tests.test("callNominalType") { @CustomActor () -> () in
let x = MainActorIsolated()
let x = await MainActorIsolated()
// We would crash without hopping here.
await x.test()
}
Expand Down
18 changes: 15 additions & 3 deletions test/Concurrency/assume_mainactor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class Klass {
// Implicit deinit
// CHECK: // Klass.deinit
// CHECK-NEXT: // Isolation: nonisolated
// CHECK-NEXT: // Isolation: unspecified
// CHECK-NEXT: sil hidden [ossa] @$s16assume_mainactor5KlassCfd : $@convention(method) (@guaranteed Klass) -> @owned Builtin.NativeObject {

// Implicit deallocating deinit
Expand Down Expand Up @@ -163,7 +163,7 @@ nonisolated func nonisolatedFunctionTest() async {

actor MyActor {
// CHECK: // variable initialization expression of MyActor.k
// CHECK-NEXT: // Isolation: actor_instance
// CHECK-NEXT: // Isolation: unspecified
// CHECK-NEXT: sil hidden [transparent] [ossa] @$s16assume_mainactor7MyActorC1kAA5KlassCvpfi : $@convention(thin) () -> @owned Klass {

// CHECK: // MyActor.k.getter
Expand All @@ -175,9 +175,21 @@ actor MyActor {
// CHECK-NEXT: sil hidden [transparent] [ossa] @$s16assume_mainactor7MyActorC1kAA5KlassCvs : $@convention(method) (@owned Klass, @sil_isolated @guaranteed MyActor) -> () {
var k = Klass()

// CHECK: // static MyActor.f()
// CHECK-NEXT: // Isolation: unspecified
// CHECK-NEXT: sil hidden [ossa] @$s16assume_mainactor7MyActorC1fyyFZ : $@convention(method) (@thick MyActor.Type) -> ()
static func f() {}

struct Nested {
// CHECK: // MyActor.Nested.f()
// CHECK-NEXT: // Isolation: unspecified
// CHECK-NEXT: sil hidden [ossa] @$s16assume_mainactor7MyActorC6NestedV1fyyF : $@convention(method) (MyActor.Nested) -> ()
func f() {}
}

// Implicit deinit
// CHECK: // MyActor.deinit
// CHECK-NEXT: // Isolation: nonisolated
// CHECK-NEXT: // Isolation: unspecified
// CHECK-NEXT: sil hidden [ossa] @$s16assume_mainactor7MyActorCfd : $@convention(method) (@guaranteed MyActor) -> @owned Builtin.NativeObject {

// Non-async init should be nonisolated
Expand Down
4 changes: 2 additions & 2 deletions test/Concurrency/assume_mainactor_typechecker_errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func unspecifiedFunctionTest2() async {
}

nonisolated func nonisolatedFunctionTest() async {
let k = StructContainingKlass()
let k = await StructContainingKlass()
await unspecifiedAsync(k.k) // expected-error {{non-sendable type 'Klass' of property 'k' cannot exit main actor-isolated context}}
await nonisolatedAsync(k.k) // expected-error {{non-sendable type 'Klass' of property 'k' cannot exit main actor-isolated context}}
await mainActorAsync(k.k) // expected-error {{non-sendable type 'Klass' of property 'k' cannot exit main actor-isolated context}}
Expand All @@ -66,7 +66,7 @@ func testTask() async {

func testTaskDetached() async {
Task.detached {
let k = Klass(getDataFromSocket())
let k = await Klass(getDataFromSocket())
// Have to pop back onto the main thread to do something.
await k.doSomething()
}
Expand Down