Sub topic 7
Follow-set rules and ambiguity errors
This chapter assumes you can write a macro_rules! macro with several rules, use fragment specifiers such as expr and ident, and write repetitions with $(...).
Why the compiler restricts what follows a fragment
A macro parser reads your invocation left to right and never backs up. When it reaches a fragment like $x:expr, it hands the remaining tokens to the real Rust expression parser. That parser decides where the expression ends. If the matcher allowed any token after expr, a later Rust version that adds new expression syntax could change where the expression ends and silently break existing macros.
To prevent this, the compiler limits which tokens may come directly after certain fragments. These limits are the follow-set rules. The compiler checks them when it reads the macro definition, not when you call the macro.
The follow-set table
| Fragment | May be followed by | Column |
|---|---|---|
| `expr`, `stmt` | `=>`, `,`, `;` | |
| `pat` | `=>`, `,`, `=`, `if`, `in` | |
| `pat_param` | `=>`, `,`, `=`, `\ | `, `if`, `in` |
| `ty`, `path` | `{`, `[`, `=>`, `,`, `>`, `=`, `:`, `;`, `\ | `, `as`, `where`, or a `block` fragment |
| `ident`, `tt`, `literal`, `lifetime`, `block`, `item`, `meta` | any token |
The vis fragment has its own, more permissive rules. The table lists only the most common cases.
Reading the error
Suppose you write the matcher ($a:expr + $b:expr). The compiler rejects the definition with a message like this: $a:expr is followed by +, which is not allowed for expr fragments. It then lists the allowed tokens: =>, , or ;.
A repetition without a separator causes the same error. In $($x:expr)*, the next token after one expr could be the start of another expr, so the compiler rejects it.
Three ways to fix it
- Put an allowed token after the fragment, usually a comma.
- Use a fragment with no restriction, such as
literal,ident, ortt. - Use
ttand parse the pieces yourself in a later rule.
macro_rules! sum {
($a:expr, $b:expr) => {
$a + $b
};
}
macro_rules! add {
($a:literal + $b:literal) => {
$a + $b
};
}
fn main() {
println!("{} {}", sum!(1 + 2, 3), add!(4 + 5));
}This program prints 6 9. sum! uses a comma after the first expr. add! can use + because literal has no follow-set restriction. The cost is that add! accepts only literals, so add!(x + 1) fails.
Ambiguity errors
Ambiguity errors are different. They appear when you invoke the macro, not when you define it. They happen when the parser cannot tell which part of the matcher the next token belongs to.
Consider the matcher ($($a:ident)* $b:ident). The definition is accepted. When you call it with x y z, the parser reaches z and cannot tell whether it belongs to the repetition or to $b. It cannot look ahead, so it reports "local ambiguity when calling macro" and lists the parsing options.
To fix this, make the boundary visible to the parser:
- Add a separator or a fixed token between the two parts, for example
$($a:ident),* ; $b:ident. - Put the single item first and the repetition last, for example
$b:ident $($a:ident)*.
Exercise
The matcher ($name:ident: $t:ty = $v:expr) is valid, but ($t:ty $v:expr) is rejected. Using the follow-set table, explain why.
Show answer
In the first matcher, ident can be followed by any token, : is in the allowed set for ty, and = is also in the allowed set for ty. The expr at the end has nothing after it, so it breaks no rule. In the second matcher, ty is directly followed by an expr fragment, which is not one of the allowed followers for ty, so the compiler rejects the definition. Adding a comma, as in ($t:ty, $v:expr), fixes it.