Skip to content

Commit 216f8a0

Browse files
committed
[TypeChecker] NFC: Add test-cases for Swift -> C pointer conversions
Make sure that conversions are properly supported when arguments either is wrapped in an optional type or requires a value-to-optional promotion because parameter is optional pointer.
1 parent d95e92c commit 216f8a0

File tree

2 files changed

+160
-33
lines changed

2 files changed

+160
-33
lines changed

test/Constraints/Inputs/c_pointer_conversions.h

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,46 @@
22

33
#include "stdint.h"
44

5-
/*
6-
void doit(NSArray<NSCopying> *array);
7-
NSArray<NSCopying> *gimmeArray(void);
8-
*/
9-
105
void char_ptr_func(char * _Nonnull buffer);
116
void const_char_ptr_func(const char * _Nonnull buffer);
127

8+
void opt_char_ptr_func(char * _Nullable buffer);
9+
void const_opt_char_ptr_func(const char * _Nullable buffer);
10+
1311
void unsigned_char_ptr_func(unsigned char * _Nonnull buffer);
1412
void const_unsigned_char_ptr_func(const unsigned char * _Nonnull buffer);
1513

14+
void opt_unsigned_char_ptr_func(char * _Nullable buffer);
15+
void const_opt_unsigned_char_ptr_func(const char * _Nullable buffer);
16+
1617
void int_16_ptr_func(int16_t * _Nonnull buffer);
1718
void int_32_ptr_func(int32_t * _Nonnull buffer);
1819
void int_64_ptr_func(int64_t * _Nonnull buffer);
1920

21+
void opt_int_16_ptr_func(int16_t * _Nullable buffer);
22+
void opt_int_32_ptr_func(int32_t * _Nullable buffer);
23+
void opt_int_64_ptr_func(int64_t * _Nullable buffer);
24+
2025
void const_int_16_ptr_func(const int16_t * _Nonnull buffer);
2126
void const_int_32_ptr_func(const int32_t * _Nonnull buffer);
2227
void const_int_64_ptr_func(const int64_t * _Nonnull buffer);
2328

29+
void const_opt_int_16_ptr_func(const int16_t * _Nullable buffer);
30+
void const_opt_int_32_ptr_func(const int32_t * _Nullable buffer);
31+
void const_opt_int_64_ptr_func(const int64_t * _Nullable buffer);
32+
2433
void uint_16_ptr_func(uint16_t * _Nonnull buffer);
2534
void uint_32_ptr_func(uint32_t * _Nonnull buffer);
2635
void uint_64_ptr_func(uint64_t * _Nonnull buffer);
2736

37+
void opt_uint_16_ptr_func(uint16_t * _Nullable buffer);
38+
void opt_uint_32_ptr_func(uint32_t * _Nullable buffer);
39+
void opt_uint_64_ptr_func(uint64_t * _Nullable buffer);
40+
2841
void const_uint_16_ptr_func(const uint16_t * _Nonnull buffer);
2942
void const_uint_32_ptr_func(const uint32_t * _Nonnull buffer);
3043
void const_uint_64_ptr_func(const uint64_t * _Nonnull buffer);
3144

32-
/*
33-
@interface LinkDescriptor
34-
@end
35-
36-
@interface Test
37-
- (BOOL)f:(NSArray<LinkDescriptor *> * _Nonnull)descriptors error:(NSError * __autoreleasing * __nullable)error;
38-
- (void)f:(NSArray<LinkDescriptor *> * _Nonnull)descriptors completion:(nullable void (^)(BOOL success, NSError * _Nullable error))completion;
39-
@end
40-
*/
45+
void const_opt_uint_16_ptr_func(const uint16_t * _Nullable buffer);
46+
void const_opt_uint_32_ptr_func(const uint32_t * _Nullable buffer);
47+
void const_opt_uint_64_ptr_func(const uint64_t * _Nullable buffer);

test/Constraints/swift_to_c_pointer_conversions.swift.gyb

