Skip to content

Concurrency override actor isolation fixes #35270

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
21 changes: 19 additions & 2 deletions lib/Sema/TypeCheckConcurrency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1752,6 +1752,16 @@ static Optional<ActorIsolation> getIsolationFromWitnessedRequirements(
return std::get<1>(isolatedRequirements.front());
}

// Check whether a declaration is an asynchronous handler.
static bool isAsyncHandler(ValueDecl *value) {
if (auto func = dyn_cast<AbstractFunctionDecl>(value)) {
if (func->isAsyncHandler())
return true;
}

return false;
}

ActorIsolation ActorIsolationRequest::evaluate(
Evaluator &evaluator, ValueDecl *value) const {
// If this declaration has one of the actor isolation attributes, report
Expand Down Expand Up @@ -1807,12 +1817,15 @@ ActorIsolation ActorIsolationRequest::evaluate(
// If the declaration overrides another declaration, it must have the same
// actor isolation.
if (auto overriddenValue = value->getOverriddenDecl()) {
if (auto isolation = getActorIsolation(overriddenValue)) {
// Ignore the overridden declaration's isolation for an async handler,
// because async handlers dispatch to wherever they need to be.
if (!isAsyncHandler(value)) {
auto isolation = getActorIsolation(overriddenValue);
SubstitutionMap subs;
if (auto env = value->getInnermostDeclContext()
->getGenericEnvironmentOfContext()) {
subs = SubstitutionMap::getOverrideSubstitutions(
overriddenValue, value, subs);
overriddenValue, value, subs);
}

return inferredIsolation(isolation.subst(subs));
Expand Down Expand Up @@ -1888,6 +1901,10 @@ void swift::checkOverrideActorIsolation(ValueDecl *value) {
if (!overridden)
return;

// Actor isolation doesn't matter for async handlers.
if (isAsyncHandler(value))
return;

// Determine the actor isolation of this declaration.
auto isolation = getActorIsolation(value);

Expand Down
31 changes: 31 additions & 0 deletions test/Concurrency/global_actor_inference.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,37 @@ struct OtherContainer<U> {
}
}

class SuperclassWithGlobalActors {
@GenericGlobalActor<Int> func f() { }
@GenericGlobalActor<Int> func g() { } // expected-note{{overridden declaration is here}}
func h() { }
func i() { }
func j() { }
}

@GenericGlobalActor<String>
class SubclassWithGlobalActors : SuperclassWithGlobalActors {
override func f() { } // okay: inferred to @GenericGlobalActor<Int>

@GenericGlobalActor<String> override func g() { } // expected-error{{global actor 'GenericGlobalActor<String>'-isolated instance method 'g()' has different actor isolation from global actor 'GenericGlobalActor<Int>'-isolated overridden declaration}}

override func h() { } // okay: inferred to unspecified

func onGenericGlobalActorString() { }
@GenericGlobalActor<Int> func onGenericGlobalActorInt() { }

@asyncHandler @GenericGlobalActor<String>
override func i() { // okay to differ from superclass because it's an asyncHandler.
onGenericGlobalActorString()
}

@asyncHandler
override func j() { // okay, isolated to GenericGlobalActor<String>
onGenericGlobalActorString() // okay
onGenericGlobalActorInt() // expected-error{{call is 'async' but is not marked with 'await'}}
}
}

// ----------------------------------------------------------------------
// Global actor inference for unspecified contexts
// ----------------------------------------------------------------------
Expand Down
7 changes: 0 additions & 7 deletions test/Incremental/Verifier/single-file-private/AnyObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,9 @@ func lookupOnAnyObject(object: AnyObject) { // expected-provides {{lookupOnAnyOb
// expected-member {{Swift.CustomStringConvertible.someMethod}}
// expected-member {{Swift.CustomDebugStringConvertible.someMethod}}
// expected-member {{Swift.Equatable.someMember}}
// expected-member{{Swift.CustomDebugStringConvertible.init}}
// expected-member{{Swift.CVarArg.someMember}}
// expected-member{{Foundation._KeyValueCodingAndObservingPublishing.someMember}}
// expected-member{{Swift.Equatable.init}}
// expected-member{{Swift.Hashable.init}}
// expected-member{{Swift.CVarArg.init}}
// expected-member{{Foundation._KeyValueCodingAndObserving.someMember}}
// expected-member{{Foundation._KeyValueCodingAndObservingPublishing.init}}
// expected-member{{Swift.CustomDebugStringConvertible.someMember}}
// expected-member{{Swift.CustomStringConvertible.someMember}}
// expected-member{{Swift.CustomStringConvertible.init}}
// expected-member{{Swift.Hashable.someMember}}
// expected-member{{Foundation._KeyValueCodingAndObserving.init}}