]> git.lizzy.rs Git - rust.git/commit
proc_macro: Avoid cached TokenStream more often
authorAlex Crichton <alex@alexcrichton.com>
Tue, 10 Apr 2018 19:52:47 +0000 (12:52 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Tue, 10 Apr 2018 20:05:13 +0000 (13:05 -0700)
commit6d7cfd4f1ac571f1d9b29f5ddf836909fe5f127b
tree0b3c8f6c7ec31e29b4cd074187f0b893951cc48f
parent4b9b70c394e7f341b4016fce4cbf763d404b26f9
proc_macro: Avoid cached TokenStream more often

This commit adds even more pessimization to use the cached `TokenStream` inside
of an AST node. As a reminder the `proc_macro` API requires taking an arbitrary
AST node and transforming it back into a `TokenStream` to hand off to a
procedural macro. Such functionality isn't actually implemented in rustc today,
so the way `proc_macro` works today is that it stringifies an AST node and then
reparses for a list of tokens.

This strategy unfortunately loses all span information, so we try to avoid it
whenever possible. Implemented in #43230 some AST nodes have a `TokenStream`
cache representing the tokens they were originally parsed from. This
`TokenStream` cache, however, has turned out to not always reflect the current
state of the item when it's being tokenized. For example `#[cfg]` processing or
macro expansion could modify the state of an item. Consequently we've seen a
number of bugs (#48644 and #49846) related to using this stale cache.

This commit tweaks the usage of the cached `TokenStream` to compare it to our
lossy stringification of the token stream. If the tokens that make up the cache
and the stringified token stream are the same then we return the cached version
(which has correct span information). If they differ, however, then we will
return the stringified version as the cache has been invalidated and we just
haven't figured that out.

Closes #48644
Closes #49846
src/libsyntax/parse/token.rs
src/libsyntax/tokenstream.rs
src/test/run-pass-fulldeps/proc-macro/auxiliary/modify-ast.rs [new file with mode: 0644]
src/test/run-pass-fulldeps/proc-macro/modify-ast.rs [new file with mode: 0644]