Skip to content

Commit ddccd12

Browse files
committed
address review comments
1 parent e5bc1d2 commit ddccd12

File tree

4 files changed

+80
-90
lines changed

4 files changed

+80
-90
lines changed

src/SUMMARY.md

-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@
8888
- [Behavior not considered unsafe](behavior-not-considered-unsafe.md)
8989

9090
- [Application Binary Interface](abi.md)
91-
- [#[used]](used.md)
9291

9392
[Appendix: Influences](influences.md)
9493

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

+23-21
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
# Attributes
22

3-
> **<sup>Syntax</sup>**
4-
> _Attribute_ :
5-
> &nbsp;&nbsp; _InnerAttribute_ | _OuterAttribute_
6-
>
7-
> _InnerAttribute_ :
8-
> &nbsp;&nbsp; `#![` MetaItem `]`
9-
>
10-
> _OuterAttribute_ :
11-
> &nbsp;&nbsp; `#[` MetaItem `]`
12-
>
13-
> _MetaItem_ :
14-
> &nbsp;&nbsp; &nbsp;&nbsp; IDENTIFIER
15-
> &nbsp;&nbsp; | IDENTIFIER `=` LITERAL
16-
> &nbsp;&nbsp; | IDENTIFIER `(` LITERAL `)`
17-
> &nbsp;&nbsp; | IDENTIFIER `(` _MetaSeq_ `)`
18-
>
19-
> _MetaSeq_ :
20-
> &nbsp;&nbsp; &nbsp;&nbsp; EMPTY
21-
> &nbsp;&nbsp; | _MetaItem_
3+
> **<sup>Syntax</sup>**
4+
> _Attribute_ :
5+
> &nbsp;&nbsp; _InnerAttribute_ | _OuterAttribute_
6+
>
7+
> _InnerAttribute_ :
8+
> &nbsp;&nbsp; `#![` MetaItem `]`
9+
>
10+
> _OuterAttribute_ :
11+
> &nbsp;&nbsp; `#[` MetaItem `]`
12+
>
13+
> _MetaItem_ :
14+
> &nbsp;&nbsp; &nbsp;&nbsp; IDENTIFIER
15+
> &nbsp;&nbsp; | IDENTIFIER `=` LITERAL
16+
> &nbsp;&nbsp; | IDENTIFIER `(` LITERAL `)`
17+
> &nbsp;&nbsp; | IDENTIFIER `(` _MetaSeq_ `)`
18+
>
19+
> _MetaSeq_ :
20+
> &nbsp;&nbsp; &nbsp;&nbsp; EMPTY
21+
> &nbsp;&nbsp; | _MetaItem_
2222
> &nbsp;&nbsp; | _MetaItem_ `,` _MetaSeq_
2323
2424
Any [item declaration] or [generic lifetime or type parameter][generics] may
@@ -116,7 +116,7 @@ On an `extern` block, the following attributes are interpreted:
116116
declarations in this block to be linked correctly. `link` supports an optional
117117
`kind` key with three possible values: `dylib`, `static`, and `framework`. See
118118
[external blocks](items/external-blocks.html) for more about external blocks.
119-
Two examples: `#[link(name = "readline")]` and
119+
Two examples: `#[link(name = "readline")]` and
120120
`#[link(name = "CoreFoundation", kind = "framework")]`.
121121
- `linked_from` - indicates what native library this block of FFI items is
122122
coming from. This attribute is of the form `#[linked_from = "foo"]` where
@@ -165,6 +165,8 @@ information on macro scope.
165165
object file that this item's contents will be placed into.
166166
- `no_mangle` - on any item, do not apply the standard name mangling. Set the
167167
symbol for this item to its identifier.
168+
- `used` - on statics, this forces the compiler to keep the variable in the
169+
output object file.
168170

169171
### Deprecation
170172

@@ -450,7 +452,7 @@ When used on a function in an implementation, the attribute does nothing.
450452
> { five() };
451453
> if true { five() } else { 0i32 };
452454
> match true {
453-
> _ => five()
455+
> _ => five()
454456
> };
455457
> }
456458
> ```

src/used.md

-62
This file was deleted.

0 commit comments

Comments
 (0)