Skip to content

Commit 382a8e7

Browse files
authored
Merge pull request #74237 from hborla/override-isolation
[Concurrency] Don't ignore mismatching isolation for overrides of Clang-imported superclass methods.
2 parents 5b81405 + 128a8bc commit 382a8e7

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2152,7 +2152,11 @@ namespace {
21522152
.limitBehaviorUntilSwiftVersion(behavior, 6);
21532153
}
21542154

2155-
// Always emit the note with fix-it.
2155+
// Overrides cannot be isolated to a global actor; the isolation
2156+
// must match the overridden decl.
2157+
if (fn->getOverriddenDecl())
2158+
return false;
2159+
21562160
fn->diagnose(diag::add_globalactor_to_function,
21572161
globalActor->getWithoutParens().getString(),
21582162
fn, globalActor)
@@ -4941,6 +4945,7 @@ static OverrideIsolationResult validOverrideIsolation(
49414945
ValueDecl *overridden, ActorIsolation overriddenIsolation) {
49424946
ConcreteDeclRef valueRef = getDeclRefInContext(value);
49434947
auto declContext = value->getInnermostDeclContext();
4948+
auto &ctx = declContext->getASTContext();
49444949

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

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

49704977
return OverrideIsolationResult::Disallowed;
49714978
}

test/ClangImporter/objc_isolation_complete.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,19 @@ func unsatisfiedPreconcurrencyIsolation(view: MyView) {
1515
// expected-warning@+1 {{main actor-isolated property 'isVisible' can not be referenced from a non-isolated context}}
1616
_ = view.isVisible
1717
}
18+
19+
@preconcurrency @MainActor
20+
class IsolatedSub: NXSender {
21+
var mainActorState = 0 // expected-note {{property declared here}}
22+
override func sendAny(_: any Sendable) -> any Sendable {
23+
return mainActorState
24+
// expected-warning@-1 {{main actor-isolated property 'mainActorState' can not be referenced from a non-isolated context}}
25+
}
26+
27+
@MainActor
28+
override func sendOptionalAny(_: (any Sendable)?) -> (any Sendable)? {
29+
// 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}}
30+
31+
return mainActorState
32+
}
33+
}

test/Concurrency/actor_isolation.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,9 +1492,6 @@ class None {
14921492
// try to add inferred isolation while overriding
14931493
@MainActor
14941494
class MA_None1: None {
1495-
1496-
// FIXME: bad note, since the problem is a mismatch in overridden vs inferred isolation; this wont help.
1497-
// expected-note@+1 {{add '@MainActor' to make instance method 'method()' part of global actor 'MainActor'}}
14981495
override func method() {
14991496
beets_ma() // expected-error {{call to main actor-isolated global function 'beets_ma()' in a synchronous nonisolated context}}
15001497
}

0 commit comments

Comments
 (0)