Why is a local variable not being dropped before calling a non-returning function? Do I need to call drop on every value that should be called before a non-returning function?
This would be an extra rule in the automatic drop behavior, increasing complexity of the implicit parts of the language. Such complexity creates surprises for users.
The non-returning function might be intended to execute in context of a guard value, such as tracing::Span::enter(), even if it doesn't explicitly use that value.
Only if it's needed for your application. And if it is, I would suggest creating a block rather than using drop() calls; this is more robust against changes to the code that introduce more variables.
fn main() {
{
println!("starting ...");
let x = OnDrop::new(String::from("Hello"), || println!("string got dropped"));
}
never_returns();
}
It's maybe worth noting that this does prevent the use of tail call optimization techniques. The reserved become keyword is thus planned to modify drop timing to happen before the tail call (if/when it gets implemented).