TokenStream
-based attributes, paths in attribute and derive macro invocations by jseyfried · Pull Request #40346 · rust-lang/rust (original) (raw)
Found one more interesting problem with arbitrary token trees in attributes: they make good error recovery very challenging.
Consider, for example, this code:
#[cfg(foo, fn foo() { }
mod m { }
With the previous restricted grammar, the parser, after consuming foo,
and waiting for the next meta item, would see a fn
token, which can not start a meta item, but belongs to the FIRST(item)
. So the natural and easy thing to do would be to report an error, stop parsing the attributes, and then proceed with parsing the following function and module.
Now, when arbitrary token trees are allowed in attributes, the parser has to parse all of the following code as a part of an attribute, and so it would not recognize than a function and a module are, in fact, a function and a module.
Granted, the similar effect is also present with Rust macros, but it is expected that macros are annoying to work with :-)