Skip to content

Commit 95b7e6f

Browse files
committed
Rollup merge of rust-lang#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 rust-lang#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 (rust-lang#48644 and rust-lang#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 rust-lang#48644 Closes rust-lang#49846
2 parents fbbc990 + 6d7cfd4 commit 95b7e6f

File tree

4 files changed

+121
-12
lines changed

4 files changed

+121
-12
lines changed

src/libsyntax/parse/token.rs

+22-9
Original file line numberDiff line numberDiff line change
@@ -527,8 +527,17 @@ impl Token {
527527
// all span information.
528528
//
529529
// As a result, some AST nodes are annotated with the token
530-
// stream they came from. Attempt to extract these lossless
531-
// token streams before we fall back to the stringification.
530+
// stream they came from. Here we attempt to extract these
531+
// lossless token streams before we fall back to the
532+
// stringification.
533+
//
534+
// During early phases of the compiler, though, the AST could
535+
// get modified directly (e.g. attributes added or removed) and
536+
// the internal cache of tokens my not be invalidated or
537+
// updated. Consequently if the "lossless" token stream
538+
// disagrees with our actuall stringification (which has
539+
// historically been much more battle-tested) then we go with
540+
// the lossy stream anyway (losing span information).
532541
let mut tokens = None;
533542

534543
match nt.0 {
@@ -555,13 +564,17 @@ impl Token {
555564
_ => {}
556565
}
557566

558-
tokens.unwrap_or_else(|| {
559-
nt.1.force(|| {
560-
// FIXME(jseyfried): Avoid this pretty-print + reparse hack
561-
let source = pprust::token_to_string(self);
562-
parse_stream_from_source_str(FileName::MacroExpansion, source, sess, Some(span))
563-
})
564-
})
567+
let tokens_for_real = nt.1.force(|| {
568+
// FIXME(#43081): Avoid this pretty-print + reparse hack
569+
let source = pprust::token_to_string(self);
570+
parse_stream_from_source_str(FileName::MacroExpansion, source, sess, Some(span))
571+
});
572+
if let Some(tokens) = tokens {
573+
if tokens.eq_unspanned(&tokens_for_real) {
574+
return tokens
575+
}
576+
}
577+
return tokens_for_real
565578
}
566579
}
567580

src/libsyntax/tokenstream.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ impl TokenTree {
118118
(&TokenTree::Token(_, ref tk), &TokenTree::Token(_, ref tk2)) => tk == tk2,
119119
(&TokenTree::Delimited(_, ref dl), &TokenTree::Delimited(_, ref dl2)) => {
120120
dl.delim == dl2.delim &&
121-
dl.stream().trees().zip(dl2.stream().trees()).all(|(tt, tt2)| tt.eq_unspanned(&tt2))
121+
dl.stream().eq_unspanned(&dl2.stream())
122122
}
123123
(_, _) => false,
124124
}
@@ -240,12 +240,14 @@ impl TokenStream {
240240

241241
/// Compares two TokenStreams, checking equality without regarding span information.
242242
pub fn eq_unspanned(&self, other: &TokenStream) -> bool {
243-
for (t1, t2) in self.trees().zip(other.trees()) {
243+
let mut t1 = self.trees();
244+
let mut t2 = other.trees();
245+
for (t1, t2) in t1.by_ref().zip(t2.by_ref()) {
244246
if !t1.eq_unspanned(&t2) {
245247
return false;
246248
}
247249
}
248-
true
250+
t1.next().is_none() && t2.next().is_none()
249251
}
250252

251253
/// Precondition: `self` consists of a single token tree.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// no-prefer-dynamic
12+
13+
#![crate_type = "proc-macro"]
14+
#![feature(proc_macro)]
15+
16+
extern crate proc_macro;
17+
18+
use proc_macro::*;
19+
20+
#[proc_macro_attribute]
21+
pub fn assert1(_a: TokenStream, b: TokenStream) -> TokenStream {
22+
assert_eq(b.clone(), "pub fn foo() {}".parse().unwrap());
23+
b
24+
}
25+
26+
#[proc_macro_derive(Foo, attributes(foo))]
27+
pub fn assert2(a: TokenStream) -> TokenStream {
28+
assert_eq(a, "pub struct MyStructc { _a: i32, }".parse().unwrap());
29+
TokenStream::empty()
30+
}
31+
32+
fn assert_eq(a: TokenStream, b: TokenStream) {
33+
let mut a = a.into_iter();
34+
let mut b = b.into_iter();
35+
for (a, b) in a.by_ref().zip(&mut b) {
36+
match (a, b) {
37+
(TokenTree::Group(a), TokenTree::Group(b)) => {
38+
assert_eq!(a.delimiter(), b.delimiter());
39+
assert_eq(a.stream(), b.stream());
40+
}
41+
(TokenTree::Op(a), TokenTree::Op(b)) => {
42+
assert_eq!(a.op(), b.op());
43+
assert_eq!(a.spacing(), b.spacing());
44+
}
45+
(TokenTree::Literal(a), TokenTree::Literal(b)) => {
46+
assert_eq!(a.to_string(), b.to_string());
47+
}
48+
(TokenTree::Term(a), TokenTree::Term(b)) => {
49+
assert_eq!(a.to_string(), b.to_string());
50+
}
51+
(a, b) => panic!("{:?} != {:?}", a, b),
52+
}
53+
}
54+
55+
assert!(a.next().is_none());
56+
assert!(b.next().is_none());
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// aux-build:modify-ast.rs
12+
13+
#![feature(proc_macro)]
14+
15+
extern crate modify_ast;
16+
17+
use modify_ast::*;
18+
19+
#[derive(Foo)]
20+
pub struct MyStructc {
21+
#[cfg_attr(my_cfg, foo)]
22+
_a: i32,
23+
}
24+
25+
macro_rules! a {
26+
($i:item) => ($i)
27+
}
28+
29+
a! {
30+
#[assert1]
31+
pub fn foo() {}
32+
}
33+
34+
fn main() {
35+
let _a = MyStructc { _a: 0 };
36+
foo();
37+
}

0 commit comments

Comments
 (0)