Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c207931

Browse files
authoredMar 17, 2019
Merge pull request #537 from ehuss/attribute-reorg-syntax
Reorg and update attributes
2 parents e97bb87 + 53c5cff commit c207931

28 files changed

+862
-564
lines changed
 

‎src/SUMMARY.md

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,30 @@
2121

2222
- [Conditional compilation](conditional-compilation.md)
2323

24-
- [Items and attributes](items-and-attributes.md)
25-
- [Items](items.md)
26-
- [Modules](items/modules.md)
27-
- [Extern crates](items/extern-crates.md)
28-
- [Use declarations](items/use-declarations.md)
29-
- [Functions](items/functions.md)
30-
- [Type aliases](items/type-aliases.md)
31-
- [Structs](items/structs.md)
32-
- [Enumerations](items/enumerations.md)
33-
- [Unions](items/unions.md)
34-
- [Constant items](items/constant-items.md)
35-
- [Static items](items/static-items.md)
36-
- [Traits](items/traits.md)
37-
- [Implementations](items/implementations.md)
38-
- [External blocks](items/external-blocks.md)
24+
- [Items](items.md)
25+
- [Modules](items/modules.md)
26+
- [Extern crates](items/extern-crates.md)
27+
- [Use declarations](items/use-declarations.md)
28+
- [Functions](items/functions.md)
29+
- [Type aliases](items/type-aliases.md)
30+
- [Structs](items/structs.md)
31+
- [Enumerations](items/enumerations.md)
32+
- [Unions](items/unions.md)
33+
- [Constant items](items/constant-items.md)
34+
- [Static items](items/static-items.md)
35+
- [Traits](items/traits.md)
36+
- [Implementations](items/implementations.md)
37+
- [External blocks](items/external-blocks.md)
3938
- [Type and lifetime parameters](items/generics.md)
4039
- [Associated Items](items/associated-items.md)
4140
- [Visibility and Privacy](visibility-and-privacy.md)
42-
- [Attributes](attributes.md)
41+
42+
- [Attributes](attributes.md)
43+
- [Testing](attributes/testing.md)
44+
- [Derive](attributes/derive.md)
45+
- [Diagnostics](attributes/diagnostics.md)
46+
- [Code generation](attributes/codegen.md)
47+
- [Limits](attributes/limits.md)
4348

4449
- [Statements and expressions](statements-and-expressions.md)
4550
- [Statements](statements.md)

‎src/abi.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,40 @@ $ nm -C foo.o
5656
0000000000000000 T foo::quux
5757
```
5858

59+
## The `no_mangle` attribute
60+
61+
The *`no_mangle` attribute* may be used on any [item] to disable standard
62+
symbol name mangling. The symbol for the item will be the identifier of the
63+
item's name.
64+
65+
## The `link_section` attribute
66+
67+
The *`link_section` attribute* specifies the section of the object file that a
68+
[function] or [static]'s content will be placed into. It uses the
69+
[_MetaNameValueStr_] syntax to specify the section name.
70+
71+
```rust,ignore
72+
#[no_mangle]
73+
#[link_section = ".example_section"]
74+
pub static VAR1: u32 = 1;
75+
```
76+
77+
## The `export_name` attribute
78+
79+
The *`export_name` attribute* specifies the name of the symbol that will be
80+
exported on a [function] or [static]. It uses the [_MetaNameValueStr_] syntax
81+
to specify the symbol name.
82+
83+
```rust,ignore
84+
#[export_name = "exported_symbol_name"]
85+
pub fn name_in_rust() { }
86+
```
87+
88+
[_MetaNameValueStr_]: attributes.html#meta-item-attribute-syntax
5989
[`static` items]: items/static-items.html
6090
[attribute]: attributes.html
6191
[extern functions]: items/functions.html#extern-functions
6292
[external blocks]: items/external-blocks.html
93+
[function]: items/functions.html
94+
[item]: items.html
95+
[static]: items/static-items.html

‎src/attributes-redirect.html

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<script>
2+
(function() {
3+
var fragments = {
4+
"#cold-attribute": "codegen.html#the-cold-attribute",
5+
"#conditional-compilation": "conditional-compilation.html",
6+
"#deprecation": "diagnostics.html#the-deprecated-attribute",
7+
"#derive": "attributes/derive.html",
8+
"#documentation": "../rustdoc/the-doc-attribute.html",
9+
"#ffi-attributes": "attributes.html#built-in-attributes-index",
10+
"#inline-attribute": "codegen.html#the-inline-attribute",
11+
"#lint-check-attributes": "diagnostics.html#lint-check-attributes",
12+
"#macro-related-attributes": "attributes.html#built-in-attributes-index",
13+
"#miscellaneous-attributes": "attributes.html#built-in-attributes-index",
14+
"#must_use": "diagnostics.html#the-must_use-attribute",
15+
"#optimization-hints": "codegen.html#optimization-hints",
16+
"#path": "items/modules.html#the-path-attribute",
17+
"#preludes": "crates-and-source-files.html#preludes-and-no_std",
18+
"#testing": "testing.html",
19+
"#tool-lint-attributes": "diagnostics.html#tool-lint-attributes",
20+
"#crate-only-attributes": "attributes.html#built-in-attributes-index",
21+
};
22+
var target = fragments[window.location.hash];
23+
if (target) {
24+
var url = window.location.toString();
25+
var base = url.substring(0, url.lastIndexOf('/'));
26+
window.location.replace(base + "/" + target);
27+
}
28+
})();
29+
</script>

‎src/attributes.md

Lines changed: 153 additions & 482 deletions
Large diffs are not rendered by default.

‎src/attributes/codegen.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Code generation attributes
2+
3+
The following [attributes] are used for controlling code generation.
4+
5+
## Optimization hints
6+
7+
The `cold` and `inline` [attributes] give suggestions to generate code in a
8+
way that may be faster than what it would do without the hint. The attributes
9+
are only hints, and may be ignored.
10+
11+
Both attributes can be used on [functions]. When applied to a function in a
12+
[trait], they apply only to that function when used as a default function for
13+
a trait implementation and not to all trait implementations. The attributes
14+
have no effect on a trait function without a body.
15+
16+
### The `inline` attribute
17+
18+
The *`inline` [attribute]* suggests that a copy of the attributed function
19+
should be placed in the caller, rather than generating code to call the
20+
function where it is defined.
21+
22+
> ***Note***: The `rustc` compiler automatically inlines functions based on
23+
> internal heuristics. Incorrectly inlining functions can make the program
24+
> slower, so this attribute should be used with care.
25+
26+
There are three ways to use the inline attribute:
27+
28+
* `#[inline]` suggests performing an inline expansion.
29+
* `#[inline(always)]` suggests that an inline expansion should always be
30+
performed.
31+
* `#[inline(never)]` suggests that an inline expansion should never be
32+
performed.
33+
34+
### The `cold` attribute
35+
36+
The *`cold` [attribute]* suggests that the attributed function is unlikely to
37+
be called.
38+
39+
## The `no_builtins` attribute
40+
41+
The *`no_builtins` [attribute]* may be applied at the crate level to disable
42+
optimizing certain code patterns to invocations of library functions that are
43+
assumed to exist.
44+
45+
[attribute]: attributes.html
46+
[attributes]: attributes.html
47+
[functions]: items/functions.html
48+
[trait]: items/traits.html

