Skip to content

Update used to use the attribute template #1905

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 56 additions & 48 deletions src/abi.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,53 +13,62 @@ r[abi.used]
## The `used` attribute

r[abi.used.intro]
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. excluding final binaries)
even if the variable is not used, or referenced, by any other item in the crate.
However, the linker is still free to remove such an item.

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
```
The *`used` [attribute]* forces a [`static` item][items.static] to be kept in the output object file (.o, .rlib, etc. excluding final binaries) even if the static is never used, or referenced, by an other item in the crate. However, the linker is still free to remove such an item.

> [!EXAMPLE]
> This 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
> ```

r[abi.used.syntax]
The `used` attribute uses the [MetaWord] syntax and thus does not take any inputs.

r[abi.used.allowed-positions]
The `used` attribute may only be applied to [`static` items][items.static].

r[abi.used.duplicates]
Only the first instance of `used` on an item is honored. Subsequent `used` attributes are ignored.

> [!NOTE]
> `rustc` currently warns on subsequent duplicate `used` attributes.

r[abi.no_mangle]
## The `no_mangle` attribute
Expand Down Expand Up @@ -135,7 +144,6 @@ r[abi.export_name.edition2024]
> [!EDITION-2024]
> Before the 2024 edition it is allowed to use the `export_name` attribute without the `unsafe` qualification.

[`static` items]: items/static-items.md
[attribute]: attributes.md
[extern functions]: items/functions.md#extern-function-qualifier
[external blocks]: items/external-blocks.md
Expand Down