Skip to content

Commit 20a10c6

Browse files
committed
[SE-0466] Treat explicit "nonisolated" like implicit "nonisolated" on protocols
Make explicit "nonisolated" also not special on protocols, so a nonisolated protocol does not suppress default isolation. SendableMetatype is the proper way to suppress default isolation for a protocol. Unfortunately, these rules made it appear like issue #82168 was fixed, when in fact it was not. Keep the test case, but as a failing test, and we'll investigate separately.
1 parent 5abbf2e commit 20a10c6

File tree

3 files changed

+87
-33
lines changed

3 files changed

+87
-33
lines changed

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5301,6 +5301,33 @@ getIsolationFromAttributes(const Decl *decl, bool shouldDiagnose = true,
53015301
llvm_unreachable("Forgot about an attribute?");
53025302
}
53035303

5304+
/// Determine the default isolation for the given declaration context.
5305+
static DefaultIsolation getDefaultIsolationForContext(const DeclContext *dc) {
5306+
// Check whether there is a file-specific setting.
5307+
if (auto *sourceFile = dc->getParentSourceFile()) {
5308+
if (auto defaultIsolationInFile = sourceFile->getDefaultIsolation())
5309+
return defaultIsolationInFile.value();
5310+
}
5311+
5312+
// If we're in the main module, check the language option.
5313+
ASTContext &ctx = dc->getASTContext();
5314+
if (dc->getParentModule() == ctx.MainModule)
5315+
return ctx.LangOpts.DefaultIsolationBehavior;
5316+
5317+
// Otherwise, default to nonisolated.
5318+
return DefaultIsolation::Nonisolated;
5319+
}
5320+
5321+
/// Determines whether explicit 'nonisolated' is different from 'unspecified'
5322+
/// in the given context.
5323+
static bool explicitNonisolatedIsSpecial(const DeclContext *dc) {
5324+
ASTContext &ctx = dc->getASTContext();
5325+
if (ctx.LangOpts.hasFeature(Feature::NoExplicitNonIsolated))
5326+
return false;
5327+
5328+
return getDefaultIsolationForContext(dc) == DefaultIsolation::Nonisolated;
5329+
}
5330+
53045331
/// Infer isolation from witnessed protocol requirements.
53055332
static std::optional<InferredActorIsolation>
53065333
getIsolationFromWitnessedRequirements(ValueDecl *value) {
@@ -5317,11 +5344,13 @@ getIsolationFromWitnessedRequirements(ValueDecl *value) {
53175344
if (dc->getSelfProtocolDecl())
53185345
return std::nullopt;
53195346

5320-
// Prevent isolation inference from requirements if the conforming type
5321-
// has an explicit `nonisolated` attribute.
5322-
if (auto *NTD = dc->getSelfNominalTypeDecl()) {
5323-
if (NTD->getAttrs().hasAttribute<NonisolatedAttr>())
5324-
return std::nullopt;
5347+
if (explicitNonisolatedIsSpecial(dc)) {
5348+
// Prevent isolation inference from requirements if the conforming type
5349+
// has an explicit `nonisolated` attribute.
5350+
if (auto *NTD = dc->getSelfNominalTypeDecl()) {
5351+
if (NTD->getAttrs().hasAttribute<NonisolatedAttr>())
5352+
return std::nullopt;
5353+
}
53255354
}
53265355

53275356
// Walk through each of the conformances in this context, collecting any
@@ -5379,8 +5408,13 @@ getIsolationFromWitnessedRequirements(ValueDecl *value) {
53795408
}
53805409

53815410
case ActorIsolation::GlobalActor:
5411+
break;
5412+
53825413
case ActorIsolation::Nonisolated:
53835414
case ActorIsolation::NonisolatedUnsafe:
5415+
if (!explicitNonisolatedIsSpecial(requirement->getDeclContext()))
5416+
continue;
5417+
53845418
break;
53855419
}
53865420

@@ -5488,7 +5522,8 @@ getIsolationFromConformances(NominalTypeDecl *nominal) {
54885522
case ActorIsolation::NonisolatedUnsafe:
54895523
break;
54905524
case ActorIsolation::Nonisolated:
5491-
if (inferredIsolation.source.kind == IsolationSource::Kind::Explicit) {
5525+
if (inferredIsolation.source.kind == IsolationSource::Kind::Explicit &&
5526+
explicitNonisolatedIsSpecial(nominal)) {
54925527
if (!foundIsolation) {
54935528
// We found an explicitly 'nonisolated' protocol.
54945529
foundIsolation = {
@@ -6109,23 +6144,6 @@ static bool sendableConformanceRequiresNonisolated(NominalTypeDecl *nominal) {
61096144
return requiresNonisolated;
61106145
}
61116146

6112-
/// Determine the default isolation for the given declaration context.
6113-
static DefaultIsolation getDefaultIsolationForContext(const DeclContext *dc) {
6114-
// Check whether there is a file-specific setting.
6115-
if (auto *sourceFile = dc->getParentSourceFile()) {
6116-
if (auto defaultIsolationInFile = sourceFile->getDefaultIsolation())
6117-
return defaultIsolationInFile.value();
6118-
}
6119-
6120-
// If we're in the main module, check the language option.
6121-
ASTContext &ctx = dc->getASTContext();
6122-
if (dc->getParentModule() == ctx.MainModule)
6123-
return ctx.LangOpts.DefaultIsolationBehavior;
6124-
6125-
// Otherwise, default to nonisolated.
6126-
return DefaultIsolation::Nonisolated;
6127-
}
6128-
61296147
/// Determine the default isolation and isolation source for this declaration,
61306148
/// which may still be overridden by other inference rules.
61316149
static std::tuple<InferredActorIsolation, ValueDecl *,
@@ -6321,12 +6339,9 @@ static bool shouldSelfIsolationOverrideDefault(
63216339
if (isa<NominalTypeDecl>(dc))
63226340
return true;
63236341

6324-
// The NoExplicitNonIsolated feature
6325-
if (ctx.LangOpts.hasFeature(Feature::NoExplicitNonIsolated))
6326-
return false;
6327-
6328-
// Suppress when the default isolation is nonisolated.
6329-
return getDefaultIsolationForContext(dc) == DefaultIsolation::Nonisolated;
6342+
/// Only allow explicit nonisolated to override the default when it's
6343+
/// treated as special.
6344+
return explicitNonisolatedIsSpecial(dc);
63306345
}
63316346
}
63326347

test/Concurrency/assume_mainactor_typechecker_errors.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,33 @@ extension MyOtherStruct {
116116
// expected-swift6-error@-1{{call to main actor-isolated instance method 'memberOfInt()' in a synchronous nonisolated context}}
117117
}
118118
}
119+
120+
nonisolated protocol P {
121+
func g()
122+
}
123+
124+
struct MyP: P {
125+
func g() {
126+
17.memberOfInt() // okay, on main actor
127+
}
128+
}
129+
130+
// https://github.com/swiftlang/swift/issues/82168 -
131+
nonisolated protocol OtherP {
132+
associatedtype AT
133+
static var at: AT { get }
134+
}
135+
136+
nonisolated struct KP<R: OtherP, V> {
137+
init(keyPath: KeyPath<R, V>) {}
138+
}
139+
140+
struct S: OtherP {
141+
let p: Int
142+
struct AT {
143+
let kp = KP(keyPath: \S.p)
144+
}
145+
146+
// FIXME: This should not be an error.
147+
static let at = AT() // expected-swift6-error{{'S.AT' cannot be constructed because it has no accessible initializers}}
148+
}

test/Concurrency/isolated_conformance_default_actor.swift

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
// REQUIRES: concurrency
44

5+
@MainActor func onMain() { }
6+
57
nonisolated
68
protocol P {
79
func f()
@@ -12,8 +14,14 @@ protocol Q {
1214
func g()
1315
}
1416

17+
// expected-note@+4{{turn data races into runtime errors with '@preconcurrency'}}
18+
// expected-note@+3{{isolate this conformance to the main actor with '@MainActor'}}
19+
// expected-note@+2{{mark all declarations used in the conformance 'nonisolated'}}
20+
// expected-error@+1{{conformance of 'CImplicitMainActorNonisolatedConformance' to protocol 'P' crosses into main actor-isolated code and can cause data races}}
1521
class CImplicitMainActorNonisolatedConformance: nonisolated P {
16-
func f() { } // error: explicitly nonisolated conformance
22+
func f() { // expected-note{{main actor-isolated instance method 'f()' cannot satisfy nonisolated requirement}}
23+
onMain() // okay, f is on @MainActor
24+
}
1725
}
1826

1927

@@ -73,9 +81,6 @@ func acceptSendablePMeta<T: Sendable & P>(_: T.Type) { }
7381
func acceptSendableQMeta<T: Sendable & Q>(_: T.Type) { }
7482

7583
nonisolated func testConformancesFromNonisolated() {
76-
let _: any P = CExplicitMainActor() // okay
77-
let _: any P = CImplicitMainActor() // okay
78-
7984
let _: any P = CNonIsolated()
8085
let _: any P = CImplicitMainActorNonisolatedConformance()
8186

@@ -84,6 +89,10 @@ nonisolated func testConformancesFromNonisolated() {
8489
let _: any Q = CImplicitMainActor()
8590

8691
// Error, these are main-actor-isolated conformances
92+
let _: any P = CExplicitMainActor() // expected-error{{main actor-isolated conformance of 'CExplicitMainActor' to 'P' cannot be used in nonisolated context}}
93+
let _: any P = CImplicitMainActor() // expected-error{{main actor-isolated conformance of 'CImplicitMainActor' to 'P' cannot be used in nonisolated context}}
94+
95+
8796
let _: any Equatable.Type = EquatableStruct.self // expected-error{{main actor-isolated conformance of 'EquatableStruct' to 'Equatable' cannot be used in nonisolated context}}
8897
let _: any Hashable.Type = HashableStruct.self // expected-error{{main actor-isolated conformance of 'HashableStruct' to 'Hashable' cannot be used in nonisolated context}}
8998
let _: any RawRepresentable.Type = RawRepresentableEnum.self

0 commit comments

Comments
 (0)