@@ -49,6 +49,8 @@ the matcher and the transcriber must be surrounded by delimiters. Macros can
4949expand to expressions, statements, items (including traits, impls, and foreign
5050items), types, or patterns.
5151
52+ ## Transcription
53+
5254When a macro is invoked, the macro expander looks up macro invocations by name,
5355and tries each macro rule in turn. It transcribes the first successful match; if
5456this results in an error, then future matches are not tried. When matching, no
@@ -60,19 +62,56 @@ invocation unambiguously:
6062
6163``` rust,compile_fail
6264macro_rules! ambiguity {
63- ($($i:ident)* $j:ident) => { ($($i)-*) * $j };
65+ ($($i:ident)* $j:ident) => { };
6466}
6567
6668ambiguity!(error); // Error: local ambiguity
6769```
6870
6971In 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
78117In 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
98137the fragment kind is specified in the matcher. Metavariables are replaced with
99138the syntax element that matched them. The keyword metavariable ` $crate ` can be
100139used to refer to the current crate; see [ Hygiene] below. Metavariables can be
101140transcribed more than once or not at all.
102141
103- ## Repititions
142+ ## Repetitions
104143
105144In 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,
107146optionally with a separator token between. The separator token can be any token
108147other than a delimiter or one of the repetition operators, but ` ; ` and ` , ` are
109148the 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
117160The repeated fragment both matches and transcribes to the specified number of
118161the fragment, separated by the separator token. Metavariables are matched to
119162every 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
123166compiler knows how to expand them properly:
124167
1251681 . 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.
1311741 . 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:
147190For historical reasons, the scoping of macros by example does not work entirely like
148191items. Macros have two forms of scope: textual scope, and path-based scope.
149192Textual 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.
151194Path-based scope works exactly the same way that item scoping does. The scoping,
152195exporting, and importing of macros is controlled largely by attributes.
153196
154197When 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
156199results, then it is looked up in path-based scoping. If the macro's name is
157200qualified 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
200243It 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
245290module's macro scope not end when the module is closed, by applying it to a
246291module:
247292
@@ -257,7 +302,7 @@ m!();
257302```
258303
259304Second, 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
261306imported this way are imported into the prelude of the crate, not textually,
262307which means that they can be shadowed by any other name. While macros imported
263308by ` #[macro_use] ` can be used before the import statement, in case of a
@@ -270,7 +315,7 @@ module.
270315extern crate lazy_static;
271316
272317lazy_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
276321Macros to be imported with ` #[macro_use] ` must be exported with
@@ -302,7 +347,7 @@ mod mac {
302347Macros labeled with ` #[macro_export] ` are always ` pub ` and can be referred to
303348by other crates, either by path or by ` #[macro_use] ` as described above.
304349
305- ## Hygiene
350+ ## Hygiene
306351
307352By default, all identifiers referred to in a macro are expanded as-is, and are
308353looked 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
418463expansions, 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
431476For 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