Skip to content

Commit 944f521

Browse files
committed
Require the types of 'nonisolated' declarations to be Sendable.
A nonisolated declaration is accessible concurrently, so it must be of Sendable type to be safe. This plugs a hole in data-race preventation that will need to be documented in an amendment to SE-0313.
1 parent c2b1cee commit 944f521

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "TypeCheckType.h"
2020
#include "swift/Strings.h"
2121
#include "swift/AST/ASTWalker.h"
22+
#include "swift/AST/GenericEnvironment.h"
2223
#include "swift/AST/Initializer.h"
2324
#include "swift/AST/ParameterList.h"
2425
#include "swift/AST/ProtocolConformance.h"
@@ -3032,6 +3033,19 @@ ActorIsolation ActorIsolationRequest::evaluate(
30323033
// If this declaration has one of the actor isolation attributes, report
30333034
// that.
30343035
if (auto isolationFromAttr = getIsolationFromAttributes(value)) {
3036+
// Nonisolated declarations must involve Sendable types.
3037+
if (*isolationFromAttr == ActorIsolation::Independent) {
3038+
SubstitutionMap subs;
3039+
if (auto genericEnv = value->getInnermostDeclContext()
3040+
->getGenericEnvironmentOfContext()) {
3041+
subs = genericEnv->getForwardingSubstitutionMap();
3042+
}
3043+
diagnoseNonConcurrentTypesInReference(
3044+
ConcreteDeclRef(value, subs),
3045+
value->getDeclContext()->getParentModule(), value->getLoc(),
3046+
ConcurrentReferenceKind::Nonisolated);
3047+
}
3048+
30353049
return *isolationFromAttr;
30363050
}
30373051

lib/Sema/TypeCheckConcurrency.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ enum class ConcurrentReferenceKind {
8181
LocalCapture,
8282
/// Concurrent function
8383
ConcurrentFunction,
84+
/// Nonisolated declaration.
85+
Nonisolated,
8486
};
8587

8688
/// The isolation restriction in effect for a given declaration that is

test/Concurrency/concurrent_value_checking.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,14 @@ func testKeyPaths(dict: [NC: Int], nc: NC) {
156156
_ = \HasNC.dict[nc] // expected-warning{{cannot form key path that captures non-sendable type 'NC'}}
157157
}
158158

159+
// ----------------------------------------------------------------------
160+
// Sendable restriction on nonisolated declarations.
161+
// ----------------------------------------------------------------------
162+
actor ANI {
163+
// FIXME: improve diagnostics to talk about nonisolated
164+
nonisolated let nc = NC() // expected-warning{{cannot use property 'nc' with a non-sendable type 'NC' across actors}}
165+
nonisolated func f() -> NC? { nil } // expected-warning{{cannot call function returning non-sendable type 'NC?' across actors}}
166+
}
159167

160168
// ----------------------------------------------------------------------
161169
// Sendable restriction on conformances.

0 commit comments

Comments
 (0)