Skip to content

Commit

Permalink
Rollup merge of rust-lang#49823 - Zoxc:term-str, r=alexcrichton
Browse files Browse the repository at this point in the history
Remove usages of Term::as_str and mark it for removal

Returning references to rustc internal data structures is a bad idea since their lifetimes are unrelated to the lifetimes of proc_macro values.

See rust-lang#46972 and the `Taming thread-local storage` section of https://internals.rust-lang.org/t/parallelizing-rustc-using-rayon/6606

r? @alexcrichton
  • Loading branch information
alexcrichton authored May 10, 2018
2 parents cdfd283 + 221b7ca commit 54018b1
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/libproc_macro/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,7 @@ impl Term {
}
}

// FIXME: Remove this, do not stabilize
/// Get a reference to the interned string.
#[unstable(feature = "proc_macro", issue = "38356")]
pub fn as_str(&self) -> &str {
Expand All @@ -779,7 +780,7 @@ impl Term {
#[unstable(feature = "proc_macro", issue = "38356")]
impl fmt::Display for Term {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.as_str().fmt(f)
self.sym.as_str().fmt(f)
}
}

Expand Down Expand Up @@ -1176,7 +1177,7 @@ impl TokenTree {
},
self::TokenTree::Term(tt) => {
let ident = ast::Ident::new(tt.sym, tt.span.0);
let sym_str = tt.sym.as_str();
let sym_str = tt.sym.to_string();
let token = if sym_str.starts_with("'") {
Lifetime(ident)
} else if sym_str.starts_with("r#") {
Expand Down
2 changes: 1 addition & 1 deletion src/libproc_macro/quote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ impl Quote for Op {

impl Quote for Term {
fn quote(self) -> TokenStream {
quote!(::Term::new((quote self.as_str()), (quote self.span())))
quote!(::Term::new((quote self.sym.as_str()), (quote self.span())))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ fn assert_doc(slice: &mut &[TokenTree]) {
}

match &tokens[0] {
TokenTree::Term(tt) => assert_eq!("doc", tt.as_str()),
TokenTree::Term(tt) => assert_eq!("doc", &*tt.to_string()),
_ => panic!("expected `doc`"),
}
match &tokens[1] {
Expand Down Expand Up @@ -118,11 +118,11 @@ fn assert_invoc(slice: &mut &[TokenTree]) {

fn assert_foo(slice: &mut &[TokenTree]) {
match &slice[0] {
TokenTree::Term(tt) => assert_eq!(tt.as_str(), "fn"),
TokenTree::Term(tt) => assert_eq!(&*tt.to_string(), "fn"),
_ => panic!("expected fn"),
}
match &slice[1] {
TokenTree::Term(tt) => assert_eq!(tt.as_str(), "foo"),
TokenTree::Term(tt) => assert_eq!(&*tt.to_string(), "foo"),
_ => panic!("expected foo"),
}
match &slice[2] {
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-pass-fulldeps/auxiliary/cond_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn cond(input: TokenStream) -> TokenStream {
panic!("Invalid macro usage in cond: {}", cond);
}
let is_else = match test {
TokenTree::Term(word) => word.as_str() == "else",
TokenTree::Term(word) => &*word.to_string() == "else",
_ => false,
};
conds.push(if is_else || input.peek().is_none() {
Expand Down

0 comments on commit 54018b1

Please sign in to comment.