Skip to content

Commit f6d851d

Browse files
committed
Suppress pointer conversion warnings for BitwiseCopyable elements.
Now that BitwiseCopyable is accepted, it should work as the recommended workaround for unsafe pointer conversion warnings: Forming 'UnsafeMutableRawPointer' to a variable of type 'S'; this is likely incorrect because 'S' may contain an object reference. The check for trivial element types is in SILGenExpr, diagnoseImplicitRawConversion. For now, we can hack SILGenExpr to specifically disable the warning for BitwiseCopyable, just as it was done for FixedWidthInteger in prior releases. Fixes rdar://128229439 (Conversion from BitwiseCopyable to UnsafeRawPointer should not warn.) (cherry picked from commit 6bd0ef0)
1 parent 7a48ca7 commit f6d851d

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

lib/SILGen/SILGenExpr.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6170,6 +6170,11 @@ static void diagnoseImplicitRawConversion(Type sourceTy, Type pointerTy,
61706170
return;
61716171

61726172
auto *SM = SGF.getModule().getSwiftModule();
6173+
if (auto *bitwiseCopyableDecl = SM->getASTContext().getProtocol(
6174+
KnownProtocolKind::BitwiseCopyable)) {
6175+
if (SM->checkConformance(eltTy, bitwiseCopyableDecl))
6176+
return;
6177+
}
61736178
if (auto *fixedWidthIntegerDecl = SM->getASTContext().getProtocol(
61746179
KnownProtocolKind::FixedWidthInteger)) {
61756180
if (SM->checkConformance(eltTy, fixedWidthIntegerDecl))

test/SILGen/diagnose_implicit_raw_conversion.swift

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ struct NonTrivial {
2323
}
2424

2525
// SILGen diagnostics prohibits these implicit casts:
26-
func test_errors<T>(arg: T, sarg: String) {
26+
func test_errors<T>(arg: T, sarg: String, anyBCArg: BitwiseCopyable) {
2727
var nonTrivial = NonTrivial(c: C())
2828
readBytes(&nonTrivial) // expected-warning {{forming 'UnsafeRawPointer' to a variable of type 'NonTrivial'; this is likely incorrect because 'NonTrivial' may contain an object reference.}}
2929
writeBytes(&nonTrivial) // expected-warning {{forming 'UnsafeMutableRawPointer' to a variable of type 'NonTrivial'; this is likely incorrect because 'NonTrivial' may contain an object reference.}}
@@ -50,6 +50,28 @@ func test_errors<T>(arg: T, sarg: String) {
5050
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.}}
5151
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.}}
5252

53+
var anyBC: BitwiseCopyable = anyBCArg
54+
readBytes(&anyBC) // expected-warning {{forming 'UnsafeRawPointer' to a variable of type 'any BitwiseCopyable'; this is likely incorrect because 'any BitwiseCopyable' may contain an object reference.}}
55+
writeBytes(&anyBC) // expected-warning {{forming 'UnsafeMutableRawPointer' to a variable of type 'any BitwiseCopyable'; this is likely incorrect because 'any BitwiseCopyable' may contain an object reference.}}
56+
read_char(&anyBC) // expected-warning {{forming 'UnsafePointer<Int8>' to a variable of type 'any BitwiseCopyable'; this is likely incorrect because 'any BitwiseCopyable' may contain an object reference.}}
57+
write_char(&anyBC) // expected-warning {{forming 'UnsafeMutablePointer<Int8>' to a variable of type 'any BitwiseCopyable'; this is likely incorrect because 'any BitwiseCopyable' may contain an object reference.}}
58+
read_uchar(&anyBC) // expected-warning {{forming 'UnsafePointer<UInt8>' to a variable of type 'any BitwiseCopyable'; this is likely incorrect because 'any BitwiseCopyable' may contain an object reference.}}
59+
write_uchar(&anyBC) // expected-warning {{forming 'UnsafeMutablePointer<UInt8>' to a variable of type 'any BitwiseCopyable'; this is likely incorrect because 'any BitwiseCopyable' may contain an object reference.}}
60+
61+
let constBCArray: [BitwiseCopyable] = [anyBCArg]
62+
readBytes(constBCArray) // expected-warning {{forming 'UnsafeRawPointer' to a variable of type '[any BitwiseCopyable]'; this is likely incorrect because 'any BitwiseCopyable' may contain an object reference.}}
63+
read_char(constBCArray) // expected-warning {{forming 'UnsafePointer<CChar>' (aka 'UnsafePointer<Int8>') to a variable of type '[any BitwiseCopyable]'; this is likely incorrect because 'any BitwiseCopyable' may contain an object reference.}}
64+
read_uchar(constBCArray) // expected-warning {{forming 'UnsafePointer<UInt8>' to a variable of type '[any BitwiseCopyable]'; this is likely incorrect because 'any BitwiseCopyable' may contain an object reference.}}
65+
66+
var bcArray: [BitwiseCopyable] = [anyBCArg]
67+
readBytes(&bcArray) // expected-warning {{forming 'UnsafeRawPointer' to a variable of type '[any BitwiseCopyable]'; this is likely incorrect because 'any BitwiseCopyable' may contain an object reference.}}
68+
writeBytes(&bcArray) // expected-warning {{forming 'UnsafeMutableRawPointer' to a variable of type '[any BitwiseCopyable]'; this is likely incorrect because 'any BitwiseCopyable' may contain an object reference.}}
69+
70+
read_char(&bcArray) // expected-warning {{forming 'UnsafePointer<Int8>' to a variable of type 'Array<any BitwiseCopyable>'; this is likely incorrect because 'any BitwiseCopyable' may contain an object reference.}}
71+
write_char(&bcArray) // expected-warning {{forming 'UnsafeMutablePointer<Int8>' to a variable of type 'Array<any BitwiseCopyable>'; this is likely incorrect because 'any BitwiseCopyable' may contain an object reference.}}
72+
read_uchar(&bcArray) // expected-warning {{forming 'UnsafePointer<UInt8>' to a variable of type 'Array<any BitwiseCopyable>'; this is likely incorrect because 'any BitwiseCopyable' may contain an object reference.}}
73+
write_uchar(&bcArray) // expected-warning {{forming 'UnsafeMutablePointer<UInt8>' to a variable of type 'Array<any BitwiseCopyable>'; this is likely incorrect because 'any BitwiseCopyable' may contain an object reference.}}
74+
5375
var string: String = sarg
5476
readBytes(&string) // expected-warning {{forming 'UnsafeRawPointer' to an inout variable of type String exposes the internal representation rather than the string contents.}}
5577
writeBytes(&string) // expected-warning {{forming 'UnsafeMutableRawPointer' to an inout variable of type String exposes the internal representation rather than the string contents.}}
@@ -105,7 +127,8 @@ func test_explicit<T>(arg: T, sarg: String) {
105127
}
106128

107129
// SILGen diagnostics accepts these implicit casts:
108-
func test_accepted<I: FixedWidthInteger>(intArg: I, sarg: String, simdArg: SIMD4<Float>) {
130+
func test_accepted<I: FixedWidthInteger, BC: BitwiseCopyable>(intArg: I, bcArg: BC,
131+
sarg: String, simdArg: SIMD4<Float>) {
109132
var aggregate = Aggregate(pointer: UnsafeRawPointer(bitPattern: 0), value: 0)
110133
readBytes(&aggregate)
111134
writeBytes(&aggregate)
@@ -135,6 +158,27 @@ func test_accepted<I: FixedWidthInteger>(intArg: I, sarg: String, simdArg: SIMD4
135158
read_uchar(&intArray)
136159
write_uchar(&intArray)
137160

161+
var bc: BC = bcArg
162+
readBytes(&bc)
163+
writeBytes(&bc)
164+
read_char(&bc)
165+
write_char(&bc)
166+
read_uchar(&bc)
167+
write_uchar(&bc)
168+
169+
let constBCArray: [BC] = [bcArg]
170+
readBytes(constBCArray)
171+
read_char(constBCArray)
172+
read_uchar(constBCArray)
173+
174+
var bcArray: [BC] = [bcArg]
175+
readBytes(&bcArray)
176+
writeBytes(&bcArray)
177+
read_char(&bcArray)
178+
write_char(&bcArray)
179+
read_uchar(&bcArray)
180+
write_uchar(&bcArray)
181+
138182
let constByteArray: [UInt8] = [0]
139183
readBytes(constByteArray)
140184
read_char(constByteArray)

0 commit comments

Comments
 (0)