diff options
| author | kennytm <kennytm@gmail.com> | 2018-04-14 15:21:19 +0800 |
|---|---|---|
| committer | kennytm <kennytm@gmail.com> | 2018-04-14 15:21:19 +0800 |
| commit | 95b7e6fe92b925ddde0656d334862d225a2fdb77 (patch) | |
| tree | 6d74a8ca3cbf60269eb9f38d03707caccc03aa7a /src/libsyntax/tokenstream.rs | |
| parent | fbbc9907b1609dc5be65e658e74ea6aa77366542 (diff) | |
| parent | 6d7cfd4f1ac571f1d9b29f5ddf836909fe5f127b (diff) | |
| download | rust-95b7e6fe92b925ddde0656d334862d225a2fdb77.tar.gz rust-95b7e6fe92b925ddde0656d334862d225a2fdb77.zip | |
Rollup merge of #49852 - alexcrichton:fix-more-proc-macros, r=nrc
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
Diffstat (limited to 'src/libsyntax/tokenstream.rs')
| -rw-r--r-- | src/libsyntax/tokenstream.rs | 8 |
1 files changed, 5 insertions, 3 deletions
diff --git a/src/libsyntax/tokenstream.rs b/src/libsyntax/tokenstream.rs index 3a7a1b9a669..6ac04b3cdf6 100644 --- a/src/libsyntax/tokenstream.rs +++ b/src/libsyntax/tokenstream.rs @@ -118,7 +118,7 @@ impl TokenTree { (&TokenTree::Token(_, ref tk), &TokenTree::Token(_, ref tk2)) => tk == tk2, (&TokenTree::Delimited(_, ref dl), &TokenTree::Delimited(_, ref dl2)) => { dl.delim == dl2.delim && - dl.stream().trees().zip(dl2.stream().trees()).all(|(tt, tt2)| tt.eq_unspanned(&tt2)) + dl.stream().eq_unspanned(&dl2.stream()) } (_, _) => false, } @@ -240,12 +240,14 @@ impl TokenStream { /// Compares two TokenStreams, checking equality without regarding span information. pub fn eq_unspanned(&self, other: &TokenStream) -> bool { - for (t1, t2) in self.trees().zip(other.trees()) { + let mut t1 = self.trees(); + let mut t2 = other.trees(); + for (t1, t2) in t1.by_ref().zip(t2.by_ref()) { if !t1.eq_unspanned(&t2) { return false; } } - true + t1.next().is_none() && t2.next().is_none() } /// Precondition: `self` consists of a single token tree. |
