Skip to content

Presentation different for identical concurrency note #74751

@mattmassicotte

Description

@mattmassicotte

Motivation

I found two situations that are recommending the same thing, but in different ways.

input 1:

var global = 42

diagnostics 1:

test.swift:1:5: error: var 'global' is not concurrency-safe because it is non-isolated global shared mutable state
1 | var global = 42
  |     |- error: var 'global' is not concurrency-safe because it is non-isolated global shared mutable state
  |     |- note: convert 'global' to a 'let' constant to make 'Sendable' shared state immutable
  |     |- note: annotate 'global' with '@MainActor' if property should only be accessed from the main actor
  |     `- note: disable concurrency-safety checks if accesses are protected by an external synchronization mechanism
2 | 

Highlight:

note: annotate 'global' with '@MainActor' if property should only be accessed from the main actor

input 2:

var global = 42

func mutate() {
    global += 1
}

diagnostics 2:

test.swift:4:5: error: main actor-isolated var 'global' can not be mutated from a non-isolated context
1 | var global = 42
  |     `- note: mutation of this var is only permitted within the actor
2 | 
3 | func mutate() {
  |      `- note: add '@MainActor' to make global function 'mutate()' part of global actor 'MainActor'
4 |     global += 1
  |     `- error: main actor-isolated var 'global' can not be mutated from a non-isolated context
5 | }
6 | 

highlight: note: add '@MainActor' to make global function 'mutate()' part of global actor 'MainActor'

Proposed solution

I think the second note seems like its more general-purpose. But, I think including "annotate" does help.

note: annotate 'global' with '@MainActor' if property should only be accessed from the main actor

could become:

note: add '@MainActor' annotation to make property 'global' part of global actor 'MainActor'

Alternatives considered

No response

Additional information

No response

Metadata

Metadata

Assignees

Labels

actor isolationFeature → concurrency: Actor isolationbugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfconcurrencyFeature: umbrella label for concurrency language featuresdiagnostics QoIBug: Diagnostics Quality of Implementationglobal let & varFeature: global constants and variablesgood first issueGood for newcomersswift 6.0type checkerArea → compiler: Semantic analysis

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions