File: grouped_actor_isolation_diagnostics.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (136 lines) | stat: -rw-r--r-- 6,648 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// RUN: %target-typecheck-verify-swift -disable-availability-checking -enable-experimental-feature GroupActorErrors  -strict-concurrency=complete
// REQUIRES: concurrency

@MainActor
protocol P {
  func f()
  nonisolated func g()
}

@preconcurrency @MainActor
protocol Q {
  func f()
  nonisolated func g()
}

struct S: P {
  func f() { }
  func g() { }
}

@preconcurrency
struct NonConcurrentS: Q {
  func f() { }
  func g() { }
}

// expected-note@+1{{add '@MainActor' to make global function 'testP(s:p:)' part of global actor 'MainActor'}}
func testP(s: S, p: P) { // expected-error {{calls to '@MainActor'-isolated' code in global function 'testP(s:p:)'}}
  p.f() // expected-note{{call to main actor-isolated instance method 'f()' in a synchronous nonisolated context}}
  p.f() // expected-note{{call to main actor-isolated instance method 'f()' in a synchronous nonisolated context}}
  p.f() // expected-note{{call to main actor-isolated instance method 'f()' in a synchronous nonisolated context}}
  p.g() // OKAY
  s.f() // expected-note{{call to main actor-isolated instance method 'f()' in a synchronous nonisolated context}}
  s.f() // expected-note{{call to main actor-isolated instance method 'f()' in a synchronous nonisolated context}}
  s.f() // expected-note{{call to main actor-isolated instance method 'f()' in a synchronous nonisolated context}}
  s.g() // OKAY
}
// expected-note @+1{{add '@MainActor' to make global function 'testPreconcurrency(ncs:s:)' part of global actor 'MainActor'}}
func testPreconcurrency(ncs: NonConcurrentS, s:S ) { // expected-error {{calls to '@MainActor'-isolated' code in global function 'testPreconcurrency(ncs:s:)'}}
  ncs.f() // expected-note{{call to main actor-isolated instance method 'f()' in a synchronous nonisolated context}}
  ncs.f() // expected-note{{call to main actor-isolated instance method 'f()' in a synchronous nonisolated context}}
  ncs.f() // expected-note{{call to main actor-isolated instance method 'f()' in a synchronous nonisolated context}}
  ncs.g() // OKAY
  s.f() // expected-note{{call to main actor-isolated instance method 'f()' in a synchronous nonisolated context}}
  s.f() // expected-note{{call to main actor-isolated instance method 'f()' in a synchronous nonisolated context}}
  s.f() // expected-note{{call to main actor-isolated instance method 'f()' in a synchronous nonisolated context}}
  s.g() // OKAY
}

// expected-note @+1{{add '@MainActor' to make global function 'testOnlyPreconcurrency(ncs:)' part of global actor 'MainActor'}}
func testOnlyPreconcurrency(ncs: NonConcurrentS) { // expected-warning {{calls to '@MainActor'-isolated' code in global function 'testOnlyPreconcurrency(ncs:)'}}
  ncs.f() // expected-note{{call to main actor-isolated instance method 'f()' in a synchronous nonisolated context}}
  ncs.f() // expected-note{{call to main actor-isolated instance method 'f()' in a synchronous nonisolated context}}
  ncs.f() // expected-note{{call to main actor-isolated instance method 'f()' in a synchronous nonisolated context}}
  ncs.g() // OKAY
}

actor SomeActor { }

@globalActor
struct SomeGlobalActor {
  static let shared = SomeActor()
}

@propertyWrapper
struct WrapperOnActor<Wrapped: Sendable> {
  private var stored: Wrapped

  nonisolated init(wrappedValue: Wrapped) {
    stored = wrappedValue
  }

  @MainActor var wrappedValue: Wrapped {
    get { }
    set { }
  }

  @SomeGlobalActor var projectedValue: Wrapped {
    get { }
    set { }
  }
}

struct HasWrapperOnActor {
  @WrapperOnActor var x: Int = 0
  @WrapperOnActor var y: String = ""
  @WrapperOnActor var z: (Double, Double) = (1.0,2.0)

  // expected-error@+2{{calls to '@MainActor'-isolated' code in instance method 'testWrapped()'}}
  // expected-note@+1{{add '@MainActor' to make instance method 'testWrapped()' part of global actor 'MainActor'}}
  func testWrapped() {
    _ = x // expected-note{{main actor-isolated property 'x' can not be referenced from a nonisolated context}}
    _ = y // expected-note{{main actor-isolated property 'y' can not be referenced from a nonisolated context}}
    _ = z // expected-note{{main actor-isolated property 'z' can not be referenced from a nonisolated context}}
  }

  // expected-error@+2{{calls to '@SomeGlobalActor'-isolated' code in instance method 'testProjected()'}}
  // expected-note@+1{{add '@SomeGlobalActor' to make instance method 'testProjected()' part of global actor 'SomeGlobalActor'}}
  func testProjected(){
    _ = $x // expected-note{{global actor 'SomeGlobalActor'-isolated property '$x' can not be referenced from a nonisolated context}}
    _ = $y // expected-note{{global actor 'SomeGlobalActor'-isolated property '$y' can not be referenced from a nonisolated context}}
    _ = $z // expected-note{{global actor 'SomeGlobalActor'-isolated property '$z' can not be referenced from a nonisolated context}}
  }

  @MainActor
  func testMA(){ }

  // expected-note@+1{{add '@MainActor' to make instance method 'testErrors()' part of global actor 'MainActor'}}
  func testErrors() {  // expected-error{{calls to '@MainActor'-isolated' code in instance method 'testErrors()'}}
    testMA() // expected-error{{call to main actor-isolated instance method 'testMA()' in a synchronous nonisolated context}}
  }
}

@preconcurrency @MainActor
class MainActorPreconcurrency {}

class InferMainActorPreconcurrency: MainActorPreconcurrency {
  static func predatesConcurrency() {}
  func predatesConcurrency (s: String) -> String { return s }
  func predatesConcurrency (n: Int) -> Int  { return n }
}

nonisolated func testPreconcurrency() {
  InferMainActorPreconcurrency.predatesConcurrency()
  // expected-warning@-1 {{call to main actor-isolated static method 'predatesConcurrency()' in a synchronous nonisolated context}}
}

func testPreconcurrencyGrouped() {  // expected-warning {{calls to '@MainActor'-isolated' code in global function 'testPreconcurrencyGrouped()'}}
  // expected-note@-1 {{add '@MainActor' to make global function 'testPreconcurrencyGrouped()' part of global actor 'MainActor'}}
  InferMainActorPreconcurrency.predatesConcurrency()
  // expected-note@-1 {{call to main actor-isolated static method 'predatesConcurrency()' in a synchronous nonisolated context}}
  let _ = InferMainActorPreconcurrency().predatesConcurrency(s:"swift 6")
  // expected-note@-1 {{call to main actor-isolated instance method 'predatesConcurrency(s:)' in a synchronous nonisolated context}}
  let _ = InferMainActorPreconcurrency().predatesConcurrency(n:4)
  // expected-note@-1 {{call to main actor-isolated instance method 'predatesConcurrency(n:)' in a synchronous nonisolated context}}
}