forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpointer_conversion.swift.gyb
362 lines (310 loc) · 17.6 KB
/
pointer_conversion.swift.gyb
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
// RUN: %empty-directory(%t)
// RUN: %gyb -DOPT_KIND=None %s -o %t/pointer_conversion.swift
// RUN: %line-directive %t/pointer_conversion.swift -- %target-swift-frontend -typecheck -verify %t/pointer_conversion.swift
// RUN: %gyb -DOPT_KIND=Optional %s -o %t/pointer_conversion_opt.swift
// RUN: %line-directive %t/pointer_conversion_opt.swift -- %target-swift-frontend -typecheck -verify %t/pointer_conversion_opt.swift
// RUN: %gyb -DOPT_KIND=ImplicitlyUnwrappedOptional %s -o %t/pointer_conversion_iuo.swift
// RUN: %line-directive %t/pointer_conversion_iuo.swift -- %target-swift-frontend -typecheck -verify %t/pointer_conversion_iuo.swift
%{
if OPT_KIND == 'Optional':
suffix='?'
diag_suffix='?'
elif OPT_KIND == 'ImplicitlyUnwrappedOptional':
suffix='!'
diag_suffix='?'
else:
suffix=''
diag_suffix=''
}%
class C {}
class D {}
func takesMutablePointer(_ x: UnsafeMutablePointer<Int>${suffix}) {}
func takesMutableVoidPointer(_ x: UnsafeMutableRawPointer${suffix}) {}
func takesMutableRawPointer(_ x: UnsafeMutableRawPointer${suffix}) {}
func takesMutableInt8Pointer(_ x: UnsafeMutablePointer<Int8>${suffix}) {}
func takesMutableArrayPointer(_ x: UnsafeMutablePointer<[Int]>${suffix}) {}
@discardableResult
func takesConstPointer(_ x: UnsafePointer<Int>${suffix}) -> Character { return "x" }
func takesConstInt8Pointer(_ x: UnsafePointer<Int8>${suffix}) {}
func takesConstUInt8Pointer(_ x: UnsafePointer<UInt8>${suffix}) {}
func takesConstVoidPointer(_ x: UnsafeRawPointer${suffix}) {}
func takesConstRawPointer(_ x: UnsafeRawPointer${suffix}) {}
func mutablePointerArguments(_ p: UnsafeMutablePointer<Int>,
cp: UnsafePointer<Int>) {
takesMutablePointer(nil)
% if not suffix:
// expected-error@-2 {{'nil' is not compatible with expected argument type}}
% end
takesMutablePointer(p)
takesMutablePointer(cp) // expected-error{{cannot convert value of type 'UnsafePointer<Int>' to expected argument type 'UnsafeMutablePointer<Int>${diag_suffix}'}}
var i: Int = 0
var f: Float = 0
takesMutablePointer(&i)
takesMutablePointer(&f) // expected-error{{cannot convert value of type 'UnsafeMutablePointer<Float>' to expected argument type 'UnsafeMutablePointer<Int>'}}
// expected-note@-1 {{arguments to generic parameter 'Pointee' ('Float' and 'Int') are expected to be equal}}
takesMutablePointer(i) // expected-error{{cannot convert value of type 'Int' to expected argument type 'UnsafeMutablePointer<Int>${diag_suffix}'}}
takesMutablePointer(f) // expected-error{{cannot convert value of type 'Float' to expected argument type 'UnsafeMutablePointer<Int>${diag_suffix}'}}
var ii: [Int] = [0, 1, 2]
var ff: [Float] = [0, 1, 2]
takesMutablePointer(&ii)
takesMutablePointer(&ff) // expected-error{{cannot convert value of type 'UnsafeMutablePointer<Float>' to expected argument type 'UnsafeMutablePointer<Int>'}}
// expected-note@-1 {{arguments to generic parameter 'Pointee' ('Float' and 'Int') are expected to be equal}}
takesMutablePointer(ii) // expected-error{{cannot convert value of type '[Int]' to expected argument type 'UnsafeMutablePointer<Int>${diag_suffix}'}}
takesMutablePointer(ff) // expected-error{{cannot convert value of type '[Float]' to expected argument type 'UnsafeMutablePointer<Int>${diag_suffix}'}}
takesMutableArrayPointer(&i) // expected-error{{cannot convert value of type 'UnsafeMutablePointer<Int>' to expected argument type 'UnsafeMutablePointer<[Int]>'}}
//expected-note@-1 {{arguments to generic parameter 'Pointee' ('Int' and '[Int]') are expected to be equal}}
takesMutableArrayPointer(&ii)
// We don't allow these conversions outside of function arguments.
var x: UnsafeMutablePointer<Int> = &i // expected-error {{use of extraneous '&'}}
x = &ii // expected-error {{use of extraneous '&'}}
_ = x
}
func mutableVoidPointerArguments(_ p: UnsafeMutablePointer<Int>,
cp: UnsafePointer<Int>,
fp: UnsafeMutablePointer<Float>) {
takesMutableVoidPointer(nil)
% if not suffix:
// expected-error@-2 {{'nil' is not compatible with expected argument type}}
% end
takesMutableVoidPointer(p)
takesMutableVoidPointer(fp)
takesMutableVoidPointer(cp) // expected-error{{cannot convert value of type 'UnsafePointer<Int>' to expected argument type 'UnsafeMutableRawPointer${diag_suffix}'}}
var i: Int = 0
var f: Float = 0
takesMutableVoidPointer(&i)
takesMutableVoidPointer(&f)
takesMutableVoidPointer(i) // expected-error{{cannot convert value of type 'Int' to expected argument type 'UnsafeMutableRawPointer${diag_suffix}'}}
takesMutableVoidPointer(f) // expected-error{{cannot convert value of type 'Float' to expected argument type 'UnsafeMutableRawPointer${diag_suffix}'}}
var ii: [Int] = [0, 1, 2]
var dd: [CInt] = [1, 2, 3]
var ff: [Int] = [0, 1, 2]
takesMutableVoidPointer(&ii)
takesMutableVoidPointer(&dd)
takesMutableVoidPointer(&ff)
takesMutableVoidPointer(ii) // expected-error{{cannot convert value of type '[Int]' to expected argument type 'UnsafeMutableRawPointer${diag_suffix}'}}
takesMutableVoidPointer(ff) // expected-error{{cannot convert value of type '[Int]' to expected argument type 'UnsafeMutableRawPointer${diag_suffix}'}}
// We don't allow these conversions outside of function arguments.
var x: UnsafeMutableRawPointer = &i // expected-error {{use of extraneous '&'}}
x = p // expected-error{{cannot assign value of type 'UnsafeMutablePointer<Int>' to type 'UnsafeMutableRawPointer'}}
x = &ii // expected-error {{use of extraneous '&'}}
_ = x
}
func mutableRawPointerArguments(_ p: UnsafeMutablePointer<Int>,
cp: UnsafePointer<Int>,
fp: UnsafeMutablePointer<Float>,
rp: UnsafeRawPointer) {
takesMutableRawPointer(nil)
% if not suffix:
// expected-error@-2 {{'nil' is not compatible with expected argument type}}
% end
takesMutableRawPointer(p)
takesMutableRawPointer(fp)
takesMutableRawPointer(cp) // expected-error{{cannot convert value of type 'UnsafePointer<Int>' to expected argument type 'UnsafeMutableRawPointer${diag_suffix}'}}
takesMutableRawPointer(rp) // expected-error{{cannot convert value of type 'UnsafeRawPointer' to expected argument type 'UnsafeMutableRawPointer${diag_suffix}'}}
var i: Int = 0
var f: Float = 0
takesMutableRawPointer(&i)
takesMutableRawPointer(&f)
takesMutableRawPointer(i) // expected-error{{cannot convert value of type 'Int' to expected argument type 'UnsafeMutableRawPointer${diag_suffix}'}}
takesMutableRawPointer(f) // expected-error{{cannot convert value of type 'Float' to expected argument type 'UnsafeMutableRawPointer${diag_suffix}'}}
var ii: [Int] = [0, 1, 2]
var dd: [CInt] = [1, 2, 3]
var ff: [Int] = [0, 1, 2]
takesMutableRawPointer(&ii)
takesMutableRawPointer(&dd)
takesMutableRawPointer(&ff)
takesMutableRawPointer(ii) // expected-error{{cannot convert value of type '[Int]' to expected argument type 'UnsafeMutableRawPointer${diag_suffix}'}}
takesMutableRawPointer(ff) // expected-error{{cannot convert value of type '[Int]' to expected argument type 'UnsafeMutableRawPointer${diag_suffix}'}}
// We don't allow these conversions outside of function arguments.
var x: UnsafeMutableRawPointer = &i // expected-error {{use of extraneous '&'}}
x = p // expected-error{{cannot assign value of type 'UnsafeMutablePointer<Int>' to type 'UnsafeMutableRawPointer'}}
x = &ii //expected-error {{use of extraneous '&'}}
_ = x
}
func constPointerArguments(_ p: UnsafeMutablePointer<Int>,
cp: UnsafePointer<Int>) {
takesConstPointer(nil)
% if not suffix:
// expected-error@-2 {{'nil' is not compatible with expected argument type}}
% end
takesConstPointer(p)
takesConstPointer(cp)
var i: Int = 0
var f: Float = 0
takesConstPointer(&i)
takesConstPointer(&f) // expected-error{{cannot convert value of type 'UnsafePointer<Float>' to expected argument type 'UnsafePointer<Int>'}}
// expected-note@-1 {{arguments to generic parameter 'Pointee' ('Float' and 'Int') are expected to be equal}}
var ii: [Int] = [0, 1, 2]
var ff: [Float] = [0, 1, 2]
takesConstPointer(&ii)
takesConstPointer(&ff) // expected-error{{cannot convert value of type 'UnsafePointer<Float>' to expected argument type 'UnsafePointer<Int>'}}
// expected-note@-1 {{arguments to generic parameter 'Pointee' ('Float' and 'Int') are expected to be equal}}
takesConstPointer(ii)
takesConstPointer(ff) // expected-error{{cannot convert value of type 'UnsafePointer<Float>' to expected argument type 'UnsafePointer<Int>'}}
// expected-note@-1 {{arguments to generic parameter 'Pointee' ('Float' and 'Int') are expected to be equal}}
takesConstPointer([0, 1, 2])
// <rdar://problem/22308330> QoI: CSDiags doesn't handle array -> pointer impl conversions well
takesConstPointer([0.0, 1.0, 2.0])
// expected-error@-1 3 {{cannot convert value of type 'Double' to expected element type 'Int'}}
// We don't allow these conversions outside of function arguments.
var x: UnsafePointer<Int> = &i // expected-error {{use of extraneous '&'}}
x = ii // expected-error{{cannot assign value of type '[Int]' to type 'UnsafePointer<Int>'}}
x = p // expected-error{{cannot assign value of type 'UnsafeMutablePointer<Int>' to type 'UnsafePointer<Int>'}}
}
func constVoidPointerArguments(_ p: UnsafeMutablePointer<Int>,
fp: UnsafeMutablePointer<Float>,
cp: UnsafePointer<Int>,
cfp: UnsafePointer<Float>) {
takesConstVoidPointer(nil)
% if not suffix:
// expected-error@-2 {{'nil' is not compatible with expected argument type}}
% end
takesConstVoidPointer(p)
takesConstVoidPointer(fp)
takesConstVoidPointer(cp)
takesConstVoidPointer(cfp)
var i: Int = 0
var f: Float = 0
takesConstVoidPointer(&i)
takesConstVoidPointer(&f)
var ii: [Int] = [0, 1, 2]
var ff: [Float] = [0, 1, 2]
takesConstVoidPointer(&ii)
takesConstVoidPointer(&ff)
takesConstVoidPointer(ii)
takesConstVoidPointer(ff)
takesConstVoidPointer([0, 1, 2])
takesConstVoidPointer([0.0, 1.0, 2.0])
// We don't allow these conversions outside of function arguments.
var x: UnsafeRawPointer = &i // expected-error {{use of extraneous '&'}}
x = ii // expected-error{{cannot assign value of type '[Int]' to type 'UnsafeRawPointer'}}
x = p // expected-error{{cannot assign value of type 'UnsafeMutablePointer<Int>' to type 'UnsafeRawPointer'}}
x = fp // expected-error{{cannot assign value of type 'UnsafeMutablePointer<Float>' to type 'UnsafeRawPointer'}}
x = cp // expected-error{{cannot assign value of type 'UnsafePointer<Int>' to type 'UnsafeRawPointer'}}
x = cfp // expected-error{{cannot assign value of type 'UnsafePointer<Float>' to type 'UnsafeRawPointer'}}
_ = x
}
func constRawPointerArguments(_ p: UnsafeMutablePointer<Int>,
fp: UnsafeMutablePointer<Float>,
cp: UnsafePointer<Int>,
cfp: UnsafePointer<Float>,
mrp: UnsafeMutableRawPointer) {
takesConstRawPointer(nil)
% if not suffix:
// expected-error@-2 {{'nil' is not compatible with expected argument type}}
% end
takesConstRawPointer(p)
takesConstRawPointer(fp)
takesConstRawPointer(cp)
takesConstRawPointer(cfp)
takesConstRawPointer(mrp)
var i: Int = 0
var f: Float = 0
takesConstRawPointer(&i)
takesConstRawPointer(&f)
var ii: [Int] = [0, 1, 2]
var ff: [Float] = [0, 1, 2]
takesConstRawPointer(&ii)
takesConstRawPointer(&ff)
takesConstRawPointer(ii)
takesConstRawPointer(ff)
takesConstRawPointer([0, 1, 2])
takesConstRawPointer([0.0, 1.0, 2.0])
// We don't allow these conversions outside of function arguments.
var x: UnsafeRawPointer = &i // expected-error {{use of extraneous '&'}}
x = ii // expected-error{{cannot assign value of type '[Int]' to type 'UnsafeRawPointer'}}
x = p // expected-error{{cannot assign value of type 'UnsafeMutablePointer<Int>' to type 'UnsafeRawPointer'}}
x = fp // expected-error{{cannot assign value of type 'UnsafeMutablePointer<Float>' to type 'UnsafeRawPointer'}}
x = cp // expected-error{{cannot assign value of type 'UnsafePointer<Int>' to type 'UnsafeRawPointer'}}
x = cfp // expected-error{{cannot assign value of type 'UnsafePointer<Float>' to type 'UnsafeRawPointer'}}
_ = x
}
func stringArguments(_ s: String) {
var s = s
takesConstVoidPointer(s)
takesConstRawPointer(s)
takesConstInt8Pointer(s)
takesConstUInt8Pointer(s)
takesConstPointer(s) // expected-error{{cannot convert value of type 'String' to expected argument type 'UnsafePointer<Int>${diag_suffix}'}}
takesMutableVoidPointer(s) // expected-error{{cannot convert value of type 'String' to expected argument type 'UnsafeMutableRawPointer${diag_suffix}'}}
takesMutableRawPointer(s) // expected-error{{cannot convert value of type 'String' to expected argument type 'UnsafeMutableRawPointer${diag_suffix}'}}
takesMutableInt8Pointer(s) // expected-error{{cannot convert value of type 'String' to expected argument type 'UnsafeMutablePointer<Int8>${diag_suffix}'}}
takesMutableInt8Pointer(&s) // expected-error{{cannot convert value of type 'UnsafeMutablePointer<String>' to expected argument type 'UnsafeMutablePointer<Int8>'}}
// expected-note@-1 {{arguments to generic parameter 'Pointee' ('String' and 'Int8') are expected to be equal}}
takesMutablePointer(s) // expected-error{{cannot convert value of type 'String' to expected argument type 'UnsafeMutablePointer<Int>${diag_suffix}'}}
takesMutablePointer(&s) // expected-error{{cannot convert value of type 'UnsafeMutablePointer<String>' to expected argument type 'UnsafeMutablePointer<Int>'}}
// expected-note@-1 {{arguments to generic parameter 'Pointee' ('String' and 'Int') are expected to be equal}}
}
func optionality(_ op: UnsafeMutablePointer<Float>?) {
takesMutableVoidPointer(op)
% if not suffix:
// expected-error@-2 {{value of optional type 'UnsafeMutablePointer<Float>?' must be unwrapped}}
// expected-note@-3{{coalesce}}
// expected-note@-4{{force-unwrap}}
% end
takesConstVoidPointer(op)
% if not suffix:
// expected-error@-2 {{value of optional type 'UnsafeMutablePointer<Float>?' must be unwrapped}}
// expected-note@-3{{coalesce}}
// expected-note@-4{{force-unwrap}}
% end
}
func pointerArithmetic(_ x: UnsafeMutablePointer<Int>, y: UnsafeMutablePointer<Int>,
i: Int) {
_ = x + i
_ = x - y
}
func genericPointerArithmetic<T>(_ x: UnsafeMutablePointer<T>, i: Int, t: T) -> UnsafeMutablePointer<T> {
let p = x + i
p.initialize(to: t)
}
func passPointerToClosure(_ f: (UnsafeMutablePointer<Int>) -> Int) -> Int { }
func pointerInClosure(_ f: (UnsafePointer<Int>) -> Int) -> Int {
return passPointerToClosure { f($0) }
}
struct NotEquatable {}
func arrayComparison(_ x: [NotEquatable], y: [NotEquatable], p: UnsafeMutablePointer<NotEquatable>) {
var x = x
// Don't allow implicit array-to-pointer conversions in operators.
// There is a note attached to declaration - requirement from conditional conformance of '[NotEquatable]' to 'Equatable'
let a: Bool = x == y // expected-error{{operator function '==' requires that 'NotEquatable' conform to 'Equatable'}}
let _: Bool = p == &x // Allowed!
}
func addressConversion(p: UnsafeMutablePointer<Int>, x: Int) {
var x = x
let _: Bool = p == &x
}
// <rdar://problem/19478919> QoI: poor error: '&' used with non-inout argument of type 'UnsafeMutablePointer<Int32>'
func f19478919() {
var viewport: Int = 1 // intentionally incorrect type, not Int32
func GLKProject(_ a : UnsafeMutablePointer<Int32>) {}
GLKProject(&viewport) // expected-error {{cannot convert value of type 'UnsafeMutablePointer<Int>' to expected argument type 'UnsafeMutablePointer<Int32>'}}
// expected-note@-1 {{arguments to generic parameter 'Pointee' ('Int' and 'Int32') are expected to be equal}}
func GLKProjectUP(_ a : UnsafePointer<Int32>) {}
func UP_Void(_ a : UnsafeRawPointer) {}
func UMP_Void(_ a : UnsafeMutableRawPointer) {}
UP_Void(&viewport)
UMP_Void(&viewport)
let cst = 42 // expected-note 2 {{change 'let' to 'var' to make it mutable}}
UP_Void(&cst) // expected-error {{cannot pass immutable value as inout argument: 'cst' is a 'let' constant}}
UMP_Void(&cst) // expected-error {{cannot pass immutable value as inout argument: 'cst' is a 'let' constant}}
}
// <rdar://problem/23202128> QoI: Poor diagnostic with let vs. var passed to C function
func f23202128() {
func UP(_ p: UnsafePointer<Int32>) {}
func UMP(_ p: UnsafeMutablePointer<Int32>) {}
let pipe: [Int32] = [0, 0] // expected-note {{change 'let' to 'var' to make it mutable}}}}
UMP(&pipe) // expected-error {{cannot pass immutable value as inout argument: 'pipe' is a 'let' constant}}
var pipe2: [Int] = [0, 0]
UMP(&pipe2) // expected-error {{cannot convert value of type 'UnsafeMutablePointer<Int>' to expected argument type 'UnsafeMutablePointer<Int32>'}}
// expected-note@-1 {{arguments to generic parameter 'Pointee' ('Int' and 'Int32') are expected to be equal}}
UP(pipe) // ok
UP(&pipe) // expected-error {{'&' is not allowed passing array value as 'UnsafePointer<Int32>' argument}} {{6-7=}}
}
func takesRawBuffer(_ b: UnsafeRawBufferPointer) {}
// <rdar://problem/29586888> UnsafeRawBufferPointer range subscript is inconsistent with Collection.
func f29586888(b: UnsafeRawBufferPointer) {
takesRawBuffer(b[1..<2]) // expected-error {{'subscript(_:)' is unavailable: use 'UnsafeRawBufferPointer(rebasing:)' to convert a slice into a zero-based raw buffer.}}
let slice = b[1..<2]
takesRawBuffer(slice) // expected-error {{cannot convert value of type 'UnsafeRawBufferPointer.SubSequence' (aka 'Slice<UnsafeRawBufferPointer>') to expected argument type 'UnsafeRawBufferPointer'}}
}