Skip to content

Conversation

notriddle
Copy link
Contributor

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.

trait Placeholder {}
impl<T> Placeholder for T {}
fn foo(_x: Box<dyn Placeholder>) {}

@rust-highfive
Copy link
Contributor

r? @varkor

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 3, 2021
@rust-log-analyzer

This comment has been minimized.

@notriddle
Copy link
Contributor Author

@rustbot label: +A-lint

@rustbot rustbot added the A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. label Jul 3, 2021
@varkor
Copy link
Member

varkor commented Jul 17, 2021

The actual implementation looks fine to me, but since adding new lints is a decision for the lang team, I'll defer to r? @scottmcm.

@rust-highfive rust-highfive assigned scottmcm and unassigned varkor Jul 17, 2021
@scottmcm
Copy link
Member

Given that we have the drop_bounds lint, I think having this is completely reasonable, so

@bors r=varkor

since we can always tweak or remove it later.

cc @rust-lang/lang just in case someone else has strong feelings

@bors
Copy link
Collaborator

bors commented Jul 18, 2021

📌 Commit 20e1791b9e54dff4fb96c9155a86bf7ca44105b4 has been approved by varkor

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 18, 2021
@bors
Copy link
Collaborator

bors commented Jul 18, 2021

⌛ Testing commit 20e1791b9e54dff4fb96c9155a86bf7ca44105b4 with merge 83e6f6d4ab726c5aa02bc0620946451c9807c8cd...

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Collaborator

bors commented Jul 18, 2021

💔 Test failed - checks-actions

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jul 18, 2021
Based on the conversation in rust-lang#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>) {}
```
These are all testing corner-cases in the compiler.
Adding a new warning broke these test cases, but --cap-lints stops
it from actually breaking things in production.
@notriddle notriddle force-pushed the notriddle/drop-dyn branch from 20e1791 to e054522 Compare July 18, 2021 14:57
@notriddle
Copy link
Contributor Author

@scottmcm @varkor

Added a fix for the broken clippy testcase.

@scottmcm
Copy link
Member

@bors r=varkor

@bors
Copy link
Collaborator

bors commented Jul 18, 2021

📌 Commit e054522 has been approved by varkor

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 18, 2021
@bors
Copy link
Collaborator

bors commented Jul 19, 2021

⌛ Testing commit e054522 with merge 10c0b00...

@bors
Copy link
Collaborator

bors commented Jul 19, 2021

☀️ Test successful - checks-actions
Approved by: varkor
Pushing 10c0b00 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Jul 19, 2021
@bors bors merged commit 10c0b00 into rust-lang:master Jul 19, 2021
@rustbot rustbot added this to the 1.55.0 milestone Jul 19, 2021
@notriddle notriddle deleted the notriddle/drop-dyn branch July 19, 2021 07:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants