Skip to content

Commit a3d87fa

Browse files
committed
Fix text in TRPL macros guide to match the example
Also, update description of macro invocation syntax: after rust-lang#20563 there is a number of additional limitations on macro syntax.
1 parent 0aec4db commit a3d87fa

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/doc/trpl/macros.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ the pattern in the above code:
4242
# let input_1 = T::SpecialA(0);
4343
# let input_2 = T::SpecialA(0);
4444
macro_rules! early_return {
45-
($inp:expr, $sp:path) => ( // invoke it like `(input_5 SpecialE)`
45+
($inp:expr, $sp:path) => ( // invoke it like `(input_5, SpecialE)`
4646
match $inp {
4747
$sp(x) => { return x; }
4848
_ => {}
@@ -59,7 +59,7 @@ early_return!(input_2, T::SpecialB);
5959
~~~~
6060

6161
Macros are defined in pattern-matching style: in the above example, the text
62-
`($inp:expr $sp:ident)` that appears on the left-hand side of the `=>` is the
62+
`($inp:expr, $sp:path)` that appears on the left-hand side of the `=>` is the
6363
*macro invocation syntax*, a pattern denoting how to write a call to the
6464
macro. The text on the right-hand side of the `=>`, beginning with `match
6565
$inp`, is the *macro transcription syntax*: what the macro expands to.
@@ -74,6 +74,8 @@ conforms to the following rules:
7474
2. `$` has special meaning (described below).
7575
3. The `()`s, `[]`s, and `{}`s it contains must balance. For example, `([)` is
7676
forbidden.
77+
4. Some arguments can be followed only by a limited set of separators, to
78+
avoid ambiguity (described below).
7779

7880
Otherwise, the invocation syntax is free-form.
7981

@@ -86,7 +88,8 @@ To take a fragment of Rust code as an argument, write `$` followed by a name
8688
`foo`.)
8789
* `expr` (an expression. Examples: `2 + 2`; `if true then { 1 } else { 2 }`;
8890
`f(42)`.)
89-
* `ty` (a type. Examples: `int`, `Vec<(char, String)>`, `&T`.)
91+
* `ty` (a type. Examples: `i32`, `Vec<(char, String)>`, `&T`.)
92+
* `path` (a path to struct or enum variant. Example: `T::SpecialA`)
9093
* `pat` (a pattern, usually appearing in a `match` or on the left-hand side of
9194
a declaration. Examples: `Some(t)`; `(17, 'a')`; `_`.)
9295
* `block` (a sequence of actions. Example: `{ log(error, "hi"); return 12; }`)
@@ -97,6 +100,12 @@ rules of tokenization apply,
97100
So `($x:ident -> (($e:expr)))`, though excessively fancy, would designate a macro
98101
that could be invoked like: `my_macro!(i->(( 2+2 )))`.
99102

103+
To avoid ambiguity, macro invocation syntax must conform to the following rules:
104+
* `expr` must be followed by `=>`, `,` or `;`.
105+
* `ty` and `path` must be followed by `=>`, `,`, `:`, `=`, `>` or `as`.
106+
* `pat` must be followed by `=>`, `,` or `=`.
107+
* `ident` and `block` can be followed by any token.
108+
100109
## Invocation location
101110

102111
A macro invocation may take the place of (and therefore expand to) an

0 commit comments

Comments
 (0)