Skip to content

Commit ee2dd82

Browse files
authored
Merge pull request #75235 from rofle100lvl/change_note_main_actor_variables
Change note for non marked with main actor global variables
2 parents dc13137 + 2c1e45a commit ee2dd82

10 files changed

+24
-22
lines changed

include/swift/AST/DiagnosticsSema.def

+1-3
Original file line numberDiff line numberDiff line change
@@ -5344,7 +5344,7 @@ NOTE(note_add_distributed_to_decl,none,
53445344
ERROR(invalid_isolated_calls_in_body,none,
53455345
"calls to '@%0'-isolated' code in %kind1",
53465346
(StringRef, const ValueDecl *))
5347-
NOTE(add_globalactor_to_function,none,
5347+
NOTE(add_globalactor_to_decl,none,
53485348
"add '@%0' to make %kind1 part of global actor %2",
53495349
(StringRef, const ValueDecl *, Type))
53505350
FIXIT(insert_globalactor_attr, "@%0 ", (Type))
@@ -5579,8 +5579,6 @@ ERROR(shared_immutable_state_decl,none,
55795579
NOTE(shared_state_make_immutable,none,
55805580
"convert %0 to a 'let' constant to make 'Sendable' shared state immutable",
55815581
(const ValueDecl *))
5582-
NOTE(shared_state_main_actor_node,none,
5583-
"annotate %0 with '@MainActor' if property should only be accessed from the main actor", (const ValueDecl *))
55845582
NOTE(shared_state_nonisolated_unsafe,none,
55855583
"disable concurrency-safety checks if accesses are protected by an external synchronization mechanism", (const ValueDecl *))
55865584
ERROR(actor_isolated_witness,none,

lib/Sema/TypeCheckConcurrency.cpp

+10-6
Original file line numberDiff line numberDiff line change
@@ -2439,7 +2439,7 @@ namespace {
24392439
if (fn->getOverriddenDecl())
24402440
return false;
24412441

2442-
fn->diagnose(diag::add_globalactor_to_function,
2442+
fn->diagnose(diag::add_globalactor_to_decl,
24432443
globalActor->getWithoutParens().getString(),
24442444
fn, globalActor)
24452445
.fixItInsert(fn->getAttributeInsertionLoc(false),
@@ -5436,7 +5436,7 @@ ActorIsolation ActorIsolationRequest::evaluate(
54365436

54375437
// Diagnose global state that is not either immutable plus Sendable or
54385438
// isolated to a global actor.
5439-
auto checkGlobalIsolation = [var = dyn_cast<VarDecl>(value)](
5439+
auto checkGlobalIsolation = [var = dyn_cast<VarDecl>(value), &ctx](
54405440
ActorIsolation isolation) {
54415441
// Diagnose only declarations in the same module.
54425442
//
@@ -5480,10 +5480,14 @@ ActorIsolation ActorIsolationRequest::evaluate(
54805480
}
54815481
}
54825482

5483-
diagVar->diagnose(diag::shared_state_main_actor_node,
5484-
diagVar)
5485-
.fixItInsert(diagVar->getAttributeInsertionLoc(false),
5486-
"@MainActor ");
5483+
auto mainActor = ctx.getMainActorType();
5484+
if (mainActor) {
5485+
diagVar->diagnose(diag::add_globalactor_to_decl,
5486+
mainActor->getWithoutParens().getString(),
5487+
diagVar, mainActor)
5488+
.fixItInsert(diagVar->getAttributeInsertionLoc(false),
5489+
diag::insert_globalactor_attr, mainActor);
5490+
}
54875491
diagVar->diagnose(diag::shared_state_nonisolated_unsafe,
54885492
diagVar)
54895493
.fixItInsert(diagVar->getAttributeInsertionLoc(true),
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
struct Foo {
22
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 {{annotate 'member' with '@MainActor' if property should only be accessed from the main actor}}
3+
// expected-complete-note@-1 {{add '@MainActor' to make static property 'member' part of global actor 'MainActor'}}
44
// expected-complete-note@-2{{disable concurrency-safety checks if accesses are protected by an external synchronization mechanism}}
55
}

test/Concurrency/concurrency_warnings.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class GlobalCounter { // expected-note{{class 'GlobalCounter' does not conform t
1313

1414
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}}
1515
// expected-note@-1 {{disable concurrency-safety checks if accesses are protected by an external synchronization mechanism}}
16-
// expected-note@-2 {{annotate 'rs' with '@MainActor' if property should only be accessed from the main actor}}
16+
// expected-note@-2 {{add '@MainActor' to make let 'rs' part of global actor 'MainActor'}}
1717

1818
import GlobalVariables
1919

test/Concurrency/concurrent_value_checking.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ typealias BadGenericCF<T> = @Sendable () -> T?
296296
typealias GoodGenericCF<T: Sendable> = @Sendable () -> T? // okay
297297

298298
var concurrentFuncVar: (@Sendable (NotConcurrent) -> Void)? = nil // expected-warning{{var 'concurrentFuncVar' is not concurrency-safe because it is nonisolated global shared mutable state; this is an error in the Swift 6 language mode}}
299-
// expected-note@-1 {{annotate 'concurrentFuncVar' with '@MainActor' if property should only be accessed from the main actor}}
299+
// expected-note@-1 {{add '@MainActor' to make var 'concurrentFuncVar' part of global actor 'MainActor'}}
300300
// expected-note@-2 {{disable concurrency-safety checks if accesses are protected by an external synchronization mechanism}}
301301
// expected-note@-3 {{convert 'concurrentFuncVar' to a 'let' constant to make 'Sendable' shared state immutable}}
302302

test/Concurrency/flow_isolation.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ struct CardboardBox<T> {
520520

521521
@available(SwiftStdlib 5.1, *)
522522
var globalVar: EscapeArtist? // expected-warning {{var 'globalVar' is not concurrency-safe because it is nonisolated global shared mutable state; this is an error in the Swift 6 language mode}}
523-
// expected-note@-1 {{annotate 'globalVar' with '@MainActor' if property should only be accessed from the main actor}}
523+
// expected-note@-1 {{add '@MainActor' to make var 'globalVar' part of global actor 'MainActor'}}
524524
// expected-note@-2 {{disable concurrency-safety checks if accesses are protected by an external synchronization mechanism}}
525525
// expected-note@-3 {{convert 'globalVar' to a 'let' constant to make 'Sendable' shared state immutable}}
526526

test/Concurrency/freestanding_top_level.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// RUN: %target-swift-frontend -concurrency-model=task-to-thread -typecheck -verify -verify-additional-prefix complete- -strict-concurrency=complete %s
33

44
// expected-complete-warning@+4 {{var 'global' is not concurrency-safe because it is nonisolated global shared mutable state; this is an error in the Swift 6 language mode}}
5-
// expected-complete-note@+3 {{annotate 'global' with '@MainActor' if property should only be accessed from the main actor}}{{1-1=@MainActor }}
5+
// expected-complete-note@+3 {{add '@MainActor' to make var 'global' part of global actor 'MainActor'}}{{1-1=@MainActor }}
66
// expected-complete-note@+2 {{disable concurrency-safety checks if accesses are protected by an external synchronization mechanism}}{{1-1=nonisolated(unsafe) }}
77
// expected-complete-note@+1 {{convert 'global' to a 'let' constant to make 'Sendable' shared state immutable}}{{1-4=let}}
88
var global = 10

test/Concurrency/global_variables.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ actor TestGlobalActor {
1515
var mutableIsolatedGlobal = 1
1616

1717
var mutableNonisolatedGlobal = 1 // expected-error{{var 'mutableNonisolatedGlobal' is not concurrency-safe because it is nonisolated global shared mutable state}}
18-
// expected-note@-1{{annotate 'mutableNonisolatedGlobal' with '@MainActor' if property should only be accessed from the main actor}}{{1-1=@MainActor }}
18+
// expected-note@-1{{add '@MainActor' to make var 'mutableNonisolatedGlobal' part of global actor 'MainActor'}}{{1-1=@MainActor }}
1919
// expected-note@-2{{disable concurrency-safety checks if accesses are protected by an external synchronization mechanism}}{{1-1=nonisolated(unsafe) }}
2020
// expected-note@-3{{convert 'mutableNonisolatedGlobal' to a 'let' constant to make 'Sendable' shared state immutable}}{{1-4=let}}
2121

@@ -48,25 +48,25 @@ actor TestActor {
4848
struct TestStatics {
4949
static let immutableExplicitSendable = TestSendable()
5050
static let immutableNonsendable = TestNonsendable() // expected-error{{static property 'immutableNonsendable' is not concurrency-safe because non-'Sendable' type 'TestNonsendable' may have shared mutable state}}
51-
// expected-note@-1 {{annotate 'immutableNonsendable' with '@MainActor' if property should only be accessed from the main actor}}
51+
// expected-note@-1 {{add '@MainActor' to make static property 'immutableNonsendable' part of global actor 'MainActor'}}
5252
// expected-note@-2 {{disable concurrency-safety checks if accesses are protected by an external synchronization mechanism}}
5353
static nonisolated(unsafe) let immutableNonisolatedUnsafe = TestNonsendable()
5454
static nonisolated let immutableNonisolated = TestNonsendable() // expected-error{{static property 'immutableNonisolated' is not concurrency-safe because non-'Sendable' type 'TestNonsendable' may have shared mutable state}}
5555
// expected-note@-1 {{disable concurrency-safety checks if accesses are protected by an external synchronization mechanism}}
5656
// expected-error@-2 {{'nonisolated' can not be applied to variable with non-'Sendable' type 'TestNonsendable'}}
57-
// expected-note@-3{{annotate 'immutableNonisolated' with '@MainActor' if property should only be accessed from the main actor}}
57+
// expected-note@-3{{add '@MainActor' to make static property 'immutableNonisolated' part of global actor 'MainActor'}}
5858
static nonisolated(unsafe) let immutableNonisolatedUnsafeSendable = TestSendable()
5959
// expected-warning@-1 {{'nonisolated(unsafe)' is unnecessary for a constant with 'Sendable' type 'TestSendable', consider removing it}} {{10-30=}}
6060
static let immutableInferredSendable = 0
6161
static var mutable = 0 // expected-error{{static property 'mutable' is not concurrency-safe because it is nonisolated global shared mutable state}}
6262
// expected-note@-1{{convert 'mutable' to a 'let' constant to make 'Sendable' shared state immutable}}
6363
// expected-note@-2{{disable concurrency-safety checks if accesses are protected by an external synchronization mechanism}}
64-
// expected-note@-3{{annotate 'mutable' with '@MainActor' if property should only be accessed from the main actor}}
64+
// expected-note@-3{{add '@MainActor' to make static property 'mutable' part of global actor 'MainActor'}}
6565
static var computedProperty: Int { 0 } // computed property that, though static, has no storage so is not a global
6666
@TestWrapper static var wrapped: Int // expected-error{{static property 'wrapped' is not concurrency-safe because it is nonisolated global shared mutable state}}
6767
// expected-note@-1{{convert 'wrapped' to a 'let' constant to make 'Sendable' shared state immutable}}{{23-26=let}}
6868
// expected-note@-2{{disable concurrency-safety checks if accesses are protected by an external synchronization mechanism}}{{16-16=nonisolated(unsafe) }}
69-
// expected-note@-3{{annotate 'wrapped' with '@MainActor' if property should only be accessed from the main actor}}{{3-3=@MainActor }}
69+
// expected-note@-3{{add '@MainActor' to make static property 'wrapped' part of global actor 'MainActor'}}{{3-3=@MainActor }}
7070
}
7171

7272
public actor TestPublicActor {

test/Concurrency/predates_concurrency_import.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ let nonStrictGlobal = NonStrictClass() // no warning
4444

4545
let strictGlobal = StrictStruct() // expected-warning{{let 'strictGlobal' is not concurrency-safe because non-'Sendable' type 'StrictStruct' may have shared mutable state}}
4646
// expected-note@-1{{disable concurrency-safety checks if accesses are protected by an external synchronization mechanism}}
47-
// expected-note@-2{{annotate 'strictGlobal' with '@MainActor' if property should only be accessed from the main actor}}
47+
// expected-note@-2{{add '@MainActor' to make let 'strictGlobal' part of global actor 'MainActor'}}
4848

4949
extension NonStrictClass {
5050
@Sendable func f() { }
@@ -62,7 +62,7 @@ struct HasStatics {
6262
// expected-warning@-1{{'nonisolated' can not be applied to variable with non-'Sendable' type 'StrictStruct'}}
6363
// expected-warning@-2{{static property 'ss' is not concurrency-safe because non-'Sendable' type 'StrictStruct' may have shared mutable state}}
6464
// expected-note@-3{{disable concurrency-safety checks if accesses are protected by an external synchronization mechanism}}
65-
// expected-note@-4{{annotate 'ss' with '@MainActor' if property should only be accessed from the main actor}}
65+
// expected-note@-4{{add '@MainActor' to make static property 'ss' part of global actor 'MainActor'}}
6666
}
6767

6868
extension NonStrictClass2: @retroactive MySendableProto { }

test/Concurrency/predates_concurrency_import_swift6.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func test(ss: StrictStruct, ns: NonStrictClass) {
1818

1919
let nonStrictGlobal = NonStrictClass()
2020
let strictGlobal = StrictStruct() // expected-warning{{let 'strictGlobal' is not concurrency-safe because non-'Sendable' type 'StrictStruct' may have shared mutable state}}
21-
// expected-note@-1{{annotate 'strictGlobal' with '@MainActor' if property should only be accessed from the main actor}}
21+
// expected-note@-1{{add '@MainActor' to make let 'strictGlobal' part of global actor 'MainActor'}}
2222
// expected-note@-2{{disable concurrency-safety checks if accesses are protected by an external synchronization mechanism}}
2323

2424
extension NonStrictClass {

0 commit comments

Comments
 (0)