It’s returning a slice of a buffer defined inside a labeled block. I believe this is a form of Result Location Semantics. Could anyone elaborate on why this is valid?
AFAIK it is not. RLS would be returning the whole buffer, not a slice.
Also worth noting that currently, you can reference local variables outside the scope they were defined. But the intended language semantics is the opposite. It is simply the compiler isn’t implemented correctly, and it isn’t a high enough priority to fix.
It is also possible Andrew is considering changing the intended semantics to align with the current compiler behaviour, though I haven’t heard anything about this.
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.
Perhaps I am misremembering, it changed, or it was misinformation. But I did hear, I think from a team member, that the intended behaviour was for locals to live to the end of the scope.