## Updated description First implemented in https://github.com/rust-lang/rust/pull/43230 the compiler can now tokenize a few AST nodes when necessary losslessly from the original token stream. Currently, however, this is somewhat buggy: * It only happens for items in certain situations (aka no inner attributes). The consequence of this is that more and more attributed items will have invalid span information when sent to procedural macros. * We don't invalidate the cache when later changing the item. The consequence of this is that procedural macros will receive a buggy view of what the AST node actually represents. The "real bug" here is that we haven't actually implemented converting an AST to a token tree. The initial implementation in https://github.com/rust-lang/rust/pull/43230 was effectively just a heuristic, and not even a great one! As a result we still need the ability basically to losslessly tokenize an AST node back to its original set of tokens. Some bugs that arise from this are: * Usage of `#[cfg]` modifies the AST but doesn't invalidate the cache -- https://github.com/rust-lang/rust/issues/48644 * Modules with attributes disagree on what their internal tokens are -- https://github.com/rust-lang/rust/issues/47627 * `macro_rules!` and procedural macros don't play well together -- https://github.com/rust-lang/rust/issues/49846 * ~Invoking a macro with brackets loses span information -- https://github.com/rust-lang/rust/issues/50840~ ## Original Description There's an [associated FIXME](https://github.com/rust-lang/rust/blob/4d526e0d14b43a87627cd6aca6c6f71ad1e07b6e/src/libproc_macro/lib.rs#L502-L509) in the code right now, and to fix this we'll need to implement tokenization of an AST node. Right now the thinking of how to implement this is to save all `TokenStream` instances adjacent to an AST node, and use that instead of converting back into a token stream cc @dtolnay, @nrc, @jseyfried