File: coroutine_accessors_default_implementations.swift

package info (click to toggle)
swiftlang 6.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,791,532 kB
  • sloc: cpp: 9,901,743; ansic: 2,201,431; asm: 1,091,827; python: 308,252; objc: 82,166; f90: 80,126; lisp: 38,358; pascal: 25,559; sh: 20,429; ml: 5,058; perl: 4,745; makefile: 4,484; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (258 lines) | stat: -rw-r--r-- 7,068 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// RUN: %empty-directory(%t)

// RUN: split-file %s %t

// RUN: %target-swift-frontend \
// RUN:     %t/Library.swift   \
// RUN:     %t/LibImpl.underscored.swift   \
// RUN:     -emit-module       \
// RUN:     -module-name Library \
// RUN:     -parse-as-library  \
// RUN:     -enable-library-evolution \
// RUN:     -emit-module-path %t/Library.swiftmodule \
// RUN:     -validate-tbd-against-ir=none

// RUN: %target-swift-frontend \
// RUN:     %t/Executable.swift \
// RUN:     -c \
// RUN:     -parse-as-library \
// RUN:     -module-name Executable \
// RUN:     -I %t \
// RUN:     -o %t/Executable.o \
// RUN:     -validate-tbd-against-ir=none

// RUN: %target-build-swift-dylib(%t/%target-library-name(Library)) \
// RUN:     %t/Library.swift \
// RUN:     %t/LibImpl.nonunderscored.swift   \
// RUN:     -emit-module \
// RUN:     -enable-library-evolution \
// RUN:     -enable-experimental-feature CoroutineAccessors \
// RUN:     -emit-module-path %t/Library.swiftmodule \
// RUN:     -module-name Library \
// RUN:     -Xfrontend -validate-tbd-against-ir=none

// RUN: %target-build-swift \
// RUN:     %t/Executable.o \
// RUN:     -lLibrary \
// RUN:     -L %t \
// RUN:     %target-rpath(%t) \
// RUN:     -o %t/main

// RUN: %target-codesign %t/main %t/%target-library-name(Library)
// RUN: %target-run %t/main %t/%target-library-name(Library) | %FileCheck %s

// REQUIRES: swift_feature_CoroutineAccessors
// REQUIRES: executable_test

//--- Library.swift
public protocol P {
  @_borrowed
  var i : Int { get set }
  @_borrowed
  subscript(i : Int) -> Int { get set }
}

