Skip to content

Commit b06fb74

Browse files
authored
Merge pull request #72147 from hborla/global-concurrency-diagnostics
2 parents d302860 + c0486c3 commit b06fb74

File tree

5 files changed

+21
-9
lines changed

5 files changed

+21
-9
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5541,8 +5541,12 @@ NOTE(shared_mutable_state_decl_note,none,
55415541
"isolate %0 to a global actor, or convert it to a 'let' constant and "
55425542
"conform it to 'Sendable'", (const ValueDecl *))
55435543
ERROR(shared_immutable_state_decl,none,
5544-
"%kind0 is not concurrency-safe because it is not either conforming to "
5545-
"'Sendable' or isolated to a global actor", (const ValueDecl *))
5544+
"%kind0 is not concurrency-safe because non-'Sendable' type %1 may have "
5545+
"shared mutable state",
5546+
(const ValueDecl *, Type))
5547+
NOTE(shared_immutable_state_decl_note,none,
5548+
"isolate %0 to a global actor, or conform %1 to 'Sendable'",
5549+
(const ValueDecl *, Type))
55465550
ERROR(actor_isolated_witness,none,
55475551
"%select{|distributed }0%1 %kind2 cannot be used to satisfy %3 protocol "
55485552
"requirement",

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4908,9 +4908,13 @@ ActorIsolation ActorIsolationRequest::evaluate(
49084908
diagVar = originalVar;
49094909
}
49104910
if (var->isLet()) {
4911-
if (!var->getInterfaceType()->isSendableType()) {
4912-
diagVar->diagnose(diag::shared_immutable_state_decl, diagVar)
4911+
auto type = var->getInterfaceType();
4912+
if (!type->isSendableType()) {
4913+
diagVar->diagnose(diag::shared_immutable_state_decl,
4914+
diagVar, type)
49134915
.warnUntilSwiftVersion(6);
4916+
diagVar->diagnose(diag::shared_immutable_state_decl_note,
4917+
diagVar, type);
49144918
}
49154919
} else {
49164920
diagVar->diagnose(diag::shared_mutable_state_decl, diagVar)
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
struct Foo {
2-
static let member = Bar() // expected-complete-warning {{static property 'member' is not concurrency-safe because it is not either conforming to 'Sendable' or isolated to a global actor; this is an error in the Swift 6 language mode}}
2+
static let member = Bar() // expected-complete-warning {{static property 'member' is not concurrency-safe because non-'Sendable' type 'Bar' may have shared mutable state; this is an error in the Swift 6 language mode}}
3+
// expected-complete-note@-1 {{isolate 'member' to a global actor, or conform 'Bar' to 'Sendable'}}
34
}

test/Concurrency/concurrency_warnings.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ class GlobalCounter {
88
var counter: Int = 0
99
}
1010

11-
let rs = GlobalCounter() // expected-warning {{let 'rs' is not concurrency-safe because it is not either conforming to 'Sendable' or isolated to a global actor; this is an error in the Swift 6 language mode}}
11+
let rs = GlobalCounter() // expected-warning {{let 'rs' is not concurrency-safe because non-'Sendable' type 'GlobalCounter' may have shared mutable state; this is an error in the Swift 6 language mode}}
12+
// expected-note@-1 {{isolate 'rs' to a global actor, or conform 'GlobalCounter' to 'Sendable'}}
1213

1314
var globalInt = 17 // expected-warning {{var 'globalInt' is not concurrency-safe because it is non-isolated global shared mutable state; this is an error in the Swift 6 language mode}}
1415
// expected-note@-1 {{isolate 'globalInt' to a global actor, or convert it to a 'let' constant and conform it to 'Sendable'}}

test/Concurrency/global_variables.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ public struct TestWrapper {
3939

4040
struct TestStatics {
4141
static let immutableExplicitSendable = TestSendable()
42-
static let immutableNonsendable = TestNonsendable() // expected-error{{static property 'immutableNonsendable' is not concurrency-safe because it is not either conforming to 'Sendable' or isolated to a global actor}}
42+
static let immutableNonsendable = TestNonsendable() // expected-error{{static property 'immutableNonsendable' is not concurrency-safe because non-'Sendable' type 'TestNonsendable' may have shared mutable state}}
43+
// expected-note@-1 {{isolate 'immutableNonsendable' to a global actor, or conform 'TestNonsendable' to 'Sendable'}}
4344
static nonisolated(unsafe) let immutableNonisolatedUnsafe = TestNonsendable()
44-
static nonisolated let immutableNonisolated = TestNonsendable() // expected-error{{static property 'immutableNonisolated' is not concurrency-safe because it is not either conforming to 'Sendable' or isolated to a global actor}}
45-
// expected-error@-1 {{'nonisolated' can not be applied to variable with non-'Sendable' type 'TestNonsendable'}}
45+
static nonisolated let immutableNonisolated = TestNonsendable() // expected-error{{static property 'immutableNonisolated' is not concurrency-safe because non-'Sendable' type 'TestNonsendable' may have shared mutable state}}
46+
// expected-note@-1 {{isolate 'immutableNonisolated' to a global actor, or conform 'TestNonsendable' to 'Sendable'}}
47+
// expected-error@-2 {{'nonisolated' can not be applied to variable with non-'Sendable' type 'TestNonsendable'}}
4648
static let immutableInferredSendable = 0
4749
static var mutable = 0 // expected-error{{static property 'mutable' is not concurrency-safe because it is non-isolated global shared mutable state}}
4850
// expected-note@-1{{isolate 'mutable' to a global actor, or convert it to a 'let' constant and conform it to 'Sendable'}}

0 commit comments

Comments
 (0)