Skip to content

Commit fc68e62

Browse files
committed
Update for review comments and minor additions.
- Address review comments. - Minor typo and formatting fixes. - Link `macro_use` and `macro_export` to their new home.
1 parent 884c429 commit fc68e62

File tree

5 files changed

+89
-44
lines changed

5 files changed

+89
-44
lines changed

src/attributes.md

+5-8
Original file line numberDiff line numberDiff line change
@@ -175,15 +175,10 @@ which can be used to control type layout.
175175

176176
## Macro-related attributes
177177

178-
- `macro_use` on a `mod` — macros defined in this module will be visible in the
179-
module's parent, after this module has been included.
178+
- [`macro_use`] — Expands macro visibility, or imports macros from other
179+
crates.
180180

181-
- `macro_use` on an `extern crate` — load macros from this crate. An optional
182-
list of names `#[macro_use(foo, bar)]` restricts the import to just those
183-
macros named. The `extern crate` must appear at the crate root, not inside
184-
`mod`, which ensures proper function of the `$crate` macro variable.
185-
186-
- `macro_export` - export a `macro_rules` macro for cross-crate usage.
181+
- [`macro_export`] — Exports a `macro_rules` macro for cross-crate usage.
187182

188183
- `no_link` on an `extern crate` — even if we load this crate for macros, don't
189184
link it into the output.
@@ -634,3 +629,5 @@ pub fn f() {}
634629
[`meta` macro fragment specifier]: macros-by-example.html
635630
[`used`]: abi.html#the-used-attribute
636631
[`panic_handler`]: runtime.html#the-panic_handler-attribute
632+
[`macro_use`]: macros-by-example.html#the-macro_use-attribute
633+
[`macro_export`]: macros-by-example.html#path-based-scope

src/items/extern-crates.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ into the macro-use prelude.
9898
9999
[IDENTIFIER]: identifiers.html
100100
[RFC 940]: https://github.com/rust-lang/rfcs/blob/master/text/0940-hyphens-considered-harmful.md
101-
[`#[macro_use]` attribute]: attributes.html#macro-related-attributes
101+
[`#[macro_use]` attribute]: macros-by-example.html#the-macro_use-attribute
102102
[`alloc`]: https://doc.rust-lang.org/alloc/
103103
[`crate::`]: paths.html#crate
104104
[`no_implicit_prelude`]: items/modules.html#prelude-items

src/items/modules.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ mod thread {
123123
## Prelude Items
124124

125125
Modules implicitly have some names in scope. These name are to built-in types,
126-
macros imported with `#[macro_use]` on an extern crate, and by the crate's
126+
macros imported with [`#[macro_use]`] on an extern crate, and by the crate's
127127
[prelude]. These names are all made of a single identifier. These names are not
128128
part of the module, so for example, any name `name`, `self::name` is not a
129129
valid path. The names added by the [prelude] can be removed by placing the
@@ -142,6 +142,7 @@ The built-in attributes that have meaning on a function are [`cfg`],
142142
[_InnerAttribute_]: attributes.html
143143
[_Item_]: items.html
144144
[_OuterAttribute_]: attributes.html
145+
[`#[macro_use]`]: macros-by-example.html#the-macro_use-attribute
145146
[`cfg`]: conditional-compilation.html
146147
[`deprecated`]: attributes.html#deprecation
147148
[`doc`]: attributes.html#documentation

src/macro-ambiguity.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ legal macro definition will continue to assign the same determination as to
118118
where `... tt` ends and `uu ...` begins, even as new syntactic forms are added
119119
to the language.
120120

121-
The second invariant says that a separated complex NT must use a seperator token
121+
The second invariant says that a separated complex NT must use a separator token
122122
that is part of the predetermined follow set for the internal contents of the
123123
NT. This ensures that a legal macro definition will continue to parse an input
124124
fragment into the same delimited sequence of `tt ...`'s, even as new syntactic
@@ -273,7 +273,7 @@ LAST(M), defined by case analysis on M itself (a sequence of token-trees):
273273

274274
Below are some examples of FIRST and LAST.
275275
(Note in particular how the special ε element is introduced and
276-
eliminated based on the interation between the pieces of the input.)
276+
eliminated based on the interaction between the pieces of the input.)
277277