‎src/attributes/derive.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Derive
2+
3+
The *`derive` attribute* allows new [items] to be automatically generated for
4+
data structures. It uses the [_MetaListPaths_] syntax to specify a list of
5+
traits to implement or paths to [derive macros] to process.
6+
7+
For example, the following will create an [`impl` item] for the
8+
[`PartialEq`] and [`Clone`] traits for `Foo`, and the type parameter `T` will be
9+
given the `PartialEq` or `Clone` constraints for the appropriate `impl`:
10+
11+
```rust
12+
#[derive(PartialEq, Clone)]
13+
struct Foo<T> {
14+
a: i32,
15+
b: T,
16+
}
17+
```
18+
19+
The generated `impl` for `PartialEq` is equivalent to
20+
21+
```rust
22+
# struct Foo<T> { a: i32, b: T }
23+
impl<T: PartialEq> PartialEq for Foo<T> {
24+
fn eq(&self, other: &Foo<T>) -> bool {
25+
self.a == other.a && self.b == other.b
26+
}
27+
28+
fn ne(&self, other: &Foo<T>) -> bool {
29+
self.a != other.a || self.b != other.b
30+
}
31+
}
32+
```
33+
34+
You can implement `derive` for your own traits through [procedural macros].
35+
36+
[_MetaListPaths_]: attributes.html#meta-item-attribute-syntax
37+
[`Clone`]: ../std/clone/trait.Clone.html
38+
[`PartialEq`]: ../std/cmp/trait.PartialEq.html
39+
[`impl` item]: items/implementations.html
40+
[items]: items.html
41+
[derive macros]: procedural-macros.html#derive-macros
42+
[procedural macros]: procedural-macros.html#derive-macros

‎src/attributes/diagnostics.md

