- Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathsafe.swift
310 lines (234 loc) · 8.48 KB
/
safe.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
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
// RUN: %target-typecheck-verify-swift -strict-memory-safety
// The feature flag should be enabled.
#if !hasFeature(StrictMemorySafety)
#error("Strict memory safety is not enabled!")
#endif
@unsafe
func unsafeFunction(){}
@unsafe
structUnsafeType{}
func f(){
unsafe unsafeFunction()
}
func g(){
unsafe unsafeFunction()
}
func h(_:UnsafeType){
// expected-warning@+1{{expression uses unsafe constructs but is not marked with 'unsafe'}}
unsafeFunction() // expected-note{{reference to unsafe global function 'unsafeFunction()'}}
// okay
unsafe unsafeFunction()
// expected-warning@+1{{no unsafe operations occur within 'unsafe' expression}}
unsafe g()
}
func rethrowing(body:(UnsafeType)throws->Void)rethrows{}
classHasStatics{
staticinternalfunc f(_:UnsafeType){}
}
@unsafe
func unsafeInt()->Int{5}
structHasProperties{
varcomputed:Int{
unsafe unsafeInt()
}
@unsafevarcomputedUnsafe:Int{
unsafe unsafeInt()
}
staticvarblah:Int={
unsafe unsafeInt()
}()
@unsafestaticvarblahUnsafe:Int={
unsafe unsafeInt()
}()
}
protocolP{}
extensionInt:@unsafeP{}
func acceptP(_:someP){}
func testConformance(i:Int){
// expected-warning@+1{{expression uses unsafe constructs but is not marked with 'unsafe'}}
acceptP(i) // expected-note{{'@unsafe' conformance of 'Int' to protocol 'P' involves unsafe code}}
}
func returnsOpaqueP()->someP{
5 // expected-warning{{expression uses unsafe constructs but is not marked with 'unsafe'}}
// expected-note@-1{{'@unsafe' conformance of 'Int' to protocol 'P' involves unsafe code}}
}
func returnsExistentialP()->anyP{
5 // expected-warning{{expression uses unsafe constructs but is not marked with 'unsafe'}}
// expected-note@-1{{'@unsafe' conformance of 'Int' to protocol 'P' involves unsafe code}}
}
// FIXME: Should work even if the IteratorProtocol conformance is safe
structUnsafeAsSequence:@unsafeSequence,@unsafeIteratorProtocol{
@unsafemutatingfunc next()->Int?{nil}
}
func testUnsafeAsSequenceForEach(){
letuas=UnsafeAsSequence()
// expected-warning@+1{{expression uses unsafe constructs but is not marked with 'unsafe'}}{{12-12=unsafe }}
for_in uas {} // expected-note{{conformance}}
// expected-warning@-1{{for-in loop uses unsafe constructs but is not marked with 'unsafe'}}{{documentation-file=strict-memory-safety}}{{7-7=unsafe }}
for_in unsafe uas{} // expected-warning{{for-in loop uses unsafe constructs but is not marked with 'unsafe'}}{{documentation-file=strict-memory-safety}}{{7-7=unsafe }}
forunsafe _ in unsafe uas{} // okay
}
func testForInUnsafeAmbiguity(_ integers:[Int]){
forunsafein integers {
_ = unsafe
}
forunsafe:Intin integers {
_ = unsafe
}
}
structUnsafeIterator:@unsafeIteratorProtocol{
@unsafemutatingfunc next()->Int?{nil}
}
structSequenceWithUnsafeIterator:Sequence{
func makeIterator()->UnsafeIterator{UnsafeIterator()}
}
func testUnsafeIteratorForEach(){
letswui=SequenceWithUnsafeIterator()
for_in swui {} // expected-warning{{for-in loop uses unsafe constructs but is not marked with 'unsafe'}}{{7-7=unsafe }}
forunsafe _ in swui {} // okay, it's only the iterator that's unsafe
}
classMyRange{
@unsafeinit(unchecked bounds:Range<Int>){}
convenienceinit(_ bounds:Range<Int>){
// bounds check
self.init(unchecked: bounds) // expected-warning{{expression uses unsafe constructs but is not marked with 'unsafe'}}
// expected-note@-1{{reference to unsafe initializer 'init(unchecked:)'}}
}
}
func casting(value:Any, i:Int){
// expected-warning@+1{{expression uses unsafe constructs but is not marked with 'unsafe'}}
_ = value as?UnsafeType // expected-note{{reference to unsafe type 'UnsafeType'}}
// expected-warning@+1{{expression uses unsafe constructs but is not marked with 'unsafe'}}
_ = value as!UnsafeType // expected-note{{reference to unsafe type 'UnsafeType'}}
_ = unsafe value as?UnsafeType
_ = unsafe value as!UnsafeType
// expected-warning@+1{{expression uses unsafe constructs but is not marked with 'unsafe'}}
_ = i asanyP // expected-note{{'@unsafe' conformance of 'Int' to protocol 'P' involves unsafe code}}
}
func metatypes(){
// expected-warning@+1{{expression uses unsafe constructs but is not marked with 'unsafe'}}
let _:Any.Type=UnsafeType.self // expected-note{{reference to unsafe type 'UnsafeType'}}
let _:Any.Type=unsafe UnsafeType.self
}
func testKeyPath(){
// expected-warning@+1{{expression uses unsafe constructs but is not marked with 'unsafe'}}
_ = \HasProperties.computedUnsafe // expected-note{{reference to unsafe property 'computedUnsafe'}}
_ = unsafe \HasProperties.computedUnsafe
}
func takesAutoclosure<T>(_ body:@autoclosure()->T){}
func testAutoclosure(){
// expected-warning@+1{{expression uses unsafe constructs but is not marked with 'unsafe'}}{{3-3=unsafe }}
takesAutoclosure(unsafeFunction()) // expected-note{{reference to unsafe global function 'unsafeFunction()'}}
unsafe takesAutoclosure(unsafeFunction())
takesAutoclosure(unsafe unsafeFunction())
}
// Parsing of `unsafe` expressions.
func testUnsafePositionError()->Int{
return3+ unsafe unsafeInt() // expected-error{{'unsafe' cannot appear to the right of a non-assignment operator}}
}
enumColor{
case red
}
func acceptBools(_:Bool, _:Bool){}
func acceptBoolsUnsafeLabel(unsafe _:Bool, _:Bool){}
func unsafe(_:Int){}
func unsafeFun(){
varunsafe=true
unsafe =false
unsafe.toggle()
_ =[unsafe]
_ ={ unsafe }
acceptBools(unsafe, unsafe)
acceptBoolsUnsafeLabel(unsafe: unsafe, unsafe)
letcolor:Color
// expected-warning@+1{{no unsafe operations occur within 'unsafe' expression}}{{11-18=}}
color = unsafe .red
_ = color
if unsafe {}
}
func moreUnsafeFunc(unsafe:[Int]){
let _:[Int]=unsafe[]
// expected-warning@-1{{no unsafe operations occur within 'unsafe' expression}}
_ =unsafe[1]
_ ="\(unsafe)"
}
func yetMoreUnsafeFunc(unsafe:()->Void){
unsafe()
_ =unsafe()
// expected-warning@-1{{no unsafe operations occur within 'unsafe' expression}}
}
// @safe suppresses unsafe-type-related diagnostics on an entity
structMyArray<Element>{
@safefunc withUnsafeBufferPointer<R, E>(
_ body:(UnsafeBufferPointer<Element>)throws(E)->R
)throws(E)->R{
return unsafe trybody(.init(start:nil, count:0))
}
}
extensionUnsafeBufferPointer{
@unsafevarunsafeCount:Int{17}
@safevarsafeCount:Int{ unsafe unsafeCount }
}
func testMyArray(ints:MyArray<Int>){
ints.withUnsafeBufferPointer{ buffer in
letbufferCopy= unsafe buffer
_ = unsafe bufferCopy
print(buffer.safeCount)
unsafe print(buffer.unsafeCount)
}
}
func testUnsafeLHS(){
@unsafevarvalue:Int=0
unsafe value =switch unsafe value {
case0:1
default:0
}
}
@safe
structUnsafeWrapTest{
varpointer:UnsafeMutablePointer<Int>?
func test(){
iflet pointer { // expected-warning{{expression uses unsafe constructs but is not marked with 'unsafe'}}{{19-19= = unsafe pointer}}
// expected-note@-1{{reference to property 'pointer' involves unsafe type 'UnsafeMutablePointer<Int>'}}
_ = unsafe pointer
}
}
func otherTest(pointer:UnsafeMutablePointer<Int>?){
iflet pointer { // expected-warning{{expression uses unsafe constructs but is not marked with 'unsafe'}}{{19-19= = unsafe pointer}}
// expected-note@-1{{reference to parameter 'pointer' involves unsafe type 'UnsafeMutablePointer<Int>}}
_ = unsafe pointer
}
}
}
@safe@unsafe
structConfusedStruct{} // expected-error{{struct 'ConfusedStruct' cannot be both '@safe' and '@unsafe'}}
@unsafe
structUnsafeContainingUnspecified{
typealiasA=Int
func getA()->A{0}
@safe
structY{
varvalue:Int
}
func f(){
_ =Y(value:5)
}
}
@unsafefunc f(x:UnsafeContainingUnspecified){
leta=unsafe x.getA()
_ = a
}
extensionSlice{
// Make sure we aren't diagnosing the 'defer' as unsafe.
publicfunc withContiguousMutableStorageIfAvailable<R, Element>(
_ body:(_ buffer:inoutUnsafeMutableBufferPointer<Element>)throws->R
)rethrows->R?where Base ==UnsafeMutableBufferPointer<Element>{
tryunsafe base.withContiguousStorageIfAvailable{ buffer in
letstart=unsafe base.baseAddress?.advanced(by: startIndex)
varslice=unsafe UnsafeMutableBufferPointer(start: start, count: count)
defer{
}
returntryunsafe body(&slice)
}
}
}