Description
In my understanding, we do in some circumstances guarantee that drop runs. For example:
struct Guard;
impl Drop for Guard {
fn drop(&mut self) {
println!("Hello!");
}
}
pub fn test(f: impl FnOnce()) {
let _guard = Guard;
f();
}
Here, we guarantee that no matter the environment or whatever f
does, if the stack frame of test
ever gets popped or otherwise "deallocated", then the println!
certainly happens. For example, f
might loop forever or abort the process, but it cannot "return" or "unwind" or finish in any other way that would circumvent the printing, nor can it use longjmp
to skip test
's cleanup, not can it just ask the OS to outright kill the current thread (without tearing down the entire process).
(By "guarantee" I mean "we consider it okay for safe libraries to rely on this and cause UB if it gets broken" -- but there is no immediate language-level UB caused by this, so if you do this kind of skipping of destructors in a controlled way, say for your own code which you knows has nothing droppable on the stack, then you are fine.)
This is needed to actually realize the pinning drop guarantee, but it seems not to be documented anywhere explicitly?