Lines changed: 281 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,281 @@
1+
# Diagnostic attributes
2+
3+
The following [attributes] are used for controlling or generating diagnostic
4+
messages during compilation.
5+
6+
## Lint check attributes
7+
8+
A lint check names a potentially undesirable coding pattern, such as
9+
unreachable code or omitted documentation. The lint attributes `allow`,
10+
`warn`, `deny`, and `forbid` use the [_MetaListPaths_] syntax to specify a
11+
list of lint names to change the lint level for the entity to which the
12+
attribute applies.
13+
14+
For any lint check `C`:
15+
16+
* `allow(C)` overrides the check for `C` so that violations will go
17+
unreported,
18+
* `warn(C)` warns about violations of `C` but continues compilation.
19+
* `deny(C)` signals an error after encountering a violation of `C`,
20+
* `forbid(C)` is the same as `deny(C)`, but also forbids changing the lint
21+
level afterwards,
22+
23+
> Note: The lint checks supported by `rustc` can be found via `rustc -W help`,
24+
> along with their default settings and are documented in the [rustc book].
25+
26+
```rust
27+
pub mod m1 {
28+
// Missing documentation is ignored here
29+
#[allow(missing_docs)]
30+
pub fn undocumented_one() -> i32 { 1 }
31+
32+
// Missing documentation signals a warning here
33+
#[warn(missing_docs)]
34+
pub fn undocumented_too() -> i32 { 2 }
35+
36+
// Missing documentation signals an error here
37+
#[deny(missing_docs)]
38+
pub fn undocumented_end() -> i32 { 3 }
39+
}
40+
```
41+
42+
This example shows how one can use `allow` and `warn` to toggle a particular
43+
check on and off:
44+
45+
```rust
46+
#[warn(missing_docs)]
47+
pub mod m2{
48+
#[allow(missing_docs)]
49+
pub mod nested {
50+
// Missing documentation is ignored here
51+
pub fn undocumented_one() -> i32 { 1 }
52+
53+
// Missing documentation signals a warning here,
54+
// despite the allow above.
55+
#[warn(missing_docs)]
56+
pub fn undocumented_two() -> i32 { 2 }
57+
}
58+
59+
// Missing documentation signals a warning here
60+
pub fn undocumented_too() -> i32 { 3 }
61+
}
62+
```
63+
64+
This example shows how one can use `forbid` to disallow uses of `allow` for
65+
that lint check:
66+
67+
```rust,compile_fail
68+
#[forbid(missing_docs)]
69+
pub mod m3 {
70+
// Attempting to toggle warning signals an error here
71+
#[allow(missing_docs)]
72+
/// Returns 2.
73+
pub fn undocumented_too() -> i32 { 2 }
74+
}
75+
```
76+
77+
### Tool lint attributes
78+
79+
Tool lints allows using scoped lints, to `allow`, `warn`, `deny` or `forbid`
80+
lints of certain tools.
81+
82+
Currently `clippy` is the only available lint tool.
83+
84+
Tool lints only get checked when the associated tool is active. If a lint
85+
attribute, such as `allow`, references a nonexistent tool lint, the compiler
86+
will not warn about the nonexistent lint until you use the tool.
87+
88+
Otherwise, they work just like regular lint attributes:
89+
90+
```rust
91+
// set the entire `pedantic` clippy lint group to warn
92+
#![warn(clippy::pedantic)]
93+
// silence warnings from the `filter_map` clippy lint
94+
#![allow(clippy::filter_map)]
95+
96+
fn main() {
97+
// ...
98+
}
99+
100+
// silence the `cmp_nan` clippy lint just for this function
101+
#[allow(clippy::cmp_nan)]
102+
fn foo() {
103+
// ...
104+
}
105+
```
106+
107+
## The `deprecated` attribute
108+
109+
The *`deprecated` attribute* marks an item as deprecated. `rustc` will issue
110+
warnings on usage of `#[deprecated]` items. `rustdoc` will show item
111+
deprecation, including the `since` version and `note`, if available.
112+
113+
The `deprecated` attribute has several forms:
114+
115+
- `deprecated` — Issues a generic message.
116+
- `deprecated = "message"` — Includes the given string in the deprecation
117+
message.
118+
- [_MetaListNameValueStr_] syntax with two optional fields:
119+
- `since` — Specifies a version number when the item was deprecated. `rustc`
120+
does not currently interpret the string, but external tools like [Clippy]
121+
may check the validity of the value.
122+
- `note` — Specifies a string that should be included in the deprecation
123+
message. This is typically used to provide an explanation about the
124+
deprecation and preferred alternatives.
125+
126+
The `deprecated` attribute may be applied to any [item], [trait item], [enum
127+
variant], [struct field], or [external block item]. It cannot be applied to [trait
128+
implementation items]. When applied to an item containing other items, such as
129+
a [module] or [implementation], all child items inherit the deprecation attribute.
130+
<!-- NOTE: Currently broken for macros, see https://github.com/rust-lang/rust/issues/49912
131+
Also, it is only rejected for trait impl items (AnnotationKind::Prohibited). In all
132+
other locations, it is silently ignored. Tuple struct fields are ignored.
133+
-->
134+
135+
Here is an example:
136+
137+
```rust
138+
#[deprecated(since = "5.2", note = "foo was rarely used. Users should instead use bar")]
139+
pub fn foo() {}
140+
141+
pub fn bar() {}
142+
```
143+
144+
The [RFC][1270-deprecation.md] contains motivations and more details.
145+
146+
[1270-deprecation.md]: https://github.com/rust-lang/rfcs/blob/master/text/1270-deprecation.md
147+
148+
## The `must_use` attribute
149+
150+
The *`must_use` attribute* is used to issue a diagnostic warning when a value
151+
is not "used". It can be applied to user-defined composite types
152+
([`struct`s][struct], [`enum`s][enum], and [`union`s][union]), [functions],
153+
and [traits].
154+
155+
The `must_use` attribute may include a message by using the
156+
[_MetaNameValueStr_] syntax such as `#[must_use = "example message"]`. The
157+
message will be given alongside the warning.
158+
159+
When used on user-defined composite types, if the [expression] of an
160+
[expression statement] has that type, then the `unused_must_use` lint is
161+
violated.
162+
163+
```rust
164+
#[must_use]
165+
struct MustUse {
166+
// some fields
167+
}
168+
169+
# impl MustUse {
170+
# fn new() -> MustUse { MustUse {} }
171+
# }
172+
#
173+
// Violates the `unused_must_use` lint.
174+
MustUse::new();
175+
```
176+
177+
When used on a function, if the [expression] of an [expression statement] is a
178+
[call expression] to that function, then the `unused_must_use` lint is
179+
violated.
180+
181+
```rust
182+
#[must_use]
183+
fn five() -> i32 { 5i32 }
184+
185+
// Violates the unused_must_use lint.
186+
five();
187+
```
188+
189+
When used on a [trait declaration], a [call expression] of an [expression
190+
statement] to a function that returns an [impl trait] of that trait violates
191+
the `unsued_must_use` lint.
192+
193+
```rust
194+
#[must_use]
195+
trait Critical {}
196+
impl Critical for i32 {}
197+
198+
fn get_critical() -> impl Critical {
199+
4i32
200+
}
201+
202+
// Violates the `unused_must_use` lint.
203+
get_critical();
204+
```
205+
206+
When used on a function in a trait declaration, then the behavior also applies
207+
when the call expression is a function from an implementation of the trait.
208+
209+
```rust
210+
trait Trait {
211+
#[must_use]
212+
fn use_me(&self) -> i32;
213+
}
214+
215+
impl Trait for i32 {
216+
fn use_me(&self) -> i32 { 0i32 }
217+
}
218+
219+
// Violates the `unused_must_use` lint.
220+
5i32.use_me();
221+
```
222+
223+
When used on a function in a trait implementation, the attribute does nothing.
224+
225+
> Note: Trivial no-op expressions containing the value will not violate the
226+
> lint. Examples include wrapping the value in a type that does not implement
227+
> [`Drop`] and then not using that type and being the final expression of a
228+
> [block expression] that is not used.
229+
>
230+
> ```rust
231+
> #[must_use]
232+
> fn five() -> i32 { 5i32 }
233+
>
234+
> // None of these violate the unused_must_use lint.
235+
> (five(),);
236+
> Some(five());
237+
> { five() };
238+
> if true { five() } else { 0i32 };
239+
> match true {
240+
> _ => five()
241+
> };
242+
> ```
243+
244+
> Note: It is idiomatic to use a [let statement] with a pattern of `_`
245+
> when a must-used value is purposely discarded.
246+
>
247+
> ```rust
248+
> #[must_use]
249+
> fn five() -> i32 { 5i32 }
250+
>
251+
> // Does not violate the unused_must_use lint.
252+
> let _ = five();
253+
> ```
254+
255+
[Clippy]: https://github.com/rust-lang/rust-clippy
256+
[_MetaListNameValueStr_]: attributes.html#meta-item-attribute-syntax
257+
[_MetaListPaths_]: attributes.html#meta-item-attribute-syntax
258+
[_MetaNameValueStr_]: attributes.html#meta-item-attribute-syntax
259+
[`Drop`]: special-types-and-traits.html#drop
260+
[attributes]: attributes.html
261+
[block expression]: expressions/block-expr.html
262+
[call expression]: expressions/call-expr.html
263+
[enum variant]: items/enumerations.html
264+
[enum]: items/enumerations.html
265+
[expression statement]: statements.html#expression-statements
266+
[expression]: expressions.html
267+
[external block item]: items/external-blocks.html
268+
[functions]: items/functions.html
269+
[impl trait]: types/impl-trait.html
270+
[implementation]: items/implementations.html
271+
[item]: items.html
272+
[let statement]: statements.html#let-statements
273+
[module]: items/modules.html
274+
[rustc book]: ../rustc/lints/index.html
275+
[struct field]: items/structs.html
276+
[struct]: items/structs.html
277+
[trait declaration]: items/traits.html
278+
[trait implementation items]: items/implementations.html#trait-implementations
279+
[trait item]: items/traits.html
280+
[traits]: items/traits.html
281+
[union]: items/unions.html