Lines changed: 139 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,37 +41,73 @@ func test_mutable_raw_pointer(ptr: UnsafeMutableRawPointer) {
4141
%for TestPtrSize in ['16', '32', '64']:
4242
// Immutable pointers can be converted only to their immutable (regardless of sign) counterparts.
4343
func test_${TestPtrSize}_bit_ptrs(sptr: UnsafePointer<Int${TestPtrSize}>,
44-
uptr: UnsafePointer<UInt${TestPtrSize}>) {
45-
char_ptr_func(sptr) // expected-error {{}}
46-
char_ptr_func(uptr) // expected-error {{}}
44+
uptr: UnsafePointer<UInt${TestPtrSize}>,
45+
osptr: UnsafePointer<Int${TestPtrSize}>?,
46+
ouptr: UnsafePointer<UInt${TestPtrSize}>?) {
47+
% for pointer in ['sptr', 'uptr']:
48+
char_ptr_func(${pointer}) // expected-error {{}}
49+
opt_char_ptr_func(${pointer}) // expected-error {{}}
4750

48-
const_char_ptr_func(sptr) // Ok
49-
const_char_ptr_func(uptr) // Ok
51+
const_char_ptr_func(${pointer}) // Ok
52+
const_opt_char_ptr_func(${pointer}) // Ok
5053

51-
unsigned_char_ptr_func(sptr) // expected-error {{}}
52-
unsigned_char_ptr_func(uptr) // expected-error {{}}
54+
unsigned_char_ptr_func(${pointer}) // expected-error {{}}
55+
unsigned_opt_char_ptr_func(${pointer}) // expected-error {{}}
5356

54-
const_unsigned_char_ptr_func(sptr) // Ok
55-
const_unsigned_char_ptr_func(uptr) // Ok
57+
const_unsigned_char_ptr_func(${pointer}) // Ok
58+
const_opt_unsigned_char_ptr_func(${pointer}) // Ok
59+
% end
60+
61+
% for pointer in ['osptr', 'ouptr']:
62+
opt_char_ptr_func(${pointer}) // expected-error {{}}
63+
const_opt_char_ptr_func(${pointer}) // Ok
64+
unsigned_opt_char_ptr_func(${pointer}) // expected-error {{}}
65+
const_opt_unsigned_char_ptr_func(${pointer}) // Ok
66+
% end
5667

5768
% for pointer in ['sptr', 'uptr']:
5869
% for Size in ['16', '32', '64']:
5970

6071
% if Size == TestPtrSize:
6172
int_${TestPtrSize}_ptr_func(${pointer}) // expected-error {{}}
6273
uint_${TestPtrSize}_ptr_func(${pointer}) // expected-error {{}}
74+
opt_int_${TestPtrSize}_ptr_func(${pointer}) // expected-error {{}}
75+
opt_uint_${TestPtrSize}_ptr_func(${pointer}) // expected-error {{}}
6376

6477
const_int_${TestPtrSize}_ptr_func(${pointer}) // Ok
6578
const_uint_${TestPtrSize}_ptr_func(${pointer}) // Ok
79+
const_opt_int_${TestPtrSize}_ptr_func(${pointer}) // Ok
80+
const_opt_uint_${TestPtrSize}_ptr_func(${pointer}) // Ok
6681
% else:
6782
int_${Size}_ptr_func(${pointer}) // expected-error {{}}
6883
uint_${Size}_ptr_func(${pointer}) // expected-error {{}}
84+
opt_int_${Size}_ptr_func(${pointer}) // expected-error {{}}
85+
opt_uint_${Size}_ptr_func(${pointer}) // expected-error {{}}
6986

7087
const_int_${Size}_ptr_func(${pointer}) // expected-error {{}} expected-note {{}}
7188
const_uint_${Size}_ptr_func(${pointer}) // expected-error {{}} expected-note {{}}
89+
const_opt_int_${Size}_ptr_func(${pointer}) // expected-error {{}} expected-note {{}}
90+
const_opt_uint_${Size}_ptr_func(${pointer}) // expected-error {{}} expected-note {{}}
7291
% end
7392
% end
7493
% end
94+
95+
96+
% for pointer in ['osptr', 'ouptr']:
97+
% for Size in ['16', '32', '64']:
98+
% if Size == TestPtrSize:
99+
opt_int_${TestPtrSize}_ptr_func(${pointer}) // expected-error {{}}
100+
opt_uint_${TestPtrSize}_ptr_func(${pointer}) // expected-error {{}}
101+
const_opt_int_${TestPtrSize}_ptr_func(${pointer}) // Ok
102+
const_opt_uint_${TestPtrSize}_ptr_func(${pointer}) // Ok
103+
% else:
104+
opt_int_${Size}_ptr_func(${pointer}) // expected-error {{}}
105+
opt_uint_${Size}_ptr_func(${pointer}) // expected-error {{}}
106+
const_opt_int_${Size}_ptr_func(${pointer}) // expected-error {{}} expected-note {{}}
107+
const_opt_uint_${Size}_ptr_func(${pointer}) // expected-error {{}} expected-note {{}}
108+
% end
109+
% end
110+
% end
75111
}
76112

77113
% end
@@ -80,37 +116,121 @@ func test_${TestPtrSize}_bit_ptrs(sptr: UnsafePointer<Int${TestPtrSize}>,
80116
// Mutable pointers can be converted to both immutable and mutable pointers when
81117
// passed to C/ObjC imported functions.
82118
func test_mutable_${TestPtrSize}_bit_ptrs(sptr: UnsafeMutablePointer<Int${TestPtrSize}>,
83-
uptr: UnsafeMutablePointer<UInt${TestPtrSize}>) {
84-
char_ptr_func(sptr) // Ok
85-
char_ptr_func(uptr) // Ok
119+
uptr: UnsafeMutablePointer<UInt${TestPtrSize}>,
120+
osptr: UnsafeMutablePointer<Int${TestPtrSize}>?,
121+
ouptr: UnsafeMutablePointer<UInt${TestPtrSize}>?) {
122+
% for pointer in ['sptr', 'uptr']:
123+
char_ptr_func(${pointer}) // Ok
124+
opt_char_ptr_func(${pointer}) // Ok
125+
126+
const_char_ptr_func(${pointer}) // Ok
127+
const_opt_char_ptr_func(${pointer}) // Ok
86128

87-
const_char_ptr_func(sptr) // Ok
88-
const_char_ptr_func(uptr) // Ok
129+
unsigned_char_ptr_func(${pointer}) // Ok
130+
opt_unsigned_char_ptr_func(${pointer}) // Ok
89131

90-
unsigned_char_ptr_func(sptr) // Ok
91-
unsigned_char_ptr_func(uptr) // Ok
132+
const_unsigned_char_ptr_func(${pointer}) // Ok
133+
const_opt_unsigned_char_ptr_func(${pointer}) // Ok
134+
% end
92135

93-
const_unsigned_char_ptr_func(sptr) // Ok
94-
const_unsigned_char_ptr_func(uptr) // Ok
136+
% for pointer in ['osptr', 'ouptr']:
137+
opt_char_ptr_func(${pointer}) // Ok
138+
const_opt_char_ptr_func(${pointer}) // Ok
139+
opt_unsigned_char_ptr_func(${pointer}) // Ok
140+
const_opt_unsigned_char_ptr_func(${pointer}) // Ok
141+
% end
95142

96143
% for pointer in ['sptr', 'uptr']:
97144
% for Size in ['16', '32', '64']:
98145

99146
% if Size == TestPtrSize:
100147
int_${TestPtrSize}_ptr_func(${pointer}) // Ok
101148
uint_${TestPtrSize}_ptr_func(${pointer}) // Ok
149+
opt_int_${TestPtrSize}_ptr_func(${pointer}) // Ok
150+
opt_uint_${TestPtrSize}_ptr_func(${pointer}) // Ok
102151

103152
const_int_${TestPtrSize}_ptr_func(${pointer}) // Ok
104153
const_uint_${TestPtrSize}_ptr_func(${pointer}) // Ok
154+
const_opt_int_${TestPtrSize}_ptr_func(${pointer}) // Ok
155+
const_opt_uint_${TestPtrSize}_ptr_func(${pointer}) // Ok
105156
% else:
106157
int_${Size}_ptr_func(${pointer}) // expected-error {{}} expected-note {{}}
107158
uint_${Size}_ptr_func(${pointer}) // expected-error {{}} expected-note {{}}
159+
opt_int_${Size}_ptr_func(${pointer}) // expected-error {{}} expected-note {{}}
160+
opt_uint_${Size}_ptr_func(${pointer}) // expected-error {{}} expected-note {{}}
108161

109162
const_int_${Size}_ptr_func(${pointer}) // expected-error {{}} expected-note {{}}
110163
const_uint_${Size}_ptr_func(${pointer}) // expected-error {{}} expected-note {{}}
164+
const_opt_int_${Size}_ptr_func(${pointer}) // expected-error {{}} expected-note {{}}
165+
const_opt_uint_${Size}_ptr_func(${pointer}) // expected-error {{}} expected-note {{}}
166+
% end
167+
% end
168+
% end
169+
170+
% for pointer in ['osptr', 'ouptr']:
171+
% for Size in ['16', '32', '64']:
172+
% if Size == TestPtrSize:
173+
opt_int_${TestPtrSize}_ptr_func(${pointer}) // Ok
174+
opt_uint_${TestPtrSize}_ptr_func(${pointer}) // Ok
175+
const_opt_int_${TestPtrSize}_ptr_func(${pointer}) // Ok
176+
const_opt_uint_${TestPtrSize}_ptr_func(${pointer}) // Ok
177+
% else:
178+
opt_int_${Size}_ptr_func(${pointer}) // expected-error {{}} expected-note {{}}
179+
opt_uint_${Size}_ptr_func(${pointer}) // expected-error {{}} expected-note {{}}
180+
const_opt_int_${Size}_ptr_func(${pointer}) // expected-error {{}} expected-note {{}}
181+
const_opt_uint_${Size}_ptr_func(${pointer}) // expected-error {{}} expected-note {{}}
182+
% end
111183
% end
112184
% end
113-
% end
114185
}
115186

116187
% end
188+
189+
func test_raw_ptr_value_to_optional_promotion(
190+
riptr: UnsafeRawPointer,
191+
rmptr: UnsafeMutableRawPointer) {
192+
opt_char_ptr_func(riptr) // expected-error {{}}
193+
const_opt_char_ptr_func(riptr) // Ok
194+
195+
opt_char_ptr_func(rmptr) // Ok
196+
const_opt_char_ptr_func(rmptr) // Ok
197+
198+
opt_unsigned_char_ptr_func(riptr) // expected-error {{}}
199+
const_opt_unsigned_char_ptr_func(riptr) // Ok
200+
201+
opt_unsigned_char_ptr_func(rmptr) // Ok
202+
const_opt_unsigned_char_ptr_func(rmptr) // Ok
203+
204+
% for Ptr in ['riptr', 'rmptr']:
205+
% for Size in ['16', '32', '64']:
206+
opt_int_${Size}_ptr_func(${Ptr}) // expected-error {{}}
207+
opt_uint_${Size}_ptr_func(${Ptr}) // expected-error {{}}
208+
209+
const_opt_int_${Size}_ptr_func(${Ptr}) // expected-error {{}}
210+
const_opt_uint_${Size}_ptr_func(${Ptr}) // expected-error {{}}
211+
% end
212+
% end
213+
}
214+
215+
func test_raw_ptr_optional_to_optional_conversion(
216+
riptr: UnsafeRawPointer?,
217+
rmptr: UnsafeMutableRawPointer?) {
218+
opt_char_ptr_func(riptr) // expected-error {{}}
219+
const_opt_char_ptr_func(riptr) // Ok
220+
221+
opt_char_ptr_func(rmptr) // Ok
222+
const_opt_char_ptr_func(rmptr) // Ok
223+
224+
opt_unsigned_char_ptr_func(rmptr) // Ok
225+
const_opt_unsigned_char_ptr_func(rmptr) // Ok
226+
227+
% for Ptr in ['riptr', 'rmptr']:
228+
% for Size in ['16', '32', '64']:
229+
opt_int_${Size}_ptr_func(${Ptr}) // expected-error {{}}
230+
opt_uint_${Size}_ptr_func(${Ptr}) // expected-error {{}}
231+
232+
const_opt_int_${Size}_ptr_func(${Ptr}) // expected-error {{}}
233+
const_opt_uint_${Size}_ptr_func(${Ptr}) // expected-error {{}}
234+
% end
235+
% end
236+
}

0 commit comments

Comments
 (0)