forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlifetime_attr.swift
More file actions
189 lines (154 loc) · 6.23 KB
/
Copy pathlifetime_attr.swift
File metadata and controls
189 lines (154 loc) · 6.23 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
// RUN: %target-typecheck-verify-swift -disable-availability-checking -enable-experimental-feature Lifetimes
// REQUIRES: swift_feature_Lifetimes
struct NE : ~Escapable {
@_lifetime(copy self) // expected-error{{invalid lifetime dependence specifier on non-existent self}}
init() {}
}
@_lifetime(copy nonexisting) // expected-error{{invalid parameter name specified 'nonexisting'}}
func invalidAttrOnNonExistingParam(_ ne: NE) -> NE {
ne
}
@_lifetime(copy self) // expected-error{{invalid lifetime dependence specifier on non-existent self}}
func invalidAttrOnNonExistingSelf(_ ne: NE) -> NE {
ne
}
@_lifetime(copy 0) // expected-error{{expected 'copy', 'borrow', or '&' followed by an identifier or 'self' in lifetime dependence specifier}}
func invalidAttrOnExistingParamIndex(_ ne: NE) -> NE {
ne
}
@_lifetime(2) // expected-error{{expected 'copy', 'borrow', or '&' followed by an identifier or 'self' in lifetime dependence specifier}}
func invalidAttrOnNonExistingParamIndex(_ ne: NE) -> NE {
ne
}
@_lifetime(copy ne, borrow ne) // expected-error{{duplicate lifetime dependence specifier}}
func invalidDuplicateLifetimeDependence1(_ ne: borrowing NE) -> NE {
ne
}
class Klass {}
@_lifetime(borrow x) // expected-error{{invalid use of borrow dependence with consuming ownership}}
func invalidDependenceConsumeKlass(_ x: consuming Klass) -> NE {
NE()
}
@_lifetime(&x) // expected-error{{invalid use of & dependence with borrowing ownership}}
// expected-note @-1{{use '@_lifetime(borrow x)' instead}}
func invalidDependenceBorrowKlass(_ x: borrowing Klass) -> NE {
NE()
}
@_lifetime(borrow x) // expected-error{{invalid use of borrow dependence with inout ownership}}
// expected-note @-1{{use '@_lifetime(&x)' instead}}
func invalidDependenceInoutKlass(_ x: inout Klass) -> NE {
NE()
}
@_lifetime(borrow x) // OK
func invalidDependenceConsumeInt(_ x: consuming Int) -> NE {
NE()
}
@_lifetime(&x) // expected-error{{invalid use of & dependence with borrowing ownership}}
// expected-note @-1{{use '@_lifetime(borrow x)' instead}}
func invalidDependenceBorrowInt(_ x: borrowing Int) -> NE {
NE()
}
@_lifetime(borrow x) // expected-error{{invalid use of borrow dependence with inout ownership}}
// expected-note @-1{{use '@_lifetime(&x)' instead}}
func invalidDependenceInoutInt(_ x: inout Int) -> NE {
NE()
}
@_lifetime(result: copy source1) // expected-error{{invalid duplicate target lifetime dependencies on function}}
@_lifetime(result: copy source2)
func invalidTarget(_ result: inout NE, _ source1: consuming NE, _ source2: consuming NE) {
result = source1
}
@_lifetime(result: copy source) // expected-error{{invalid duplicate target lifetime dependencies on function}}
@_lifetime(result: borrow source)
func invalidSource(_ result: inout NE, _ source: consuming NE) {
result = source
}
@_lifetime(immortal)
func immortalConflict(_ immortal: Int) -> NE { // expected-error{{conflict between the parameter name and 'immortal' contextual keyword}}
NE()
}
do {
struct Test: ~Escapable { // expected-error{{cannot infer implicit initialization lifetime. Add an initializer with '@_lifetime(...)' for each parameter the result depends on}}
var v1: Int
var v2: NE
}
_ = \Test.v1 // expected-error {{key path cannot refer to nonescapable type 'Test'}}
_ = \Test.v2 // expected-error {{key path cannot refer to nonescapable type 'Test'}} expected-error {{key path cannot refer to nonescapable type 'NE'}}
func use(t: Test) {
t[keyPath: \.v1] // expected-error {{key path cannot refer to nonescapable type 'Test'}}
t[keyPath: \.v2] // expected-error {{key path cannot refer to nonescapable type 'Test'}} expected-error {{key path cannot refer to nonescapable type 'NE'}}
}
}
// rdar://146401190 ([nonescapable] implement non-inout parameter dependencies)
@_lifetime(span: borrow holder)
func testParameterDep(holder: AnyObject, span: Span<Int>) {} // expected-error{{lifetime-dependent parameter 'span' must be 'inout'}}
@_lifetime(&ne)
func inoutLifetimeDependence(_ ne: inout NE) -> NE {
ne
}
@_lifetime(copy k) // expected-error{{cannot copy the lifetime of an Escapable type}}
// expected-note@-1{{use '@_lifetime(&k)' instead}}
func dependOnEscapable(_ k: inout Klass) -> NE {
NE()
}
@_lifetime(copy k) // expected-error{{cannot copy the lifetime of an Escapable type}}
// expected-note@-1{{use '@_lifetime(borrow k)' instead}}
func dependOnEscapable(_ k: borrowing Klass) -> NE {
NE()
}
@_lifetime(copy k) // expected-error{{cannot copy the lifetime of an Escapable type}}
// expected-note@-1{{use '@_lifetime(borrow k)' instead}}
func dependOnEscapable(_ k: consuming Klass) -> NE {
NE()
}
struct Wrapper : ~Escapable {
var _ne: NE
var ne: NE {
@_lifetime(copy self)
get {
_ne
}
@_lifetime(self: &self)
nonmutating _modify {// expected-error{{lifetime-dependent parameter 'self' must be 'inout'}}
// expected-error@-1{{cannot infer the lifetime dependence scope on a method with a ~Escapable parameter, specify '@_lifetime(borrow self)' or '@_lifetime(copy self)'}}
}
}
var otherNE: NE {
@_lifetime(copy self)
get {
_ne
}
@_lifetime(self: borrow newValue)
set {
self._ne = newValue
}
@_lifetime(&self)
_modify {
yield &self._ne
}
}
}
@_lifetime(inValue) // expected-error{{invalid lifetime dependence on an Escapable result}}
func getInt(_ inValue: Int) -> Int {
return inValue
}
@_lifetime(_outValue: borrow inValue) // expected-error{{invalid lifetime dependence on an Escapable target}}
func getInt(_outValue: inout Int, _ inValue: Int) {
_outValue = inValue
}
@_lifetime(inValue) // expected-error{{invalid lifetime dependence on an Escapable result}}
func getGeneric<T>(_ inValue: T) -> T {
return inValue
}
@_lifetime(_outValue: borrow inValue) // expected-error{{invalid lifetime dependence on an Escapable target}}
func getGeneric<T>(_outValue: inout T, _ inValue: T) {
_outValue = inValue
}
@_lifetime(borrow inValue)
func getGeneric<T : ~Escapable>(_ inValue: T) -> T {
return inValue
}
@_lifetime(_outValue: borrow inValue)
func getGeneric<T : ~Escapable>(_outValue: inout T, _ inValue: T) {
_outValue = inValue
}