Skip to content

Commit 17d009a

Browse files
authored
Merge pull request #412 from Havvy/proc-macros-2
Expand Procedural Macro Docs
2 parents 40ebbee + 1975f82 commit 17d009a

File tree

5 files changed

+341
-33
lines changed

5 files changed

+341
-33
lines changed

src/attributes.md

+31-6
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ Attributes may appear as any of:
3535

3636
_Inner attributes_, written with a bang ("!") after the hash ("#"), apply to the
3737
item that the attribute is declared within. _Outer attributes_, written without
38-
the bang after the hash, apply to the item or generic parameter that follow the
39-
attribute.
38+
the bang after the hash, apply to the thing that follows the attribute.
4039

4140
Attributes may be applied to many things in the language:
4241

@@ -82,6 +81,24 @@ fn some_unused_variables() {
8281
}
8382
```
8483

84+
There are three kinds of attributes:
85+
86+
* Built-in attributes
87+
* Macro attributes
88+
* Derive mode helper attributes
89+
90+
## Active and inert attributes
91+
92+
An attribute is either active or inert. During attribute processing, *active
93+
attributes* remove themselves from the thing they are on while *inert attriutes*
94+
stay on.
95+
96+
The `cfg` and `cfg_attr` attributes are active. The `test` attribute is inert
97+
when compiling for tests and active otherwise. Attribute macros are active.
98+
All other attributes are inert.
99+
100+
---
101+
85102
The rest of this page describes or links to descriptions of which attribute
86103
names have meaning.
87104

@@ -161,14 +178,20 @@ which can be used to control type layout.
161178

162179
- `macro_reexport` on an `extern crate` — re-export the named macros.
163180

164-
- `macro_export` - export a macro for cross-crate usage.
181+
- `macro_export` - export a `macro_rules` macro for cross-crate usage.
165182

166183
- `no_link` on an `extern crate` — even if we load this crate for macros, don't
167184
link it into the output.
168185

169186
See the [macros section of the first edition of the
170187
book](../book/first-edition/macros.html#scoping-and-macro-importexport) for more
171-
information on macro scope.
188+
information on `macro_rules` macro scope.
189+
190+
- `proc_macro` - Defines a [bang macro].
191+
192+
- `proc_macro_derive` - Defines a [derive macro].
193+
194+
- `proc_macro_attribute` - Defines an [attribute macro].
172195

173196
## Miscellaneous attributes
174197

@@ -525,9 +548,11 @@ You can implement `derive` for your own traits through [procedural macros].
525548
[match expressions]: expressions/match-expr.html
526549
[external blocks]: items/external-blocks.html
527550
[items]: items.html
551+
[attribute macro]: procedural-macros.html#attribute-macros
552+
[bang macro]: procedural-macros.html#bang-macros
528553
[conditional compilation]: conditional-compilation.html
529-
[trait]: items/traits.html
530-
[main]: crates-and-source-files.html
554+
[derive macro]: procedural-macros.html#derive-macros
555+
[trait]: items/traits.html[main]: crates-and-source-files.html
531556
[`Termination`]: ../std/process/trait.Termination.html
532557
[where clause]: items/where-clauses.html
533558
[trait or lifetime bounds]: trait-bounds.html

src/conditional-compilation.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ The following configurations must be defined by the implementation:
8484
This can be used to enable extra debugging code in development but not in
8585
production. For example, it controls the behavior of the standard library's
8686
`debug_assert!` macro.
87+
* `proc_macro` - Set when the crate being compiled is being compiled with the
88+
`proc_macro` [crate type].
8789

8890
You can also set another [attribute] based on a `cfg` variable with `cfg_attr`:
8991

@@ -96,4 +98,5 @@ This is the same as `#[b]` if `a` is set by `cfg`, and nothing otherwise.
9698
Lastly, configuration options can be used in expressions by invoking the `cfg!`
9799
macro: `cfg!(a)` evaluates to `true` if `a` is set, and `false` otherwise.
98100

99-
[attribute]: attributes.html
101+
[attribute]: attributes.html
102+
[crate type]: linkage.html

src/items/functions.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ fn test_only() {
134134
135135
The attributes that have meaning on a function are [`cfg`], [`deprecated`],
136136
[`doc`], `export_name`, `link_section`, `no_mangle`, [the lint check
137-
attributes], [`must_use`], [the testing attributes], and [the optimization hint
137+
attributes], [`must_use`], [the procedural macro attributes], [the testing
138+
attributes], and [the optimization hint
138139
attributes].
139140

140141
[external blocks]: items/external-blocks.html
@@ -147,6 +148,7 @@ attributes].
147148
[attributes]: attributes.html
148149
[`cfg`]: conditional-compilation.html
149150
[the lint check attributes]: attributes.html#lint-check-attributes
151+
[the procedural macro attributes]: procedural-macros.html
150152
[the testing attributes]: attributes.html#testing
151153
[the optimization hint attributes]: attributes.html#optimization-hints
152154
[`deprecated`]: attributes.html#deprecation

