forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsemantics.swift
More file actions
572 lines (478 loc) · 18.1 KB
/
Copy pathsemantics.swift
File metadata and controls
572 lines (478 loc) · 18.1 KB
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
// RUN: %target-swift-frontend %s -emit-sil \
// RUN: -o /dev/null \
// RUN: -verify \
// RUN: -sil-verify-all \
// RUN: -module-name test \
// RUN: -enable-builtin-module \
// RUN: -enable-experimental-feature LifetimeDependence \
// RUN: -enable-experimental-feature AddressableParameters \
// RUN: -enable-experimental-feature AddressableTypes
// REQUIRES: swift_in_compiler
// REQUIRES: swift_feature_LifetimeDependence
// REQUIRES: swift_feature_AddressableParameters
// REQUIRES: swift_feature_AddressableTypes
import Builtin
struct NotEscapable: ~Escapable {}
// Lifetime dependence semantics by example.
public struct Span<T>: ~Escapable {
private var base: UnsafePointer<T>?
private var count: Int
@lifetime(borrow base)
init(base: UnsafePointer<T>?, count: Int) {
self.base = base
self.count = count
}
@lifetime(borrow generic)
init<S>(base: UnsafePointer<T>?, count: Int, generic: borrowing S) {
self.base = base
self.count = count
}
public subscript(_ position: Int) -> T {
unsafeAddress {
return base!.advanced(by: position)
}
}
}
extension Span {
@lifetime(copy self)
consuming func dropFirst() -> Span<T> {
let nextPointer = self.base.flatMap { $0 + 1 }
let local = Span(base: nextPointer, count: self.count - 1)
return _overrideLifetime(local, copying: self)
}
}
extension Span {
@lifetime(copy self)
mutating func droppingPrefix(length: Int) -> /* */ Span<T> {
let oldBase = base
let result = Span(base: oldBase, count: length)
if let base = self.base {
self.base = base + length
self.count -= length
}
return _overrideLifetime(result, copying: self)
}
}
struct MutableSpan<T>: ~Escapable, ~Copyable {
let base: UnsafeMutablePointer<T>
let count: Int
@lifetime(&base)
init(base: UnsafeMutablePointer<T>, count: Int) {
self.base = base
self.count = count
}
}
extension Array {
@lifetime(borrow self)
borrowing func span() -> Span<Element> {
/* not the real implementation */
let p = self.withUnsafeBufferPointer { $0.baseAddress! }
let span = Span(base: p, count: 1)
return _overrideLifetime(span, borrowing: self)
}
}
extension Array {
@lifetime(&self)
mutating func mutableSpan() -> MutableSpan<Element> {
/* not the real implementation */
let p = self.withUnsafeMutableBufferPointer { $0.baseAddress! }
let span = MutableSpan<Element>(base: p, count: 1)
return _overrideLifetime(span, mutating: &self)
}
}
struct InnerTrivial {
var p: UnsafePointer<Int>
@lifetime(borrow self)
borrowing func span() -> Span<Int> {
Span(base: p, count: 1)
}
}
struct TrivialHolder {
var p: UnsafePointer<Int>
var pa: UnsafePointer<AddressableInt>
var addressableInt: AddressableInt { unsafeAddress { pa } }
@lifetime(borrow self)
borrowing func span() -> Span<Int> {
Span(base: p, count: 1)
}
}
struct Holder {
let object: AnyObject
var p: UnsafePointer<Int>
var pa: UnsafePointer<AddressableInt>
var addressableInt: AddressableInt { unsafeAddress { pa } }
@lifetime(borrow self)
borrowing func span() -> Span<Int> {
Span(base: p, count: 1)
}
}
@_addressableForDependencies
struct AddressableInt {
let value: Int
@lifetime(borrow self)
borrowing func span() -> Span<Int> {
// TODO: we actually want the address of self.value
let p = UnsafePointer<Int>(Builtin.unprotectedAddressOfBorrow(self))
let span = Span(base: p, count: 1)
return _overrideLifetime(span, borrowing: self)
}
}
@_addressableForDependencies
struct AddressableObject {
let object: AnyObject
@lifetime(borrow self)
borrowing func span() -> Span<AnyObject> {
// TODO: we actually want the address of self.object
let p = UnsafePointer<AnyObject>(Builtin.unprotectedAddressOfBorrow(self))
let span = Span(base: p, count: 1)
return _overrideLifetime(span, borrowing: self)
}
}
struct Outer {
var _innerTrivial: InnerTrivial
var _innerObject: Holder
let trivialPointer: UnsafePointer<InnerTrivial>
let objectPointer: UnsafePointer<Holder>
var innerTrivialAddress: InnerTrivial {
unsafeAddress {
trivialPointer
}
}
var innerObjectAddress: Holder {
unsafeAddress {
objectPointer
}
}
var innerTrivialTemp: InnerTrivial {
get { _innerTrivial }
}
var innerObjectTemp: Holder {
get { _innerObject }
}
/* TODO: rdar://137608270 Add Builtin.addressof() support for @addressable arguments
@addressableSelf
var innerAddress: Inner {
unsafeAddress {
Builtin.addressof(inner)
}
}
*/
}
func parse(_ span: Span<Int>) {}
@lifetime(copy arg)
func copySpan<T>(_ arg: Span<T>) -> /* */ Span<T> { arg }
@lifetime(borrow arg)
func reborrowSpan<T>(_ arg: Span<T>) -> Span<T> { arg }
// =============================================================================
// Initialization
// =============================================================================
struct View: ~Escapable {
let owner: AnyObject
@lifetime(borrow owner)
init(owner: borrowing AnyObject) {
self.owner = copy owner // OK
}
}
struct MutableView: ~Escapable, ~Copyable {
let owner: AnyObject
// A copy of a borrow is indistinguishable with the borrow scope.
@lifetime(borrow owner)
init(owner: borrowing AnyObject) {
self.owner = copy owner // OK
}
@lifetime(&mutableOwner)
init(mutableOwner: inout AnyObject) {
// Initialization of a ~Escapable value is unenforced. So we can initialize
// the `owner` property without locally borrowing `mutableOwner`.
self.owner = mutableOwner // OK
}
}
struct Container<T> {
var owner: AnyObject
let pointer: UnsafeMutablePointer<T>
let count: Int
}
// Dependence on an empty initialized value should be scoped to variable decl.
@lifetime(copy x)
func f(x: NotEscapable) -> NotEscapable {
let local = NotEscapable() // expected-error {{lifetime-dependent variable 'local' escapes its scope}}
// expected-note @-1{{it depends on the lifetime of this parent value}}
return local // expected-note {{this use causes the lifetime-dependent value to escape}}
}
// =============================================================================
// Scoped dependence on values
// =============================================================================
// The duration of a scoped dependence is the lexical scope of the variable.
func testScopedLet(_ arg: [Int] ) {
let a: Array<Int> = arg
let span: Span<Int> // expected-error {{lifetime-dependent variable 'span' escapes its scope}}
do {
let a2 = a // expected-note {{it depends on the lifetime of variable 'a2'}}
span = a2.span()
}
parse(span) // expected-note {{this use of the lifetime-dependent value is out of scope}}
}
func testScopedCopy(_ arg: [Int] ) {
let a: Array<Int> = arg
let span: Span<Int> // expected-error {{lifetime-dependent variable 'span' escapes its scope}}
do {
let a2 = a // expected-note {{it depends on the lifetime of variable 'a2'}}
span = a2.span()
}
parse(span) // expected-note {{this use of the lifetime-dependent value is out of scope}}
}
// =============================================================================
// Inherited dependence on values
// =============================================================================
func testInheritedCopy(_ arg: [Int] ) {
let a: Array<Int> = arg
let result: Span<Int>
do {
let span = a.span()
let temp = span
result = copySpan(temp)
}
parse(result) // ✅ Safe: within lifetime of 'a'
}
func testInheritedCopyVar(_ arg: [Int] ) {
let a1: Array<Int> = arg
let a2: Array<Int> = arg
var span = a1.span()
var result: Span<Int>
do {
var temp = span
result = copySpan(temp)
span = a2.span()
temp = a2.span()
// 'result' still depends on 'a1', not 'a2'
}
parse(result) // ✅ Safe: within lifetime of 'a'
}
// =============================================================================
// Scoped dependence on inherited dependence
// =============================================================================
func testScopedOfInheritedWithCall(_ arg: [Int] ) {
let a: Array<Int> = arg
let span = a.span()
// TODO: should be // ✅ Safe: 'copySpan' result should be borrowed over `parse`
// rdar://128821299 ([nonescaping] extend borrowed arguments that are the source of a scoped dependence)
parse(reborrowSpan(copySpan(span))) // expected-error {{lifetime-dependent value escapes its scope}}
// expected-note @-1{{this use of the lifetime-dependent value is out of scope}}
// expected-note @-2{{it depends on the lifetime of this parent value}}
}
func testScopedOfInheritedWithLet(_ arg: [Int] ) {
let a: Array<Int> = arg
let span = a.span()
// TODO: should be // ✅ Safe: 'copySpan' result should be borrowed over `result`
// rdar://128821299 ([nonescaping] extend borrowed arguments that are the source of a scoped dependence)
let result = reborrowSpan(copySpan(span)) // expected-error {{lifetime-dependent variable 'result' escapes its scope}}
// expected-note @-1{{it depends on the lifetime of this parent value}}
_ = result
} // expected-note {{this use of the lifetime-dependent value is out of scope}}
// =============================================================================
// Scoped dependence on trivial values
// =============================================================================
@lifetime(borrow a)
func testTrivialScope<T>(a: Array<T>) -> Span<T> {
let p = a.withUnsafeBufferPointer { $0.baseAddress! }
return Span(base: p, count: 1)
// expected-error @-1{{lifetime-dependent value escapes its scope}}
// expected-note @-3{{it depends on the lifetime of variable 'p'}}
// expected-note @-3{{this use causes the lifetime-dependent value to escape}}
}
extension Span {
public func withThrowingClosure<E: Error>(_ body: () throws(E) -> ()) throws(E) -> () {
try body()
}
}
// Test dependence on an local variable that needs to be extended into the dead-end block of a never-throwing apply.
public func testTrivialLocalDeadEnd(p: UnsafePointer<Int>) {
let pointer = p
let span = Span(base: pointer, count: 1)
span.withThrowingClosure {}
}
// Test dependence on a borrow of a trivial inout. The access scope can be ignored since we don't care about the
// in-memory value.
@lifetime(borrow p)
public func testTrivialInoutBorrow(p: inout UnsafePointer<Int>) -> Span<Int> {
return Span(base: p, count: 1)
}
// =============================================================================
// Scoped dependence on global values
// =============================================================================
private let immortalInt = 0
private let immortalString = ""
@lifetime(immortal)
func testImmortalInt() -> Span<Int> {
let nilBasedBuffer = UnsafeBufferPointer<Int>(start: nil, count: 0)
let span = Span(base: nilBasedBuffer.baseAddress, count: nilBasedBuffer.count)
return _overrideLifetime(span, borrowing: immortalInt)
}
@lifetime(immortal)
func testImmortalString() -> Span<String> {
let nilBasedBuffer = UnsafeBufferPointer<String>(start: nil, count: 0)
let span = Span(base: nilBasedBuffer.baseAddress, count: nilBasedBuffer.count)
return _overrideLifetime(span, borrowing: immortalString)
}
let ptr = UnsafePointer<Int>(bitPattern: 1)!
let globalTrivial = InnerTrivial(p: ptr)
// An immortal span can depend on a caller's local borrow scope even though the callee sees no such dependency.
@lifetime(borrow local)
func testGlobal(local: InnerTrivial) -> Span<Int> {
globalTrivial.span()
}
// =============================================================================
// Scoped dependence on mutable values
// =============================================================================
@lifetime(&a)
func testInoutBorrow(a: inout [Int]) -> Span<Int> {
a.span() // expected-error {{lifetime-dependent value escapes its scope}}
// expected-note @-1{{it depends on this scoped access to variable 'a'}}
} // expected-note {{this use causes the lifetime-dependent value to escape}}
@lifetime(&a)
func testInoutMutableBorrow(a: inout [Int]) -> MutableSpan<Int> {
a.mutableSpan()
}
// =============================================================================
// Scoped dependence on property access
// =============================================================================
extension Container {
@lifetime(borrow self)
func span() -> Span<T> {
// borrowing the 'pointer' member is allowed.
Span(base: self.pointer, count: self.count) // OK
}
@lifetime(borrow self)
func view() -> View {
// borrowing the 'view' member is allowed.
View(owner: self.owner) // OK
}
@lifetime(&self)
mutating func mutableView() -> MutableView {
// Reading 'self.owner' creates a local borrow scope. This new MutableView
// depends on a the local borrow scope for 'self.owner', so it cannot be
// returned.
MutableView(owner: self.owner) // expected-error {{lifetime-dependent value escapes its scope}}
// expected-note @-1{{it depends on this scoped access to variable 'self'}}
} // expected-note {{this use causes the lifetime-dependent value to escape}}
@lifetime(&self)
mutating func mutableViewModifiesOwner() -> MutableView {
// Passing '&self.owner' inout creates a nested exclusive access that must extend to all uses of the new
// MutableView. This is allowed because the nested access is logically extended to the end of the function (without
// violating exclusivity).
MutableView(mutableOwner: &self.owner)
}
@lifetime(&self)
mutating func mutableSpan() -> MutableSpan<T> {
// Reading 'self.pointer' creates a local borrow scope. The local borrow
// scope is ignored because 'pointer' is a trivial value. Instead, the new
// MutableSpan depends directly on 'inout self'.
MutableSpan(base: self.pointer, count: self.count) // OK
}
}
@lifetime(borrow outer)
func testBorrowStoredTrivial(outer: Outer) -> Span<Int> {
outer._innerTrivial.span()
}
@lifetime(borrow outer)
func testBorrowStoredObject(outer: Outer) -> Span<Int> {
outer._innerObject.span()
}
@lifetime(borrow outer)
func testBorrowTrivialAddressProjection(outer: Outer) -> Span<Int> {
outer.innerTrivialAddress.span()
}
@lifetime(borrow outer)
func testBorrowObjectAddressProjection(outer: Outer) -> Span<Int> {
outer.innerObjectAddress.span()
}
func testExprExtendTrivialTemp(outer: Outer) {
parse(outer.innerTrivialTemp.span())
}
func testExprExtendObjectTemp(outer: Outer) {
parse(outer.innerObjectTemp.span())
// expected-error @-1{{lifetime-dependent value escapes its scope}}
// expected-note @-2{{it depends on the lifetime of this parent value}}
// expected-note @-3{{this use of the lifetime-dependent value is out of scope}}
}
func testLocalExtendTrivialTemp(outer: Outer) {
let span = outer.innerTrivialTemp.span()
parse(span)
}
func testLocalExtendObjectTemp(outer: Outer) {
let span = outer.innerObjectTemp.span()
// expected-error @-1{{lifetime-dependent variable 'span' escapes its scope}}
// expected-note @-2{{it depends on the lifetime of this parent value}}
parse(span) // expected-note {{this use of the lifetime-dependent value is out of scope}}
}
@lifetime(borrow outer)
func testReturnTrivialTemp(outer: Outer) -> Span<Int> {
outer.innerTrivialTemp.span()
// expected-error @-1{{lifetime-dependent value escapes its scope}}
// expected-note @-2{{it depends on the lifetime of this parent value}}
} // expected-note {{this use causes the lifetime-dependent value to escape}}
@lifetime(borrow outer)
func testReturnObjectTemp(outer: Outer) -> Span<Int> {
outer.innerObjectTemp.span()
// expected-error @-1{{lifetime-dependent value escapes its scope}}
// expected-note @-2{{it depends on the lifetime of this parent value}}
} // expected-note {{this use causes the lifetime-dependent value to escape}}
// =============================================================================
// Scoped dependence on addressable parameters
// =============================================================================
// @_addressableForDependencies supports returning a Span.
@lifetime(borrow arg)
func testAddressableInt(arg: AddressableInt) -> Span<Int> {
arg.span()
}
// @_addressableForDependencies supports returning a Span.
@lifetime(borrow arg)
func testAddressableObject(arg: AddressableObject) -> Span<AnyObject> {
arg.span()
}
// Helper: create a dependence on the argument's address.
@lifetime(borrow arg)
func dependsOnTrivialAddressHelper(arg: @_addressable TrivialHolder) -> Span<Int> {
arg.span()
}
// Helper: create a dependence on the argument's address.
@lifetime(borrow arg)
func dependsOnAddressHelper(arg: @_addressable Holder) -> Span<Int> {
arg.span()
}
/* TODO: requires -enable-address-dependencies
// Non-addressable error returning a Span.
@lifetime(borrow arg)
func testTrivialNonAddressable(arg: TrivialHolder) -> Span<Int> {
dependsOnTrivialAddressHelper(arg: arg)
// todo-error @-1{{lifetime-dependent value escapes its scope}
// todo-note @-3{{it depends on the lifetime of variable 'arg'}}
} // todo-note {{this use causes the lifetime-dependent value to escape}}
// Non-addressable error returning a Span.
@lifetime(borrow arg)
func testNonAddressable(arg: Holder) -> Span<Int> {
dependsOnAddressHelper(arg: arg)
// todo-error @-1{{lifetime-dependent value escapes its scope}
// todo-note @-3{{it depends on the lifetime of variable 'arg'}}
} // todo-note {{this use causes the lifetime-dependent value to escape}}
*/
/* TODO: rdar://145872854 (SILGen: @addressable inout arguments are copied)
@lifetime(borrow arg)
func test(arg: inout AddressableInt) -> Span<Int> {
arg.span()
}
// unsafeAddress generates an addressable value with a local scope.
@lifetime(borrow arg)
func testBorrowedAddressableInt(arg: Holder) -> Int {
let span = arg.addressableInt.span()
return span[0]
}
// unsafeAddress generates an addressable value.
// Error returning a dependence on its local scope.
@lifetime(borrow arg)
func testBorrowedAddressableIntReturn(arg: Holder) -> Span<Int> {
arg.addressableInt.span()
// todo-error @-1{{lifetime-dependent value escapes its scope}
// todo-note @-2{{it depends on the lifetime of this parent value}}
} // todo-note {{this use causes the lifetime-dependent value to escape}}
*/