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
|
// RUN: %empty-directory(%t)
// RUN: %gyb %s -o %t/swift_to_c_pointer_conversions.swift
// RUN: %line-directive %t/swift_to_c_pointer_conversions.swift -- %target-swift-frontend(mock-sdk: %clang-importer-sdk-nosource -I %t) -import-objc-header %S/Inputs/c_pointer_conversions.h %t/swift_to_c_pointer_conversions.swift -typecheck -verify
// REQUIRES: objc_interop
func test_raw_ptr(ptr: UnsafeRawPointer) {
char_ptr_func(ptr) // expected-error {{}}
unsigned_char_ptr_func(ptr) // expected-error {{}}
const_char_ptr_func(ptr) // Ok
const_unsigned_char_ptr_func(ptr) // Ok
% for Size in ['16', '32', '64']:
int_${Size}_ptr_func(ptr) // expected-error {{}}
uint_${Size}_ptr_func(ptr) // expected-error {{}}
const_int_${Size}_ptr_func(ptr) // expected-error {{}}
const_uint_${Size}_ptr_func(ptr) // expected-error {{}}
% end
}
func test_mutable_raw_pointer(ptr: UnsafeMutableRawPointer) {
char_ptr_func(ptr) // Ok
unsigned_char_ptr_func(ptr) // Ok
const_char_ptr_func(ptr) // Ok
const_unsigned_char_ptr_func(ptr) // Ok
% for Size in ['16', '32', '64']:
int_${Size}_ptr_func(ptr) // expected-error {{}}
uint_${Size}_ptr_func(ptr) // expected-error {{}}
const_int_${Size}_ptr_func(ptr) // expected-error {{}}
const_uint_${Size}_ptr_func(ptr) // expected-error {{}}
% end
}
%for TestPtrSize in ['8', '16', '32', '64']:
// Immutable pointers can be converted only to their immutable (regardless of sign) counterparts.
func test_${TestPtrSize}_bit_ptrs(sptr: UnsafePointer<Int${TestPtrSize}>,
uptr: UnsafePointer<UInt${TestPtrSize}>,
osptr: UnsafePointer<Int${TestPtrSize}>?,
ouptr: UnsafePointer<UInt${TestPtrSize}>?) {
% for pointer in ['sptr', 'uptr']:
char_ptr_func(${pointer}) // expected-error {{}}
opt_char_ptr_func(${pointer}) // expected-error {{}}
const_char_ptr_func(${pointer}) // Ok
const_opt_char_ptr_func(${pointer}) // Ok
unsigned_char_ptr_func(${pointer}) // expected-error {{}}
unsigned_opt_char_ptr_func(${pointer}) // expected-error {{}}
const_unsigned_char_ptr_func(${pointer}) // Ok
const_opt_unsigned_char_ptr_func(${pointer}) // Ok
% end
% for pointer in ['osptr', 'ouptr']:
opt_char_ptr_func(${pointer}) // expected-error {{}}
const_opt_char_ptr_func(${pointer}) // Ok
unsigned_opt_char_ptr_func(${pointer}) // expected-error {{}}
const_opt_unsigned_char_ptr_func(${pointer}) // Ok
% end
% for pointer in ['sptr', 'uptr']:
% for Size in ['16', '32', '64']:
% if Size == TestPtrSize:
int_${TestPtrSize}_ptr_func(${pointer}) // expected-error {{}}
uint_${TestPtrSize}_ptr_func(${pointer}) // expected-error {{}}
opt_int_${TestPtrSize}_ptr_func(${pointer}) // expected-error {{}}
opt_uint_${TestPtrSize}_ptr_func(${pointer}) // expected-error {{}}
const_int_${TestPtrSize}_ptr_func(${pointer}) // Ok
const_uint_${TestPtrSize}_ptr_func(${pointer}) // Ok
const_opt_int_${TestPtrSize}_ptr_func(${pointer}) // Ok
const_opt_uint_${TestPtrSize}_ptr_func(${pointer}) // Ok
% else:
int_${Size}_ptr_func(${pointer}) // expected-error {{}}
uint_${Size}_ptr_func(${pointer}) // expected-error {{}}
opt_int_${Size}_ptr_func(${pointer}) // expected-error {{}}
opt_uint_${Size}_ptr_func(${pointer}) // expected-error {{}}
const_int_${Size}_ptr_func(${pointer}) // expected-error {{}} expected-note {{}}
const_uint_${Size}_ptr_func(${pointer}) // expected-error {{}} expected-note {{}}
const_opt_int_${Size}_ptr_func(${pointer}) // expected-error {{}} expected-note {{}}
const_opt_uint_${Size}_ptr_func(${pointer}) // expected-error {{}} expected-note {{}}
% end
% end
% end
% for pointer in ['osptr', 'ouptr']:
% for Size in ['16', '32', '64']:
% if Size == TestPtrSize:
opt_int_${TestPtrSize}_ptr_func(${pointer}) // expected-error {{}}
opt_uint_${TestPtrSize}_ptr_func(${pointer}) // expected-error {{}}
const_opt_int_${TestPtrSize}_ptr_func(${pointer}) // Ok
const_opt_uint_${TestPtrSize}_ptr_func(${pointer}) // Ok
% else:
opt_int_${Size}_ptr_func(${pointer}) // expected-error {{}}
opt_uint_${Size}_ptr_func(${pointer}) // expected-error {{}}
const_opt_int_${Size}_ptr_func(${pointer}) // expected-error {{}} expected-note {{}}
const_opt_uint_${Size}_ptr_func(${pointer}) // expected-error {{}} expected-note {{}}
% end
% end
% end
}
% end
%for TestPtrSize in ['8', '16', '32', '64']:
// Mutable pointers can be converted to both immutable and mutable pointers when
// passed to C/ObjC imported functions.
func test_mutable_${TestPtrSize}_bit_ptrs(sptr: UnsafeMutablePointer<Int${TestPtrSize}>,
uptr: UnsafeMutablePointer<UInt${TestPtrSize}>,
osptr: UnsafeMutablePointer<Int${TestPtrSize}>?,
ouptr: UnsafeMutablePointer<UInt${TestPtrSize}>?) {
% for pointer in ['sptr', 'uptr']:
char_ptr_func(${pointer}) // Ok
opt_char_ptr_func(${pointer}) // Ok
const_char_ptr_func(${pointer}) // Ok
const_opt_char_ptr_func(${pointer}) // Ok
unsigned_char_ptr_func(${pointer}) // Ok
opt_unsigned_char_ptr_func(${pointer}) // Ok
const_unsigned_char_ptr_func(${pointer}) // Ok
const_opt_unsigned_char_ptr_func(${pointer}) // Ok
% end
% for pointer in ['osptr', 'ouptr']:
opt_char_ptr_func(${pointer}) // Ok
const_opt_char_ptr_func(${pointer}) // Ok
opt_unsigned_char_ptr_func(${pointer}) // Ok
const_opt_unsigned_char_ptr_func(${pointer}) // Ok
% end
% for pointer in ['sptr', 'uptr']:
% for Size in ['16', '32', '64']:
% if Size == TestPtrSize:
int_${TestPtrSize}_ptr_func(${pointer}) // Ok
uint_${TestPtrSize}_ptr_func(${pointer}) // Ok
opt_int_${TestPtrSize}_ptr_func(${pointer}) // Ok
opt_uint_${TestPtrSize}_ptr_func(${pointer}) // Ok
const_int_${TestPtrSize}_ptr_func(${pointer}) // Ok
const_uint_${TestPtrSize}_ptr_func(${pointer}) // Ok
const_opt_int_${TestPtrSize}_ptr_func(${pointer}) // Ok
const_opt_uint_${TestPtrSize}_ptr_func(${pointer}) // Ok
% else:
int_${Size}_ptr_func(${pointer}) // expected-error {{}} expected-note {{}}
uint_${Size}_ptr_func(${pointer}) // expected-error {{}} expected-note {{}}
opt_int_${Size}_ptr_func(${pointer}) // expected-error {{}} expected-note {{}}
opt_uint_${Size}_ptr_func(${pointer}) // expected-error {{}} expected-note {{}}
const_int_${Size}_ptr_func(${pointer}) // expected-error {{}} expected-note {{}}
const_uint_${Size}_ptr_func(${pointer}) // expected-error {{}} expected-note {{}}
const_opt_int_${Size}_ptr_func(${pointer}) // expected-error {{}} expected-note {{}}
const_opt_uint_${Size}_ptr_func(${pointer}) // expected-error {{}} expected-note {{}}
% end
% end
% end
% for pointer in ['osptr', 'ouptr']:
% for Size in ['16', '32', '64']:
% if Size == TestPtrSize:
opt_int_${TestPtrSize}_ptr_func(${pointer}) // Ok
opt_uint_${TestPtrSize}_ptr_func(${pointer}) // Ok
const_opt_int_${TestPtrSize}_ptr_func(${pointer}) // Ok
const_opt_uint_${TestPtrSize}_ptr_func(${pointer}) // Ok
% else:
opt_int_${Size}_ptr_func(${pointer}) // expected-error {{}} expected-note {{}}
opt_uint_${Size}_ptr_func(${pointer}) // expected-error {{}} expected-note {{}}
const_opt_int_${Size}_ptr_func(${pointer}) // expected-error {{}} expected-note {{}}
const_opt_uint_${Size}_ptr_func(${pointer}) // expected-error {{}} expected-note {{}}
% end
% end
% end
}
% end
func test_raw_ptr_value_to_optional_promotion(
riptr: UnsafeRawPointer,
rmptr: UnsafeMutableRawPointer) {
opt_char_ptr_func(riptr) // expected-error {{}}
const_opt_char_ptr_func(riptr) // Ok
opt_char_ptr_func(rmptr) // Ok
const_opt_char_ptr_func(rmptr) // Ok
opt_unsigned_char_ptr_func(riptr) // expected-error {{}}
const_opt_unsigned_char_ptr_func(riptr) // Ok
opt_unsigned_char_ptr_func(rmptr) // Ok
const_opt_unsigned_char_ptr_func(rmptr) // Ok
% for Ptr in ['riptr', 'rmptr']:
% for Size in ['16', '32', '64']:
opt_int_${Size}_ptr_func(${Ptr}) // expected-error {{}}
opt_uint_${Size}_ptr_func(${Ptr}) // expected-error {{}}
const_opt_int_${Size}_ptr_func(${Ptr}) // expected-error {{}}
const_opt_uint_${Size}_ptr_func(${Ptr}) // expected-error {{}}
% end
% end
}
func test_raw_ptr_optional_to_optional_conversion(
riptr: UnsafeRawPointer?,
rmptr: UnsafeMutableRawPointer?) {
opt_char_ptr_func(riptr) // expected-error {{}}
const_opt_char_ptr_func(riptr) // Ok
opt_char_ptr_func(rmptr) // Ok
const_opt_char_ptr_func(rmptr) // Ok
opt_unsigned_char_ptr_func(rmptr) // Ok
const_opt_unsigned_char_ptr_func(rmptr) // Ok
% for Ptr in ['riptr', 'rmptr']:
% for Size in ['16', '32', '64']:
opt_int_${Size}_ptr_func(${Ptr}) // expected-error {{}}
opt_uint_${Size}_ptr_func(${Ptr}) // expected-error {{}}
const_opt_int_${Size}_ptr_func(${Ptr}) // expected-error {{}}
const_opt_uint_${Size}_ptr_func(${Ptr}) // expected-error {{}}
% end
% end
}
// Check that implicit conversions don't make expressions ambiguous
func test_overloaded_ref_is_not_ambiguous() {
func overloaded_func() -> UnsafeRawPointer { fatalError() }
func overloaded_func() -> UnsafePointer<Int8> { fatalError() }
const_char_ptr_func(overloaded_func()) // Ok
const_opt_char_ptr_func(overloaded_func()) // Ok
}
func test_tailored_diagnostic(ptr: UnsafeRawPointer, tptr: UnsafePointer<Int8>) {
func swift_uint8_func(_: UnsafePointer<UInt8>) {}
func swift_int8_func(_: UnsafePointer<Int8>) {}
func opt_arg_func(_: UnsafePointer<Int8>?) {}
swift_uint8_func(ptr)
// expected-error@-1 {{cannot convert value of type 'UnsafeRawPointer' to expected argument type 'UnsafePointer<UInt8>' because local function 'swift_uint8_func' was not imported from C header}}
swift_uint8_func(tptr)
// expected-error@-1 {{cannot convert value of type 'UnsafePointer<Int8>' to expected argument type 'UnsafePointer<UInt8>' because local function 'swift_uint8_func' was not imported from C header}}
swift_int8_func(ptr)
// expected-error@-1 {{cannot convert value of type 'UnsafeRawPointer' to expected argument type 'UnsafePointer<Int8>' because local function 'swift_int8_func' was not imported from C header}}
swift_int8_func(tptr) // Ok
opt_arg_func(ptr)
// expected-error@-1 {{cannot convert value of type 'UnsafeRawPointer' to expected argument type 'UnsafePointer<Int8>?' because local function 'opt_arg_func' was not imported from C header}}
opt_arg_func(tptr) // Ok
let optrS8: UnsafePointer<Int8>? = nil
let optrU8: UnsafePointer<UInt8>? = nil
opt_arg_func(optrS8) // Ok
opt_arg_func(optrU8)
// expected-error@-1 {{cannot convert value of type 'UnsafePointer<UInt8>?' to expected argument type 'UnsafePointer<Int8>?' because local function 'opt_arg_func' was not imported from C header}}
}
func test_inout_to_pointer_conversion() {
% for Size in ['16', '32', '64']:
var x${Size}: Int${Size} = 0
void_ptr_func(&x${Size}) // Ok
const_void_ptr_func(&x${Size}) // Ok
opt_void_ptr_func(&x${Size}) // Ok
char_ptr_func(&x${Size}) // Ok
opt_char_ptr_func(&x${Size}) // Ok
const_char_ptr_func(&x${Size}) // Ok
const_opt_char_ptr_func(&x${Size}) // Ok
int_${Size}_ptr_func(&x${Size}) // Ok
uint_${Size}_ptr_func(&x${Size}) // Ok
opt_int_${Size}_ptr_func(&x${Size}) // Ok
opt_uint_${Size}_ptr_func(&x${Size}) // Ok
const_int_${Size}_ptr_func(&x${Size}) // OK
const_uint_${Size}_ptr_func(&x${Size}) // OK
const_opt_int_${Size}_ptr_func(&x${Size}) // OK
const_opt_uint_${Size}_ptr_func(&x${Size}) // OK
% end
}
func test_array_to_pointer_conversion() {
% for Size in ['16', '32', '64']:
var x${Size}: [Int${Size}] = []
void_ptr_func(&x${Size}) // Ok
const_void_ptr_func(x${Size}) // Ok
opt_void_ptr_func(x${Size})
// expected-error@-1 {{cannot convert value of type '[Int${Size}]' to expected argument type 'UnsafeMutableRawPointer?'}}
char_ptr_func(x${Size})
// expected-error@-1 {{cannot convert value of type '[Int${Size}]' to expected argument type 'UnsafeMutablePointer<CChar>' (aka 'UnsafeMutablePointer<Int8>')}}
opt_char_ptr_func(x${Size})
// expected-error@-1 {{cannot convert value of type '[Int${Size}]' to expected argument type 'UnsafeMutablePointer<CChar>?' (aka 'Optional<UnsafeMutablePointer<Int8>>')}}
const_char_ptr_func(x${Size}) // Ok
const_opt_char_ptr_func(x${Size}) // Ok
int_${Size}_ptr_func(x${Size})
// expected-error@-1 {{cannot convert value of type '[Int${Size}]' to expected argument type 'UnsafeMutablePointer<Int${Size}>'}}
uint_${Size}_ptr_func(x${Size})
// expected-error@-1 {{cannot convert value of type '[Int${Size}]' to expected argument type 'UnsafeMutablePointer<UInt${Size}>'}}
opt_int_${Size}_ptr_func(x${Size})
// expected-error@-1 {{cannot convert value of type '[Int${Size}]' to expected argument type 'UnsafeMutablePointer<Int${Size}>?'}}
opt_uint_${Size}_ptr_func(x${Size})
// expected-error@-1 {{cannot convert value of type '[Int${Size}]' to expected argument type 'UnsafeMutablePointer<UInt${Size}>?'}}
const_int_${Size}_ptr_func(x${Size}) // OK
const_uint_${Size}_ptr_func(x${Size}) // OK
const_opt_int_${Size}_ptr_func(x${Size}) // OK
const_opt_uint_${Size}_ptr_func(x${Size}) // OK
% end
}
|