Skip to content

Commit e47b299

Browse files
japaricehuss
authored andcommitted
address review comments
1 parent 26d49a1 commit e47b299

File tree

4 files changed

+59
-69
lines changed

4 files changed

+59
-69
lines changed

src/SUMMARY.md

-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@
112112
- [Constant Evaluation](const_eval.md)
113113

114114
- [Application Binary Interface](abi.md)
115-
- [#[used]](used.md)
116115

117116
[Appendix: Influences](influences.md)
118117

src/abi.md

+57-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,60 @@
11
# Application Binary Interface (ABI)
22

3-
This section documents (or will document) features that affect the ABI of a Rust program / binary,
4-
rlib, dylib, etc. A (likely incomplete) list of such features is shown below:
3+
This section documents features that affect the ABI of a Rust program / binary, rlib, dylib, etc. A
4+
list of such features is shown below:
55

6-
- #[used]
7-
- #[no_mangle]
8-
- #[link_section]
9-
- extern "$ABI" fn
6+
- `#[export_name]`
7+
- `#[link_section]`
8+
- `#[no_mangle]`
9+
- `#[used]`
10+
- `extern "$ABI" fn`
11+
12+
## `#[used]`
13+
14+
The `#[used]` attribute can only be applied to `static` variables. This attribute forces the
15+
compiler to keep the variable in the output object file (.o, .rlib, etc.) even if the variable is
16+
not used, or referenced, by any other item in the crate.
17+
18+
Below is an example that shows under what conditions the compiler keeps a `static` variable in the
19+
output object file.
20+
21+
``` rust
22+
// foo.rs
23+
24+
#![feature(used)]
25+
26+
// kept because of #[used]
27+
#[used]
28+
static FOO: u32 = 0;
29+
30+
// removed because it's unused
31+
#[allow(dead_code)]
32+
static BAR: u32 = 0;
33+
34+
// kept because it's referenced by a public, reachable function
35+
pub static BAZ: u32 = 0;
36+
37+
pub static QUUX: u32 = 0;
38+
39+
pub fn quux() -> &'static u32 {
40+
&QUUX
41+
}
42+
43+
// removed because it's referenced by a private, unused (dead) function
44+
static CORGE: u32 = 0;
45+
46+
#[allow(dead_code)]
47+
fn corge() -> &'static u32 {
48+
&CORGE
49+
}
50+
```
51+
52+
``` console
53+
$ rustc -O --emit=obj --crate-type=rlib foo.rs
54+
55+
$ nm -C foo.o
56+
0000000000000000 R foo::BAZ
57+
0000000000000000 r foo::FOO
58+
0000000000000000 R foo::QUUX
59+
0000000000000000 T foo::quux
60+
```

src/attributes.md

+2
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,8 @@ which can be used to control type layout.
204204
object file that this item's contents will be placed into.
205205
- `no_mangle` - on any item, do not apply the standard name mangling. Set the
206206
symbol for this item to its identifier.
207+
- `used` - on statics, this forces the compiler to keep the variable in the
208+
output object file.
207209

208210
### Deprecation
209211

src/used.md

-62
This file was deleted.

0 commit comments

Comments
 (0)