Skip to content

Commit 2623c4b

Browse files
authored
Merge pull request swiftlang#83863 from xedin/rdar-158629300-6.2
[6.2][CSSimplify] SE-0324: Use correct conversion when converting array types
2 parents d03de08 + 26ba69a commit 2623c4b

File tree

3 files changed

+64
-10
lines changed

3 files changed

+64
-10
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8070,7 +8070,8 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
80708070
// for imported declarations.
80718071
if (isArgumentOfImportedDecl(locator)) {
80728072
conversionsOrFixes.push_back(
8073-
ConversionRestrictionKind::InoutToCPointer);
8073+
baseIsArray ? ConversionRestrictionKind::ArrayToCPointer
8074+
: ConversionRestrictionKind::InoutToCPointer);
80748075
}
80758076
}
80768077
}

test/SILGen/diagnose_implicit_raw_conversion.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ func test_errors<T>(arg: T, sarg: String, anyBCArg: BitwiseCopyable) {
4545
readBytes(&array) // expected-warning {{forming 'UnsafeRawPointer' to a variable of type '[T]'; this is likely incorrect because 'T' may contain an object reference.}}
4646
writeBytes(&array) // expected-warning {{forming 'UnsafeMutableRawPointer' to a variable of type '[T]'; this is likely incorrect because 'T' may contain an object reference.}}
4747

48-
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.}}
49-
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.}}
50-
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.}}
51-
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.}}
48+
read_char(&array) // expected-warning {{forming 'UnsafePointer<CChar>' (aka 'UnsafePointer<Int8>') to a variable of type '[T]'; this is likely incorrect because 'T' may contain an object reference.}}
49+
write_char(&array) // expected-warning {{forming 'UnsafeMutablePointer<CChar>' (aka 'UnsafeMutablePointer<Int8>') to a variable of type '[T]'; this is likely incorrect because 'T' may contain an object reference.}}
50+
read_uchar(&array) // expected-warning {{forming 'UnsafePointer<UInt8>' to a variable of type '[T]'; this is likely incorrect because 'T' may contain an object reference.}}
51+
write_uchar(&array) // expected-warning {{forming 'UnsafeMutablePointer<UInt8>' to a variable of type '[T]'; this is likely incorrect because 'T' may contain an object reference.}}
5252

5353
var anyBC: BitwiseCopyable = anyBCArg
5454
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.}}
@@ -67,10 +67,10 @@ func test_errors<T>(arg: T, sarg: String, anyBCArg: BitwiseCopyable) {
6767
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.}}
6868
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.}}
6969

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.}}
70+
read_char(&bcArray) // 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.}}
71+
write_char(&bcArray) // expected-warning {{forming 'UnsafeMutablePointer<CChar>' (aka 'UnsafeMutablePointer<Int8>') to a variable of type '[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 '[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 '[any BitwiseCopyable]'; this is likely incorrect because 'any BitwiseCopyable' may contain an object reference.}}
7474

7575
var string: String = sarg
7676
readBytes(&string) // expected-warning {{forming 'UnsafeRawPointer' to an inout variable of type String exposes the internal representation rather than the string contents.}}

test/SILGen/pointer_conversion.swift

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
// RUN: %target-swift-emit-silgen -Xllvm -sil-print-types -module-name pointer_conversion -sdk %S/Inputs -I %S/Inputs -enable-source-import %s -enable-objc-interop | %FileCheck %s
1+
// RUN: %target-swift-emit-silgen -Xllvm -sil-print-types -module-name pointer_conversion \
2+
// RUN: -sdk %S/Inputs -I %S/Inputs -enable-source-import %s -enable-objc-interop \
3+
// RUN: -import-objc-header %S/Inputs/readbytes.h \
4+
// RUN: | %FileCheck %s
25

