Skip to content

[Concurrency] Don't ignore mismatching isolation for overrides of Clang-imported superclass methods. #74237

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
Jun 9, 2024
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
11 changes: 9 additions & 2 deletions lib/Sema/TypeCheckConcurrency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2152,7 +2152,11 @@ namespace {
.limitBehaviorUntilSwiftVersion(behavior, 6);
}

// Always emit the note with fix-it.
// Overrides cannot be isolated to a global actor; the isolation
// must match the overridden decl.
if (fn->getOverriddenDecl())
return false;

fn->diagnose(diag::add_globalactor_to_function,
globalActor->getWithoutParens().getString(),
fn, globalActor)
Expand Down Expand Up @@ -4941,6 +4945,7 @@ static OverrideIsolationResult validOverrideIsolation(
ValueDecl *overridden, ActorIsolation overriddenIsolation) {
ConcreteDeclRef valueRef = getDeclRefInContext(value);
auto declContext = value->getInnermostDeclContext();
auto &ctx = declContext->getASTContext();

auto refResult = ActorReferenceResult::forReference(
valueRef, SourceLoc(), declContext, std::nullopt, std::nullopt, isolation,
Expand All @@ -4964,8 +4969,10 @@ static OverrideIsolationResult validOverrideIsolation(

// If the overridden declaration is from Objective-C with no actor
// annotation, allow it.
if (overridden->hasClangNode() && !overriddenIsolation)
if (ctx.LangOpts.StrictConcurrencyLevel != StrictConcurrency::Complete &&
overridden->hasClangNode() && !overriddenIsolation) {
return OverrideIsolationResult::Allowed;
}

return OverrideIsolationResult::Disallowed;
}
Expand Down
16 changes: 16 additions & 0 deletions test/ClangImporter/objc_isolation_complete.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,19 @@ func unsatisfiedPreconcurrencyIsolation(view: MyView) {
// expected-warning@+1 {{main actor-isolated property 'isVisible' can not be referenced from a non-isolated context}}
_ = view.isVisible
}

@preconcurrency @MainActor
class IsolatedSub: NXSender {
var mainActorState = 0 // expected-note {{property declared here}}
override func sendAny(_: any Sendable) -> any Sendable {
return mainActorState
// expected-warning@-1 {{main actor-isolated property 'mainActorState' can not be referenced from a non-isolated context}}
}

@MainActor
override func sendOptionalAny(_: (any Sendable)?) -> (any Sendable)? {
// expected-warning@-1 {{main actor-isolated instance method 'sendOptionalAny' has different actor isolation from nonisolated overridden declaration; this is an error in the Swift 6 language mode}}

return mainActorState
}
}
3 changes: 0 additions & 3 deletions test/Concurrency/actor_isolation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1492,9 +1492,6 @@ class None {
// try to add inferred isolation while overriding
@MainActor
class MA_None1: None {

// FIXME: bad note, since the problem is a mismatch in overridden vs inferred isolation; this wont help.
// expected-note@+1 {{add '@MainActor' to make instance method 'method()' part of global actor 'MainActor'}}
override func method() {
beets_ma() // expected-error {{call to main actor-isolated global function 'beets_ma()' in a synchronous nonisolated context}}
}
Expand Down