‎src/attributes/limits.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Limits
2+
3+
The following [attributes] affect compile-time limits.
4+
5+
## The `recursion_limit` attribute
6+
7+
The *`recursion_limit` attribute* may be applied at the crate level to set the
8+
maximum depth for potentially infinitely-recursive compile-time operations
9+
like auto-dereference or macro expansion. It uses the [_MetaNameValueStr_]
10+
syntax to specify the recursion depth. The default is
11+
`#![recursion_limit="64"]`.
12+
13+
[attributes]: attributes.html
14+
[_MetaNameValueStr_]: attributes.html#meta-item-attribute-syntax

‎src/attributes/testing.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Testing attributes
2+
3+
The following [attributes] are used for specifying functions for performing
4+
tests. Compiling a crate in "test" mode enables building the test functions
5+
along with a test harness for executing the tests. Enabling the test mode also
6+
enables the [`test` conditional compilation option].
7+
8+
## The `test` attribute
9+
10+
The *`test` attribute* marks a function to be executed as a test. These
11+
functions are only compiled when in test mode. Test functions must be free,
12+
monomorphic functions that take no arguments, and the return type must be one
13+
of the following:
14+
15+
* `()`
16+
* `Result<(), E> where E: Error`
17+
<!-- * `!` -->
18+
<!-- * Result<!, E> where E: Error` -->
19+
20+
> Note: The implementation of which return types are allowed is determined by
21+
> the unstable [`Termination`] trait.
22+
23+
<!-- If the previous section needs updating (from "must take no arguments"
24+
onwards, also update it in the crates-and-source-files.md file -->
25+
26+
> Note: The test mode is enabled by passing the `--test` argument to `rustc`
27+
> or using `cargo test`.
28+
29+
Tests that return `()` pass as long as they terminate and do not panic. Tests
30+
that return a `Result<(), E>` pass as long as they return `Ok(())`. Tests that
31+
do not terminate neither pass nor fail.
32+
33+
```rust
34+
# use std::io;
35+
# fn setup_the_thing() -> io::Result<i32> { Ok(1) }
36+
# fn do_the_thing(s: &i32) -> io::Result<()> { Ok(()) }
37+
#[test]
38+
fn test_the_thing() -> io::Result<()> {
39+
let state = setup_the_thing()?; // expected to succeed
40+
do_the_thing(&state)?; // expected to succeed
41+
Ok(())
42+
}
43+
```
44+
45+
## The `ignore` attribute
46+
47+
A function annotated with the `test` attribute can also be annotated with the
48+
`ignore` attribute. The *`ignore` attribute* tells the test harness to not
49+
execute that function as a test. It will still be compiled when in test mode.
50+
51+
The `ignore` attribute may optionally be written with the [_MetaNameValueStr_]
52+
syntax to specify a reason why the test is ignored.
53+
54+
```rust
55+
#[test]
56+
#[ignore = "not yet implemented"]
57+
fn mytest() {
58+
//
59+
}
60+
```
61+
62+
> **Note**: The `rustc` test harness supports the `--include-ignored` flag to
63+
> force ignored tests to be run.
64+
65+
## The `should_panic` attribute
66+
67+
A function annotated with the `test` attribute that returns `()` can also be
68+
annotated with the `should_panic` attribute. The *`should_panic` attribute*
69+
makes the test only pass if it actually panics.
70+
71+
The `should_panic` attribute may optionally take an input string that must
72+
appear within the panic message. If the string is not found in the message,
73+
then the test will fail. The string may be passed using the
74+
[_MetaNameValueStr_] syntax or the [_MetaListNameValueStr_] syntax with an
75+
`expected` field.
76+
77+
```rust
78+
#[test]
79+
#[should_panic(expected = "values don't match")]
80+
fn mytest() {
81+
assert_eq!(1, 2, "values don't match");
82+
}
83+
```
84+
85+
[_MetaListNameValueStr_]: attributes.html#meta-item-attribute-syntax
86+
[_MetaNameValueStr_]: attributes.html#meta-item-attribute-syntax
87+
[`Termination`]: ../std/process/trait.Termination.html
88+
[`test` conditional compilation option]: conditional-compilation.html#test
89+
[attributes]: attributes.html
90+
[trait or lifetime bounds]: trait-bounds.html
91+
[where clauses]: items/generics.html#where-clauses

‎src/comments.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Non-doc comments are interpreted as a form of whitespace.
4343

4444
Line doc comments beginning with exactly _three_ slashes (`///`), and block
4545
doc comments (`/** ... */`), both inner doc comments, are interpreted as a
46-
special syntax for `doc` [attributes]. That is, they are equivalent to writing
46+
special syntax for [`doc` attributes]. That is, they are equivalent to writing
4747
`#[doc="..."]` around the body of the comment, i.e., `/// Foo` turns into
4848
`#[doc="Foo"]` and `/** Bar */` turns into `#[doc="Bar"]`.
4949