36
// FIXME: rdar://problem/19648117 Needs splitting objc parts out
47
// REQUIRES: objc_interop
@@ -138,6 +141,56 @@ func arrayToPointer() {
138141
// CHECK: [[TAKES_OPT_CONST_POINTER:%.*]] = function_ref @$s18pointer_conversion20takesOptConstPointer_3andySPySiGSg_SitF :
139142
// CHECK: apply [[TAKES_OPT_CONST_POINTER]]([[OPTPTR]], [[RESULT1]])
140143
// CHECK: destroy_value [[OWNER]]
144+
145+
// -- test implicit conversions allowed by C functions
146+
read_char(ints)
147+
// CHECK: [[CONVERT_FN:%.*]] = function_ref @$ss35_convertConstArrayToPointerArgumentyyXlSg_q_tSayxGs01_E0R_r0_lF
148+
// CHECK: apply [[CONVERT_FN]]<Int, UnsafePointer<Int8>>([[PTR_RESULT:%[0-9]+]], {{.*}})
149+
// CHECK: [[PTR:%[0-9]+]] = load [trivial] [[PTR_RESULT]] : $*UnsafePointer<Int8>
150+
// CHECK: [[PTR_MD:%[0-9]+]] = mark_dependence [[PTR]] : $UnsafePointer<Int8> on
151+
// CHECK: [[PTR_OPT:%[0-9]+]] = enum $Optional<UnsafePointer<Int8>>, #Optional.some!enumelt, [[PTR_MD]]
152+
// CHECK: [[READ_FN:%.*]] = function_ref @read_char : $@convention(c) (Optional<UnsafePointer<Int8>>) -> ()
153+
// CHECK: apply [[READ_FN]]([[PTR_OPT]]) : $@convention(c) (Optional<UnsafePointer<Int8>>) -> ()
154+
read_uchar(ints);
155+
// CHECK: [[CONVERT_FN:%.*]] = function_ref @$ss35_convertConstArrayToPointerArgumentyyXlSg_q_tSayxGs01_E0R_r0_lF
156+
// CHECK: apply [[CONVERT_FN]]<Int, UnsafePointer<UInt8>>([[PTR_RESULT:%[0-9]+]], {{.*}})
157+
// CHECK: [[PTR:%[0-9]+]] = load [trivial] [[PTR_RESULT]] : $*UnsafePointer<UInt8>
158+
// CHECK: [[PTR_MD:%[0-9]+]] = mark_dependence [[PTR]] : $UnsafePointer<UInt8> on
159+
// CHECK: [[PTR_OPT:%[0-9]+]] = enum $Optional<UnsafePointer<UInt8>>, #Optional.some!enumelt, [[PTR_MD]]
160+
// CHECK: [[READ_FN:%.*]] = function_ref @read_uchar : $@convention(c) (Optional<UnsafePointer<UInt8>>) -> ()
161+
// CHECK: apply [[READ_FN]]([[PTR_OPT]]) : $@convention(c) (Optional<UnsafePointer<UInt8>>) -> ()
162+
read_void(ints);
163+
// CHECK: [[CONVERT_FN:%.*]] = function_ref @$ss35_convertConstArrayToPointerArgumentyyXlSg_q_tSayxGs01_E0R_r0_lF
164+
// CHECK: apply [[CONVERT_FN]]<Int, UnsafeRawPointer>([[PTR_RESULT:%[0-9]+]], {{.*}})
165+
// CHECK: [[PTR:%[0-9]+]] = load [trivial] [[PTR_RESULT]] : $*UnsafeRawPointer
166+
// CHECK: [[PTR_MD:%[0-9]+]] = mark_dependence [[PTR]] : $UnsafeRawPointer on
167+
// CHECK: [[PTR_OPT:%[0-9]+]] = enum $Optional<UnsafeRawPointer>, #Optional.some!enumelt, [[PTR_MD]]
168+
// CHECK: [[READ_FN:%.*]] = function_ref @read_void : $@convention(c) (Optional<UnsafeRawPointer>) -> ()
169+
// CHECK: apply [[READ_FN]]([[PTR_OPT]]) : $@convention(c) (Optional<UnsafeRawPointer>) -> ()
170+
write_char(&ints);
171+
// CHECK: [[CONVERT_FN:%.*]] = function_ref @$ss37_convertMutableArrayToPointerArgumentyyXlSg_q_tSayxGzs01_E0R_r0_lF
172+
// CHECK: apply [[CONVERT_FN]]<Int, UnsafeMutablePointer<Int8>>([[PTR_RESULT:%[0-9]+]], {{.*}})
173+
// CHECK: [[PTR:%[0-9]+]] = load [trivial] [[PTR_RESULT]] : $*UnsafeMutablePointer<Int8>
174+
// CHECK: [[PTR_MD:%[0-9]+]] = mark_dependence [[PTR]] : $UnsafeMutablePointer<Int8> on
175+
// CHECK: [[PTR_OPT:%[0-9]+]] = enum $Optional<UnsafeMutablePointer<Int8>>, #Optional.some!enumelt, [[PTR_MD]]
176+
// CHECK: [[WRITE_FN:%.*]] = function_ref @write_char : $@convention(c) (Optional<UnsafeMutablePointer<Int8>>) -> ()
177+
// CHECK: apply [[WRITE_FN]]([[PTR_OPT]]) : $@convention(c) (Optional<UnsafeMutablePointer<Int8>>) -> ()
178+
write_uchar(&ints);
179+
// CHECK: [[CONVERT_FN:%.*]] = function_ref @$ss37_convertMutableArrayToPointerArgumentyyXlSg_q_tSayxGzs01_E0R_r0_lF
180+
// CHECK: apply [[CONVERT_FN]]<Int, UnsafeMutablePointer<UInt8>>([[PTR_RESULT:%[0-9]+]], {{.*}})
181+
// CHECK: [[PTR:%[0-9]+]] = load [trivial] [[PTR_RESULT]] : $*UnsafeMutablePointer<UInt8>
182+
// CHECK: [[PTR_MD:%[0-9]+]] = mark_dependence [[PTR]] : $UnsafeMutablePointer<UInt8> on
183+
// CHECK: [[PTR_OPT:%[0-9]+]] = enum $Optional<UnsafeMutablePointer<UInt8>>, #Optional.some!enumelt, [[PTR_MD]]
184+
// CHECK: [[WRITE_FN:%.*]] = function_ref @write_uchar : $@convention(c) (Optional<UnsafeMutablePointer<UInt8>>) -> ()
185+
// CHECK: apply [[WRITE_FN]]([[PTR_OPT]]) : $@convention(c) (Optional<UnsafeMutablePointer<UInt8>>) -> ()
186+
write_void(&ints);
187+
// CHECK: [[CONVERT_FN:%.*]] = function_ref @$ss37_convertMutableArrayToPointerArgumentyyXlSg_q_tSayxGzs01_E0R_r0_lF
188+
// CHECK: apply [[CONVERT_FN]]<Int, UnsafeMutableRawPointer>([[PTR_RESULT:%[0-9]+]], {{.*}})
189+
// CHECK: [[PTR:%[0-9]+]] = load [trivial] [[PTR_RESULT]] : $*UnsafeMutableRawPointer
190+
// CHECK: [[PTR_MD:%[0-9]+]] = mark_dependence [[PTR]] : $UnsafeMutableRawPointer on
191+
// CHECK: [[PTR_OPT:%[0-9]+]] = enum $Optional<UnsafeMutableRawPointer>, #Optional.some!enumelt, [[PTR_MD]]
192+
// CHECK: [[WRITE_FN:%.*]] = function_ref @write_void : $@convention(c) (Optional<UnsafeMutableRawPointer>) -> ()
193+
// CHECK: apply [[WRITE_FN]]([[PTR_OPT]]) : $@convention(c) (Optional<UnsafeMutableRawPointer>) -> ()
141194
}
142195

143196
// CHECK-LABEL: sil hidden [ossa] @$s18pointer_conversion15stringToPointeryySSF

0 commit comments

Comments
 (0)