Skip to content

Commit 621ea81

Browse files
committed
[ConstraintSystem] NFC: Add test-cases for interactions between Swift and C pointers
1 parent 9e7a670 commit 621ea81

File tree

2 files changed

+156
-0
lines changed

2 files changed

+156
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
@import Foundation;
2+
3+
#include "stdint.h"
4+
5+
/*
6+
void doit(NSArray<NSCopying> *array);
7+
NSArray<NSCopying> *gimmeArray(void);
8+
*/
9+
10+
void char_ptr_func(char * _Nonnull buffer);
11+
void const_char_ptr_func(const char * _Nonnull buffer);
12+
13+
void unsigned_char_ptr_func(unsigned char * _Nonnull buffer);
14+
void const_unsigned_char_ptr_func(const unsigned char * _Nonnull buffer);
15+
16+
void int_16_ptr_func(int16_t * _Nonnull buffer);
17+
void int_32_ptr_func(int32_t * _Nonnull buffer);
18+
void int_64_ptr_func(int64_t * _Nonnull buffer);
19+
20+
void const_int_16_ptr_func(const int16_t * _Nonnull buffer);
21+
void const_int_32_ptr_func(const int32_t * _Nonnull buffer);
22+
void const_int_64_ptr_func(const int64_t * _Nonnull buffer);
23+
24+
void uint_16_ptr_func(uint16_t * _Nonnull buffer);
25+
void uint_32_ptr_func(uint32_t * _Nonnull buffer);
26+
void uint_64_ptr_func(uint64_t * _Nonnull buffer);
27+
28+
void const_uint_16_ptr_func(const uint16_t * _Nonnull buffer);
29+
void const_uint_32_ptr_func(const uint32_t * _Nonnull buffer);
30+
void const_uint_64_ptr_func(const uint64_t * _Nonnull buffer);
31+
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+
*/
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// %target-swift-frontend(mock-sdk: %clang-importer-sdk-nosource -I %t) -import-objc-header %S/Inputs/overloaded_async_method.h %s -typecheck -verify
2+
3+
// RUN: %empty-directory(%t)
4+
5+
// RUN: %gyb %s -o %t/swift_to_c_pointer_conversions.swift
6+
7+
// 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
8+
9+
func test_raw_ptr(ptr: UnsafeRawPointer) {
10+
char_ptr_func(ptr) // expected-error {{}}
11+
unsigned_char_ptr_func(ptr) // expected-error {{}}
12+
13+
const_char_ptr_func(ptr) // Ok
14+
const_unsigned_char_ptr_func(ptr) // Ok
15+
16+
% for Size in ['16', '32', '64']:
17+
int_${Size}_ptr_func(ptr) // expected-error {{}}
18+
uint_${Size}_ptr_func(ptr) // expected-error {{}}
19+
20+
const_int_${Size}_ptr_func(ptr) // expected-error {{}}
21+
const_uint_${Size}_ptr_func(ptr) // expected-error {{}}
22+
% end
23+
}
24+
25+
func test_mutable_raw_pointer(ptr: UnsafeMutableRawPointer) {
26+
char_ptr_func(ptr) // Ok
27+
unsigned_char_ptr_func(ptr) // Ok
28+
29+
const_char_ptr_func(ptr) // Ok
30+
const_unsigned_char_ptr_func(ptr) // Ok
31+
32+
% for Size in ['16', '32', '64']:
33+
int_${Size}_ptr_func(ptr) // expected-error {{}}
34+
uint_${Size}_ptr_func(ptr) // expected-error {{}}
35+
36+
const_int_${Size}_ptr_func(ptr) // expected-error {{}}
37+
const_uint_${Size}_ptr_func(ptr) // expected-error {{}}
38+
% end
39+
}
40+
41+
%for TestPtrSize in ['16', '32', '64']:
42+
// Immutable pointers can be converted only to their immutable (regardless of sign) counterparts.
43+
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 {{}}
47+
48+
const_char_ptr_func(sptr) // Ok
49+
const_char_ptr_func(uptr) // Ok
50+
51+
unsigned_char_ptr_func(sptr) // expected-error {{}}
52+
unsigned_char_ptr_func(uptr) // expected-error {{}}
53+
54+
const_unsigned_char_ptr_func(sptr) // Ok
55+
const_unsigned_char_ptr_func(uptr) // Ok
56+
57+
% for pointer in ['sptr', 'uptr']:
58+
% for Size in ['16', '32', '64']:
59+
60+
% if Size == TestPtrSize:
61+
int_${TestPtrSize}_ptr_func(${pointer}) // expected-error {{}}
62+
uint_${TestPtrSize}_ptr_func(${pointer}) // expected-error {{}}
63+
64+
const_int_${TestPtrSize}_ptr_func(${pointer}) // Ok
65+
const_uint_${TestPtrSize}_ptr_func(${pointer}) // Ok
66+
% else:
67+
int_${Size}_ptr_func(${pointer}) // expected-error {{}}
68+
uint_${Size}_ptr_func(${pointer}) // expected-error {{}}
69+
70+
const_int_${Size}_ptr_func(${pointer}) // expected-error {{}} expected-note {{}}
71+
const_uint_${Size}_ptr_func(${pointer}) // expected-error {{}} expected-note {{}}
72+
% end
73+
% end
74+
% end
75+
}
76+
77+
% end
78+
79+
%for TestPtrSize in ['16', '32', '64']:
80+
// Mutable pointers can be converted to both immutable and mutable pointers when
81+
// passed to C/ObjC imported functions.
82+
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
86+
87+
const_char_ptr_func(sptr) // Ok
88+
const_char_ptr_func(uptr) // Ok
89+
90+
unsigned_char_ptr_func(sptr) // Ok
91+
unsigned_char_ptr_func(uptr) // Ok
92+
93+
const_unsigned_char_ptr_func(sptr) // Ok
94+
const_unsigned_char_ptr_func(uptr) // Ok
95+
96+
% for pointer in ['sptr', 'uptr']:
97+
% for Size in ['16', '32', '64']:
98+
99+
% if Size == TestPtrSize:
100+
int_${TestPtrSize}_ptr_func(${pointer}) // Ok
101+
uint_${TestPtrSize}_ptr_func(${pointer}) // Ok
102+
103+
const_int_${TestPtrSize}_ptr_func(${pointer}) // Ok
104+
const_uint_${TestPtrSize}_ptr_func(${pointer}) // Ok
105+
% else:
106+
int_${Size}_ptr_func(${pointer}) // expected-error {{}} expected-note {{}}
107+
uint_${Size}_ptr_func(${pointer}) // expected-error {{}} expected-note {{}}
108+
109+
const_int_${Size}_ptr_func(${pointer}) // expected-error {{}} expected-note {{}}
110+
const_uint_${Size}_ptr_func(${pointer}) // expected-error {{}} expected-note {{}}
111+
% end
112+
% end
113+
% end
114+
}
115+
116+
% end

0 commit comments

Comments
 (0)