]> git.lizzy.rs Git - rust.git/blob - src/test/ui/lint-ctypes.rs
Rollup merge of #53110 - Xanewok:save-analysis-remap-path, r=nrc
[rust.git] / src / test / ui / lint-ctypes.rs
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #![deny(improper_ctypes)]
12 #![feature(libc)]
13
14 extern crate libc;
15
16 use std::marker::PhantomData;
17
18 trait Mirror { type It: ?Sized; }
19 impl<T: ?Sized> Mirror for T { type It = Self; }
20 #[repr(C)]
21 pub struct StructWithProjection(*mut <StructWithProjection as Mirror>::It);
22 #[repr(C)]
23 pub struct StructWithProjectionAndLifetime<'a>(
24     &'a mut <StructWithProjectionAndLifetime<'a> as Mirror>::It
25 );
26 pub type I32Pair = (i32, i32);
27 #[repr(C)]
28 pub struct ZeroSize;
29 pub type RustFn = fn();
30 pub type RustBadRet = extern fn() -> Box<u32>;
31 pub type CVoidRet = ();
32 pub struct Foo;
33 #[repr(transparent)]
34 pub struct TransparentI128(i128);
35 #[repr(transparent)]
36 pub struct TransparentStr(&'static str);
37 #[repr(transparent)]
38 pub struct TransparentBadFn(RustBadRet);
39 #[repr(transparent)]
40 pub struct TransparentInt(u32);
41 #[repr(transparent)]
42 pub struct TransparentRef<'a>(&'a TransparentInt);
43 #[repr(transparent)]
44 pub struct TransparentLifetime<'a>(*const u8, PhantomData<&'a ()>);
45 #[repr(transparent)]
46 pub struct TransparentUnit<U>(f32, PhantomData<U>);
47 #[repr(transparent)]
48 pub struct TransparentCustomZst(i32, ZeroSize);
49
50 #[repr(C)]
51 pub struct ZeroSizeWithPhantomData(::std::marker::PhantomData<i32>);
52
53 extern {
54     pub fn ptr_type1(size: *const Foo); //~ ERROR: uses type `Foo`
55     pub fn ptr_type2(size: *const Foo); //~ ERROR: uses type `Foo`
56     pub fn slice_type(p: &[u32]); //~ ERROR: uses type `[u32]`
57     pub fn str_type(p: &str); //~ ERROR: uses type `str`
58     pub fn box_type(p: Box<u32>); //~ ERROR uses type `std::boxed::Box<u32>`
59     pub fn char_type(p: char); //~ ERROR uses type `char`
60     pub fn i128_type(p: i128); //~ ERROR uses type `i128`
61     pub fn u128_type(p: u128); //~ ERROR uses type `u128`
62     pub fn trait_type(p: &Clone); //~ ERROR uses type `dyn std::clone::Clone`
63     pub fn tuple_type(p: (i32, i32)); //~ ERROR uses type `(i32, i32)`
64     pub fn tuple_type2(p: I32Pair); //~ ERROR uses type `(i32, i32)`
65     pub fn zero_size(p: ZeroSize); //~ ERROR struct has no fields
66     pub fn zero_size_phantom(p: ZeroSizeWithPhantomData); //~ ERROR composed only of PhantomData
67     pub fn zero_size_phantom_toplevel()
68         -> ::std::marker::PhantomData<bool>; //~ ERROR: composed only of PhantomData
69     pub fn fn_type(p: RustFn); //~ ERROR function pointer has Rust-specific
70     pub fn fn_type2(p: fn()); //~ ERROR function pointer has Rust-specific
71     pub fn fn_contained(p: RustBadRet); //~ ERROR: uses type `std::boxed::Box<u32>`
72     pub fn transparent_i128(p: TransparentI128); //~ ERROR: uses type `i128`
73     pub fn transparent_str(p: TransparentStr); //~ ERROR: uses type `str`
74     pub fn transparent_fn(p: TransparentBadFn); //~ ERROR: uses type `std::boxed::Box<u32>`
75
76     pub fn good3(fptr: Option<extern fn()>);
77     pub fn good4(aptr: &[u8; 4 as usize]);
78     pub fn good5(s: StructWithProjection);
79     pub fn good6(s: StructWithProjectionAndLifetime);
80     pub fn good7(fptr: extern fn() -> ());
81     pub fn good8(fptr: extern fn() -> !);
82     pub fn good9() -> ();
83     pub fn good10() -> CVoidRet;
84     pub fn good11(size: isize);
85     pub fn good12(size: usize);
86     pub fn good13(n: TransparentInt);
87     pub fn good14(p: TransparentRef);
88     pub fn good15(p: TransparentLifetime);
89     pub fn good16(p: TransparentUnit<ZeroSize>);
90     pub fn good17(p: TransparentCustomZst);
91     #[allow(improper_ctypes)]
92     pub fn good18(_: &String);
93 }
94
95 #[allow(improper_ctypes)]
96 extern {
97     pub fn good19(_: &String);
98 }
99
100 #[cfg(not(target_arch = "wasm32"))]
101 extern {
102     pub fn good1(size: *const libc::c_int);
103     pub fn good2(size: *const libc::c_uint);
104 }
105
106 fn main() {
107 }