Skip to content

Commit 1611126

Browse files
committed
[Concurrency] A declaration is only 'nonisolated' if it has an explicit 'nonisolated' attribute.
1 parent d41695a commit 1611126

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4912,6 +4912,13 @@ getIsolationFromConformances(NominalTypeDecl *nominal) {
49124912
if (isa<ProtocolDecl>(nominal))
49134913
return std::nullopt;
49144914

4915+
if (nominal->getAttrs().getAttribute<NonisolatedAttr>()) {
4916+
return InferredActorIsolation {
4917+
getActorIsolationOfContext(nominal->getDeclContext()),
4918+
IsolationSource(nominal, IsolationSource::LexicalContext)
4919+
};
4920+
}
4921+
49154922
std::optional<InferredActorIsolation> foundIsolation;
49164923
for (auto conformance :
49174924
nominal->getLocalConformances(ConformanceLookupKind::NonStructural)) {
@@ -5467,14 +5474,6 @@ InferredActorIsolation ActorIsolationRequest::evaluate(
54675474
value->getAttrs().add(preconcurrency);
54685475
}
54695476

5470-
// If this declaration is non-isolated, we are done.
5471-
auto ctxIsolation = getActorIsolationOfContext(dc);
5472-
if (ctxIsolation.isNonisolated()) {
5473-
return {
5474-
ctxIsolation,
5475-
IsolationSource(dc->getAsDecl(), IsolationSource::LexicalContext)};
5476-
}
5477-
54785477
if (FuncDecl *fd = dyn_cast<FuncDecl>(value)) {
54795478
// Main.main() and Main.$main are implicitly MainActor-protected.
54805479
// Any other isolation is an error.

test/Concurrency/nonisolated_rules.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@ nonisolated struct NonisolatedStruct: GloballyIsolated {
4747
struct Nested {} // 'Nested' is not @MainActor-isolated
4848
}
4949

50+
// expected-note@+1 {{calls to global function 'requireMain()' from outside of its actor context are implicitly asynchronous}}
51+
@MainActor func requireMain() {}
52+
53+
nonisolated struct S1: GloballyIsolated {
54+
var x: NonSendable
55+
func f() {
56+
// expected-error@+1 {{call to main actor-isolated global function 'requireMain()' in a synchronous nonisolated context}}
57+
requireMain()
58+
}
59+
}
60+
5061
// MARK: - Protocols
5162

5263
nonisolated protocol Refined: GloballyIsolated {}

0 commit comments

Comments
 (0)