You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Auto merge of #86848 - notriddle:notriddle/drop-dyn, r=varkor
feat(rustc_lint): add `dyn_drop`
Based on the conversation in #86747.
Explanation
-----------
A trait object bound of the form `dyn Drop` is most likely misleading and not what the programmer intended.
`Drop` bounds do not actually indicate whether a type can be trivially dropped or not, because a composite type containing `Drop` types does not necessarily implement `Drop` itself. Naïvely, one might be tempted to write a deferred drop system, to pull cleaning up memory out of a latency-sensitive code path, using `dyn Drop` trait objects. However, this breaks down e.g. when `T` is `String`, which does not implement `Drop`, but should probably be accepted.
To write a trait object bound that accepts anything, use a placeholder trait with a blanket implementation.
```rust
trait Placeholder {}
impl<T> Placeholder for T {}
fn foo(_x: Box<dyn Placeholder>) {}
```
error: types that do not implement `Drop` can still have drop glue, consider instead using `std::mem::needs_drop` to detect whether a type is trivially dropped
2
+
--> $DIR/dyn-drop.rs:3:19
3
+
|
4
+
LL | fn foo(_: Box<dyn Drop>) {}
5
+
| ^^^^
6
+
|
7
+
note: the lint level is defined here
8
+
--> $DIR/dyn-drop.rs:1:9
9
+
|
10
+
LL | #![deny(dyn_drop)]
11
+
| ^^^^^^^^
12
+
13
+
error: types that do not implement `Drop` can still have drop glue, consider instead using `std::mem::needs_drop` to detect whether a type is trivially dropped
14
+
--> $DIR/dyn-drop.rs:4:16
15
+
|
16
+
LL | fn bar(_: &dyn Drop) {}
17
+
| ^^^^
18
+
19
+
error: types that do not implement `Drop` can still have drop glue, consider instead using `std::mem::needs_drop` to detect whether a type is trivially dropped
20
+
--> $DIR/dyn-drop.rs:5:16
21
+
|
22
+
LL | fn baz(_: *mut Drop) {}
23
+
| ^^^^
24
+
25
+
error: types that do not implement `Drop` can still have drop glue, consider instead using `std::mem::needs_drop` to detect whether a type is trivially dropped
26
+
--> $DIR/dyn-drop.rs:7:15
27
+
|
28
+
LL | _x: Box<dyn Drop>
29
+
| ^^^^
30
+
31
+
error: types that do not implement `Drop` can still have drop glue, consider instead using `std::mem::needs_drop` to detect whether a type is trivially dropped
0 commit comments