Skip to content

Clarify the meaning of reference-typed constants #22

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
steveklabnik opened this issue Mar 23, 2017 · 0 comments · Fixed by #867
Closed

Clarify the meaning of reference-typed constants #22

steveklabnik opened this issue Mar 23, 2017 · 0 comments · Fixed by #867
Labels
Language Cleanup Improvements to existing language which is correct but not clear, or missing examples, or the like.

Comments

@steveklabnik
Copy link
Member

Migrated from rust-lang/rust#34817


I used pub const FOO: &'static Bar = &Bar { // crate-private stuff here ... }; in a crate. Within that crate, FOO as *const Bar was consistently the same address. However, when used in another crate, FOO as *const Bar became another address.

Apparently what's happening is that the RHS of const is inlined but within a given crate (effectively allowing a crate to inline stuff that's private to another crate), duplicates are later coalesced when making the values being referred to into the data section of the object file.

What I thought was supposed to be happening was this:

I expected the Bar { // crate-private stuff here ... } bit to be evaluated once in the crate where the constant was declared and the result getting uniquely baked into the data section of the object file. I then expected the value that gets inlined at contant use time to be the address of that unique location.

Especially considering that this appears to result in a constant address within a crate but blows up when the constant is used in another crate, it would be useful for the Book and the Reference to spell out more clearly that the constant isn't just the reference value of the right-hand-side expression but the entire right-hand-side expression itself and explicitly warn about the instantiations of the constant expression getting coalesced on a per-crate basis, which gives the appearance of the reference being the value of a uniquely evaluated expression rather than the expression itself as long as you are only testing within a single crate.

@Havvy Havvy added the Enhancement Improvement in a non-prose section of the book label Sep 24, 2017
@alercah alercah added New Content Missing features or aspects of language not currently documented. Language Cleanup Improvements to existing language which is correct but not clear, or missing examples, or the like. and removed Enhancement Improvement in a non-prose section of the book New Content Missing features or aspects of language not currently documented. labels Jul 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Language Cleanup Improvements to existing language which is correct but not clear, or missing examples, or the like.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants