Fix two bugs with the isolation of defer bodies.

The first bug is that we weren't computing isolation correctly for
nested defers. This is an unlikely pattern of code, but it's good to fix.

The second bug is that getActorIsolationOfContext was looking through
defers, but getActorIsolation itself was not. This was causing defer
bodies to be emitted in SILGen without an isolation parameter, which
meant that #isolation could not possibly provide the right value. Fixing
this involves teaching SILGen that non-async functions can have
nonisolated(nonsending) isolation, but that's relatively straightforward.

This commit doesn't fix #isolation or adequately test SILGen, but that'll
be handled in a follow-up.
This commit is contained in:
John McCall
2025-09-09 11:53:46 -04:00
parent 5fb314b475
commit 367520cd3f
4 changed files with 36 additions and 5 deletions

View File

@@ -3285,6 +3285,9 @@ public:
/// `AbstractStorageDecl`, returns `false`.
bool isAsync() const;
/// Returns whether this function represents a defer body.
bool isDeferBody() const;
private:
bool isObjCDynamic() const {
return isObjC() && isDynamic();

View File

@@ -11879,6 +11879,12 @@ PrecedenceGroupDecl *InfixOperatorDecl::getPrecedenceGroup() const {
nullptr);
}
bool ValueDecl::isDeferBody() const {
if (auto fn = dyn_cast<FuncDecl>(this))
return fn->isDeferBody();
return false;
}
bool FuncDecl::isDeferBody() const {
return getBaseIdentifier() == getASTContext().getIdentifier("$defer");
}
@@ -12072,12 +12078,16 @@ ActorIsolation swift::getActorIsolationOfContext(
getClosureActorIsolation) {
auto &ctx = dc->getASTContext();
auto dcToUse = dc;
// Defer bodies share actor isolation of their enclosing context.
if (auto FD = dyn_cast<FuncDecl>(dcToUse)) {
if (FD->isDeferBody()) {
dcToUse = FD->getDeclContext();
}
// Defer bodies share the actor isolation of their enclosing context.
// We don't actually have to do this check here because
// getActorIsolation does consider it already, but it's nice to
// avoid some extra request evaluation in a trivial case.
while (auto FD = dyn_cast<FuncDecl>(dcToUse)) {
if (!FD->isDeferBody()) break;
dcToUse = FD->getDeclContext();
}
if (auto *vd = dyn_cast_or_null<ValueDecl>(dcToUse->getAsDecl()))
return getActorIsolation(vd);

View File

@@ -6353,6 +6353,14 @@ static bool shouldSelfIsolationOverrideDefault(
static InferredActorIsolation computeActorIsolation(Evaluator &evaluator,
ValueDecl *value) {
// Defer bodies share the actor isolation of their enclosing context.
if (value->isDeferBody()) {
return {
getActorIsolationOfContext(value->getDeclContext()),
IsolationSource()
};
}
// If this declaration has actor-isolated "self", it's isolated to that
// actor.
if (evaluateOrDefault(evaluator, HasIsolatedSelfRequest{value}, false)) {

View File

@@ -36,6 +36,16 @@ func testNonDefer_negative() {
// CHECK-NEXT: function_ref
// CHECK-NEXT: apply
@MainActor func testGlobalActor_nested_positive() {
defer {
defer {
requiresMainActor()
}
doSomething()
}
doSomething()
}
#if NEGATIVES
// expected-note @+1 {{add '@MainActor' to make global function 'testGlobalActor_negative()' part of global actor 'MainActor'}}
func testGlobalActor_negative() {