Drop implementation for structs


#1

I’m (also) working on an interpreter for rust using the MIR. Currently I’m looking into the drop behaviour of objects and how they have to be treated differently depending on type information.

My current assumptions:

  1. Types which implement Copy don’t have to be cleaned up.

  2. Types with move semantics without Drop also need no cleanup (?).

  3. Types which implement Drop have to be cleaned up, but only if the object is still alive (has not been moved).

For #3 MIR generates explicit drops. But I’m not sure about #2. Is there a case where there is an object with move semantics without drop glue attached, which still needs some cleanup?


#2

I’m not at all familiar with the compiler internals, but types that (transitively) contain other types that implement Drop need also some kind of cleanup.
Not sure if those are considered part of the 3rd category though.


#3

After a little testing it seems that these cases are taken care of by category 3.

From my understanding only mutable references fall into 2, since they don’t really manage the resource they control but they also are not allowed to be Copy.


#4

Or structs containing only mutable references and Copy values.