|
| 1 | +// RUN: %target-swift-emit-silgen -import-objc-header %S/Inputs/readbytes.h %s -o /dev/null -verify |
| 2 | +// |
| 3 | +// Diagnose invalid conversion from an inout argument to a raw pointer. |
| 4 | + |
| 5 | +func readBytes(_ pointer: UnsafeRawPointer) {} |
| 6 | +func writeBytes(_ pointer: UnsafeMutableRawPointer) {} |
| 7 | +func readInt8(_ pointer: UnsafePointer<Int8>) {} |
| 8 | +func writeInt8(_ pointer: UnsafeMutablePointer<Int8>) {} |
| 9 | +func readUInt8(_ pointer: UnsafePointer<UInt8>) {} |
| 10 | +func writeUInt8(_ pointer: UnsafeMutablePointer<UInt8>) {} |
| 11 | + |
| 12 | +struct Aggregate { |
| 13 | + var pointer: UnsafeRawPointer? |
| 14 | + var value: Int32 |
| 15 | +} |
| 16 | + |
| 17 | +class C { |
| 18 | + init() {} |
| 19 | +} |
| 20 | + |
| 21 | +struct NonTrivial { |
| 22 | + var c: C |
| 23 | +} |
| 24 | + |
| 25 | +// SILGen diagnostics prohibits these implicit casts: |
| 26 | +func test_errors<T>(arg: T, sarg: String) { |
| 27 | + var nonTrivial = NonTrivial(c: C()) |
| 28 | + readBytes(&nonTrivial) // expected-warning {{forming 'UnsafeRawPointer' to a variable of type 'NonTrivial'; this is likely incorrect because 'NonTrivial' may contain an object reference.}} |
| 29 | + writeBytes(&nonTrivial) // expected-warning {{forming 'UnsafeMutableRawPointer' to a variable of type 'NonTrivial'; this is likely incorrect because 'NonTrivial' may contain an object reference.}} |
| 30 | + |
| 31 | + var t: T = arg |
| 32 | + readBytes(&t) // expected-warning {{forming 'UnsafeRawPointer' to a variable of type 'T'; this is likely incorrect because 'T' may contain an object reference.}} |
| 33 | + writeBytes(&t) // expected-warning {{forming 'UnsafeMutableRawPointer' to a variable of type 'T'; this is likely incorrect because 'T' may contain an object reference.}} |
| 34 | + read_char(&t) // expected-warning {{forming 'UnsafePointer<Int8>' to a variable of type 'T'; this is likely incorrect because 'T' may contain an object reference.}} |
| 35 | + write_char(&t) // expected-warning {{forming 'UnsafeMutablePointer<Int8>' to a variable of type 'T'; this is likely incorrect because 'T' may contain an object reference.}} |
| 36 | + read_uchar(&t) // expected-warning {{forming 'UnsafePointer<UInt8>' to a variable of type 'T'; this is likely incorrect because 'T' may contain an object reference.}} |
| 37 | + write_uchar(&t) // expected-warning {{forming 'UnsafeMutablePointer<UInt8>' to a variable of type 'T'; this is likely incorrect because 'T' may contain an object reference.}} |
| 38 | + |
| 39 | + let constArray: [T] = [arg] |
| 40 | + readBytes(constArray) // expected-warning {{forming 'UnsafeRawPointer' to a variable of type '[T]'; this is likely incorrect because 'T' may contain an object reference.}} |
| 41 | + |
| 42 | + var array: [T] = [arg] |
| 43 | + readBytes(&array) // expected-warning {{forming 'UnsafeRawPointer' to a variable of type '[T]'; this is likely incorrect because 'T' may contain an object reference.}} |
| 44 | + writeBytes(&array) // expected-warning {{forming 'UnsafeMutableRawPointer' to a variable of type '[T]'; this is likely incorrect because 'T' may contain an object reference.}} |
| 45 | + |
| 46 | + read_char(&array) // expected-warning {{forming 'UnsafePointer<Int8>' to a variable of type 'Array<T>'; this is likely incorrect because 'T' may contain an object reference.}} |
| 47 | + write_char(&array) // expected-warning {{forming 'UnsafeMutablePointer<Int8>' to a variable of type 'Array<T>'; this is likely incorrect because 'T' may contain an object reference.}} |
| 48 | + read_uchar(&array) // expected-warning {{forming 'UnsafePointer<UInt8>' to a variable of type 'Array<T>'; this is likely incorrect because 'T' may contain an object reference.}} |
| 49 | + write_uchar(&array) // expected-warning {{forming 'UnsafeMutablePointer<UInt8>' to a variable of type 'Array<T>'; this is likely incorrect because 'T' may contain an object reference.}} |
| 50 | + |
| 51 | + var string: String = sarg |
| 52 | + readBytes(&string) // expected-warning {{forming 'UnsafeRawPointer' to an inout variable of type String exposes the internal representation rather than the string contents.}} |
| 53 | + writeBytes(&string) // expected-warning {{forming 'UnsafeMutableRawPointer' to an inout variable of type String exposes the internal representation rather than the string contents.}} |
| 54 | + read_char(&string) // expected-warning {{forming 'UnsafePointer<Int8>' to an inout variable of type String exposes the internal representation rather than the string contents.}} |
| 55 | + write_char(&string) // expected-warning {{forming 'UnsafeMutablePointer<Int8>' to an inout variable of type String exposes the internal representation rather than the string contents.}} |
| 56 | + read_uchar(&string) // expected-warning {{forming 'UnsafePointer<UInt8>' to an inout variable of type String exposes the internal representation rather than the string contents.}} |
| 57 | + write_uchar(&string) // expected-warning {{forming 'UnsafeMutablePointer<UInt8>' to an inout variable of type String exposes the internal representation rather than the string contents.}} |
| 58 | +} |
| 59 | + |
| 60 | +// Test the explicit cast workaround for all of the above errors |
| 61 | +func test_explicit<T>(arg: T, sarg: String) { |
| 62 | + var nonTrivial = NonTrivial(c: C()) |
| 63 | + withUnsafeBytes(of: &nonTrivial) { |
| 64 | + readBytes($0.baseAddress!) |
| 65 | + } |
| 66 | + withUnsafeMutableBytes(of: &nonTrivial) { |
| 67 | + writeBytes($0.baseAddress!) |
| 68 | + } |
| 69 | + |
| 70 | + var t: T = arg |
| 71 | + withUnsafeBytes(of: &t) { |
| 72 | + readBytes($0.baseAddress!) |
| 73 | + read_char($0.baseAddress!) |
| 74 | + read_uchar($0.baseAddress!) |
| 75 | + } |
| 76 | + withUnsafeMutableBytes(of: &t) { |
| 77 | + writeBytes($0.baseAddress!) |
| 78 | + write_char($0.baseAddress!) |
| 79 | + write_uchar($0.baseAddress!) |
| 80 | + } |
| 81 | + let constArray: [T] = [arg] |
| 82 | + constArray.withUnsafeBytes() { |
| 83 | + readBytes($0.baseAddress!) |
| 84 | + } |
| 85 | + var array: [T] = [arg] |
| 86 | + array.withUnsafeBytes() { |
| 87 | + readBytes($0.baseAddress!) |
| 88 | + read_char($0.baseAddress!) |
| 89 | + read_uchar($0.baseAddress!) |
| 90 | + } |
| 91 | + array.withUnsafeMutableBytes() { |
| 92 | + writeBytes($0.baseAddress!) |
| 93 | + write_char($0.baseAddress!) |
| 94 | + write_uchar($0.baseAddress!) |
| 95 | + } |
| 96 | + |
| 97 | + let string: String = sarg |
| 98 | + readBytes(string) |
| 99 | + read_char(string) |
| 100 | + read_uchar(string) |
| 101 | +} |
| 102 | + |
| 103 | +// SILGen diagnostics accepts these implicit casts: |
| 104 | +func test_accepted<I: FixedWidthInteger>(intArg: I, sarg: String, simdArg: SIMD4<Float>) { |
| 105 | + var aggregate = Aggregate(pointer: UnsafeRawPointer(bitPattern: 0), value: 0) |
| 106 | + readBytes(&aggregate) |
| 107 | + writeBytes(&aggregate) |
| 108 | + read_char(&aggregate) |
| 109 | + write_char(&aggregate) |
| 110 | + read_uchar(&aggregate) |
| 111 | + write_uchar(&aggregate) |
| 112 | + |
| 113 | + var int: I = intArg |
| 114 | + readBytes(&int) |
| 115 | + writeBytes(&int) |
| 116 | + read_char(&int) |
| 117 | + write_char(&int) |
| 118 | + read_uchar(&int) |
| 119 | + write_uchar(&int) |
| 120 | + |
| 121 | + let constArray: [I] = [intArg] |
| 122 | + readBytes(constArray) |
| 123 | + // read_char(constArray) - this case never worked because of a bug in SE-0324 |
| 124 | + // read_uchar(constArray) - this case never worked because of a bug in SE-0324 |
| 125 | + |
| 126 | + var intArray: [I] = [intArg] |
| 127 | + readBytes(&intArray) |
| 128 | + writeBytes(&intArray) |
| 129 | + read_char(&intArray) |
| 130 | + write_char(&intArray) |
| 131 | + read_uchar(&intArray) |
| 132 | + write_uchar(&intArray) |
| 133 | + |
| 134 | + var byteArray: [UInt8] = [0] |
| 135 | + readBytes(&byteArray) |
| 136 | + writeBytes(&byteArray) |
| 137 | + readUInt8(&byteArray) |
| 138 | + writeUInt8(&byteArray) |
| 139 | + write_char(&byteArray) |
| 140 | + read_uchar(byteArray) |
| 141 | + write_uchar(&byteArray) |
| 142 | + |
| 143 | + let string: String = sarg |
| 144 | + readBytes(string) |
| 145 | + readUInt8(string) |
| 146 | + read_char(string) |
| 147 | + read_uchar(string) |
| 148 | + |
| 149 | + var simd: SIMD4<Float> = simdArg |
| 150 | + readBytes(&simd) |
| 151 | + writeBytes(&simd) |
| 152 | + read_char(&simd) |
| 153 | + write_char(&simd) |
| 154 | + read_uchar(&simd) |
| 155 | + write_uchar(&simd) |
| 156 | +} |
0 commit comments