public func readFromSomeP<T : P>(_ p: T) {
  print(#function, "p.i", p.i)
  print(#function, "p[5]", p[5])
}

public func increment(int: inout Int) {
  print(#function, "before increment", int)
  int += 1
  print(#function, "after increment", int)
}

public func modifySomeP<T : P>(_ p: inout T) {
  print(#function, "begin")
  increment(int: &p.i)
  increment(int: &p[5])
  print(#function, "end")
}

public func getSomeP() -> some P {
  return I()
}

public func getAnyP() -> any P {
  return I()
}

//--- LibImpl.underscored.swift

struct I : P {
  var i : Int = 0
  subscript(i : Int) -> Int { 
    _read {
      let i = 0
      yield i
    }
    _modify {
      var i = 0
      yield &i
    }
  }
}

//--- LibImpl.nonunderscored.swift

struct I : P {
  var _iImpl : Int
  var i : Int {
    read {
      print(#function, "before yield", _iImpl)
      yield _iImpl
      print(#function, "after yield", _iImpl)
    }
    modify {
      print(#function, "before yield", _iImpl)
      yield &_iImpl
      print(#function, "after yield", _iImpl)
    }
  }
  var _sImpl: [Int]
  subscript(i : Int) -> Int {
    read {
      print(#function, "before yield", _sImpl[i])
      yield _sImpl[i]
      print(#function, "after yield", _sImpl[i])
    }
    modify {
      print(#function, "before yield", _sImpl[i])
      yield &_sImpl[i]
      print(#function, "after yield", _sImpl[i])
    }
  }
  init() {
    self._iImpl = 42
    self._sImpl = [1, 1, 2, 3, 5, 8, 13, 21]
  }
}

//--- Executable.swift
import Library

public struct S : P {
  var _iImpl : Int
  public var i : Int {
    _read {
      print(#function, "before yield", _iImpl)
      yield _iImpl
      print(#function, "after yield", _iImpl)
    }
    _modify {
      print(#function, "before yield", _iImpl)
      yield &_iImpl
      print(#function, "after yield", _iImpl)
    }
  }
  var _sImpl: [Int]
  public subscript(i : Int) -> Int {
    _read {
      print(#function, "before yield", _sImpl[i])
      yield _sImpl[i]
      print(#function, "after yield", _sImpl[i])
    }
    _modify {
      print(#function, "before yield", _sImpl[i])
      yield &_sImpl[i]
      print(#function, "after yield", _sImpl[i])
    }
  }
  init() {
    self._iImpl = 42
    self._sImpl = [1, 1, 2, 3, 5, 8, 13, 21]
  }
}

func readFromSomePLocal<T : P>(_ p: T) {
  print(#function, "p.i", p.i)
  print(#function, "p[5]", p[5])
}

func modifySomePLocal<T : P>(_ p: inout T) {
  print(#function, "begin")
  increment(int: &p.i)
  increment(int: &p[5])
  print(#function, "end")
}

@main struct M {
  static func main() {
    var s = S()
// CHECK:      i before yield 42
// CHECK-NEXT: i after yield 42
// CHECK-NEXT: readFromSomeP(_:) p.i 42
// CHECK-NEXT: subscript(_:) before yield 8
// CHECK-NEXT: subscript(_:) after yield 8
// CHECK-NEXT: readFromSomeP(_:) p[5] 8
    readFromSomeP(s)
// CHECK-NEXT: modifySomeP(_:) begin
// CHECK-NEXT: i before yield 42
// CHECK-NEXT: increment(int:) before increment 42
// CHECK-NEXT: increment(int:) after increment 43
// CHECK-NEXT: i after yield 43
// CHECK-NEXT: subscript(_:) before yield 8
// CHECK-NEXT: increment(int:) before increment 8
// CHECK-NEXT: increment(int:) after increment 9
// CHECK-NEXT: subscript(_:) after yield 9
// CHECK-NEXT: modifySomeP(_:) end
    modifySomeP(&s)
// CHECK-NEXT: modifySomePLocal(_:) begin
// CHECK-NEXT: i before yield 43
// CHECK-NEXT: increment(int:) before increment 43
// CHECK-NEXT: increment(int:) after increment 44
// CHECK-NEXT: i after yield 44
// CHECK-NEXT: subscript(_:) before yield 9
// CHECK-NEXT: increment(int:) before increment 9
// CHECK-NEXT: increment(int:) after increment 10
// CHECK-NEXT: subscript(_:) after yield 10
// CHECK-NEXT: modifySomePLocal(_:) end
    modifySomePLocal(&s)
// CHECK-NEXT: i before yield 44
// CHECK-NEXT: i after yield 44
// CHECK-NEXT: readFromSomePLocal(_:) p.i 44
// CHECK-NEXT: subscript(_:) before yield 10
// CHECK-NEXT: subscript(_:) after yield 10
// CHECK-NEXT: readFromSomePLocal(_:) p[5] 10
    readFromSomePLocal(s)
    var sp = getSomeP()
// CHECK-NEXT: i before yield 42
// CHECK-NEXT: i after yield 42
// CHECK-NEXT: readFromSomePLocal(_:) p.i 42
// CHECK-NEXT: subscript(_:) before yield 8
// CHECK-NEXT: subscript(_:) after yield 8
// CHECK-NEXT: readFromSomePLocal(_:) p[5] 8
    readFromSomePLocal(sp)
// CHECK-NEXT: modifySomePLocal(_:) begin
// CHECK-NEXT: i before yield 42
// CHECK-NEXT: increment(int:) before increment 42
// CHECK-NEXT: increment(int:) after increment 43
// CHECK-NEXT: i after yield 43
// CHECK-NEXT: subscript(_:) before yield 8
// CHECK-NEXT: increment(int:) before increment 8
// CHECK-NEXT: increment(int:) after increment 9
// CHECK-NEXT: subscript(_:) after yield 9
// CHECK-NEXT: modifySomePLocal(_:) end
    modifySomePLocal(&sp)
// CHECK-NEXT: modifySomeP(_:) begin
// CHECK-NEXT: i before yield 43
// CHECK-NEXT: increment(int:) before increment 43
// CHECK-NEXT: increment(int:) after increment 44
// CHECK-NEXT: i after yield 44
// CHECK-NEXT: subscript(_:) before yield 9
// CHECK-NEXT: increment(int:) before increment 9
// CHECK-NEXT: increment(int:) after increment 10
// CHECK-NEXT: subscript(_:) after yield 10
// CHECK-NEXT: modifySomeP(_:) end
    modifySomeP(&sp)
// CHECK-NEXT: i before yield 44
// CHECK-NEXT: i after yield 44
// CHECK-NEXT: readFromSomeP(_:) p.i 44
// CHECK-NEXT: subscript(_:) before yield 10
// CHECK-NEXT: subscript(_:) after yield 10
// CHECK-NEXT: readFromSomeP(_:) p[5] 10
    readFromSomeP(sp)
  }
}