Skip to content

Commit 081e3c9

Browse files
authored
Merge pull request #1905 from ehuss/used
Update `used` to use the attribute template
2 parents 4fd6140 + f737c4d commit 081e3c9

1 file changed

Lines changed: 50 additions & 47 deletions

File tree

src/abi.md

Lines changed: 50 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -9,57 +9,60 @@ See *[extern functions]* for information on specifying the ABI for exporting
99
functions. See *[external blocks]* for information on specifying the ABI for
1010
linking external libraries.
1111

12+
<!-- template:attributes -->
1213
r[abi.used]
1314
## The `used` attribute
1415

1516
r[abi.used.intro]
16-
The *`used` attribute* can only be applied to [`static` items]. This [attribute] forces the
17-
compiler to keep the variable in the output object file (.o, .rlib, etc. excluding final binaries)
18-
even if the variable is not used, or referenced, by any other item in the crate.
19-
However, the linker is still free to remove such an item.
17+
The *`used` [attribute]* forces a [static] to be kept in the output object file (.o, .rlib, etc., excluding final binaries) even if it's never used or referenced by any other item in the crate. The linker, however, is still free to remove it.
18+
19+
> [!EXAMPLE]
20+
> ```rust
21+
> // lib.rs
22+
>
23+
> // This is kept because of `#[used]`.
24+
> #[used]
25+
> static S1: u8 = 0;
26+
>
27+
> // This is removable because it's unused.
28+
> #[allow(dead_code)]
29+
> static S2: u8 = 0;
30+
>
31+
> // This is kept because it's publicly reachable.
32+
> pub static S3: u8 = 0;
33+
>
34+
> // This is kept because it's referenced by a publicly
35+
> // reachable function.
36+
> static S4: u8 = 0;
37+
> #[unsafe(no_mangle)] pub fn f4() -> &'static u8 { &S4 }
38+
>
39+
> // This is removable because it's referenced only by a
40+
> // private, unused (dead) function.
41+
> static S5: u8 = 0;
42+
> #[allow(dead_code)]
43+
> fn f5() -> &'static u8 { &S5 }
44+
> ```
45+
>
46+
> ```console
47+
> $ rustc -O --emit=obj --crate-type=rlib lib.rs
48+
> $ LC_ALL=C nm -C lib.o
49+
> 0000000000000000 R lib::S1
50+
> 0000000000000000 R lib::S3
51+
> 0000000000000000 r lib::S4
52+
> 0000000000000000 T f4
53+
> ```
54+
55+
r[abi.used.syntax]
56+
The `used` attribute uses the [MetaWord] syntax.
57+
58+
r[abi.used.allowed-positions]
59+
The `used` attribute may only be applied to [`static` items].
60+
61+
r[abi.used.duplicates]
62+
Only the first use of `used` on an item has effect.
2063
21-
Below is an example that shows under what conditions the compiler keeps a `static` item in the
22-
output object file.
23-
24-
``` rust
25-
// foo.rs
26-
27-
// This is kept because of `#[used]`:
28-
#[used]
29-
static FOO: u32 = 0;
30-
31-
// This is removable because it is unused:
32-
#[allow(dead_code)]
33-
static BAR: u32 = 0;
34-
35-
// This is kept because it is publicly reachable:
36-
pub static BAZ: u32 = 0;
37-
38-
// This is kept because it is referenced by a public, reachable function:
39-
static QUUX: u32 = 0;
40-
41-
pub fn quux() -> &'static u32 {
42-
&QUUX
43-
}
44-
45-
// This is removable because it is referenced by a private, unused (dead) function:
46-
static CORGE: u32 = 0;
47-
48-
#[allow(dead_code)]
49-
fn corge() -> &'static u32 {
50-
&CORGE
51-
}
52-
```
53-
54-
``` console
55-
$ rustc -O --emit=obj --crate-type=rlib foo.rs
56-
57-
$ nm -C foo.o
58-
0000000000000000 R foo::BAZ
59-
0000000000000000 r foo::FOO
60-
0000000000000000 R foo::QUUX
61-
0000000000000000 T foo::quux
62-
```
64+
> [!NOTE]
65+
> `rustc` lints against any use following the first.
6366
6467
r[abi.no_mangle]
6568
## The `no_mangle` attribute
@@ -149,10 +152,10 @@ r[abi.export_name.edition2024]
149152
> [!EDITION-2024]
150153
> Before the 2024 edition it is allowed to use the `export_name` attribute without the `unsafe` qualification.
151154
152-
[`static` items]: items/static-items.md
153155
[attribute]: attributes.md
154156
[extern functions]: items/functions.md#extern-function-qualifier
155157
[external blocks]: items/external-blocks.md
156158
[function]: items/functions.md
157159
[item]: items.md
160+
[`static` items]: items.static
158161
[static]: items/static-items.md

0 commit comments

Comments
 (0)