Skip to content

Commit a71b9a2

Browse files
committed
Look into tuple, array, ADT args in raw pointer heuristic
1 parent 6ccbe56 commit a71b9a2

File tree

3 files changed

+74
-19
lines changed

3 files changed

+74
-19
lines changed

clippy_lints/src/non_send_field_in_send_ty.rs

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ impl<'tcx> LateLintPass<'tcx> for NonSendFieldInSendTy {
121121
};
122122
}
123123
},
124-
)
124+
);
125125
}
126126
}
127127
}
@@ -145,6 +145,8 @@ impl<'tcx> NonSendField<'tcx> {
145145
}
146146
}
147147

148+
/// Given a type, collect all of its generic parameters.
149+
/// Example: MyStruct<P, Box<Q, R>> => vec![P, Q, R]
148150
fn collect_generic_params<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Vec<Ty<'tcx>> {
149151
ty.walk(cx.tcx)
150152
.filter_map(|inner| match inner.unpack() {
@@ -155,14 +157,47 @@ fn collect_generic_params<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Vec<Ty<
155157
.collect()
156158
}
157159

158-
fn ty_allowed_in_send<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, send_trait: DefId) -> bool {
159-
raw_pointer_in_ty_def(cx, ty) || implements_trait(cx, ty, send_trait, &[]) || is_copy(cx, ty)
160+
/// Determine if the given type is allowed in an ADT that implements `Send`
161+
fn ty_allowed_in_send(cx: &LateContext<'tcx>, ty: Ty<'tcx>, send_trait: DefId) -> bool {
162+
// TODO: check configuration and call `ty_implements_send_or_copy()` or
163+
// `ty_allowed_with_raw_pointer_heuristic()`
164+
ty_allowed_with_raw_pointer_heuristic(cx, ty, send_trait)
160165
}
161166

162-
/// Returns `true` if the type itself is a raw pointer or has a raw pointer as a
163-
/// generic parameter, e.g., `Vec<*const u8>`.
164-
/// Note that it does not look into enum variants or struct fields.
165-
fn raw_pointer_in_ty_def<'tcx>(cx: &LateContext<'tcx>, target_ty: Ty<'tcx>) -> bool {
167+
/// Determine if the given type is `Send` or `Copy`
168+
fn ty_implements_send_or_copy(cx: &LateContext<'tcx>, ty: Ty<'tcx>, send_trait: DefId) -> bool {
169+
implements_trait(cx, ty, send_trait, &[]) || is_copy(cx, ty)
170+
}
171+
172+
/// Heuristic to allow cases like `Vec<*const u8>`
173+
fn ty_allowed_with_raw_pointer_heuristic<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, send_trait: DefId) -> bool {
174+
if ty_implements_send_or_copy(cx, ty, send_trait) {
175+
true
176+
} else {
177+
// The type is known to be `!Send` and `!Copy`
178+
match ty.kind() {
179+
ty::Tuple(_) => ty
180+
.tuple_fields()
181+
.all(|ty| ty_allowed_with_raw_pointer_heuristic(cx, ty, send_trait)),
182+
ty::Array(ty, _) | ty::Slice(ty) => ty_allowed_with_raw_pointer_heuristic(cx, ty, send_trait),
183+
ty::Adt(_, substs) => {
184+
if contains_raw_pointer(cx, ty) {
185+
// descends only if ADT contains any raw pointers
186+
substs.iter().all(|generic_arg| match generic_arg.unpack() {
187+
GenericArgKind::Type(ty) => ty_allowed_with_raw_pointer_heuristic(cx, ty, send_trait),
188+
GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => true,
189+
})
190+
} else {
191+
false
192+
}
193+
},
194+
ty::RawPtr(_) => true,
195+
_ => false,
196+
}
197+
}
198+
}
199+
200+
fn contains_raw_pointer<'tcx>(cx: &LateContext<'tcx>, target_ty: Ty<'tcx>) -> bool {
166201
for ty_node in target_ty.walk(cx.tcx) {
167202
if_chain! {
168203
if let GenericArgKind::Type(inner_ty) = ty_node.unpack();

tests/ui/non_send_field_in_send_ty.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,23 @@ pub struct MultiParam<A, B> {
7676

7777
unsafe impl<A, B> Send for MultiParam<A, B> {}
7878

79-
// Raw pointers are allowed
79+
// Tests for raw pointer heuristic
8080
extern "C" {
81-
type SomeFfiType;
81+
type NonSend;
8282
}
8383

84-
pub struct FpTest {
85-
vec: Vec<*const SomeFfiType>,
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>>,
8693
}
8794

88-
unsafe impl Send for FpTest {}
95+
unsafe impl Send for HeuristicTest {}
8996

9097
// Test attributes
9198
#[allow(clippy::non_send_field_in_send_ty)]

tests/ui/non_send_field_in_send_ty.stderr

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,44 +115,57 @@ LL | vec: Vec<(A, B)>,
115115
| ^^^^^^^^^^^^^^^^
116116
= help: add bounds on type parameters `A, B` that satisfy `std::vec::Vec<(A, B)>: Send`
117117

118+
error: this implementation is unsound, as some fields in `HeuristicTest` are `!Send`
119+
--> $DIR/non_send_field_in_send_ty.rs:95:1
120+
|
121+
LL | unsafe impl Send for HeuristicTest {}
122+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
123+
|
124+
note: the field `field4` has type `(*const NonSend, std::rc::Rc<u8>)` which is not `Send`
125+
--> $DIR/non_send_field_in_send_ty.rs:90:5
126+
|
127+
LL | field4: (*const NonSend, Rc<u8>),
128+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
129+
= help: use a thread-safe type that implements `Send`
130+
118131
error: this implementation is unsound, as some fields in `AttrTest3<T>` are `!Send`
119-
--> $DIR/non_send_field_in_send_ty.rs:107:1
132+
--> $DIR/non_send_field_in_send_ty.rs:114:1
120133
|
121134
LL | unsafe impl<T> Send for AttrTest3<T> {}
122135
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
123136
|
124137
note: the field `0` has type `T` which is not `Send`
125-
--> $DIR/non_send_field_in_send_ty.rs:102:11
138+
--> $DIR/non_send_field_in_send_ty.rs:109:11
126139
|
127140
LL | Enum2(T),
128141
| ^
129142
= help: add `T: Send` bound in `Send` impl
130143

131144
error: this implementation is unsound, as some fields in `Complex<P, u32>` are `!Send`
132-
--> $DIR/non_send_field_in_send_ty.rs:115:1
145+
--> $DIR/non_send_field_in_send_ty.rs:122:1
133146
|
134147
LL | unsafe impl<P> Send for Complex<P, u32> {}
135148
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
136149
|
137150
note: the field `field1` has type `P` which is not `Send`
138-
--> $DIR/non_send_field_in_send_ty.rs:111:5
151+
--> $DIR/non_send_field_in_send_ty.rs:118:5
139152
|
140153
LL | field1: A,
141154
| ^^^^^^^^^
142155
= help: add `P: Send` bound in `Send` impl
143156

144157
error: this implementation is unsound, as some fields in `Complex<Q, std::sync::MutexGuard<'static, bool>>` are `!Send`
145-
--> $DIR/non_send_field_in_send_ty.rs:118:1
158+
--> $DIR/non_send_field_in_send_ty.rs:125:1
146159
|
147160
LL | unsafe impl<Q: Send> Send for Complex<Q, MutexGuard<'static, bool>> {}
148161
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
149162
|
150163
note: the field `field2` has type `std::sync::MutexGuard<'static, bool>` which is not `Send`
151-
--> $DIR/non_send_field_in_send_ty.rs:112:5
164+
--> $DIR/non_send_field_in_send_ty.rs:119:5
152165
|
153166
LL | field2: B,
154167
| ^^^^^^^^^
155168
= help: use a thread-safe type that implements `Send`
156169

157-
error: aborting due to 11 previous errors
170+
error: aborting due to 12 previous errors
158171

0 commit comments

Comments
 (0)