|
| 1 | +// Trying to cover as many ty_kinds as possible in the code for ImproperCTypes lint |
| 2 | +//@ edition:2018 |
| 3 | + |
| 4 | +#![allow(dead_code,unused_variables)] |
| 5 | +#![deny(improper_ctypes,improper_ctypes_definitions)] |
| 6 | + |
| 7 | +// we want ALL the ty_kinds, including the feature-gated ones |
| 8 | +#![feature(extern_types)] |
| 9 | +#![feature(never_type)] |
| 10 | +#![feature(inherent_associated_types)] //~ WARN: is incomplete |
| 11 | +#![feature(async_trait_bounds)] |
| 12 | +#![feature(pattern_types, rustc_attrs)] |
| 13 | +#![feature(pattern_type_macro)] |
| 14 | + |
| 15 | +// ty_kinds not found so far: |
| 16 | +// Placeholder, Bound, Infer, Error, |
| 17 | +// Alias<Weak|Inherent> |
| 18 | +// FnDef, Closure, Coroutine, ClosureCoroutine, CoroutineWitness, |
| 19 | + |
| 20 | +use std::ptr::from_ref; |
| 21 | +use std::ptr::NonNull; |
| 22 | +use std::mem::{MaybeUninit, size_of}; |
| 23 | +use std::num::NonZero; |
| 24 | +use std::pat::pattern_type; |
| 25 | + |
| 26 | +#[repr(C)] |
| 27 | +struct SomeStruct{ |
| 28 | + a: u8, |
| 29 | + b: i32, |
| 30 | +} |
| 31 | +impl SomeStruct{ |
| 32 | + extern "C" fn klol( |
| 33 | + // Ref[Struct] |
| 34 | + &self |
| 35 | + ){} |
| 36 | +} |
| 37 | + |
| 38 | +#[repr(C)] |
| 39 | +#[derive(Clone,Copy)] |
| 40 | +struct TemplateStruct<T> where T: std::ops::Add+Copy { |
| 41 | + one: T, |
| 42 | + two: T, |
| 43 | +} |
| 44 | +impl<T: std::ops::Add+Copy> TemplateStruct<T> { |
| 45 | + type Out = <T as std::ops::Add>::Output; |
| 46 | +} |
| 47 | + |
| 48 | +extern "C" fn tstruct_sum<T: std::ops::Add+Copy>( |
| 49 | + // Ref[Struct] |
| 50 | + slf: &TemplateStruct<T> |
| 51 | + // Alias<Projection> ...not Inherent. dangit |
| 52 | +) -> TemplateStruct<T>::Out { |
| 53 | + slf.one + slf.two |
| 54 | +} |
| 55 | + |
| 56 | +#[repr(C)] |
| 57 | +union SomeUnion{ |
| 58 | + sz: u8, |
| 59 | + us: i8, |
| 60 | +} |
| 61 | +#[repr(C)] |
| 62 | +enum SomeEnum{ |
| 63 | + Everything=42, |
| 64 | + NotAU8=256, |
| 65 | + SomePrimeNumber=23, |
| 66 | +} |
| 67 | + |
| 68 | +pub trait TimesTwo: std::ops::Add<Self> + Sized + Clone |
| 69 | + where for<'a> &'a Self: std::ops::Add<&'a Self>, |
| 70 | + *const Self: std::ops::Add<*const Self>, |
| 71 | + Box<Self>: std::ops::Add<Box<Self>>, |
| 72 | +{ |
| 73 | + extern "C" fn t2_own( |
| 74 | + // Param |
| 75 | + self |
| 76 | + // Alias<Projection> |
| 77 | + ) -> <Self as std::ops::Add<Self>>::Output { |
| 78 | + self.clone() + self |
| 79 | + } |
| 80 | + // it ICEs (https://github.com/rust-lang/rust/issues/134587) :( |
| 81 | + //extern "C" fn t2_ptr( |
| 82 | + // // Ref[Param] |
| 83 | + // slf: *const Self |
| 84 | + // // Alias<Projection> |
| 85 | + //) -> <*const Self as std::ops::Add<*const Self>>::Output { |
| 86 | + // slf + slf |
| 87 | + //} |
| 88 | + extern "C" fn t2_box( |
| 89 | + // Box[Param] |
| 90 | + self: Box<Self> |
| 91 | + // Alias<Projection> |
| 92 | + ) -> <Box<Self> as std::ops::Add<Box<Self>>>::Output { |
| 93 | + self.clone() + self |
| 94 | + } |
| 95 | + extern "C" fn t2_ref( |
| 96 | + // Ref[Param] |
| 97 | + &self |
| 98 | + // Alias<Projection> |
| 99 | + ) -> <&Self as std::ops::Add<&Self>>::Output { |
| 100 | + self + self |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +extern "C" {type ExtType;} |
| 105 | + |
| 106 | +#[repr(C)] |
| 107 | +pub struct StructWithDyn(dyn std::fmt::Debug); |
| 108 | + |
| 109 | +extern "C" { |
| 110 | + // variadic args aren't listed as args in a way that allows type checking. |
| 111 | + // this is fine (TM) |
| 112 | + fn variadic_function(e: ...); |
| 113 | +} |
| 114 | + |
| 115 | +extern "C" fn all_ty_kinds<'a,const N:usize,T>( |
| 116 | + // UInt, Int, Float, Bool |
| 117 | + u:u8, i:i8, f:f64, b:bool, |
| 118 | + // Struct |
| 119 | + s:String, //~ ERROR: uses type `String` |
| 120 | + // Ref[Str] |
| 121 | + s2:&str, //~ ERROR: uses type `&str` |
| 122 | + // Char |
| 123 | + c: char, //~ ERROR: uses type `char` |
| 124 | + // Ref[Slice] |
| 125 | + s3:&[u8], //~ ERROR: uses type `&[u8]` |
| 126 | + // Array (this gets caught outside of the code we want to test) |
| 127 | + s4:[u8;N], //~ ERROR: uses type `[u8; N]` |
| 128 | + // Tuple |
| 129 | + p:(u8, u8), //~ ERROR: uses type `(u8, u8)` |
| 130 | + // also Tuple |
| 131 | + (p2, p3):(u8, u8), //~ ERROR: uses type `(u8, u8)` |
| 132 | + // Pat |
| 133 | + nz: pattern_type!(u32 is 1..), //~ ERROR: uses type `(u32) is 1..=` |
| 134 | + // Struct |
| 135 | + SomeStruct{b:p4,..}: SomeStruct, |
| 136 | + // Union |
| 137 | + u2: SomeUnion, |
| 138 | + // Enum, |
| 139 | + e: SomeEnum, |
| 140 | + // Param |
| 141 | + d: impl Clone, |
| 142 | + // Param |
| 143 | + t: T, |
| 144 | + // Ptr[Foreign] |
| 145 | + e2: *mut ExtType, |
| 146 | + // Ref[Struct] |
| 147 | + e3: &StructWithDyn, //~ ERROR: uses type `&StructWithDyn` |
| 148 | + // Never |
| 149 | + x:!, |
| 150 | + //r1: &u8, r2: *const u8, r3: Box<u8>, |
| 151 | + // FnPtr |
| 152 | + f2: fn(u8)->u8, //~ ERROR: uses type `fn(u8) -> u8` |
| 153 | + // Ref[Dynamic] |
| 154 | + f3: &'a dyn Fn(u8)->u8, //~ ERROR: uses type `&dyn Fn(u8) -> u8` |
| 155 | + // Ref[Dynamic] |
| 156 | + d2: &dyn std::cmp::PartialOrd<u8>, //~ ERROR: uses type `&dyn PartialOrd<u8>` |
| 157 | + // Param, |
| 158 | + a: impl async Fn(u8)->u8, //FIXME: eventually, be able to peer into type params |
| 159 | + // Alias<Opaque> (this gets caught outside of the code we want to test) |
| 160 | +) -> impl std::fmt::Debug { //~ ERROR: uses type `impl Debug` |
| 161 | + 3_usize |
| 162 | +} |
| 163 | + |
| 164 | +extern "C" { |
| 165 | +fn all_ty_kinds_in_ptr( |
| 166 | + // Ptr[UInt], Ptr[Int], Ptr[Float], Ptr[Bool] |
| 167 | + u: *const u8, i: *const i8, f: *const f64, b: *const bool, |
| 168 | + // Ptr[Struct] |
| 169 | + s: *const String, //~ ERROR: uses type `String` |
| 170 | + // Ptr[Str] |
| 171 | + s2: *const str, //~ ERROR: uses type `*const str` |
| 172 | + // Ptr[Char] |
| 173 | + c: *const char, //~ ERROR: uses type `char` |
| 174 | + // Ptr[Slice] |
| 175 | + s3: *const [u8], //~ ERROR: uses type `*const [u8]` |
| 176 | + // deactivated here, because this is a function *declaration* (param N unacceptable) |
| 177 | + // s4: *const [u8;N], |
| 178 | + // Ptr[Tuple] |
| 179 | + p: *const (u8,u8), //~ ERROR: uses type `(u8, u8)` |
| 180 | + // deactivated here, because this is a function *declaration* (pattern unacceptable) |
| 181 | + // (p2, p3):(*const u8, *const u8), |
| 182 | + // Pat |
| 183 | + nz: *const pattern_type!(u32 is 1..), //~ ERROR: uses type `(u32) is 1..=` |
| 184 | + // deactivated here, because this is a function *declaration* (pattern unacceptable) |
| 185 | + //SomeStruct{b: ref p4,..}: & SomeStruct, |
| 186 | + // Ptr[Union] |
| 187 | + u2: *const SomeUnion, |
| 188 | + // Ptr[Enum], |
| 189 | + e: *const SomeEnum, |
| 190 | + // deactivated here, because this is a function *declaration* (impl type unacceptable) |
| 191 | + //d: *const impl Clone, |
| 192 | + // deactivated here, because this is a function *declaration* (type param unacceptable) |
| 193 | + //t: *const T, |
| 194 | + // Ptr[Foreign] |
| 195 | + e2: *mut ExtType, |
| 196 | + // Ptr[Struct] |
| 197 | + e3: *const StructWithDyn, //~ ERROR: uses type `*const StructWithDyn` |
| 198 | + // Ptr[Never] |
| 199 | + x: *const !, |
| 200 | + //r1: &u8, r2: *const u8, r3: Box<u8>, |
| 201 | + // Ptr[FnPtr] |
| 202 | + f2: *const fn(u8)->u8, //~ ERROR: uses type `fn(u8) -> u8` |
| 203 | + // Ptr[Dynamic] |
| 204 | + f3: *const dyn Fn(u8)->u8, //~ ERROR: uses type `*const dyn Fn(u8) -> u8` |
| 205 | + // Ptr[Dynamic] |
| 206 | + d2: *const dyn std::cmp::PartialOrd<u8>, //~ ERROR: uses type `*const dyn PartialOrd<u8>` |
| 207 | + // deactivated here, because this is a function *declaration* (impl type unacceptable) |
| 208 | + //a: *const impl async Fn(u8)->u8, |
| 209 | + // Alias<Opaque> (this gets caught outside of the code we want to test) |
| 210 | +) -> *const dyn std::fmt::Debug; //~ ERROR: uses type `*const dyn Debug` |
| 211 | +} |
| 212 | + |
| 213 | +extern "C" fn all_ty_kinds_in_ref<'a, const N:usize,T>( |
| 214 | + // Ref[UInt], Ref[Int], Ref[Float], Ref[Bool] |
| 215 | + u: &u8, i: &'a i8, f: &f64, b: &bool, |
| 216 | + // Ref[Struct] |
| 217 | + s: &String, |
| 218 | + // Ref[Str] |
| 219 | + s2: &str, //~ ERROR: uses type `&str` |
| 220 | + // Ref[Char] |
| 221 | + c: &char, |
| 222 | + // Ref[Slice] |
| 223 | + s3: &[u8], //~ ERROR: uses type `&[u8]` |
| 224 | + // Ref[Array] (this gets caught outside of the code we want to test) |
| 225 | + s4: &[u8;N], |
| 226 | + // Ref[Tuple] |
| 227 | + p: &(u8, u8), |
| 228 | + // also Tuple |
| 229 | + (p2, p3):(&u8, &u8), //~ ERROR: uses type `(&u8, &u8)` |
| 230 | + // Pat |
| 231 | + nz: &pattern_type!(u32 is 1..), |
| 232 | + // Ref[Struct] |
| 233 | + SomeStruct{b: ref p4,..}: &SomeStruct, |
| 234 | + // Ref[Union] |
| 235 | + u2: &SomeUnion, |
| 236 | + // Ref[Enum], |
| 237 | + e: &SomeEnum, |
| 238 | + // Ref[Param] |
| 239 | + d: &impl Clone, |
| 240 | + // Ref[Param] |
| 241 | + t: &T, |
| 242 | + // Ref[Foreign] |
| 243 | + e2: &ExtType, |
| 244 | + // Ref[Struct] |
| 245 | + e3: &StructWithDyn, //~ ERROR: uses type `&StructWithDyn` |
| 246 | + // Ref[Never] |
| 247 | + x: &!, |
| 248 | + //r1: &u8, r2: &u8, r3: Box<u8>, |
| 249 | + // Ref[FnPtr] |
| 250 | + f2: &fn(u8)->u8, |
| 251 | + // Ref[Dynamic] |
| 252 | + f3: &dyn Fn(u8)->u8, //~ ERROR: uses type `&dyn Fn(u8) -> u8` |
| 253 | + // Ref[Dynamic] |
| 254 | + d2: &dyn std::cmp::PartialOrd<u8>, //~ ERROR: uses type `&dyn PartialOrd<u8>` |
| 255 | + // Ref[Param], |
| 256 | + a: &impl async Fn(u8)->u8, |
| 257 | + // Ref[Dynamic] (this gets caught outside of the code we want to test) |
| 258 | +) -> &'a dyn std::fmt::Debug { //~ ERROR: uses type `&dyn Debug` |
| 259 | + i |
| 260 | +} |
| 261 | + |
| 262 | +extern "C" fn all_ty_kinds_in_box<const N:usize,T>( |
| 263 | + // Box[UInt], Box[Int], Box[Float], Box[Bool] |
| 264 | + u: Box<u8>, i: Box<i8>, f: Box<f64>, b: Box<bool>, |
| 265 | + // Box[Struct] |
| 266 | + s: Box<String>, |
| 267 | + // Box[Str] |
| 268 | + s2: Box<str>, //~ ERROR: uses type `Box<str>` |
| 269 | + // Box[Char] |
| 270 | + c: Box<char>, |
| 271 | + // Box[Slice] |
| 272 | + s3: Box<[u8]>, //~ ERROR: uses type `Box<[u8]>` |
| 273 | + // Box[Array] (this gets caught outside of the code we want to test) |
| 274 | + s4: Box<[u8;N]>, |
| 275 | + // Box[Tuple] |
| 276 | + p: Box<(u8,u8)>, |
| 277 | + // also Tuple |
| 278 | + (p2,p3):(Box<u8>, Box<u8>), //~ ERROR: uses type `(Box<u8>, Box<u8>)` |
| 279 | + // Pat |
| 280 | + nz: Box<pattern_type!(u32 is 1..)>, |
| 281 | + // Ref[Struct] |
| 282 | + SomeStruct{b: ref p4,..}: &SomeStruct, |
| 283 | + // Box[Union] |
| 284 | + u2: Box<SomeUnion>, |
| 285 | + // Box[Enum], |
| 286 | + e: Box<SomeEnum>, |
| 287 | + // Box[Param] |
| 288 | + d: Box<impl Clone>, |
| 289 | + // Box[Param] |
| 290 | + t: Box<T>, |
| 291 | + // Box[Foreign] |
| 292 | + e2: Box<ExtType>, |
| 293 | + // Box[Struct] |
| 294 | + e3: Box<StructWithDyn>, //~ ERROR: uses type `Box<StructWithDyn>` |
| 295 | + // Box[Never] |
| 296 | + x: Box<!>, |
| 297 | + //r1: Box<u8, r2: Box<u8, r3: Box<u8>, |
| 298 | + // Box[FnPtr] |
| 299 | + f2: Box<fn(u8)->u8>, |
| 300 | + // Box[Dynamic] |
| 301 | + f3: Box<dyn Fn(u8)->u8>, //~ ERROR: uses type `Box<dyn Fn(u8) -> u8>` |
| 302 | + // Box[Dynamic] |
| 303 | + d2: Box<dyn std::cmp::PartialOrd<u8>>, //~ ERROR: uses type `Box<dyn PartialOrd<u8>>` |
| 304 | + // Box[Param], |
| 305 | + a: Box<impl async Fn(u8)->u8>, |
| 306 | + // Box[Dynamic] (this gets caught outside of the code we want to test) |
| 307 | +) -> Box<dyn std::fmt::Debug> { //~ ERROR: uses type `Box<dyn Debug>` |
| 308 | + i |
| 309 | +} |
| 310 | + |
| 311 | +fn main() {} |
0 commit comments