]> git.lizzy.rs Git - rust.git/blob - tests/ui/non_send_fields_in_send_ty.rs
Auto merge of #8359 - flip1995:rustup, r=flip1995
[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 // Test types that contain `NonNull` instead of raw pointers (#8045)
73 pub struct WrappedNonNull(UnsafeCell<NonNull<()>>);
74
75 unsafe impl Send for WrappedNonNull {}
76
77 // Multiple type parameters
78 pub struct MultiParam<A, B> {
79     vec: Vec<(A, B)>,
80 }
81
82 unsafe impl<A, B> Send for MultiParam<A, B> {}
83
84 // Tests for raw pointer heuristic
85 extern "C" {
86     type NonSend;
87 }
88
89 pub struct HeuristicTest {
90     // raw pointers are allowed
91     field1: Vec<*const NonSend>,
92     field2: [*const NonSend; 3],
93     field3: (*const NonSend, *const NonSend, *const NonSend),
94     // not allowed when it contains concrete `!Send` field
95     field4: (*const NonSend, Rc<u8>),
96     // nested raw pointer is also allowed
97     field5: Vec<Vec<*const NonSend>>,
98 }
99
100 unsafe impl Send for HeuristicTest {}
101
102 // Test attributes
103 #[allow(clippy::non_send_fields_in_send_ty)]
104 pub struct AttrTest1<T>(T);
105
106 pub struct AttrTest2<T> {
107     #[allow(clippy::non_send_fields_in_send_ty)]
108     field: T,
109 }
110
111 pub enum AttrTest3<T> {
112     #[allow(clippy::non_send_fields_in_send_ty)]
113     Enum1(T),
114     Enum2(T),
115 }
116
117 unsafe impl<T> Send for AttrTest1<T> {}
118 unsafe impl<T> Send for AttrTest2<T> {}
119 unsafe impl<T> Send for AttrTest3<T> {}
120
121 // Multiple non-overlapping `Send` for a single type
122 pub struct Complex<A, B> {
123     field1: A,
124     field2: B,
125 }
126
127 unsafe impl<P> Send for Complex<P, u32> {}
128
129 // `MutexGuard` is non-Send
130 unsafe impl<Q: Send> Send for Complex<Q, MutexGuard<'static, bool>> {}
131
132 fn main() {}