@@ -122,4 +122,4 @@ pub mod outer_module {
122122
}
123123
```
124124

125-
[attributes]: attributes.html
125+
[`doc` attributes]: ../rustdoc/the-doc-attribute.html

‎src/conditional-compilation.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ Example values:
167167
### `test`
168168

169169
Enabled when compiling the test harness. Done with `rustc` by using the
170-
[`--test`] flag.
170+
[`--test`] flag. See [Testing] for more on testing support.
171171

172172
### `debug_assertions`
173173

@@ -297,6 +297,7 @@ println!("I'm running on a {} machine!", machine_kind);
297297
[IDENTIFIER]: identifiers.html
298298
[RAW_STRING_LITERAL]: tokens.html#raw-string-literals
299299
[STRING_LITERAL]: tokens.html#string-literals
300+
[Testing]: attributes/testing.html
300301
[_Attr_]: attributes.html
301302
[`--cfg`]: ../rustc/command-line-arguments.html#a--cfg-configure-the-compilation-environment
302303
[`--test`]: ../rustc/command-line-arguments.html#a--test-build-a-test-harness
@@ -307,5 +308,3 @@ println!("I'm running on a {} machine!", machine_kind);
307308
[attribute]: attributes.html
308309
[attributes]: attributes.html
309310
[crate type]: linkage.html
310-
[expressions]: expressions.html
311-
[items]: items.html

‎src/crates-and-source-files.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,25 @@ type must be one of the following:
123123
> the unstable [`Termination`] trait.
124124
125125
<!-- If the previous section needs updating (from "must take no arguments"
126-
onwards, also update it in the attributes.md file, testing section -->
126+
onwards, also update it in the testing.md file -->
127+
128+
### The `no_main` attribute
129+
130+
The *`no_main` [attribute]* may be applied at the crate level to disable
131+
emitting the `main` symbol for an executable binary. This is useful when some
132+
other object being linked to defines `main`.
133+
134+
## The `crate_name` attribute
135+
136+
The *`crate_name` [attribute]* may be applied at the crate level to specify the
137+
name of the crate with the [_MetaNameValueStr_] syntax.
138+
139+
```rust,ignore
140+
#![crate_name = "mycrate"]
141+
```
142+
143+
The crate name must not be empty, and must only contain [Unicode alphanumeric]
144+
or `-` (U+002D) characters.
127145

128146
[^phase-distinction]: This distinction would also exist in an interpreter.
129147
Static checks like syntactic analysis, type checking, and lints should
@@ -133,8 +151,10 @@ type must be one of the following:
133151
ECMA-335 CLI model, a *library* in the SML/NJ Compilation Manager, a *unit*
134152
in the Owens and Flatt module system, or a *configuration* in Mesa.
135153

154+
[Unicode alphanumeric]: ../std/primitive.char.html#method.is_alphanumeric
136155
[_InnerAttribute_]: attributes.html
137156
[_Item_]: items.html
157+
[_MetaNameValueStr_]: attributes.html#meta-item-attribute-syntax
138158
[_shebang_]: https://en.wikipedia.org/wiki/Shebang_(Unix)
139159
[_utf8 byte order mark_]: https://en.wikipedia.org/wiki/Byte_order_mark#UTF-8
140160
[`Termination`]: ../std/process/trait.Termination.html

‎src/expressions/block-expr.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ fn is_unix_platform() -> bool {
148148
[statement]: statements.html
149149
[statements]: statements.html
150150
[struct]: expressions/struct-expr.html
151-
[the lint check attributes]: attributes.html#lint-check-attributes
151+
[the lint check attributes]: attributes/diagnostics.html#lint-check-attributes
152152
[tuple expressions]: expressions/tuple-expr.html
153153
[unsafe operations]: unsafety.html
154154
[value expressions]: expressions.html#place-expressions-and-value-expressions

‎src/expressions/match-expr.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ let message = match maybe_digit {
128128
## Attributes on match arms
129129
130130
Outer attributes are allowed on match arms. The only attributes that have
131-
meaning on match arms are [`cfg`], `cold`, and the [lint check attributes].
131+
meaning on match arms are [`cfg`], [`cold`], and the [lint check attributes].
132132
133133
[Inner attributes] are allowed directly after the opening brace of the match
134134
expression in the same expression contexts as [attributes on block
@@ -141,7 +141,8 @@ expressions].
141141
[_InnerAttribute_]: attributes.html
142142
[_OuterAttribute_]: attributes.html
143143
[`cfg`]: conditional-compilation.html
144-
[lint check attributes]: attributes.html#lint-check-attributes
144+
[`cold`]: attributes/codegen.html#the-cold-attribute
145+
[lint check attributes]: attributes/diagnostics.html#lint-check-attributes
145146
[Range Expression]: expressions/range-expr.html
146147
147148
[_Pattern_]: patterns.html

‎src/items-and-attributes.md

Lines changed: 0 additions & 7 deletions
This file was deleted.

‎src/items/extern-crates.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,18 @@ by using an underscore with the form `extern crate foo as _`. This may be
9393
useful for crates that only need to be linked, but are never referenced, and
9494
will avoid being reported as unused.
9595
96-
The [`#[macro_use]` attribute] will work as usual and import the macro names
96+
The [`macro_use` attribute] works as usual and import the macro names
9797
into the macro-use prelude.
9898
99+
## The `no_link` attribute
100+
101+
The *`no_link` attribute* may be specified on an `extern crate` item to
102+
prevent linking the crate into the output. This is commonly used to load a
103+
crate to access only its macros.
104+
99105
[IDENTIFIER]: identifiers.html
100106
[RFC 940]: https://github.com/rust-lang/rfcs/blob/master/text/0940-hyphens-considered-harmful.md
101-
[`#[macro_use]` attribute]: macros-by-example.html#the-macro_use-attribute
107+
[`macro_use` attribute]: macros-by-example.html#the-macro_use-attribute
102108
[`alloc`]: https://doc.rust-lang.org/alloc/
103109
[`crate::`]: paths.html#crate
104110
[`no_implicit_prelude`]: items/modules.html#prelude-items