src/linkage.md

+19-12
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
# Linkage
22

3-
The Rust compiler supports various methods to link crates together both
3+
> Note: This section is described more in terms of the compiler than of
4+
> the language.
5+
6+
The compiler supports various methods to link crates together both
47
statically and dynamically. This section will explore the various methods to
5-
link Rust crates together, and more information about native libraries can be
8+
link crates together, and more information about native libraries can be
69
found in the [FFI section of the book][ffi].
710

811
[ffi]: ../book/ffi.html
@@ -35,7 +38,7 @@ be ignored in favor of only building the artifacts specified by command line.
3538

3639
* `--crate-type=staticlib`, `#[crate_type = "staticlib"]` - A static system
3740
library will be produced. This is different from other library outputs in that
38-
the Rust compiler will never attempt to link to `staticlib` outputs. The
41+
the compiler will never attempt to link to `staticlib` outputs. The
3942
purpose of this output type is to create a static library containing all of
4043
the local crate's code along with all upstream dependencies. The static
4144
library is actually a `*.a` archive on linux and osx and a `*.lib` file on
@@ -44,28 +47,29 @@ be ignored in favor of only building the artifacts specified by command line.
4447
dynamic dependencies on other Rust code.
4548

4649
* `--crate-type=cdylib`, `#[crate_type = "cdylib"]` - A dynamic system
47-
library will be produced. This is used when compiling Rust code as
50+
library will be produced. This is used when compiling
4851
a dynamic library to be loaded from another language. This output type will
4952
create `*.so` files on Linux, `*.dylib` files on macOS, and `*.dll` files on
5053
Windows.
5154

5255
* `--crate-type=rlib`, `#[crate_type = "rlib"]` - A "Rust library" file will be
5356
produced. This is used as an intermediate artifact and can be thought of as a
5457
"static Rust library". These `rlib` files, unlike `staticlib` files, are
55-
interpreted by the Rust compiler in future linkage. This essentially means
58+
interpreted by the compiler in future linkage. This essentially means
5659
that `rustc` will look for metadata in `rlib` files like it looks for metadata
5760
in dynamic libraries. This form of output is used to produce statically linked
5861
executables as well as `staticlib` outputs.
5962

6063
* `--crate-type=proc-macro`, `#[crate_type = "proc-macro"]` - The output
6164
produced is not specified, but if a `-L` path is provided to it then the
6265
compiler will recognize the output artifacts as a macro and it can be loaded
63-
for a program. If a crate is compiled with the `proc-macro` crate type it
64-
will forbid exporting any items in the crate other than those functions
65-
tagged `#[proc_macro_derive]` and those functions must also be placed at the
66-
crate root. Finally, the compiler will automatically set the
67-
`cfg(proc_macro)` annotation whenever any crate type of a compilation is the
68-
`proc-macro` crate type.
66+
for a program. Crates compiled with this crate type must only export
67+
[procedural macros]. The compiler will automatically set the `proc_macro`
68+
[configuration option]. The crates are always compiled with the same target
69+
that the compiler itself was built with. For example, if you are executing
70+
the compiler from Linux with an `x86_64` CPU, the target will be
71+
`x86_64-unknown-linux-gnu` even if the crate is a dependency of another crate
72+
being built for a different target.
6973

7074
Note that these outputs are stackable in the sense that if multiple are
7175
specified, then the compiler will produce each form of output at once without
@@ -124,7 +128,7 @@ dependencies will be used:
124128

125129
In general, `--crate-type=bin` or `--crate-type=lib` should be sufficient for
126130
all compilation needs, and the other options are just available if more
127-
fine-grained control is desired over the output format of a Rust crate.
131+
fine-grained control is desired over the output format of a crate.
128132

129133
## Static and dynamic C runtimes
130134

@@ -205,3 +209,6 @@ a statically linked binary on MSVC you would execute:
205209
```ignore,notrust
206210
RUSTFLAGS='-C target-feature=+crt-static' cargo build --target x86_64-pc-windows-msvc
207211
```
212+
213+
[configuration option]: conditional-compilation.html
214+
[procedural macros]: procedural-macros.html

0 commit comments

Comments
 (0)