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