‎src/items/external-blocks.md

Lines changed: 59 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,16 @@ Functions within external blocks may be called by Rust code, just like
4242
functions defined in Rust. The Rust compiler automatically translates between
4343
the Rust ABI and the foreign ABI.
4444

45-
Functions within external blocks may be variadic by specifying `...` after one
46-
or more named arguments in the argument list:
45+
A function declared in an extern block is implicitly `unsafe`. When coerced to
46+
a function pointer, a function declared in an extern block has type `unsafe
47+
extern "abi" for<'l1, ..., 'lm> fn(A1, ..., An) -> R`, where `'l1`, ... `'lm`
48+
are its lifetime parameters, `A1`, ..., `An` are the declared types of its
49+
parameters and `R` is the declared return type.
4750

48-
```rust,ignore
49-
extern {
50-
fn foo(x: i32, ...);
51-
}
52-
```
51+
It is `unsafe` to access a static item declared in an extern block, whether or
52+
not it's mutable.
5353

54-
A number of [attributes] control the behavior of external blocks.
54+
## ABI
5555

5656
By default external blocks assume that the library they are calling uses the
5757
standard C ABI on the specific platform. Other ABIs may be specified using an
@@ -92,38 +92,76 @@ Finally, there are some rustc-specific ABI strings:
9292
* `extern "platform-intrinsic"` -- Specific platform intrinsics -- like, for
9393
example, `sqrt` -- have this ABI. You should never have to deal with it.
9494

95-
The `link` attribute allows the name of the library to be specified. When
96-
specified the compiler will attempt to link against the native library of the
97-
specified name.
95+
## Variadic functions
96+
97+
Functions within external blocks may be variadic by specifying `...` after one
98+
or more named arguments in the argument list:
9899

99100
```rust,ignore
100-
#[link(name = "crypto")]
101-
extern { }
101+
extern {
102+
fn foo(x: i32, ...);
103+
}
102104
```
103105

104-
A function declared in an extern block is implicitly `unsafe`. When coerced to
105-
a function pointer, a function declared in an extern block has type `unsafe
106-
extern "abi" for<'l1, ..., 'lm> fn(A1, ..., An) -> R`, where `'l1`, ... `'lm`
107-
are its lifetime parameters, `A1`, ..., `An` are the declared types of its
108-
parameters and `R` is the declared return type.
106+
## Attributes on extern blocks
109107

110-
It is `unsafe` to access a static item declared in an extern block, whether or
111-
not it's mutable.
108+
The following [attributes] control the behavior of external blocks.
109+
110+
### The `link` attribute
111+
112+
The *`link` attribute* specifies the name of a native library that the
113+
compiler should link with. It uses the [_MetaListNameValueStr_] syntax to
114+
specify its inputs. The `name` key is the name of the native library to link.
115+
The `kind` key is an optional value which specifies the kind of library with
116+
the following possible values:
117+
118+
- `dylib` — Indicates a dynamic library. This is the default if `kind` is not
119+
specified.
120+
- `static` — Indicates a static library.
121+
- `framework` — Indicates a macOS framework. This is only valid for macOS
122+
targets.
123+
124+
```rust,ignore
125+
#[link(name = "crypto")]
126+
extern {
127+
// …
128+
}
129+
130+
#[link(name = "CoreFoundation", kind = "framework")]
131+
extern {
132+
// …
133+
}
134+
```
112135

113136
It is valid to add the `link` attribute on an empty extern block. You can use
114137
this to satisfy the linking requirements of extern blocks elsewhere in your
115138
code (including upstream crates) instead of adding the attribute to each extern
116139
block.
117140

141+
### The `link_name` attribute
142+
143+
The `link_name` attribute may be specified on declarations inside an `extern`
144+
block to indicate the symbol to import for the given function or static. It
145+
uses the [_MetaNameValueStr_] syntax to specify the name of the symbol.
146+
147+
```rust,ignore
148+
extern {
149+
#[link_name = "actual_symbol_name"]
150+
fn name_in_rust();
151+
}
152+
```
153+
118154
[IDENTIFIER]: identifiers.html
119155
[_Abi_]: items/functions.html
120156
[_FunctionParam_]: items/functions.html
121157
[_FunctionParameters_]: items/functions.html
122158
[_FunctionReturnType_]: items/functions.html
123159
[_Generics_]: items/generics.html
124160
[_InnerAttribute_]: attributes.html
161+
[_MetaListNameValueStr_]: attributes.html#meta-item-attribute-syntax
162+
[_MetaNameValueStr_]: attributes.html#meta-item-attribute-syntax
125163
[_OuterAttribute_]: attributes.html
126164
[_Type_]: types.html#type-expressions
127165
[_Visibility_]: visibility-and-privacy.html
128166
[_WhereClause_]: items/generics.html#where-clauses
129-
[attributes]: attributes.html#ffi-attributes
167+
[attributes]: attributes.html

