You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I discovered this because some of our test automation code was patching our binary to modify the executable to change a static, but those tests are now broken since we can no longer rely on the statics being a precise memory location. This is fine, but the docs should be updated to reflect this.
The text was updated successfully, but these errors were encountered:
Yeah, this is definitely a valid optimization; changing an immutable static is undefined behaviour so the compiler is entitled to assume that it never happens, meaning it can inline the value in constant propagation.
So I agree that the language is misleading; it may be worth explicitly calling out that the compiler may inline the value of non-mutable statics without interior mutability. As for your specific case, interior or explicit mutability on the static may help, but I'm actually not sure whether that's legal either, if the compiler can prove no Rust code modifies it.
@alercah Some conversation in the Rust irc suggested the idea of introducing interior mutability to the static, but they also came to the same conclusion that you did about the compiler likely ignoring it if it can prove the Rust code does not modify it. I'll have to find a more compiler friendly way to work with our tests, that's not a big deal. Just wanted to make sure the docs were accurate 😄.
The docs indicate that a static is never inlined:
https://doc.rust-lang.org/beta/reference/items/static-items.html#static-items
However, as of rust 1.25 stable, it seems that statics are actually occasionally inlined, even in debug mode. See the ASM generated here; main directly inlined
a
andb
as $89858 and $89860: https://play.rust-lang.org/?gist=16ecb92477b3b447e3b4131d956ca5b6&version=stable&mode=debugI discovered this because some of our test automation code was patching our binary to modify the executable to change a static, but those tests are now broken since we can no longer rely on the statics being a precise memory location. This is fine, but the docs should be updated to reflect this.
The text was updated successfully, but these errors were encountered: