The Drop is implicitly implemented for all types in Rust. Why would it not be an implied trait?
Next, there is wanting of some linear type support in rust, which conflate with Drop and panics...
However, we can make an opposite feature: ImplicitDrop: Drop trait. It's also implied for all fitiing types, can be opted out via ?ImplicitDrop bound and a negative impl.
The types which do such an opt out can still be dropped, except this must happen explicitly (via drop or drop_in_place).
So at the end we have following situation:
Do a type implementDrop?
Yes (explicitly of by compiler's derive) Do a type implementImplicitDrop?
Yes (has not opted out) Behaves as today
No
Cannot go out of scope - must be dropped manually. (but panic still calls drop implicitly) (New)
No (has a negative impl)
Cannot go out of scope, or be manually dropped => needs special care of the programmer. Is a linear type => must be used somehow. (New)
To reduce possibilities of misuse, we should say that any type which needs destruction should rely on Drop.
There’s previous discussion on the topic. One of the most recent probably
which includes links to more topics, too.
That’s not quite accurate. The trait Drop itself is always explicit when it’s implemented. But types without Drop implementations can have so-called “drop glue”, which can (somewhat) be tested with mem::needs_drop. The situation as it currently stands also means that T: Drop bounds in (generic) Rust code are always useless.
It's not really all that complicated a situation currently.
When a value goes out of scope (or you call ptr::drop_in_place on it), its drop glue is run. Said drop glue does two things:
If the type of the value has an implementation of Drop, calls Drop::drop(&mut value)
Recursively runs the drop glue of each of the value's fields
(This can actually sometimes lead to a problematic amount of IR bloat sometimes, in that the fields' drop calls are separate inlining candidates rather than one sharable, outlinable group.)
As an optimization, if mem::needs_drop returns false, you know that drop glue is recursively guaranteed to be a noöp, and can skip running any drop glue.
It's important to note that a type doesn't have to implement Drop to be disposed of. In fact, any type which implements Copy implicitly has a guarantee to never implement Drop.
An important part of the impl !Trait proposal is that it's not removing anything. All it's doing is saying you'll never impl Trait. It would be correct to say that there's impl<T: Copy> !Drop for T {} in the standard library.
Note that there are a bunch of rules that depend on types not being Drop. For example, you can't move out of a field of a type that implements Drop, but you can move out of a type which needs_drop because of a field but does not have its own Drop.
Thus a change like this would be a breaking change.
Indeed. The solution is either to add more magic - "is there Drop specialization for type or not?" - or to go with trait approach. I think of something like Dropable auto trait for drop glue, which Drop requires. Opting out from this makes (values of) a type to require programmer attention.
Some of it is explained in the linked thread, but roughly: I think positive-only reasoning better fits conceptually. It allows you to uniformly think of traits as capabilities that allow you to do certain things: right now Drop is the only trait that signals obligation. Avoiding negative reasoning makes it possible to soundly make a default assumption that a trait is not implemented for a type until proven otherwise.