‎src/items/functions.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ fn test_only() {
207207
> function items.
208208
209209
The attributes that have meaning on a function are [`cfg`], [`deprecated`],
210-
[`doc`], `export_name`, `link_section`, `no_mangle`, [the lint check
210+
[`doc`], [`export_name`], [`link_section`], [`no_mangle`], [the lint check
211211
attributes], [`must_use`], [the procedural macro attributes], [the testing
212212
attributes], and [the optimization hint attributes]. Functions also accept
213213
attributes macros.
@@ -232,13 +232,16 @@ attributes macros.
232232
[Trait]: items/traits.html
233233
[attributes]: attributes.html
234234
[`cfg`]: conditional-compilation.html
235-
[the lint check attributes]: attributes.html#lint-check-attributes
235+
[the lint check attributes]: attributes/diagnostics.html#lint-check-attributes
236236
[the procedural macro attributes]: procedural-macros.html
237-
[the testing attributes]: attributes.html#testing
238-
[the optimization hint attributes]: attributes.html#optimization-hints
239-
[`deprecated`]: attributes.html#deprecation
240-
[`doc`]: attributes.html#documentation
241-
[`must_use`]: attributes.html#must_use
237+
[the testing attributes]: attributes/testing.html
238+
[the optimization hint attributes]: attributes/codegen.html#optimization-hints
239+
[`deprecated`]: attributes/diagnostics.html#the-deprecated-attribute
240+
[`doc`]: ../rustdoc/the-doc-attribute.html
241+
[`must_use`]: attributes/diagnostics.html#the-must_use-attribute
242242
[patterns]: patterns.html
243243
[`?Sized`]: trait-bounds.html#sized
244244
[trait bounds]: trait-bounds.html
245+
[`export_name`]: abi.html#the-export_name-attribute
246+
[`link_section`]: abi.html#the-link_section-attribute
247+
[`no_mangle`]: abi.html#the-no_mangle-attribute

‎src/items/implementations.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,8 @@ attributes].
220220
[associated constants]: items/associated-items.html#associated-constants
221221
[attributes]: attributes.html
222222
[`cfg`]: conditional-compilation.html
223-
[`deprecated`]: attributes.html#deprecation
224-
[`doc`]: attributes.html#documentation
223+
[`deprecated`]: attributes/diagnostics.html#the-deprecated-attribute
224+
[`doc`]: ../rustdoc/the-doc-attribute.html
225225
[path]: paths.html
226-
[the lint check attributes]: attributes.html#lint-check-attributes
226+
[the lint check attributes]: attributes/diagnostics.html#lint-check-attributes
227227
[Unsafe traits]: items/traits.html#unsafe-traits

‎src/items/modules.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ alternately be expressed with `crate::util`'s contents in a file named
6565
> convention as it is more consistent, and avoids having many files named
6666
> `mod.rs` within a project.
6767
68-
### `path` Attribute
68+
### The `path` attribute
6969

7070
The directories and files used for loading external file modules can be
7171
influenced with the `path` attribute.
@@ -144,11 +144,11 @@ The built-in attributes that have meaning on a function are [`cfg`],
144144
[_OuterAttribute_]: attributes.html
145145
[`#[macro_use]`]: macros-by-example.html#the-macro_use-attribute
146146
[`cfg`]: conditional-compilation.html
147-
[`deprecated`]: attributes.html#deprecation
148-
[`doc`]: attributes.html#documentation
147+
[`deprecated`]: attributes/diagnostics.html#the-deprecated-attribute
148+
[`doc`]: ../rustdoc/the-doc-attribute.html
149149
[IDENTIFIER]: identifiers.html
150150
[attribute]: attributes.html
151151
[items]: items.html
152152
[module path]: paths.html
153153
[prelude]: crates-and-source-files.html#preludes-and-no_std
154-
[the lint check attributes]: attributes.html#lint-check-attributes
154+
[the lint check attributes]: attributes/diagnostics.html#lint-check-attributes

‎src/macros-by-example.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,8 @@ imported this way are imported into the prelude of the crate, not textually,
307307
which means that they can be shadowed by any other name. While macros imported
308308
by `#[macro_use]` can be used before the import statement, in case of a
309309
conflict, the last macro imported wins. Optionally, a list of macros to import
310-
can be specified; this is not supported when `#[macro_use]` is applied to a
311-
module.
310+
can be specified using the [_MetaListIdents_] syntax; this is not supported
311+
when `#[macro_use]` is applied to a module.
312312

313313
```rust,ignore
314314
#[macro_use(lazy_static)] // Or #[macro_use] to import all macros.
@@ -487,6 +487,7 @@ For more detail, see the [formal specification].
487487
[_Item_]: items.html
488488
[_LiteralExpression_]: expressions/literal-expr.html
489489
[_MetaItem_]: attributes.html#meta-item-attribute-syntax
490+
[_MetaListIdents_]: attributes.html#meta-item-attribute-syntax
490491
[_Pattern_]: patterns.html
491492
[_Statement_]: statements.html
492493
[_TokenTree_]: macros.html#macro-invocation

‎src/procedural-macros.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ as `make_answer!{}`, `make_answer!();` or `make_answer![];`.
113113

114114
### Derive macros
115115

116-
*Derive macros* define new inputs for the `derive` [attribute]. These macros
116+
*Derive macros* define new inputs for the [`derive` attribute]. These macros
117117
can create new [items] given the token stream of a [struct], [enum], or [union].
118118
They can also define [derive macro helper attributes].
119119

@@ -269,7 +269,7 @@ fn invoke4() {}
269269
[`TokenStream`]: ../proc_macro/struct.TokenStream.html
270270
[`TokenStream`s]: ../proc_macro/struct.TokenStream.html
271271
[`compile_error`]: ../std/macro.compile_error.html
272-
[`derive`]: attributes.html#derive
272+
[`derive` attribute]: attributes/derive.html
273273
[`proc_macro` crate]: ../proc_macro/index.html
274274
[Cargo's build scripts]: ../cargo/reference/build-scripts.html
275275
[Derive macros]: #derive-macros

‎src/runtime.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,29 @@ defaults to unwinding the stack but that can be [changed to abort the
5151
process][abort]. The standard library's panic behavior can be modified at
5252
runtime with the [set_hook] function.
5353

54+
## The `global_allocator` attribute
55+
56+
The *`global_allocator` attribute* is used on a [static item] implementing the
57+
[`GlobalAlloc`] trait to set the global allocator.
58+
59+
## The `windows_subsystem` attribute
60+
61+
The *`windows_subsystem` attribute* may be applied at the crate level to set
62+
the [subsystem] when linking on a Windows target. It uses the
63+
[_MetaNameValueStr_] syntax to specify the subsystem with a value of either
64+
`console` or `windows`. This attribute is ignored on non-Windows targets, and
65+
for non-`bin` [crate types].
66+
67+
```rust,ignore
68+
#![windows_subsystem = "windows"]
69+
```
70+
71+
[_MetaNameValueStr_]: attributes.html#meta-item-attribute-syntax
72+
[`GlobalAlloc`]: ../alloc/alloc/trait.GlobalAlloc.html
5473
[`PanicInfo`]: ../core/panic/struct.PanicInfo.html
5574
[abort]: ../book/ch09-01-unrecoverable-errors-with-panic.html
5675
[attribute]: attributes.html
76+
[crate types]: linkage.html
5777
[set_hook]: ../std/panic/fn.set_hook.html
78+
[static item]: items/static-items.html
79+
[subsystem]: https://msdn.microsoft.com/en-us/library/fcc1zstk.aspx

‎src/statements.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ statement are [`cfg`], and [the lint check attributes].
125125
[variables]: variables.html
126126
[outer attributes]: attributes.html
127127
[`cfg`]: conditional-compilation.html
128-
[the lint check attributes]: attributes.html#lint-check-attributes
128+
[the lint check attributes]: attributes/diagnostics.html#lint-check-attributes
129129
[pattern]: patterns.html
130130
[_ExpressionStatement_]: #expression-statements
131131
[_Expression_]: expressions.html

‎src/type-layout.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ Closures have no layout guarantees.
112112
All user-defined composite types (`struct`s, `enum`s, and `union`s) have a
113113
*representation* that specifies what the layout is for the type.
114114

115-
The possible representations for a type are the default representation, `C`, the
116-
primitive representations, and `packed`. Multiple representations can be applied
117-
to a single type.
115+
The possible representations for a type are the default representation, `C`,
116+
the primitive representations, `packed`, and `transparent`. Multiple
117+
representations can be applied to a single type.
118118

119119
The representation of a type can be changed by applying the `repr` attribute
120120
to it. The following example shows a struct with a `C` representation.
@@ -258,8 +258,8 @@ layout is unspecified.
258258
### Primitive representations
259259

260260
The *primitive representations* are the representations with the same names as
261-
the primitive integer types. That is: `u8`, `u16`, `u32`, `u64`, `usize`, `i8`,
262-
`i16`, `i32`, `i64`, and `isize`.
261+
the primitive integer types. That is: `u8`, `u16`, `u32`, `u64`, `u128`,
262+
`usize`, `i8`, `i16`, `i32`, `i64`, `i128`, and `isize`.
263263

264264
Primitive representations can only be applied to enumerations.
265265

‎src/types/closure.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,4 @@ Because captures are often by reference, the following general rules arise:
174174
[`Sized`]: special-types-and-traits.html#sized
175175
[`Sync`]: special-types-and-traits.html#sync
176176
[closure expression]: expressions/closure-expr.html
177-
[derived]: attributes.html#derive
177+
[derived]: attributes/derive.html

‎src/types/struct.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ New instances of a `struct` can be constructed with a [struct expression].
77

88
The memory layout of a `struct` is undefined by default to allow for compiler
99
optimizations like field reordering, but it can be fixed with the
10-
`#[repr(...)]` attribute. In either case, fields may be given in any order in a
10+
[`repr` attribute]. In either case, fields may be given in any order in a
1111
corresponding struct *expression*; the resulting `struct` value will always
1212
have the same memory layout.
1313

@@ -24,5 +24,6 @@ value that inhabits such a type.
2424
[^structtype]: `struct` types are analogous to `struct` types in C, the
2525
*record* types of the ML family, or the *struct* types of the Lisp family.
2626

27+
[`repr` attribute]: type-layout.html#representations
2728
[struct expression]: expressions/struct-expr.html
2829
[visibility modifiers]: visibility-and-privacy.html

‎src/undocumented.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ to shrink!
2121
[Flexible target specification]: https://github.com/rust-lang/rfcs/pull/131
2222
[Conditional compilation]: conditional-compilation.html
2323
[`dllimport`]: https://github.com/rust-lang/rfcs/pull/1717
24-
[FFI attributes]: attributes.html#ffi-attributes
24+
[FFI attributes]: attributes.html
2525
[define `crt_link`]: https://github.com/rust-lang/rfcs/pull/1721

0 commit comments

Comments
 (0)
Please sign in to comment.