I'd always assumed that the compiler would insert
StorageDead statements into MIR when a variable comes in and out of "scope".
In this example however, the result of the calls to
checked_add are never marked live or dead.
I was wondering why that is?
(Why am I asking? In our Rust JIT, we were hoping to use
StorageLive/Dead as hints to the register allocator).
temporaries might not get them
Also on the cleanup path no variables get
we're considering scrapping these annotations entirely and computing them on the fly for each statement
Thanks for your replies!
It's looking less likely that we can rely on
StorageDead for our (niche) purposes.
I've just had a prod around in the compiler. It's not clear to me how a temporary is different from short-lived local. Is there a definition somewhere?
you can rely on them, but if they aren't there, you need to assume the thing lives for the entire function
this is how miri does it
I see. Thanks.
And when does the compiler use these temporaries?
I don't understand the question. Are you asking when the code being compiled uses these temporaries? That would be "anywhere within the function, we don't know unless we check the body"
In other wording: when does the compiler opt to use a temporary variable instead of a regular local variable? It's not clear to me the difference between a temporary variable and a normal one.
There is not really a difference on the MIR level. It just uses a "internal" variables that aren't present in surface Rust to implement checked arithmetic and drop flags, and those don't get storage statements (maybe there's a few other cases?).
bodies of constants and statics also don't use
I'm pretty sure they do? I saw a comment in rustc claiming they didn't, but
--emit=mir says otherwise.
variables that aren't present in surface Rust
^ Ah ha! That's the detail I was missing. Thanks!
ah, then it's probably just all the temporary variables that go into the return value of a constant or static that don't have storage statements, but there it makes a lot of sense, because these values are leaked to static memory