]> git.lizzy.rs Git - rust.git/blob - tests/ui/non_send_fields_in_send_ty.rs
author: fix some bugs
[rust.git] / tests / ui / non_send_fields_in_send_ty.rs
1 #![warn(clippy::non_send_fields_in_send_ty)]
2 #![feature(extern_types)]
3
4 use std::cell::UnsafeCell;
5 use std::ptr::NonNull;
6 use std::rc::Rc;
7 use std::sync::{Arc, Mutex, MutexGuard};
8
9 // disrustor / RUSTSEC-2020-0150
10 pub struct RingBuffer<T> {
11     data: Vec<UnsafeCell<T>>,
12     capacity: usize,
13     mask: usize,
14 }
15
16 unsafe impl<T> Send for RingBuffer<T> {}
17
18 // noise_search / RUSTSEC-2020-0141
19 pub struct MvccRwLock<T> {
20     raw: *const T,
21     lock: Mutex<Box<T>>,
22 }
23
24 unsafe impl<T> Send for MvccRwLock<T> {}
25
26 // async-coap / RUSTSEC-2020-0124
27 pub struct ArcGuard<RC, T> {
28     inner: T,
29     head: Arc<RC>,
30 }
31
32 unsafe impl<RC, T: Send> Send for ArcGuard<RC, T> {}
33
34 // rusb / RUSTSEC-2020-0098
35 extern "C" {
36     type libusb_device_handle;
37 }
38
39 pub trait UsbContext {
40     // some user trait that does not guarantee `Send`
41 }
42
43 pub struct DeviceHandle<T: UsbContext> {
44     context: T,
45     handle: NonNull<libusb_device_handle>,
46 }
47
48 unsafe impl<T: UsbContext> Send for DeviceHandle<T> {}
49
50 // Other basic tests
51 pub struct NoGeneric {
52     rc_is_not_send: Rc<String>,
53 }
54
55 unsafe impl Send for NoGeneric {}
56
57 pub struct MultiField<T> {
58     field1: T,
59     field2: T,
60     field3: T,
61 }
62
63 unsafe impl<T> Send for MultiField<T> {}
64
65 pub enum MyOption<T> {
66     MySome(T),
67     MyNone,
68 }
69
70 unsafe impl<T> Send for MyOption<T> {}
71
72 // Multiple type parameters
73 pub struct MultiParam<A, B> {
74     vec: Vec<(A, B)>,
75 }
76
77 unsafe impl<A, B> Send for MultiParam<A, B> {}
78
79 // Tests for raw pointer heuristic
80 extern "C" {
81     type NonSend;
82 }
83
84 pub struct HeuristicTest {
85     // raw pointers are allowed
86     field1: Vec<*const NonSend>,
87     field2: [*const NonSend; 3],
88     field3: (*const NonSend, *const NonSend, *const NonSend),
89     // not allowed when it contains concrete `!Send` field
90     field4: (*const NonSend, Rc<u8>),
91     // nested raw pointer is also allowed
92     field5: Vec<Vec<*const NonSend>>,
93 }
94
95 unsafe impl Send for HeuristicTest {}
96
97 // Test attributes
98 #[allow(clippy::non_send_fields_in_send_ty)]
99 pub struct AttrTest1<T>(T);
100
101 pub struct AttrTest2<T> {
102     #[allow(clippy::non_send_fields_in_send_ty)]
103     field: T,
104 }
105
106 pub enum AttrTest3<T> {
107     #[allow(clippy::non_send_fields_in_send_ty)]
108     Enum1(T),
109     Enum2(T),
110 }
111
112 unsafe impl<T> Send for AttrTest1<T> {}
113 unsafe impl<T> Send for AttrTest2<T> {}
114 unsafe impl<T> Send for AttrTest3<T> {}
115
116 // Multiple non-overlapping `Send` for a single type
117 pub struct Complex<A, B> {
118     field1: A,
119     field2: B,
120 }
121
122 unsafe impl<P> Send for Complex<P, u32> {}
123
124 // `MutexGuard` is non-Send
125 unsafe impl<Q: Send> Send for Complex<Q, MutexGuard<'static, bool>> {}
126
127 fn main() {}