278278
Our first example is presented in a tree structure to elaborate on how
279279
the analysis of the matcher composes. (Some of the simpler subtrees
@@ -365,7 +365,7 @@ why particular matchers are legal and others are not.
365365

366366
* `( $($a:tt $b:tt)* ; )` : legal, because FIRST(`$b:tt`) = { `$b:tt` } is ⊆ FOLLOW(`tt`) = ANYTOKEN, as is FIRST(`;`) = { `;` }.
367367

368-
* `( $($t:tt),* , $(t:tt),* )` : legal, (though any attempt to actually use this macro will signal a local ambguity error during expansion).
368+
* `( $($t:tt),* , $(t:tt),* )` : legal, (though any attempt to actually use this macro will signal a local ambiguity error during expansion).
369369

370370
* `($ty:ty $(; not sep)* -)` : illegal, because FIRST(`$(; not sep)* -`) = { `;`, `-` } is not in FOLLOW(`ty`).
371371

src/macros-by-example.md

+78-31
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ the matcher and the transcriber must be surrounded by delimiters. Macros can
4949
expand to expressions, statements, items (including traits, impls, and foreign
5050
items), types, or patterns.
5151

52+
## Transcription
53+
5254
When a macro is invoked, the macro expander looks up macro invocations by name,
5355
and tries each macro rule in turn. It transcribes the first successful match; if
5456
this results in an error, then future matches are not tried. When matching, no
@@ -60,19 +62,56 @@ invocation unambiguously:
6062

6163
```rust,compile_fail
6264
macro_rules! ambiguity {
63-
($($i:ident)* $j:ident) => { ($($i)-*) * $j };
65+
($($i:ident)* $j:ident) => { };
6466
}
6567
6668
ambiguity!(error); // Error: local ambiguity
6769
```
6870

6971
In both the matcher and the transcriber, the `$` token is used to invoke special
70-
behaviours from the macro engine. Tokens that aren't part of such an invocation
71-
are matched and transcribed literally, with one exception. The exception is that
72-
the outer delimiters for the matcher will match any pair of delimiters. Thus,
73-
for instance, the matcher `(())` will match `{()}` but not `{{}}`. The character
72+
behaviours from the macro engine (described below in [Metavariables] and
73+
[Repetitions]). Tokens that aren't part of such an invocation are matched and
74+
transcribed literally, with one exception. The exception is that the outer
75+
delimiters for the matcher will match any pair of delimiters. Thus, for
76+
instance, the matcher `(())` will match `{()}` but not `{{}}`. The character
7477
`$` cannot be matched or transcribed literally.
7578

79+
When forwarding a matched fragment to another macro-by-example, matchers in
80+
the second macro will see an opaque AST of the fragment type. The second macro
81+
can't use literal tokens to match the fragments in the matcher, only a
82+
fragment specifier of the same type. The `ident`, `lifetime`, and `tt`
83+
fragment types are an exception, and can be matched by literal tokens. The
84+
following illustrates this restriction:
85+
86+
```rust,compile_fail
87+
macro_rules! foo {
88+
($l:expr) => { bar!($l); }
89+
// ERROR: ^^ no rules expected this token in macro call
90+
}
91+
92+
macro_rules! bar {
93+
(3) => {}
94+
}
95+
96+
foo!(3);
97+
```
98+
99+
The following illustrates how tokens can be directly matched after matching a
100+
`tt` fragment:
101+
102+
```rust
103+
// compiles OK
104+
macro_rules! foo {
105+
($l:tt) => { bar!($l); }
106+
}
107+
108+
macro_rules! bar {
109+
(3) => {}
110+
}
111+
112+
foo!(3);
113+
```
114+
76115
## Metavariables
77116

78117
In the matcher, `$` _name_ `:` _fragment-specifier_ matches a Rust syntax
@@ -94,40 +133,44 @@ fragment specifiers are:
94133
* `vis`: a possibly empty [_Visibility_] qualifier
95134
* `literal`: matches `-`<sup>?</sup>[_LiteralExpression_]
96135

97-
In the transcriber, metavariables are referred to simply by $`_name_`, since
136+
In the transcriber, metavariables are referred to simply by `$`_name_, since
98137
the fragment kind is specified in the matcher. Metavariables are replaced with
99138
the syntax element that matched them. The keyword metavariable `$crate` can be
100139
used to refer to the current crate; see [Hygiene] below. Metavariables can be
101140
transcribed more than once or not at all.
102141

103-
## Repititions
142+
## Repetitions
104143

105144
In both the matcher and transcriber, repetitions are indicated by placing the
106-
tokens to be repeated inside `$( ... )`, followed by a repetition operator,
145+
tokens to be repeated inside `$(``)`, followed by a repetition operator,
107146
optionally with a separator token between. The separator token can be any token
108147
other than a delimiter or one of the repetition operators, but `;` and `,` are
109148
the most common. For instance, `$( $i:ident ),*` represents any number of
110-
identifiers separated by commas. Nested repititions are permitted.
149+
identifiers separated by commas. Nested repetitions are permitted.
150+
151+
The repetition operators are:
111152

112-
The repetition operators are `*`, which indicates any number of repetitions,
113-
`+`, which indicates any number but at least one, and `?` which indicates an
114-
optional fragment with zero or one occurrences. Since `?` represents at most one
115-
occurrence, it cannot be used with a separator.
153+
- `*` — indicates any number of repetitions.
154+
- `+` — indicates any number but at least one.
155+
- `?` — indicates an optional fragment with zero or one occurrences.
156+
157+
Since `?` represents at most one occurrence, it cannot be used with a
158+
separator.
116159

117160
The repeated fragment both matches and transcribes to the specified number of
118161
the fragment, separated by the separator token. Metavariables are matched to
119162
every repetition of their corresponding fragment. For instance, the `$( $i:ident
120163
),*` example above matches `$i` to all of the identifiers in the list.
121164

122-
During transcription, additional restrictions apply to repititions so that the
165+
During transcription, additional restrictions apply to repetitions so that the
123166
compiler knows how to expand them properly:
124167

125168
1. A metavariable must appear in exactly the same number, kind, and nesting
126169
order of repetitions in the transcriber as it did in the matcher. So for the
127-
matcher `$( $i:ident ),*`, the transcribers `=> $i`, `=> $( $( $i)* )*`, and
128-
`=> $( $i )+` are all illegal, but `=> { $( $i );* }` is correct and
129-
replaces a comma-separated list of identifiers with a semicolon-separated
130-
list.
170+
matcher `$( $i:ident ),*`, the transcribers `=> { $i }`,
171+
`=> { $( $( $i)* )* }`, and `=> { $( $i )+ }` are all illegal, but
172+
`=> { $( $i );* }` is correct and replaces a comma-separated list of
173+
identifiers with a semicolon-separated list.
131174
1. Second, each repetition in the transcriber must contain at least one
132175
metavariable to decide now many times to expand it. If multiple
133176
metavariables appear in the same repetition, they must be bound to the same
@@ -147,12 +190,12 @@ compiler knows how to expand them properly:
147190
For historical reasons, the scoping of macros by example does not work entirely like
148191
items. Macros have two forms of scope: textual scope, and path-based scope.
149192
Textual scope is based on the order that things appear in source files, or even
150-
across multiple files, and is the default scoping. It's explained further below.
193+
across multiple files, and is the default scoping. It is explained further below.
151194
Path-based scope works exactly the same way that item scoping does. The scoping,
152195
exporting, and importing of macros is controlled largely by attributes.
153196

154197
When a macro is invoked by an unqualified identifier (not part of a multi-part
155-
path), it's first looked up in textual scoping. If this does not yield any
198+
path), it is first looked up in textual scoping. If this does not yield any
156199
results, then it is looked up in path-based scoping. If the macro's name is
157200
qualified with a path, then it is only looked up in path-based scoping.
158201

