-
Notifications
You must be signed in to change notification settings - Fork 501
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
document #[used] #361
Merged
Merged
document #[used] #361
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# Application Binary Interface (ABI) | ||
|
||
This section documents features that affect the ABI of the compiled output of | ||
a crate. | ||
|
||
See *[extern functions]* for information on specifying the ABI for exporting | ||
functions. See *[external blocks]* for information on specifying the ABI for | ||
linking external libraries. | ||
|
||
## The `used` attribute | ||
|
||
The *`used` attribute* can only be applied to [`static` items]. This [attribute] forces the | ||
compiler to keep the variable in the output object file (.o, .rlib, etc.) even if the variable is | ||
not used, or referenced, by any other item in the crate. | ||
|
||
Below is an example that shows under what conditions the compiler keeps a `static` item in the | ||
output object file. | ||
|
||
``` rust | ||
// foo.rs | ||
|
||
// This is kept because of `#[used]`: | ||
#[used] | ||
static FOO: u32 = 0; | ||
|
||
// This is removable because it is unused: | ||
#[allow(dead_code)] | ||
static BAR: u32 = 0; | ||
|
||
// This is kept because it is publicly reachable: | ||
pub static BAZ: u32 = 0; | ||
|
||
// This is kept because it is referenced by a public, reachable function: | ||
static QUUX: u32 = 0; | ||
|
||
pub fn quux() -> &'static u32 { | ||
&QUUX | ||
} | ||
|
||
// This is removable because it is referenced by a private, unused (dead) function: | ||
static CORGE: u32 = 0; | ||
|
||
#[allow(dead_code)] | ||
fn corge() -> &'static u32 { | ||
&CORGE | ||
} | ||
``` | ||
|
||
``` console | ||
$ rustc -O --emit=obj --crate-type=rlib foo.rs | ||
|
||
$ nm -C foo.o | ||
0000000000000000 R foo::BAZ | ||
0000000000000000 r foo::FOO | ||
0000000000000000 R foo::QUUX | ||
0000000000000000 T foo::quux | ||
``` | ||
|
||
[`static` items]: items/static-items.html | ||
[attribute]: attributes.html | ||
[extern functions]: items/functions.html#extern-functions | ||
[external blocks]: items/external-blocks.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like adding more attributes to the misc. attributes section. Could we rename Misc. attributes to "Object file attributes"? We could throw
no_main
,no_start
andwindows_subsystem
in that heading too possibly (I really don't know much about object files...so taking a guess here). Whereglobal_allocator
goes, I don't know. Perchance just make it its own section?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm looking at regrouping and updating the attributes on the attributes page. I'd like to do that in a separate PR, if that's OK? I've opened issue #534 to discuss this further, I'd be happy if you could leave some feedback since you've done most of the initial work on this.