Skip to content

Docs indicate that statics are never inlined, but this is not true as of rustc 1.25 #322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ncoish opened this issue Apr 30, 2018 · 3 comments
Assignees
Labels
C-bug Incorrect statements, terminology, or rendering issues

Comments

@ncoish
Copy link

ncoish commented Apr 30, 2018

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 and b as $89858 and $89860: https://play.rust-lang.org/?gist=16ecb92477b3b447e3b4131d956ca5b6&version=stable&mode=debug

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.

@matthewjasper matthewjasper added the C-bug Incorrect statements, terminology, or rendering issues label Apr 30, 2018
@alercah
Copy link
Contributor

alercah commented Apr 30, 2018

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.

@ncoish
Copy link
Author

ncoish commented Apr 30, 2018

@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 😄.

@Havvy Havvy self-assigned this May 1, 2018
@alercah
Copy link
Contributor

alercah commented May 3, 2018

Fixed in #323.

@alercah alercah closed this as completed May 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Incorrect statements, terminology, or rendering issues
Projects
None yet
Development

No branches or pull requests

4 participants