@@ -194,7 +237,7 @@ mod has_macro {
194237
195238
//// src/has_macro/uses_macro.rs
196239
197-
m!{} // OK: appears after delcaration of m in src/lib.rs
240+
m!{} // OK: appears after declaration of m in src/lib.rs
198241
```
199242

200243
It is not an error to define a macro multiple times; the most recent declaration
@@ -241,7 +284,9 @@ fn foo() {
241284
// m!(); // Error: m is not in scope.
242285
```
243286

244-
The `#[macro_use]` attribute has two purposes. First, it can be used to make a
287+
### The `macro_use` attribute
288+
289+
The *`macro_use` attribute* has two purposes. First, it can be used to make a
245290
module's macro scope not end when the module is closed, by applying it to a
246291
module:
247292

@@ -257,7 +302,7 @@ m!();
257302
```
258303

259304
Second, it can be used to import macros from another crate, by attaching it to
260-
an `extern crate` declaration appearing in the crate's root module. Macros
305+
an `extern crate` declaration appearing in the crate's root module. Macros
261306
imported this way are imported into the prelude of the crate, not textually,
262307
which means that they can be shadowed by any other name. While macros imported
263308
by `#[macro_use]` can be used before the import statement, in case of a
@@ -270,7 +315,7 @@ module.
270315
extern crate lazy_static;
271316
272317
lazy_static!{};
273-
// self::lazy_static!{} // Error: lazy_static is not defined inself
318+
// self::lazy_static!{} // Error: lazy_static is not defined in `self`
274319
```
275320

276321
Macros to be imported with `#[macro_use]` must be exported with
@@ -302,7 +347,7 @@ mod mac {
302347
Macros labeled with `#[macro_export]` are always `pub` and can be referred to
303348
by other crates, either by path or by `#[macro_use]` as described above.
304349

305-
## Hygiene
350+
## Hygiene
306351

307352
By default, all identifiers referred to in a macro are expanded as-is, and are
308353
looked up at the macro's invocation site. This can lead to issues if a macro
@@ -418,22 +463,24 @@ When repetitions are involved, then the rules apply to every possible number of
418463
expansions, taking separators into account. This means:
419464

420465
* If the repetition includes a separator, that separator must be able to
421-
follow the contents of the repitition.
422-
* If the repitition can repeat multiple times (`*` or `+`), then the contents
466+
follow the contents of the repetition.
467+
* If the repetition can repeat multiple times (`*` or `+`), then the contents
423468
must be able to follow themselves.
424469
* The contents of the repetition must be able to follow whatever comes
425470
before, and whatever comes after must be able to follow the contents of the
426-
repitition.
427-
* If the repitition can match zero times (`*` or `?`), then whatever comes
471+
repetition.
472+
* If the repetition can match zero times (`*` or `?`), then whatever comes
428473
after must be able to follow whatever comes before.
429474

430475

431476
For more detail, see the [formal specification].
432477

478+
[Hygiene]: #hygiene
433479
[IDENTIFIER]: identifiers.html
434480
[IDENTIFIER_OR_KEYWORD]: identifiers.html
435481
[LIFETIME_TOKEN]: tokens.html#lifetimes-and-loop-labels
436-
[formal specification]: macro-ambiguity.html
482+
[Metavariables]: #metavariables
483+
[Repetitions]: #repetitions
437484
[_BlockExpression_]: expressions/block-expr.html
438485
[_DelimTokenTree_]: macros.html
439486
[_Expression_]: expressions.html
@@ -447,5 +494,5 @@ For more detail, see the [formal specification].
447494
[_TypePath_]: paths.html#paths-in-types
448495
[_Type_]: types.html#type-expressions
449496
[_Visibility_]: visibility-and-privacy.html
497+
[formal specification]: macro-ambiguity.html
450498
[token]: tokens.html
451-
[Hygiene]: #hygiene

0 commit comments

Comments
 (0)