-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathexplicit_lifetime_dependence_specifiers.swift
161 lines (144 loc) · 6.31 KB
/
explicit_lifetime_dependence_specifiers.swift
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
// RUN: %target-swift-frontend %s \
// RUN: -emit-sil \
// RUN: -enable-builtin-module \
// RUN: -enable-experimental-feature LifetimeDependence \
// RUN: | %FileCheck %s
// REQUIRES: swift_feature_LifetimeDependence
import Builtin
@_unsafeNonescapableResult
@lifetime(borrow source)
internal func _overrideLifetime<
T: ~Copyable & ~Escapable, U: ~Copyable & ~Escapable
>(
_ dependent: consuming T, borrowing source: borrowing U
) -> T {
// TODO: Remove @_unsafeNonescapableResult. Instead, the unsafe dependence
// should be expressed by a builtin that is hidden within the function body.
dependent
}
@_unsafeNonescapableResult
@lifetime(copy source)
internal func _overrideLifetime<
T: ~Copyable & ~Escapable, U: ~Copyable & ~Escapable
>(
_ dependent: consuming T, copying source: borrowing U
) -> T {
// TODO: Remove @_unsafeNonescapableResult. Instead, the unsafe dependence
// should be expressed by a builtin that is hidden within the function body.
dependent
}
struct BufferView : ~Escapable {
let ptr: UnsafeRawBufferPointer
// CHECK-LABEL: sil hidden @$s39explicit_lifetime_dependence_specifiers10BufferViewVyACSWcfC : $@convention(method) (UnsafeRawBufferPointer, @thin BufferView.Type) -> @lifetime(borrow 0) @owned BufferView {
@lifetime(borrow ptr)
init(_ ptr: UnsafeRawBufferPointer) {
self.ptr = ptr
}
@_unsafeNonescapableResult
init?(_ ptr: UnsafeRawBufferPointer, _ i: Int) {
if (i % 2 == 0) {
return nil
}
self.ptr = ptr
}
@lifetime(borrow ptr)
init(independent ptr: UnsafeRawBufferPointer) {
self.ptr = ptr
}
// CHECK-LABEL: sil hidden @$s39explicit_lifetime_dependence_specifiers10BufferViewVyACSW_SaySiGhtcfC : $@convention(method) (UnsafeRawBufferPointer, @guaranteed Array<Int>, @thin BufferView.Type) -> @lifetime(borrow 1) @owned BufferView {
@lifetime(borrow a)
init(_ ptr: UnsafeRawBufferPointer, _ a: borrowing Array<Int>) {
self.ptr = ptr
}
// CHECK-LABEL: sil hidden @$s39explicit_lifetime_dependence_specifiers10BufferViewVyACSW_AA7WrapperVtcfC : $@convention(method) (UnsafeRawBufferPointer, @owned Wrapper, @thin BufferView.Type) -> @lifetime(copy 1) @owned BufferView {
@lifetime(copy a)
init(_ ptr: UnsafeRawBufferPointer, _ a: consuming Wrapper) {
self.ptr = ptr
}
// CHECK-LABEL: sil hidden @$s39explicit_lifetime_dependence_specifiers10BufferViewVyACSW_AA7WrapperVSaySiGhtcfC : $@convention(method) (UnsafeRawBufferPointer, @owned Wrapper, @guaranteed Array<Int>, @thin BufferView.Type) -> @lifetime(copy 1, borrow 2) @owned BufferView {
@lifetime(copy a, borrow b)
init(_ ptr: UnsafeRawBufferPointer, _ a: consuming Wrapper, _ b: borrowing Array<Int>) {
self.ptr = ptr
}
}
struct MutableBufferView : ~Escapable, ~Copyable {
let ptr: UnsafeMutableRawBufferPointer
@lifetime(borrow ptr)
init(_ ptr: UnsafeMutableRawBufferPointer) {
self.ptr = ptr
}
}
func testBasic() {
let capacity = 4
let a = Array(0..<capacity)
a.withUnsafeBytes {
let view = BufferView($0)
let derivedView = derive(view)
let newView = consumeAndCreate(derivedView)
use(newView)
}
}
// CHECK-LABEL: sil hidden @$s39explicit_lifetime_dependence_specifiers6deriveyAA10BufferViewVADF : $@convention(thin) (@guaranteed BufferView) -> @lifetime(borrow 0) @owned BufferView {
@lifetime(borrow x)
func derive(_ x: borrowing BufferView) -> BufferView {
return BufferView(independent: x.ptr)
}
// CHECK-LABEL: sil hidden @$s39explicit_lifetime_dependence_specifiers16consumeAndCreateyAA10BufferViewVADnF : $@convention(thin) (@owned BufferView) -> @lifetime(copy 0) @owned BufferView {
@lifetime(copy x)
func consumeAndCreate(_ x: consuming BufferView) -> BufferView {
let bv = BufferView(independent: x.ptr)
return _overrideLifetime(bv, copying: x)
}
// CHECK-LABEL: sil hidden @$s39explicit_lifetime_dependence_specifiers17deriveThisOrThat1yAA10BufferViewVAD_ADtF : $@convention(thin) (@guaranteed BufferView, @guaranteed BufferView) -> @lifetime(copy 1, borrow 0) @owned BufferView {
@lifetime(borrow this, copy that)
func deriveThisOrThat1(_ this: borrowing BufferView, _ that: borrowing BufferView) -> BufferView {
if (Int.random(in: 1..<100) == 0) {
return BufferView(independent: this.ptr)
}
return BufferView(independent: that.ptr)
}
// CHECK-LABEL: sil hidden @$s39explicit_lifetime_dependence_specifiers17deriveThisOrThat2yAA10BufferViewVAD_ADntF : $@convention(thin) (@guaranteed BufferView, @owned BufferView) -> @lifetime(copy 1, borrow 0) @owned BufferView {
@lifetime(borrow this, copy that)
func deriveThisOrThat2(_ this: borrowing BufferView, _ that: consuming BufferView) -> BufferView {
if (Int.random(in: 1..<100) == 0) {
return BufferView(independent: this.ptr)
}
let bv = BufferView(independent: that.ptr)
return _overrideLifetime(bv, copying: that)
}
func use(_ x: borrowing BufferView) {}
struct Wrapper : ~Escapable {
let view: BufferView
@lifetime(copy view)
init(_ view: consuming BufferView) {
self.view = view
}
// CHECK-LABEL: sil hidden @$s39explicit_lifetime_dependence_specifiers7WrapperV8getView1AA10BufferViewVyF : $@convention(method) (@guaranteed Wrapper) -> @lifetime(borrow 0) @owned BufferView {
@lifetime(borrow self)
borrowing func getView1() -> BufferView {
return view
}
// CHECK-LABEL: sil hidden @$s39explicit_lifetime_dependence_specifiers7WrapperV8getView2AA10BufferViewVyF : $@convention(method) (@owned Wrapper) -> @lifetime(copy 0) @owned BufferView {
@lifetime(copy self)
consuming func getView2() -> BufferView {
return view
}
}
struct Container : ~Escapable {
let ptr: UnsafeRawBufferPointer
@lifetime(borrow ptr)
init(_ ptr: UnsafeRawBufferPointer) {
self.ptr = ptr
}
}
// CHECK-LABEL: sil hidden @$s39explicit_lifetime_dependence_specifiers16getConsumingViewyAA06BufferG0VAA9ContainerVnF : $@convention(thin) (@owned Container) -> @lifetime(copy 0) @owned BufferView {
@lifetime(copy x)
func getConsumingView(_ x: consuming Container) -> BufferView {
let bv = BufferView(independent: x.ptr)
return _overrideLifetime(bv, copying: x)
}
// CHECK-LABEL: sil hidden @$s39explicit_lifetime_dependence_specifiers16getBorrowingViewyAA06BufferG0VAA9ContainerVF : $@convention(thin) (@guaranteed Container) -> @lifetime(borrow 0) @owned BufferView {
@lifetime(borrow x)
func getBorrowingView(_ x: borrowing Container) -> BufferView {
return BufferView(independent: x.ptr)
}