]> git.lizzy.rs Git - rust.git/blob - src/test/ui/lint/lint-ctypes-fn.rs
Update code to account for extern ABI requirement
[rust.git] / src / test / ui / lint / lint-ctypes-fn.rs
1 #![feature(rustc_private)]
2
3 #![allow(private_in_public)]
4 #![deny(improper_ctypes_definitions)]
5
6 extern crate libc;
7
8 use std::default::Default;
9 use std::marker::PhantomData;
10
11 trait Mirror { type It: ?Sized; }
12
13 impl<T: ?Sized> Mirror for T { type It = Self; }
14
15 #[repr(C)]
16 pub struct StructWithProjection(*mut <StructWithProjection as Mirror>::It);
17
18 #[repr(C)]
19 pub struct StructWithProjectionAndLifetime<'a>(
20     &'a mut <StructWithProjectionAndLifetime<'a> as Mirror>::It
21 );
22
23 pub type I32Pair = (i32, i32);
24
25 #[repr(C)]
26 pub struct ZeroSize;
27
28 pub type RustFn = fn();
29
30 pub type RustBadRet = extern "C" fn() -> Box<u32>;
31
32 pub type CVoidRet = ();
33
34 pub struct Foo;
35
36 #[repr(transparent)]
37 pub struct TransparentI128(i128);
38
39 #[repr(transparent)]
40 pub struct TransparentStr(&'static str);
41
42 #[repr(transparent)]
43 pub struct TransparentBadFn(RustBadRet);
44
45 #[repr(transparent)]
46 pub struct TransparentInt(u32);
47
48 #[repr(transparent)]
49 pub struct TransparentRef<'a>(&'a TransparentInt);
50
51 #[repr(transparent)]
52 pub struct TransparentLifetime<'a>(*const u8, PhantomData<&'a ()>);
53
54 #[repr(transparent)]
55 pub struct TransparentUnit<U>(f32, PhantomData<U>);
56
57 #[repr(transparent)]
58 pub struct TransparentCustomZst(i32, ZeroSize);
59
60 #[repr(C)]
61 pub struct ZeroSizeWithPhantomData(PhantomData<i32>);
62
63 pub extern "C" fn ptr_type1(size: *const Foo) { }
64
65 pub extern "C" fn ptr_type2(size: *const Foo) { }
66
67 pub extern "C" fn slice_type(p: &[u32]) { }
68 //~^ ERROR: uses type `[u32]`
69
70 pub extern "C" fn str_type(p: &str) { }
71 //~^ ERROR: uses type `str`
72
73 pub extern "C" fn box_type(p: Box<u32>) { }
74
75 pub extern "C" fn opt_box_type(p: Option<Box<u32>>) { }
76
77 pub extern "C" fn char_type(p: char) { }
78 //~^ ERROR uses type `char`
79
80 pub extern "C" fn i128_type(p: i128) { }
81 //~^ ERROR uses type `i128`
82
83 pub extern "C" fn u128_type(p: u128) { }
84 //~^ ERROR uses type `u128`
85
86 pub extern "C" fn tuple_type(p: (i32, i32)) { }
87 //~^ ERROR uses type `(i32, i32)`
88
89 pub extern "C" fn tuple_type2(p: I32Pair) { }
90 //~^ ERROR uses type `(i32, i32)`
91
92 pub extern "C" fn zero_size(p: ZeroSize) { }
93 //~^ ERROR uses type `ZeroSize`
94
95 pub extern "C" fn zero_size_phantom(p: ZeroSizeWithPhantomData) { }
96 //~^ ERROR uses type `ZeroSizeWithPhantomData`
97
98 pub extern "C" fn zero_size_phantom_toplevel() -> PhantomData<bool> {
99 //~^ ERROR uses type `PhantomData<bool>`
100     Default::default()
101 }
102
103 pub extern "C" fn fn_type(p: RustFn) { }
104 //~^ ERROR uses type `fn()`
105
106 pub extern "C" fn fn_type2(p: fn()) { }
107 //~^ ERROR uses type `fn()`
108
109 pub extern "C" fn fn_contained(p: RustBadRet) { }
110
111 pub extern "C" fn transparent_i128(p: TransparentI128) { }
112 //~^ ERROR: uses type `i128`
113
114 pub extern "C" fn transparent_str(p: TransparentStr) { }
115 //~^ ERROR: uses type `str`
116
117 pub extern "C" fn transparent_fn(p: TransparentBadFn) { }
118
119 pub extern "C" fn good3(fptr: Option<extern "C" fn()>) { }
120
121 pub extern "C" fn good4(aptr: &[u8; 4 as usize]) { }
122
123 pub extern "C" fn good5(s: StructWithProjection) { }
124
125 pub extern "C" fn good6(s: StructWithProjectionAndLifetime) { }
126
127 pub extern "C" fn good7(fptr: extern "C" fn() -> ()) { }
128
129 pub extern "C" fn good8(fptr: extern "C" fn() -> !) { }
130
131 pub extern "C" fn good9() -> () { }
132
133 pub extern "C" fn good10() -> CVoidRet { }
134
135 pub extern "C" fn good11(size: isize) { }
136
137 pub extern "C" fn good12(size: usize) { }
138
139 pub extern "C" fn good13(n: TransparentInt) { }
140
141 pub extern "C" fn good14(p: TransparentRef) { }
142
143 pub extern "C" fn good15(p: TransparentLifetime) { }
144
145 pub extern "C" fn good16(p: TransparentUnit<ZeroSize>) { }
146
147 pub extern "C" fn good17(p: TransparentCustomZst) { }
148
149 #[allow(improper_ctypes_definitions)]
150 pub extern "C" fn good18(_: &String) { }
151
152 #[cfg(not(target_arch = "wasm32"))]
153 pub extern "C" fn good1(size: *const libc::c_int) { }
154
155 #[cfg(not(target_arch = "wasm32"))]
156 pub extern "C" fn good2(size: *const libc::c_uint) { }
157
158 pub extern "C" fn unused_generic1<T>(size: *const Foo) { }
159
160 pub extern "C" fn unused_generic2<T>() -> PhantomData<bool> {
161 //~^ ERROR uses type `PhantomData<bool>`
162     Default::default()
163 }
164
165 pub extern "C" fn used_generic1<T>(x: T) { }
166
167 pub extern "C" fn used_generic2<T>(x: T, size: *const Foo) { }
168
169 pub extern "C" fn used_generic3<T: Default>() -> T {
170     Default::default()
171 }
172
173 pub extern "C" fn used_generic4<T>(x: Vec<T>) { }
174 //~^ ERROR: uses type `Vec<T>`
175
176 pub extern "C" fn used_generic5<T>() -> Vec<T> {
177 //~^ ERROR: uses type `Vec<T>`
178     Default::default()
179 }
180
181 fn main() {}