Skip to content

Commit e417288

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 5db8b8a commit e417288

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
@@ -18,6 +18,7 @@
1818
#include "TypeCheckType.h"
1919
#include "swift/Strings.h"
2020
#include "swift/AST/ASTWalker.h"
21+
#include "swift/AST/GenericEnvironment.h"
2122
#include "swift/AST/Initializer.h"
2223
#include "swift/AST/ParameterList.h"
2324
#include "swift/AST/ProtocolConformance.h"
@@ -2974,6 +2975,19 @@ ActorIsolation ActorIsolationRequest::evaluate(
29742975
// If this declaration has one of the actor isolation attributes, report
29752976
// that.
29762977
if (auto isolationFromAttr = getIsolationFromAttributes(value)) {
2978+
// Nonisolated declarations must involve Sendable types.
2979+
if (*isolationFromAttr == ActorIsolation::Independent) {
2980+
SubstitutionMap subs;
2981+
if (auto genericEnv = value->getInnermostDeclContext()
2982+
->getGenericEnvironmentOfContext()) {
2983+
subs = genericEnv->getForwardingSubstitutionMap();
2984+
}
2985+
diagnoseNonConcurrentTypesInReference(
2986+
ConcreteDeclRef(value, subs),
2987+
value->getDeclContext()->getParentModule(), value->getLoc(),
2988+
ConcurrentReferenceKind::Nonisolated);
2989+
}
2990+
29772991
return *isolationFromAttr;
29782992
}
29792993

lib/Sema/TypeCheckConcurrency.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ enum class ConcurrentReferenceKind {
7878
LocalCapture,
7979
/// Concurrent function
8080
ConcurrentFunction,
81+
/// Nonisolated declaration.
82+
Nonisolated,
8183
};
8284

8385
/// 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)