Hm, I’ve heard that the intended language semantics is that local variables are live until the end of enclosing function/loop
This makes sense to me:
- Extending the lifetime prevents bugs and simplifies the code.
- The optimizer should be capable of precisely tracking what is actually used and what’s not. Even with C/C++ rules, there is going to be liveness analysis in the optimization pipeline, so it’s not like the surface language-level rule changes much in the resulting code.
This is actually one of the smaller happy consequences of abandoning RAII: with RAII, you are forced to language lawyer when exactly drop happens, with a tone of finicky rules (e.g. if let temporary scope - The Rust Edition Guide). With defer, you have very explicit syntax for where side-effects happen, and the stack memory can have “whatever” lifetime.