Skip to content

Commit d9fefc8

Browse files
authored
Merge pull request #74238 from hborla/6.0-override-isolation
[6.0][Concurrency] Don't ignore mismatching isolation for overrides of Clang-imported superclass methods.
2 parents b2f4e80 + aba6edb commit d9fefc8

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
@@ -2148,7 +2148,11 @@ namespace {
21482148
.limitBehaviorUntilSwiftVersion(behavior, 6);
21492149
}
21502150

2151-
// Always emit the note with fix-it.
2151+
// Overrides cannot be isolated to a global actor; the isolation
2152+
// must match the overridden decl.
2153+
if (fn->getOverriddenDecl())
2154+
return false;
2155+
21522156
fn->diagnose(diag::add_globalactor_to_function,
21532157
globalActor->getWithoutParens().getString(),
21542158
fn, globalActor)
@@ -4925,6 +4929,7 @@ static OverrideIsolationResult validOverrideIsolation(
49254929
ValueDecl *overridden, ActorIsolation overriddenIsolation) {
49264930
ConcreteDeclRef valueRef = getDeclRefInContext(value);
49274931
auto declContext = value->getInnermostDeclContext();
4932+
auto &ctx = declContext->getASTContext();
49284933

49294934
auto refResult = ActorReferenceResult::forReference(
49304935
valueRef, SourceLoc(), declContext, std::nullopt, std::nullopt, isolation,
@@ -4948,8 +4953,10 @@ static OverrideIsolationResult validOverrideIsolation(
49484953

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

49544961
return OverrideIsolationResult::Disallowed;
49554962
}

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
@@ -1466,9 +1466,6 @@ class None {
14661466
// try to add inferred isolation while overriding
14671467
@MainActor
14681468
class MA_None1: None {
1469-
1470-
// FIXME: bad note, since the problem is a mismatch in overridden vs inferred isolation; this wont help.
1471-
// expected-note@+1 {{add '@MainActor' to make instance method 'method()' part of global actor 'MainActor'}}
14721469
override func method() {
14731470
beets_ma() // expected-error {{call to main actor-isolated global function 'beets_ma()' in a synchronous nonisolated context}}
14741471
}

0 commit comments

Comments
 (0)