diff --git a/Cargo.toml b/Cargo.toml index 86ff4b10c0..ce91d0f682 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,7 +29,7 @@ extra-traits = [] proc-macro = ["proc-macro2/proc-macro", "quote/proc-macro"] [dependencies] -proc-macro2 = { version = "0.3", default-features = false } +proc-macro2 = { version = "0.4.1", default-features = false } quote = { version = "0.5", optional = true, default-features = false } unicode-xid = "0.1" @@ -42,3 +42,6 @@ all-features = true [package.metadata.playground] all-features = true + +[patch.crates-io] +quote = { git = 'https://github.com/dtolnay/quote' } diff --git a/codegen/Cargo.toml b/codegen/Cargo.toml index 2eba11cf31..9ef5a8bc0f 100644 --- a/codegen/Cargo.toml +++ b/codegen/Cargo.toml @@ -10,4 +10,7 @@ syn = { path = "..", features = ["full", "extra-traits"] } quote = "0.5" failure = "0.1" inflections = "1.1" -proc-macro2 = "0.3" +proc-macro2 = "0.4" + +[patch.crates-io] +quote = { git = 'https://github.com/dtolnay/quote' } diff --git a/codegen/src/main.rs b/codegen/src/main.rs index ac5a426225..c513cc92b2 100644 --- a/codegen/src/main.rs +++ b/codegen/src/main.rs @@ -21,9 +21,10 @@ extern crate quote; #[macro_use] extern crate syn; -use quote::{ToTokens, Tokens}; +use quote::ToTokens; use syn::{Attribute, Data, DataStruct, DeriveInput, Ident, Item}; use failure::{err_msg, Error}; +use proc_macro2::{Span, TokenStream}; use std::io::{Read, Write}; use std::fmt::{self, Debug}; @@ -39,23 +40,23 @@ const VISIT_MUT_SRC: &str = "../src/gen/visit_mut.rs"; const IGNORED_MODS: &[&str] = &["fold", "visit", "visit_mut"]; -const EXTRA_TYPES: &[&str] = &["Ident", "Lifetime"]; +const EXTRA_TYPES: &[&str] = &["Lifetime"]; -const TERMINAL_TYPES: &[&str] = &["Span"]; +const TERMINAL_TYPES: &[&str] = &["Span", "Ident"]; -fn path_eq(a: &syn::Path, b: &syn::Path) -> bool { - if a.global() != b.global() || a.segments.len() != b.segments.len() { - return false; +fn path_eq(a: &syn::Path, b: &str) -> bool { + if a.global() { + return false } - a.segments - .iter() - .zip(b.segments.iter()) - .all(|(a, b)| a.ident == b.ident) + if a.segments.len() != 1 { + return false + } + a.segments[0].ident == b } -fn get_features(attrs: &[Attribute], mut features: Tokens) -> Tokens { +fn get_features(attrs: &[Attribute], mut features: TokenStream) -> TokenStream { for attr in attrs { - if path_eq(&attr.path, &"cfg".into()) { + if path_eq(&attr.path, "cfg") { attr.to_tokens(&mut features); } } @@ -65,7 +66,7 @@ fn get_features(attrs: &[Attribute], mut features: Tokens) -> Tokens { #[derive(Clone)] pub struct AstItem { ast: DeriveInput, - features: Tokens, + features: TokenStream, // True if this is an ast_enum_of_structs! item with a #full annotation. eos_full: bool, } @@ -82,7 +83,7 @@ impl Debug for AstItem { // NOTE: BTreeMap is used here instead of HashMap to have deterministic output. type Lookup = BTreeMap; -fn load_file>(name: P, features: &Tokens, lookup: &mut Lookup) -> Result<(), Error> { +fn load_file>(name: P, features: &TokenStream, lookup: &mut Lookup) -> Result<(), Error> { let name = name.as_ref(); let parent = name.parent().ok_or_else(|| err_msg("no parent path"))?; @@ -124,7 +125,7 @@ fn load_file>(name: P, features: &Tokens, lookup: &mut Lookup) -> // Look up the submodule file, and recursively parse it. // XXX: Only handles same-directory .rs file submodules. - let path = parent.join(&format!("{}.rs", item.ident.as_ref())); + let path = parent.join(&format!("{}.rs", item.ident)); load_file(path, &features, lookup)?; } Item::Macro(item) => { @@ -134,15 +135,15 @@ fn load_file>(name: P, features: &Tokens, lookup: &mut Lookup) -> // Try to parse the AstItem declaration out of the item. let tts = &item.mac.tts; - let found = if path_eq(&item.mac.path, &"ast_struct".into()) { + let found = if path_eq(&item.mac.path, "ast_struct") { syn::parse_str::("e!(#tts).to_string()) .map_err(|_| err_msg("failed to parse ast_struct"))? .0 - } else if path_eq(&item.mac.path, &"ast_enum".into()) { + } else if path_eq(&item.mac.path, "ast_enum") { syn::parse_str::("e!(#tts).to_string()) .map_err(|_| err_msg("failed to parse ast_enum"))? .0 - } else if path_eq(&item.mac.path, &"ast_enum_of_structs".into()) { + } else if path_eq(&item.mac.path, "ast_enum_of_structs") { syn::parse_str::("e!(#tts).to_string()) .map_err(|_| err_msg("failed to parse ast_enum_of_structs"))? .0 @@ -153,13 +154,13 @@ fn load_file>(name: P, features: &Tokens, lookup: &mut Lookup) -> // Record our features on the parsed AstItems. for mut item in found { features.to_tokens(&mut item.features); - lookup.insert(item.ast.ident, item); + lookup.insert(item.ast.ident.clone(), item); } } Item::Struct(item) => { let ident = item.ident; - if EXTRA_TYPES.contains(&ident.as_ref()) { - lookup.insert(ident, AstItem { + if EXTRA_TYPES.contains(&&ident.to_string()[..]) { + lookup.insert(ident.clone(), AstItem { ast: DeriveInput { ident: ident, vis: item.vis, @@ -188,12 +189,11 @@ mod parsing { use syn; use syn::synom::*; use syn::*; - use quote::Tokens; use proc_macro2::TokenStream; // Parses #full - returns #[cfg(feature = "full")] if it is present, and // nothing otherwise. - named!(full -> (Tokens, bool), map!(option!(do_parse!( + named!(full -> (TokenStream, bool), map!(option!(do_parse!( punct!(#) >> id: syn!(Ident) >> cond_reduce!(id == "full") >> @@ -280,7 +280,7 @@ mod parsing { keyword!(pub) >> variant: syn!(Ident) >> member: option!(map!(parens!(alt!( - call!(ast_struct_inner) => { |x: AstItem| (Path::from(x.ast.ident), Some(x)) } + call!(ast_struct_inner) => { |x: AstItem| (Path::from(x.ast.ident.clone()), Some(x)) } | syn!(Path) => { |x| (x, None) } )), |x| x.1)) >> @@ -307,7 +307,7 @@ mod parsing { // tokens to strings to re-parse them. let enum_item = { let variants = variants.1.iter().map(|v| { - let name = v.name; + let name = v.name.clone(); match v.member { Some(ref member) => quote!(#name(#member)), None => quote!(#name), @@ -333,8 +333,9 @@ mod codegen { use super::{AstItem, Lookup}; use syn::*; use syn::punctuated::Punctuated; - use quote::{ToTokens, Tokens}; + use quote::ToTokens; use std::fmt::{self, Display}; + use proc_macro2::{Span, TokenStream}; #[derive(Default)] pub struct State { @@ -348,7 +349,7 @@ mod codegen { fn under_name(name: Ident) -> Ident { use inflections::Inflect; - name.as_ref().to_snake_case().into() + Ident::new(&name.to_string().to_snake_case(), Span::call_site()) } enum RelevantType<'a> { @@ -358,7 +359,7 @@ mod codegen { Option(&'a Type), Tuple(&'a Punctuated), Simple(&'a AstItem), - Token(Tokens), + Token(TokenStream), Pass, } @@ -366,13 +367,13 @@ mod codegen { match *ty { Type::Path(TypePath { qself: None, ref path }) => { let last = path.segments.last().unwrap().into_value(); - match last.ident.as_ref() { + match &last.ident.to_string()[..] { "Box" => RelevantType::Box(first_arg(&last.arguments)), "Vec" => RelevantType::Vec(first_arg(&last.arguments)), "Punctuated" => RelevantType::Punctuated(first_arg(&last.arguments)), "Option" => RelevantType::Option(first_arg(&last.arguments)), "Brace" | "Bracket" | "Paren" | "Group" => { - RelevantType::Token(last.ident.into_tokens()) + RelevantType::Token(last.ident.clone().into_token_stream()) } _ => { if let Some(item) = lookup.get(&last.ident) { @@ -387,7 +388,7 @@ mod codegen { RelevantType::Tuple(elems) } Type::Macro(TypeMacro { ref mac }) if mac.path.segments.last().unwrap().into_value().ident == "Token" => { - RelevantType::Token(mac.into_tokens()) + RelevantType::Token(mac.into_token_stream()) } _ => RelevantType::Pass, } @@ -401,35 +402,35 @@ mod codegen { } enum Operand { - Borrowed(Tokens), - Owned(Tokens), + Borrowed(TokenStream), + Owned(TokenStream), } use self::Operand::*; use self::Kind::*; impl Operand { - fn tokens(&self) -> &Tokens { + fn tokens(&self) -> &TokenStream { match *self { Borrowed(ref n) | Owned(ref n) => n, } } - fn ref_tokens(&self) -> Tokens { + fn ref_tokens(&self) -> TokenStream { match *self { Borrowed(ref n) => n.clone(), Owned(ref n) => quote!(&#n), } } - fn ref_mut_tokens(&self) -> Tokens { + fn ref_mut_tokens(&self) -> TokenStream { match *self { Borrowed(ref n) => n.clone(), Owned(ref n) => quote!(&mut #n), } } - fn owned_tokens(&self) -> Tokens { + fn owned_tokens(&self) -> TokenStream { match *self { Borrowed(ref n) => quote!(*#n), Owned(ref n) => n.clone(), @@ -467,17 +468,17 @@ mod codegen { match kind { Visit => format!( "_visitor.visit_{under_name}({name})", - under_name = under_name(item.ast.ident), + under_name = under_name(item.ast.ident.clone()), name = name.ref_tokens(), ), VisitMut => format!( "_visitor.visit_{under_name}_mut({name})", - under_name = under_name(item.ast.ident), + under_name = under_name(item.ast.ident.clone()), name = name.ref_mut_tokens(), ), Fold => format!( "_visitor.fold_{under_name}({name})", - under_name = under_name(item.ast.ident), + under_name = under_name(item.ast.ident.clone()), name = name.owned_tokens(), ), } @@ -635,7 +636,7 @@ mod codegen { } } - fn token_visit(ty: Tokens, kind: Kind, name: &Operand) -> String { + fn token_visit(ty: TokenStream, kind: Kind, name: &Operand) -> String { match kind { Fold => format!( "{ty}(tokens_helper(_visitor, &({name}).0))", @@ -695,7 +696,7 @@ mod codegen { } pub fn generate(state: &mut State, lookup: &Lookup, s: &AstItem) { - let under_name = under_name(s.ast.ident); + let under_name = under_name(s.ast.ident.clone()); state.visit_trait.push_str(&format!( "{features}\n\ @@ -758,7 +759,7 @@ mod codegen { state.visit_mut_impl.push_str(" match *_i {\n"); state.fold_impl.push_str(" match _i {\n"); for variant in &e.variants { - let fields: Vec<(&Field, Tokens)> = match variant.fields { + let fields: Vec<(&Field, TokenStream)> = match variant.fields { Fields::Named(..) => panic!("Doesn't support enum struct variants"), Fields::Unnamed(ref fields) => { let binding = format!(" {}::{}(", s.ast.ident, variant.ident); @@ -783,7 +784,7 @@ mod codegen { state.fold_impl.push_str(", "); let mut tokens = quote!(); - Ident::from(name).to_tokens(&mut tokens); + Ident::new(&name, Span::call_site()).to_tokens(&mut tokens); (el, tokens) }) @@ -864,7 +865,7 @@ mod codegen { state.fold_impl.push_str(" }\n"); } Data::Struct(ref v) => { - let fields: Vec<(&Field, Tokens)> = match v.fields { + let fields: Vec<(&Field, TokenStream)> = match v.fields { Fields::Named(ref fields) => { state .fold_impl @@ -872,7 +873,7 @@ mod codegen { fields.named .iter() .map(|el| { - let id = el.ident; + let id = el.ident.clone(); (el, quote!(_i.#id)) }) .collect() @@ -962,10 +963,10 @@ fn main() { for &tt in TERMINAL_TYPES { use syn::*; lookup.insert( - Ident::from(tt), + Ident::new(&tt, Span::call_site()), AstItem { ast: DeriveInput { - ident: Ident::from(tt), + ident: Ident::new(tt, Span::call_site()), vis: Visibility::Public(VisPublic { pub_token: Default::default(), }), @@ -977,7 +978,7 @@ fn main() { semi_token: None, }), }, - features: Default::default(), + features: TokenStream::empty(), eos_full: false, }, ); @@ -1012,6 +1013,7 @@ macro_rules! full { #![allow(unreachable_code)] #![cfg_attr(feature = \"cargo-clippy\", allow(needless_pass_by_value))] +#[cfg(any(feature = \"full\", feature = \"derive\"))] use *; #[cfg(any(feature = \"full\", feature = \"derive\"))] use token::{{Brace, Bracket, Paren, Group}}; @@ -1042,9 +1044,6 @@ macro_rules! fold_span_only {{ }} }} -fold_span_only!(fold_ident: Ident); -#[cfg(any(feature = \"full\", feature = \"derive\"))] -fold_span_only!(fold_lifetime: Lifetime); #[cfg(any(feature = \"full\", feature = \"derive\"))] fold_span_only!(fold_lit_byte: LitByte); #[cfg(any(feature = \"full\", feature = \"derive\"))] @@ -1075,6 +1074,7 @@ fold_span_only!(fold_lit_str: LitStr); #![cfg_attr(feature = \"cargo-clippy\", allow(match_same_arms))] +#[cfg(any(feature = \"full\", feature = \"derive\"))] use *; #[cfg(any(feature = \"full\", feature = \"derive\"))] use punctuated::Punctuated; @@ -1112,6 +1112,7 @@ pub trait Visit<'ast> {{ #![cfg_attr(feature = \"cargo-clippy\", allow(match_same_arms))] +#[cfg(any(feature = \"full\", feature = \"derive\"))] use *; #[cfg(any(feature = \"full\", feature = \"derive\"))] use punctuated::Punctuated; diff --git a/src/attr.rs b/src/attr.rs index b63b302908..6f50e0a381 100644 --- a/src/attr.rs +++ b/src/attr.rs @@ -107,19 +107,19 @@ impl Attribute { }; if self.tts.is_empty() { - return Some(Meta::Word(*name)); + return Some(Meta::Word(name.clone())); } let tts = self.tts.clone().into_iter().collect::>(); if tts.len() == 1 { - if let Some(meta) = Attribute::extract_meta_list(*name, &tts[0]) { + if let Some(meta) = Attribute::extract_meta_list(name.clone(), &tts[0]) { return Some(meta); } } if tts.len() == 2 { - if let Some(meta) = Attribute::extract_name_value(*name, &tts[0], &tts[1]) { + if let Some(meta) = Attribute::extract_name_value(name.clone(), &tts[0], &tts[1]) { return Some(meta); } } @@ -149,13 +149,13 @@ impl Attribute { fn extract_name_value(ident: Ident, a: &TokenTree, b: &TokenTree) -> Option { let a = match *a { - TokenTree::Op(ref o) => o, + TokenTree::Punct(ref o) => o, _ => return None, }; if a.spacing() != Spacing::Alone { return None; } - if a.op() != '=' { + if a.as_char() != '=' { return None; } @@ -167,7 +167,7 @@ impl Attribute { lit: Lit::new(l.clone()), })) } - TokenTree::Term(ref term) => match term.as_str() { + TokenTree::Ident(ref term) => match &term.to_string()[..] { v @ "true" | v @ "false" => Some(Meta::NameValue(MetaNameValue { ident: ident, eq_token: Token![=]([a.span()]), @@ -196,21 +196,20 @@ fn nested_meta_item_from_tokens(tts: &[TokenTree]) -> Option<(NestedMeta, &[Toke } } - TokenTree::Term(sym) => { - let ident = Ident::new(sym.as_str(), sym.span()); + TokenTree::Ident(ref ident) => { if tts.len() >= 3 { - if let Some(meta) = Attribute::extract_name_value(ident, &tts[1], &tts[2]) { + if let Some(meta) = Attribute::extract_name_value(ident.clone(), &tts[1], &tts[2]) { return Some((NestedMeta::Meta(meta), &tts[3..])); } } if tts.len() >= 2 { - if let Some(meta) = Attribute::extract_meta_list(ident, &tts[1]) { + if let Some(meta) = Attribute::extract_meta_list(ident.clone(), &tts[1]) { return Some((NestedMeta::Meta(meta), &tts[2..])); } } - Some((Meta::Word(ident).into(), &tts[1..])) + Some((Meta::Word(ident.clone()).into(), &tts[1..])) } _ => None, @@ -227,11 +226,11 @@ fn list_of_nested_meta_items_from_tokens( let prev_comma = if first { first = false; None - } else if let TokenTree::Op(ref op) = tts[0] { + } else if let TokenTree::Punct(ref op) = tts[0] { if op.spacing() != Spacing::Alone { return None; } - if op.op() != ',' { + if op.as_char() != ',' { return None; } let tok = Token![,]([op.span()]); @@ -336,9 +335,9 @@ impl Meta { /// `#[derive(Copy)]`, and the `path` in `#[path = "sys/windows.rs"]`. pub fn name(&self) -> Ident { match *self { - Meta::Word(ref meta) => *meta, - Meta::List(ref meta) => meta.ident, - Meta::NameValue(ref meta) => meta.ident, + Meta::Word(ref meta) => meta.clone(), + Meta::List(ref meta) => meta.ident.clone(), + Meta::NameValue(ref meta) => meta.ident.clone(), } } } @@ -397,11 +396,11 @@ pub mod parsing { use super::*; use buffer::Cursor; use parse_error; - use proc_macro2::{Literal, Op, Spacing, Span, TokenTree}; + use proc_macro2::{Literal, Punct, Spacing, Span, TokenTree}; use synom::PResult; fn eq(span: Span) -> TokenTree { - let mut op = Op::new('=', Spacing::Alone); + let mut op = Punct::new('=', Spacing::Alone); op.set_span(span); op.into() } @@ -518,10 +517,11 @@ pub mod parsing { #[cfg(feature = "printing")] mod printing { use super::*; - use quote::{ToTokens, Tokens}; + use proc_macro2::TokenStream; + use quote::ToTokens; impl ToTokens for Attribute { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.pound_token.to_tokens(tokens); if let AttrStyle::Inner(ref b) = self.style { b.to_tokens(tokens); @@ -534,7 +534,7 @@ mod printing { } impl ToTokens for MetaList { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.ident.to_tokens(tokens); self.paren_token.surround(tokens, |tokens| { self.nested.to_tokens(tokens); @@ -543,7 +543,7 @@ mod printing { } impl ToTokens for MetaNameValue { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.ident.to_tokens(tokens); self.eq_token.to_tokens(tokens); self.lit.to_tokens(tokens); diff --git a/src/buffer.rs b/src/buffer.rs index a4af7bc6ea..d695c7709b 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -129,8 +129,8 @@ #[cfg(feature = "proc-macro")] use proc_macro as pm; -use proc_macro2::{Delimiter, Literal, Span, Term, TokenStream}; -use proc_macro2::{Group, Op, TokenTree}; +use proc_macro2::{Delimiter, Literal, Span, Ident, TokenStream}; +use proc_macro2::{Group, Punct, TokenTree}; use std::marker::PhantomData; use std::ptr; @@ -143,8 +143,8 @@ use std::fmt::{self, Debug}; enum Entry { // Mimicking types from proc-macro. Group(Span, Delimiter, TokenBuffer), - Term(Term), - Op(Op), + Ident(Ident), + Punct(Punct), Literal(Literal), // End entries contain a raw pointer to the entry from the containing // token tree, or null if this is the outermost level. @@ -177,11 +177,11 @@ impl TokenBuffer { let mut seqs = Vec::new(); for tt in stream { match tt { - TokenTree::Term(sym) => { - entries.push(Entry::Term(sym)); + TokenTree::Ident(sym) => { + entries.push(Entry::Ident(sym)); } - TokenTree::Op(op) => { - entries.push(Entry::Op(op)); + TokenTree::Punct(op) => { + entries.push(Entry::Punct(op)); } TokenTree::Literal(l) => { entries.push(Entry::Literal(l)); @@ -275,8 +275,8 @@ impl<'a> Cursor<'a> { pub fn empty() -> Self { // It's safe in this situation for us to put an `Entry` object in global // storage, despite it not actually being safe to send across threads - // (`Term` is a reference into a thread-local table). This is because - // this entry never includes a `Term` object. + // (`Ident` is a reference into a thread-local table). This is because + // this entry never includes a `Ident` object. // // This wrapper struct allows us to break the rules and put a `Sync` // object in global storage. @@ -368,22 +368,22 @@ impl<'a> Cursor<'a> { None } - /// If the cursor is pointing at a `Term`, returns it along with a cursor + /// If the cursor is pointing at a `Ident`, returns it along with a cursor /// pointing at the next `TokenTree`. - pub fn term(mut self) -> Option<(Term, Cursor<'a>)> { + pub fn ident(mut self) -> Option<(Ident, Cursor<'a>)> { self.ignore_none(); match *self.entry() { - Entry::Term(term) => Some((term, unsafe { self.bump() })), + Entry::Ident(ref term) => Some((term.clone(), unsafe { self.bump() })), _ => None, } } - /// If the cursor is pointing at an `Op`, returns it along with a cursor + /// If the cursor is pointing at an `Punct`, returns it along with a cursor /// pointing at the next `TokenTree`. - pub fn op(mut self) -> Option<(Op, Cursor<'a>)> { + pub fn punct(mut self) -> Option<(Punct, Cursor<'a>)> { self.ignore_none(); match *self.entry() { - Entry::Op(op) => Some((op, unsafe { self.bump() })), + Entry::Punct(ref op) => Some((op.clone(), unsafe { self.bump() })), _ => None, } } @@ -426,8 +426,8 @@ impl<'a> Cursor<'a> { TokenTree::from(g) } Entry::Literal(ref lit) => lit.clone().into(), - Entry::Term(term) => term.into(), - Entry::Op(op) => op.into(), + Entry::Ident(ref term) => term.clone().into(), + Entry::Punct(ref op) => op.clone().into(), Entry::End(..) => { return None; } @@ -442,8 +442,8 @@ impl<'a> Cursor<'a> { match *self.entry() { Entry::Group(span, ..) => span, Entry::Literal(ref l) => l.span(), - Entry::Term(t) => t.span(), - Entry::Op(o) => o.span(), + Entry::Ident(ref t) => t.span(), + Entry::Punct(ref o) => o.span(), Entry::End(..) => Span::call_site(), } } diff --git a/src/data.rs b/src/data.rs index 31eb1d13a5..fbbdb7073b 100644 --- a/src/data.rs +++ b/src/data.rs @@ -322,10 +322,11 @@ pub mod parsing { #[cfg(feature = "printing")] mod printing { use super::*; - use quote::{ToTokens, Tokens}; + use proc_macro2::TokenStream; + use quote::{ToTokens, TokenStreamExt}; impl ToTokens for Variant { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(&self.attrs); self.ident.to_tokens(tokens); self.fields.to_tokens(tokens); @@ -337,7 +338,7 @@ mod printing { } impl ToTokens for FieldsNamed { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.brace_token.surround(tokens, |tokens| { self.named.to_tokens(tokens); }); @@ -345,7 +346,7 @@ mod printing { } impl ToTokens for FieldsUnnamed { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.paren_token.surround(tokens, |tokens| { self.unnamed.to_tokens(tokens); }); @@ -353,7 +354,7 @@ mod printing { } impl ToTokens for Field { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(&self.attrs); self.vis.to_tokens(tokens); if let Some(ref ident) = self.ident { @@ -365,19 +366,19 @@ mod printing { } impl ToTokens for VisPublic { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.pub_token.to_tokens(tokens) } } impl ToTokens for VisCrate { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.crate_token.to_tokens(tokens); } } impl ToTokens for VisRestricted { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.pub_token.to_tokens(tokens); self.paren_token.surround(tokens, |tokens| { // XXX: If we have a path which is not "self" or "super" or diff --git a/src/derive.rs b/src/derive.rs index 3ad893d8bb..ba60f8df25 100644 --- a/src/derive.rs +++ b/src/derive.rs @@ -163,10 +163,11 @@ pub mod parsing { mod printing { use super::*; use attr::FilterAttrs; - use quote::{ToTokens, Tokens}; + use quote::ToTokens; + use proc_macro2::TokenStream; impl ToTokens for DeriveInput { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { for attr in self.attrs.outer() { attr.to_tokens(tokens); } diff --git a/src/expr.rs b/src/expr.rs index 339e29eee1..ac55c98c20 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -2178,7 +2178,7 @@ pub mod parsing { tuple!(syn!(Member), map!(punct!(:), Some), syn!(Expr)) | map!(syn!(Ident), |name| ( - Member::Named(name), + Member::Named(name.clone()), None, Expr::Path(ExprPath { attrs: Vec::new(), @@ -2583,7 +2583,7 @@ pub mod parsing { let mut pat: Pat = PatIdent { by_ref: by_ref, mutability: mutability, - ident: ident, + ident: ident.clone(), subpat: None, }.into(); if let Some(boxed) = boxed { @@ -2821,13 +2821,13 @@ mod printing { use super::*; #[cfg(feature = "full")] use attr::FilterAttrs; - use proc_macro2::Literal; - use quote::{ToTokens, Tokens}; + use proc_macro2::{Literal, TokenStream}; + use quote::{ToTokens, TokenStreamExt}; // If the given expression is a bare `ExprStruct`, wraps it in parenthesis // before appending it to `Tokens`. #[cfg(feature = "full")] - fn wrap_bare_struct(tokens: &mut Tokens, e: &Expr) { + fn wrap_bare_struct(tokens: &mut TokenStream, e: &Expr) { if let Expr::Struct(_) = *e { token::Paren::default().surround(tokens, |tokens| { e.to_tokens(tokens); @@ -2838,16 +2838,16 @@ mod printing { } #[cfg(feature = "full")] - fn attrs_to_tokens(attrs: &[Attribute], tokens: &mut Tokens) { + fn attrs_to_tokens(attrs: &[Attribute], tokens: &mut TokenStream) { tokens.append_all(attrs.outer()); } #[cfg(not(feature = "full"))] - fn attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut Tokens) {} + fn attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut TokenStream) {} #[cfg(feature = "full")] impl ToTokens for ExprBox { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.box_token.to_tokens(tokens); self.expr.to_tokens(tokens); @@ -2856,7 +2856,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprInPlace { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.place.to_tokens(tokens); self.arrow_token.to_tokens(tokens); @@ -2866,7 +2866,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprArray { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.bracket_token.surround(tokens, |tokens| { self.elems.to_tokens(tokens); @@ -2875,7 +2875,7 @@ mod printing { } impl ToTokens for ExprCall { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { attrs_to_tokens(&self.attrs, tokens); self.func.to_tokens(tokens); self.paren_token.surround(tokens, |tokens| { @@ -2886,7 +2886,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprMethodCall { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.receiver.to_tokens(tokens); self.dot_token.to_tokens(tokens); @@ -2900,7 +2900,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for MethodTurbofish { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.colon2_token.to_tokens(tokens); self.lt_token.to_tokens(tokens); self.args.to_tokens(tokens); @@ -2910,7 +2910,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for GenericMethodArgument { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { match *self { GenericMethodArgument::Type(ref t) => t.to_tokens(tokens), GenericMethodArgument::Const(ref c) => c.to_tokens(tokens), @@ -2920,7 +2920,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprTuple { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.paren_token.surround(tokens, |tokens| { self.elems.to_tokens(tokens); @@ -2934,7 +2934,7 @@ mod printing { } impl ToTokens for ExprBinary { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { attrs_to_tokens(&self.attrs, tokens); self.left.to_tokens(tokens); self.op.to_tokens(tokens); @@ -2943,7 +2943,7 @@ mod printing { } impl ToTokens for ExprUnary { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { attrs_to_tokens(&self.attrs, tokens); self.op.to_tokens(tokens); self.expr.to_tokens(tokens); @@ -2951,14 +2951,14 @@ mod printing { } impl ToTokens for ExprLit { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { attrs_to_tokens(&self.attrs, tokens); self.lit.to_tokens(tokens); } } impl ToTokens for ExprCast { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { attrs_to_tokens(&self.attrs, tokens); self.expr.to_tokens(tokens); self.as_token.to_tokens(tokens); @@ -2968,7 +2968,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprType { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { attrs_to_tokens(&self.attrs, tokens); self.expr.to_tokens(tokens); self.colon_token.to_tokens(tokens); @@ -2977,7 +2977,7 @@ mod printing { } #[cfg(feature = "full")] - fn maybe_wrap_else(tokens: &mut Tokens, else_: &Option<(Token![else], Box)>) { + fn maybe_wrap_else(tokens: &mut TokenStream, else_: &Option<(Token![else], Box)>) { if let Some((ref else_token, ref else_)) = *else_ { else_token.to_tokens(tokens); @@ -2998,7 +2998,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprIf { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.if_token.to_tokens(tokens); wrap_bare_struct(tokens, &self.cond); @@ -3009,7 +3009,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprIfLet { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.if_token.to_tokens(tokens); self.let_token.to_tokens(tokens); @@ -3023,7 +3023,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprWhile { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.label.to_tokens(tokens); self.while_token.to_tokens(tokens); @@ -3034,7 +3034,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprWhileLet { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.label.to_tokens(tokens); self.while_token.to_tokens(tokens); @@ -3048,7 +3048,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprForLoop { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.label.to_tokens(tokens); self.for_token.to_tokens(tokens); @@ -3061,7 +3061,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprLoop { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.label.to_tokens(tokens); self.loop_token.to_tokens(tokens); @@ -3071,7 +3071,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprMatch { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.match_token.to_tokens(tokens); wrap_bare_struct(tokens, &self.expr); @@ -3091,7 +3091,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprCatch { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.do_token.to_tokens(tokens); self.catch_token.to_tokens(tokens); @@ -3101,7 +3101,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprYield { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.yield_token.to_tokens(tokens); self.expr.to_tokens(tokens); @@ -3110,7 +3110,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprClosure { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.movability.to_tokens(tokens); self.capture.to_tokens(tokens); @@ -3136,7 +3136,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprUnsafe { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.unsafe_token.to_tokens(tokens); self.block.to_tokens(tokens); @@ -3145,7 +3145,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprBlock { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.block.to_tokens(tokens); } @@ -3153,7 +3153,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprAssign { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.left.to_tokens(tokens); self.eq_token.to_tokens(tokens); @@ -3163,7 +3163,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprAssignOp { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.left.to_tokens(tokens); self.op.to_tokens(tokens); @@ -3173,7 +3173,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprField { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.base.to_tokens(tokens); self.dot_token.to_tokens(tokens); @@ -3182,16 +3182,16 @@ mod printing { } impl ToTokens for Member { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { match *self { - Member::Named(ident) => ident.to_tokens(tokens), + Member::Named(ref ident) => ident.to_tokens(tokens), Member::Unnamed(ref index) => index.to_tokens(tokens), } } } impl ToTokens for Index { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { let mut lit = Literal::i64_unsuffixed(i64::from(self.index)); lit.set_span(self.span); tokens.append(lit); @@ -3199,7 +3199,7 @@ mod printing { } impl ToTokens for ExprIndex { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { attrs_to_tokens(&self.attrs, tokens); self.expr.to_tokens(tokens); self.bracket_token.surround(tokens, |tokens| { @@ -3210,7 +3210,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprRange { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.from.to_tokens(tokens); match self.limits { @@ -3222,7 +3222,7 @@ mod printing { } impl ToTokens for ExprPath { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { attrs_to_tokens(&self.attrs, tokens); ::PathTokens(&self.qself, &self.path).to_tokens(tokens) } @@ -3230,7 +3230,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprReference { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.and_token.to_tokens(tokens); self.mutability.to_tokens(tokens); @@ -3240,7 +3240,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprBreak { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.break_token.to_tokens(tokens); self.label.to_tokens(tokens); @@ -3250,7 +3250,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprContinue { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.continue_token.to_tokens(tokens); self.label.to_tokens(tokens); @@ -3259,7 +3259,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprReturn { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.return_token.to_tokens(tokens); self.expr.to_tokens(tokens); @@ -3268,7 +3268,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprMacro { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.mac.to_tokens(tokens); } @@ -3276,7 +3276,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprStruct { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.path.to_tokens(tokens); self.brace_token.surround(tokens, |tokens| { @@ -3291,7 +3291,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprRepeat { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.bracket_token.surround(tokens, |tokens| { self.expr.to_tokens(tokens); @@ -3303,7 +3303,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprGroup { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { attrs_to_tokens(&self.attrs, tokens); self.group_token.surround(tokens, |tokens| { self.expr.to_tokens(tokens); @@ -3312,7 +3312,7 @@ mod printing { } impl ToTokens for ExprParen { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { attrs_to_tokens(&self.attrs, tokens); self.paren_token.surround(tokens, |tokens| { self.expr.to_tokens(tokens); @@ -3322,7 +3322,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprTry { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.expr.to_tokens(tokens); self.question_token.to_tokens(tokens); @@ -3330,14 +3330,14 @@ mod printing { } impl ToTokens for ExprVerbatim { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.tts.to_tokens(tokens); } } #[cfg(feature = "full")] impl ToTokens for Label { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.name.to_tokens(tokens); self.colon_token.to_tokens(tokens); } @@ -3345,7 +3345,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for FieldValue { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.member.to_tokens(tokens); if let Some(ref colon_token) = self.colon_token { @@ -3357,7 +3357,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for Arm { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(&self.attrs); self.leading_vert.to_tokens(tokens); self.pats.to_tokens(tokens); @@ -3373,14 +3373,14 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for PatWild { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.underscore_token.to_tokens(tokens); } } #[cfg(feature = "full")] impl ToTokens for PatIdent { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.by_ref.to_tokens(tokens); self.mutability.to_tokens(tokens); self.ident.to_tokens(tokens); @@ -3393,7 +3393,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for PatStruct { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.path.to_tokens(tokens); self.brace_token.surround(tokens, |tokens| { self.fields.to_tokens(tokens); @@ -3408,7 +3408,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for PatTupleStruct { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.path.to_tokens(tokens); self.pat.to_tokens(tokens); } @@ -3416,14 +3416,14 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for PatPath { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { ::PathTokens(&self.qself, &self.path).to_tokens(tokens); } } #[cfg(feature = "full")] impl ToTokens for PatTuple { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.paren_token.surround(tokens, |tokens| { self.front.to_tokens(tokens); if let Some(ref dot2_token) = self.dot2_token { @@ -3445,7 +3445,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for PatBox { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.box_token.to_tokens(tokens); self.pat.to_tokens(tokens); } @@ -3453,7 +3453,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for PatRef { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.and_token.to_tokens(tokens); self.mutability.to_tokens(tokens); self.pat.to_tokens(tokens); @@ -3462,14 +3462,14 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for PatLit { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.expr.to_tokens(tokens); } } #[cfg(feature = "full")] impl ToTokens for PatRange { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.lo.to_tokens(tokens); match self.limits { RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens), @@ -3481,7 +3481,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for PatSlice { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { // XXX: This is a mess, and it will be so easy to screw it up. How // do we make this correct itself better? self.bracket_token.surround(tokens, |tokens| { @@ -3516,21 +3516,21 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for PatMacro { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.mac.to_tokens(tokens); } } #[cfg(feature = "full")] impl ToTokens for PatVerbatim { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.tts.to_tokens(tokens); } } #[cfg(feature = "full")] impl ToTokens for FieldPat { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { if let Some(ref colon_token) = self.colon_token { self.member.to_tokens(tokens); colon_token.to_tokens(tokens); @@ -3541,7 +3541,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for Block { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.brace_token.surround(tokens, |tokens| { tokens.append_all(&self.stmts); }); @@ -3550,7 +3550,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for Stmt { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { match *self { Stmt::Local(ref local) => local.to_tokens(tokens), Stmt::Item(ref item) => item.to_tokens(tokens), @@ -3565,7 +3565,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for Local { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.let_token.to_tokens(tokens); self.pats.to_tokens(tokens); diff --git a/src/file.rs b/src/file.rs index 17ea8ff2a8..99a311ddf0 100644 --- a/src/file.rs +++ b/src/file.rs @@ -111,10 +111,11 @@ pub mod parsing { mod printing { use super::*; use attr::FilterAttrs; - use quote::{ToTokens, Tokens}; + use quote::{ToTokens, TokenStreamExt}; + use proc_macro2::TokenStream; impl ToTokens for File { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.inner()); tokens.append_all(&self.items); } diff --git a/src/gen/fold.rs b/src/gen/fold.rs index b5b67b3f6b..2cd760db55 100644 --- a/src/gen/fold.rs +++ b/src/gen/fold.rs @@ -6,6 +6,7 @@ #![allow(unreachable_code)] #![cfg_attr(feature = "cargo-clippy", allow(needless_pass_by_value))] +#[cfg(any(feature = "full", feature = "derive"))] use *; #[cfg(any(feature = "full", feature = "derive"))] use token::{Brace, Bracket, Paren, Group}; @@ -430,9 +431,6 @@ macro_rules! fold_span_only { } } -fold_span_only!(fold_ident: Ident); -#[cfg(any(feature = "full", feature = "derive"))] -fold_span_only!(fold_lifetime: Lifetime); #[cfg(any(feature = "full", feature = "derive"))] fold_span_only!(fold_lit_byte: LitByte); #[cfg(any(feature = "full", feature = "derive"))] @@ -1596,6 +1594,10 @@ pub fn fold_generics(_visitor: &mut V, _i: Generics) -> Generi where_clause: (_i . where_clause).map(|it| { _visitor.fold_where_clause(it) }), } } + +pub fn fold_ident(_visitor: &mut V, _i: Ident) -> Ident { + _i +} # [ cfg ( feature = "full" ) ] pub fn fold_impl_item(_visitor: &mut V, _i: ImplItem) -> ImplItem { match _i { @@ -1980,6 +1982,13 @@ pub fn fold_label(_visitor: &mut V, _i: Label) -> Label { } } # [ cfg ( any ( feature = "full" , feature = "derive" ) ) ] +pub fn fold_lifetime(_visitor: &mut V, _i: Lifetime) -> Lifetime { + Lifetime { + apostrophe: _i . apostrophe, + ident: _visitor.fold_ident(_i . ident), + } +} +# [ cfg ( any ( feature = "full" , feature = "derive" ) ) ] pub fn fold_lifetime_def(_visitor: &mut V, _i: LifetimeDef) -> LifetimeDef { LifetimeDef { attrs: FoldHelper::lift(_i . attrs, |it| { _visitor.fold_attribute(it) }), diff --git a/src/gen/visit.rs b/src/gen/visit.rs index 6a38d1d717..3591bb7ed3 100644 --- a/src/gen/visit.rs +++ b/src/gen/visit.rs @@ -4,6 +4,7 @@ #![cfg_attr(feature = "cargo-clippy", allow(match_same_arms))] +#[cfg(any(feature = "full", feature = "derive"))] use *; #[cfg(any(feature = "full", feature = "derive"))] use punctuated::Punctuated; @@ -1246,7 +1247,6 @@ pub fn visit_generics<'ast, V: Visit<'ast> + ?Sized>(_visitor: &mut V, _i: &'ast } pub fn visit_ident<'ast, V: Visit<'ast> + ?Sized>(_visitor: &mut V, _i: &'ast Ident) { - // Skipped field _i . term; } # [ cfg ( feature = "full" ) ] pub fn visit_impl_item<'ast, V: Visit<'ast> + ?Sized>(_visitor: &mut V, _i: &'ast ImplItem) { @@ -1545,7 +1545,8 @@ pub fn visit_label<'ast, V: Visit<'ast> + ?Sized>(_visitor: &mut V, _i: &'ast La } # [ cfg ( any ( feature = "full" , feature = "derive" ) ) ] pub fn visit_lifetime<'ast, V: Visit<'ast> + ?Sized>(_visitor: &mut V, _i: &'ast Lifetime) { - // Skipped field _i . term; + // Skipped field _i . apostrophe; + _visitor.visit_ident(& _i . ident); } # [ cfg ( any ( feature = "full" , feature = "derive" ) ) ] pub fn visit_lifetime_def<'ast, V: Visit<'ast> + ?Sized>(_visitor: &mut V, _i: &'ast LifetimeDef) { diff --git a/src/gen/visit_mut.rs b/src/gen/visit_mut.rs index 2a86a0c899..848e3481ba 100644 --- a/src/gen/visit_mut.rs +++ b/src/gen/visit_mut.rs @@ -4,6 +4,7 @@ #![cfg_attr(feature = "cargo-clippy", allow(match_same_arms))] +#[cfg(any(feature = "full", feature = "derive"))] use *; #[cfg(any(feature = "full", feature = "derive"))] use punctuated::Punctuated; @@ -1247,7 +1248,6 @@ pub fn visit_generics_mut(_visitor: &mut V, _i: &mut Gener } pub fn visit_ident_mut(_visitor: &mut V, _i: &mut Ident) { - // Skipped field _i . term; } # [ cfg ( feature = "full" ) ] pub fn visit_impl_item_mut(_visitor: &mut V, _i: &mut ImplItem) { @@ -1546,7 +1546,8 @@ pub fn visit_label_mut(_visitor: &mut V, _i: &mut Label) { } # [ cfg ( any ( feature = "full" , feature = "derive" ) ) ] pub fn visit_lifetime_mut(_visitor: &mut V, _i: &mut Lifetime) { - // Skipped field _i . term; + // Skipped field _i . apostrophe; + _visitor.visit_ident_mut(& mut _i . ident); } # [ cfg ( any ( feature = "full" , feature = "derive" ) ) ] pub fn visit_lifetime_def_mut(_visitor: &mut V, _i: &mut LifetimeDef) { diff --git a/src/generics.rs b/src/generics.rs index 919b5fe846..4c2858de78 100644 --- a/src/generics.rs +++ b/src/generics.rs @@ -307,12 +307,14 @@ impl Generics { /// for that type. /// /// ``` + /// # extern crate proc_macro2; /// # extern crate syn; /// # #[macro_use] /// # extern crate quote; + /// # use proc_macro2::{Span, Ident}; /// # fn main() { /// # let generics: syn::Generics = Default::default(); - /// # let name = syn::Ident::from("MyType"); + /// # let name = Ident::new("MyType", Span::call_site()); /// let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); /// quote! { /// impl #impl_generics MyTrait for #name #ty_generics #where_clause { @@ -751,10 +753,11 @@ pub mod parsing { mod printing { use super::*; use attr::FilterAttrs; - use quote::{ToTokens, Tokens}; + use quote::{ToTokens, TokenStreamExt}; + use proc_macro2::TokenStream; impl ToTokens for Generics { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { if self.params.is_empty() { return; } @@ -791,7 +794,7 @@ mod printing { } impl<'a> ToTokens for ImplGenerics<'a> { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { if self.0.params.is_empty() { return; } @@ -846,7 +849,7 @@ mod printing { } impl<'a> ToTokens for TypeGenerics<'a> { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { if self.0.params.is_empty() { return; } @@ -894,7 +897,7 @@ mod printing { } impl<'a> ToTokens for Turbofish<'a> { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { if !self.0.params.is_empty() { ::default().to_tokens(tokens); TypeGenerics(self.0).to_tokens(tokens); @@ -903,7 +906,7 @@ mod printing { } impl ToTokens for BoundLifetimes { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.for_token.to_tokens(tokens); self.lt_token.to_tokens(tokens); self.lifetimes.to_tokens(tokens); @@ -912,7 +915,7 @@ mod printing { } impl ToTokens for LifetimeDef { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.lifetime.to_tokens(tokens); if !self.bounds.is_empty() { @@ -923,7 +926,7 @@ mod printing { } impl ToTokens for TypeParam { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.ident.to_tokens(tokens); if !self.bounds.is_empty() { @@ -938,8 +941,8 @@ mod printing { } impl ToTokens for TraitBound { - fn to_tokens(&self, tokens: &mut Tokens) { - let to_tokens = |tokens: &mut Tokens| { + fn to_tokens(&self, tokens: &mut TokenStream) { + let to_tokens = |tokens: &mut TokenStream| { self.modifier.to_tokens(tokens); self.lifetimes.to_tokens(tokens); self.path.to_tokens(tokens); @@ -952,7 +955,7 @@ mod printing { } impl ToTokens for TraitBoundModifier { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { match *self { TraitBoundModifier::None => {} TraitBoundModifier::Maybe(ref t) => t.to_tokens(tokens), @@ -961,7 +964,7 @@ mod printing { } impl ToTokens for ConstParam { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.const_token.to_tokens(tokens); self.ident.to_tokens(tokens); @@ -975,7 +978,7 @@ mod printing { } impl ToTokens for WhereClause { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { if !self.predicates.is_empty() { self.where_token.to_tokens(tokens); self.predicates.to_tokens(tokens); @@ -984,7 +987,7 @@ mod printing { } impl ToTokens for PredicateType { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.lifetimes.to_tokens(tokens); self.bounded_ty.to_tokens(tokens); self.colon_token.to_tokens(tokens); @@ -993,7 +996,7 @@ mod printing { } impl ToTokens for PredicateLifetime { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.lifetime.to_tokens(tokens); if !self.bounds.is_empty() { TokensOrDefault(&self.colon_token).to_tokens(tokens); @@ -1003,7 +1006,7 @@ mod printing { } impl ToTokens for PredicateEq { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.lhs_ty.to_tokens(tokens); self.eq_token.to_tokens(tokens); self.rhs_ty.to_tokens(tokens); diff --git a/src/ident.rs b/src/ident.rs deleted file mode 100644 index 5fddf28e1d..0000000000 --- a/src/ident.rs +++ /dev/null @@ -1,306 +0,0 @@ -// Copyright 2018 Syn Developers -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -use std::borrow::Cow; -use std::cmp::Ordering; -use std::fmt::{self, Display}; -use std::hash::{Hash, Hasher}; - -use proc_macro2::Term; -use unicode_xid::UnicodeXID; - -use proc_macro2::Span; - -/// A word of Rust code, which may be a keyword or legal variable name. -/// -/// An identifier consists of at least one Unicode code point, the first of -/// which has the XID_Start property and the rest of which have the XID_Continue -/// property. An underscore may be used as the first character as long as it is -/// not the only character. -/// -/// - The empty string is not an identifier. Use `Option`. -/// - An underscore by itself is not an identifier. Use -/// `Token![_]` instead. -/// - A lifetime is not an identifier. Use `syn::Lifetime` instead. -/// -/// An identifier constructed with `Ident::new` is permitted to be a Rust -/// keyword, though parsing one through its [`Synom`] implementation rejects -/// Rust keywords. Use `call!(Ident::parse_any)` when parsing to match the -/// behaviour of `Ident::new`. -/// -/// [`Synom`]: synom/trait.Synom.html -/// -/// # Examples -/// -/// A new ident can be created from a string using the `Ident::from` function. -/// Idents produced by `Ident::from` are set to resolve at the procedural macro -/// *def site* by default. A different span can be provided explicitly by using -/// `Ident::new`. -/// -/// ```rust -/// extern crate syn; -/// extern crate proc_macro2; -/// -/// use syn::Ident; -/// use proc_macro2::Span; -/// -/// fn main() { -/// let def_ident = Ident::from("definitely"); -/// let call_ident = Ident::new("calligraphy", Span::call_site()); -/// -/// println!("{} {}", def_ident, call_ident); -/// } -/// ``` -/// -/// An ident can be interpolated into a token stream using the `quote!` macro. -/// -/// ```rust -/// #[macro_use] -/// extern crate quote; -/// -/// extern crate syn; -/// use syn::Ident; -/// -/// fn main() { -/// let ident = Ident::from("demo"); -/// -/// // Create a variable binding whose name is this ident. -/// let expanded = quote! { let #ident = 10; }; -/// -/// // Create a variable binding with a slightly different name. -/// let temp_ident = Ident::from(format!("new_{}", ident)); -/// let expanded = quote! { let #temp_ident = 10; }; -/// } -/// ``` -/// -/// A string representation of the ident is available through the `as_ref()` and -/// `to_string()` methods. -/// -/// ```rust -/// # use syn::Ident; -/// # let ident = Ident::from("another_identifier"); -/// # -/// // Examine the ident as a &str. -/// let ident_str = ident.as_ref(); -/// if ident_str.len() > 60 { -/// println!("Very long identifier: {}", ident_str) -/// } -/// -/// // Create a String from the ident. -/// let ident_string = ident.to_string(); -/// give_away(ident_string); -/// -/// fn give_away(s: String) { /* ... */ } -/// ``` -#[derive(Copy, Clone, Debug)] -pub struct Ident { - term: Term, -} - -impl Ident { - /// Creates an ident with the given string representation. - /// - /// # Panics - /// - /// Panics if the input string is neither a keyword nor a legal variable - /// name. - pub fn new(s: &str, span: Span) -> Self { - if s.is_empty() { - panic!("ident is not allowed to be empty; use Option"); - } - - if s.starts_with('\'') { - panic!("ident is not allowed to be a lifetime; use syn::Lifetime"); - } - - if s.bytes().all(|digit| digit >= b'0' && digit <= b'9') { - panic!("ident cannot be a number, use syn::Index instead"); - } - - fn xid_ok(s: &str) -> bool { - let mut chars = s.chars(); - let first = chars.next().unwrap(); - if !(UnicodeXID::is_xid_start(first) || first == '_') { - return false; - } - for ch in chars { - if !UnicodeXID::is_xid_continue(ch) { - return false; - } - } - true - } - - if !xid_ok(s) { - panic!("{:?} is not a valid ident", s); - } - - Ident { - term: Term::new(s, span), - } - } - - pub fn span(&self) -> Span { - self.term.span() - } - - pub fn set_span(&mut self, span: Span) { - self.term.set_span(span); - } -} - -impl<'a> From<&'a str> for Ident { - fn from(s: &str) -> Self { - Ident::new(s, Span::call_site()) - } -} - -impl From for Ident { - fn from(tok: Token![self]) -> Self { - Ident::new("self", tok.0) - } -} - -impl From for Ident { - fn from(tok: Token![Self]) -> Self { - Ident::new("Self", tok.0) - } -} - -impl From for Ident { - fn from(tok: Token![super]) -> Self { - Ident::new("super", tok.0) - } -} - -impl From for Ident { - fn from(tok: Token![crate]) -> Self { - Ident::new("crate", tok.0) - } -} - -impl<'a> From> for Ident { - fn from(s: Cow<'a, str>) -> Self { - Ident::new(&s, Span::call_site()) - } -} - -impl From for Ident { - fn from(s: String) -> Self { - Ident::new(&s, Span::call_site()) - } -} - -impl AsRef for Ident { - fn as_ref(&self) -> &str { - self.term.as_str() - } -} - -impl Display for Ident { - fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - self.term.as_str().fmt(formatter) - } -} - -impl PartialEq for Ident -where - T: AsRef, -{ - fn eq(&self, other: &T) -> bool { - self.as_ref() == other.as_ref() - } -} - -impl Eq for Ident {} - -impl PartialOrd for Ident { - fn partial_cmp(&self, other: &Ident) -> Option { - Some(self.cmp(other)) - } -} - -impl Ord for Ident { - fn cmp(&self, other: &Ident) -> Ordering { - self.as_ref().cmp(other.as_ref()) - } -} - -impl Hash for Ident { - fn hash(&self, h: &mut H) { - self.as_ref().hash(h); - } -} - -#[cfg(feature = "parsing")] -pub mod parsing { - use super::*; - use buffer::Cursor; - use parse_error; - use synom::PResult; - use synom::Synom; - - impl Synom for Ident { - fn parse(input: Cursor) -> PResult { - let (term, rest) = match input.term() { - Some(term) => term, - _ => return parse_error(), - }; - if term.as_str().starts_with('\'') { - return parse_error(); - } - match term.as_str() { - "_" - // From https://doc.rust-lang.org/grammar.html#keywords - | "abstract" | "alignof" | "as" | "become" | "box" | "break" | "const" - | "continue" | "crate" | "do" | "else" | "enum" | "extern" | "false" | "final" - | "fn" | "for" | "if" | "impl" | "in" | "let" | "loop" | "macro" | "match" - | "mod" | "move" | "mut" | "offsetof" | "override" | "priv" | "proc" | "pub" - | "pure" | "ref" | "return" | "Self" | "self" | "sizeof" | "static" | "struct" - | "super" | "trait" | "true" | "type" | "typeof" | "unsafe" | "unsized" | "use" - | "virtual" | "where" | "while" | "yield" => return parse_error(), - _ => {} - } - - Ok((Ident { term: term }, rest)) - } - - fn description() -> Option<&'static str> { - Some("identifier") - } - } - - impl Ident { - /// Parses any identifier - /// - /// This is useful when parsing a DSL which allows Rust keywords as identifiers. - pub fn parse_any(input: Cursor) -> PResult { - let (term, rest) = match input.term() { - Some(term) => term, - _ => return parse_error(), - }; - if term.as_str().starts_with('\'') { - return parse_error(); - } - - Ok((Ident { term: term }, rest)) - } - } -} - -#[cfg(feature = "printing")] -mod printing { - use super::*; - use quote::{ToTokens, Tokens}; - - impl ToTokens for Ident { - fn to_tokens(&self, tokens: &mut Tokens) { - tokens.append(self.term); - } - } -} diff --git a/src/item.rs b/src/item.rs index beb5a9e97c..226e102fc1 100644 --- a/src/item.rs +++ b/src/item.rs @@ -1536,10 +1536,11 @@ pub mod parsing { mod printing { use super::*; use attr::FilterAttrs; - use quote::{ToTokens, Tokens}; + use quote::{ToTokens, TokenStreamExt}; + use proc_macro2::TokenStream; impl ToTokens for ItemExternCrate { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.vis.to_tokens(tokens); self.extern_token.to_tokens(tokens); @@ -1554,7 +1555,7 @@ mod printing { } impl ToTokens for ItemUse { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.vis.to_tokens(tokens); self.use_token.to_tokens(tokens); @@ -1565,7 +1566,7 @@ mod printing { } impl ToTokens for ItemStatic { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.vis.to_tokens(tokens); self.static_token.to_tokens(tokens); @@ -1580,7 +1581,7 @@ mod printing { } impl ToTokens for ItemConst { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.vis.to_tokens(tokens); self.const_token.to_tokens(tokens); @@ -1594,13 +1595,13 @@ mod printing { } impl ToTokens for ItemFn { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.vis.to_tokens(tokens); self.constness.to_tokens(tokens); self.unsafety.to_tokens(tokens); self.abi.to_tokens(tokens); - NamedDecl(&self.decl, self.ident).to_tokens(tokens); + NamedDecl(&self.decl, &self.ident).to_tokens(tokens); self.block.brace_token.surround(tokens, |tokens| { tokens.append_all(self.attrs.inner()); tokens.append_all(&self.block.stmts); @@ -1609,7 +1610,7 @@ mod printing { } impl ToTokens for ItemMod { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.vis.to_tokens(tokens); self.mod_token.to_tokens(tokens); @@ -1626,7 +1627,7 @@ mod printing { } impl ToTokens for ItemForeignMod { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.abi.to_tokens(tokens); self.brace_token.surround(tokens, |tokens| { @@ -1636,7 +1637,7 @@ mod printing { } impl ToTokens for ItemType { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.vis.to_tokens(tokens); self.type_token.to_tokens(tokens); @@ -1650,7 +1651,7 @@ mod printing { } impl ToTokens for ItemEnum { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.vis.to_tokens(tokens); self.enum_token.to_tokens(tokens); @@ -1664,7 +1665,7 @@ mod printing { } impl ToTokens for ItemStruct { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.vis.to_tokens(tokens); self.struct_token.to_tokens(tokens); @@ -1689,7 +1690,7 @@ mod printing { } impl ToTokens for ItemUnion { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.vis.to_tokens(tokens); self.union_token.to_tokens(tokens); @@ -1701,7 +1702,7 @@ mod printing { } impl ToTokens for ItemTrait { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.vis.to_tokens(tokens); self.unsafety.to_tokens(tokens); @@ -1721,7 +1722,7 @@ mod printing { } impl ToTokens for ItemImpl { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.defaultness.to_tokens(tokens); self.unsafety.to_tokens(tokens); @@ -1742,7 +1743,7 @@ mod printing { } impl ToTokens for ItemMacro { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.mac.path.to_tokens(tokens); self.mac.bang_token.to_tokens(tokens); @@ -1763,7 +1764,7 @@ mod printing { } impl ToTokens for ItemMacro2 { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.vis.to_tokens(tokens); self.macro_token.to_tokens(tokens); @@ -1778,13 +1779,13 @@ mod printing { } impl ToTokens for ItemVerbatim { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.tts.to_tokens(tokens); } } impl ToTokens for UsePath { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.ident.to_tokens(tokens); self.colon2_token.to_tokens(tokens); self.tree.to_tokens(tokens); @@ -1792,13 +1793,13 @@ mod printing { } impl ToTokens for UseName { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.ident.to_tokens(tokens); } } impl ToTokens for UseRename { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.ident.to_tokens(tokens); self.as_token.to_tokens(tokens); self.rename.to_tokens(tokens); @@ -1806,13 +1807,13 @@ mod printing { } impl ToTokens for UseGlob { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.star_token.to_tokens(tokens); } } impl ToTokens for UseGroup { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.brace_token.surround(tokens, |tokens| { self.items.to_tokens(tokens); }); @@ -1820,7 +1821,7 @@ mod printing { } impl ToTokens for TraitItemConst { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.const_token.to_tokens(tokens); self.ident.to_tokens(tokens); @@ -1835,7 +1836,7 @@ mod printing { } impl ToTokens for TraitItemMethod { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.sig.to_tokens(tokens); match self.default { @@ -1853,7 +1854,7 @@ mod printing { } impl ToTokens for TraitItemType { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.type_token.to_tokens(tokens); self.ident.to_tokens(tokens); @@ -1872,7 +1873,7 @@ mod printing { } impl ToTokens for TraitItemMacro { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.mac.to_tokens(tokens); self.semi_token.to_tokens(tokens); @@ -1880,13 +1881,13 @@ mod printing { } impl ToTokens for TraitItemVerbatim { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.tts.to_tokens(tokens); } } impl ToTokens for ImplItemConst { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.vis.to_tokens(tokens); self.defaultness.to_tokens(tokens); @@ -1901,7 +1902,7 @@ mod printing { } impl ToTokens for ImplItemMethod { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.vis.to_tokens(tokens); self.defaultness.to_tokens(tokens); @@ -1914,7 +1915,7 @@ mod printing { } impl ToTokens for ImplItemType { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.vis.to_tokens(tokens); self.defaultness.to_tokens(tokens); @@ -1928,7 +1929,7 @@ mod printing { } impl ToTokens for ImplItemMacro { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.mac.to_tokens(tokens); self.semi_token.to_tokens(tokens); @@ -1936,22 +1937,22 @@ mod printing { } impl ToTokens for ImplItemVerbatim { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.tts.to_tokens(tokens); } } impl ToTokens for ForeignItemFn { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.vis.to_tokens(tokens); - NamedDecl(&self.decl, self.ident).to_tokens(tokens); + NamedDecl(&self.decl, &self.ident).to_tokens(tokens); self.semi_token.to_tokens(tokens); } } impl ToTokens for ForeignItemStatic { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.vis.to_tokens(tokens); self.static_token.to_tokens(tokens); @@ -1964,7 +1965,7 @@ mod printing { } impl ToTokens for ForeignItemType { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.vis.to_tokens(tokens); self.type_token.to_tokens(tokens); @@ -1974,24 +1975,24 @@ mod printing { } impl ToTokens for ForeignItemVerbatim { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.tts.to_tokens(tokens); } } impl ToTokens for MethodSig { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.constness.to_tokens(tokens); self.unsafety.to_tokens(tokens); self.abi.to_tokens(tokens); - NamedDecl(&self.decl, self.ident).to_tokens(tokens); + NamedDecl(&self.decl, &self.ident).to_tokens(tokens); } } - struct NamedDecl<'a>(&'a FnDecl, Ident); + struct NamedDecl<'a>(&'a FnDecl, &'a Ident); impl<'a> ToTokens for NamedDecl<'a> { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.0.fn_token.to_tokens(tokens); self.1.to_tokens(tokens); self.0.generics.to_tokens(tokens); @@ -2008,7 +2009,7 @@ mod printing { } impl ToTokens for ArgSelfRef { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.and_token.to_tokens(tokens); self.lifetime.to_tokens(tokens); self.mutability.to_tokens(tokens); @@ -2017,14 +2018,14 @@ mod printing { } impl ToTokens for ArgSelf { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.mutability.to_tokens(tokens); self.self_token.to_tokens(tokens); } } impl ToTokens for ArgCaptured { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.pat.to_tokens(tokens); self.colon_token.to_tokens(tokens); self.ty.to_tokens(tokens); diff --git a/src/lib.rs b/src/lib.rs index c459395810..beababe423 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -286,6 +286,8 @@ mod macros; #[macro_use] pub mod token; +pub use proc_macro2::Ident; + #[cfg(any(feature = "full", feature = "derive"))] mod attr; #[cfg(any(feature = "full", feature = "derive"))] @@ -329,9 +331,6 @@ pub use generics::{ #[cfg(all(any(feature = "full", feature = "derive"), feature = "printing"))] pub use generics::{ImplGenerics, Turbofish, TypeGenerics}; -mod ident; -pub use ident::Ident; - #[cfg(feature = "full")] mod item; #[cfg(feature = "full")] @@ -732,7 +731,7 @@ impl<'a, T> quote::ToTokens for TokensOrDefault<'a, T> where T: quote::ToTokens + Default, { - fn to_tokens(&self, tokens: &mut quote::Tokens) { + fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) { match *self.0 { Some(ref t) => t.to_tokens(tokens), None => T::default().to_tokens(tokens), diff --git a/src/lifetime.rs b/src/lifetime.rs index 7e3f2310c6..bf8147b42c 100644 --- a/src/lifetime.rs +++ b/src/lifetime.rs @@ -10,9 +10,11 @@ use std::cmp::Ordering; use std::fmt::{self, Display}; use std::hash::{Hash, Hasher}; -use proc_macro2::{Span, Term}; +use proc_macro2::{Span, Ident}; use unicode_xid::UnicodeXID; +use token::Apostrophe; + /// A Rust lifetime: `'a`. /// /// Lifetime names must conform to the following rules: @@ -27,9 +29,10 @@ use unicode_xid::UnicodeXID; /// *This type is available if Syn is built with the `"derive"` or `"full"` /// feature.* #[cfg_attr(feature = "extra-traits", derive(Debug))] -#[derive(Copy, Clone)] +#[derive(Clone)] pub struct Lifetime { - term: Term, + pub apostrophe: Apostrophe, + pub ident: Ident, } impl Lifetime { @@ -65,28 +68,22 @@ impl Lifetime { } Lifetime { - term: Term::new(s, span), + apostrophe: Default::default(), + ident: Ident::new(&s[1..], span), } } - - pub fn span(&self) -> Span { - self.term.span() - } - - pub fn set_span(&mut self, span: Span) { - self.term.set_span(span); - } } impl Display for Lifetime { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - self.term.as_str().fmt(formatter) + "'".fmt(formatter)?; + self.ident.fmt(formatter) } } impl PartialEq for Lifetime { fn eq(&self, other: &Lifetime) -> bool { - self.term.as_str() == other.term.as_str() + self.ident.eq(&other.ident) } } @@ -100,13 +97,13 @@ impl PartialOrd for Lifetime { impl Ord for Lifetime { fn cmp(&self, other: &Lifetime) -> Ordering { - self.term.as_str().cmp(other.term.as_str()) + self.ident.cmp(&other.ident) } } impl Hash for Lifetime { fn hash(&self, h: &mut H) { - self.term.as_str().hash(h) + self.ident.hash(h) } } @@ -120,15 +117,17 @@ pub mod parsing { impl Synom for Lifetime { fn parse(input: Cursor) -> PResult { - let (term, rest) = match input.term() { - Some(term) => term, - _ => return parse_error(), + let (apostrophe, rest) = Apostrophe::parse(input)?; + let (ident, rest) = match rest.ident() { + Some(pair) => pair, + None => return parse_error(), }; - if !term.as_str().starts_with('\'') { - return parse_error(); - } - Ok((Lifetime { term: term }, rest)) + let ret = Lifetime { + ident: ident, + apostrophe: apostrophe, + }; + Ok((ret, rest)) } fn description() -> Option<&'static str> { @@ -140,11 +139,13 @@ pub mod parsing { #[cfg(feature = "printing")] mod printing { use super::*; - use quote::{ToTokens, Tokens}; + use quote::ToTokens; + use proc_macro2::TokenStream; impl ToTokens for Lifetime { - fn to_tokens(&self, tokens: &mut Tokens) { - self.term.to_tokens(tokens); + fn to_tokens(&self, tokens: &mut TokenStream) { + self.apostrophe.to_tokens(tokens); + self.ident.to_tokens(tokens); } } } diff --git a/src/lit.rs b/src/lit.rs index 5096b1b298..76df32a149 100644 --- a/src/lit.rs +++ b/src/lit.rs @@ -10,7 +10,7 @@ use proc_macro2::{Literal, Span}; use std::str; #[cfg(feature = "printing")] -use proc_macro2::Term; +use proc_macro2::Ident; #[cfg(feature = "parsing")] use proc_macro2::TokenStream; @@ -429,12 +429,12 @@ pub mod parsing { Ok((Lit::new(lit), rest)) } } - _ => match input.term() { + _ => match input.ident() { Some((term, rest)) => Ok(( Lit::Bool(LitBool { - value: if term.as_str() == "true" { + value: if term == "true" { true - } else if term.as_str() == "false" { + } else if term == "false" { false } else { return parse_error(); @@ -506,53 +506,54 @@ pub mod parsing { #[cfg(feature = "printing")] mod printing { use super::*; - use quote::{ToTokens, Tokens}; + use quote::{ToTokens, TokenStreamExt}; + use proc_macro2::TokenStream; impl ToTokens for LitStr { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.token.to_tokens(tokens); } } impl ToTokens for LitByteStr { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.token.to_tokens(tokens); } } impl ToTokens for LitByte { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.token.to_tokens(tokens); } } impl ToTokens for LitChar { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.token.to_tokens(tokens); } } impl ToTokens for LitInt { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.token.to_tokens(tokens); } } impl ToTokens for LitFloat { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.token.to_tokens(tokens); } } impl ToTokens for LitBool { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { let s = if self.value { "true" } else { "false" }; - tokens.append(Term::new(s, self.span)); + tokens.append(Ident::new(s, self.span)); } } impl ToTokens for LitVerbatim { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.token.to_tokens(tokens); } } diff --git a/src/mac.rs b/src/mac.rs index 2ccc8c9432..a486d7c9b3 100644 --- a/src/mac.rs +++ b/src/mac.rs @@ -93,10 +93,11 @@ pub mod parsing { #[cfg(feature = "printing")] mod printing { use super::*; - use quote::{ToTokens, Tokens}; + use quote::ToTokens; + use proc_macro2::TokenStream; impl ToTokens for Macro { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.path.to_tokens(tokens); self.bang_token.to_tokens(tokens); match self.delimiter { diff --git a/src/macros.rs b/src/macros.rs index 667c5baf32..0017d502db 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -133,7 +133,7 @@ macro_rules! generate_to_tokens { (($($arms:tt)*) $tokens:ident $name:ident {}) => { impl ::quote::ToTokens for $name { - fn to_tokens(&self, $tokens: &mut ::quote::Tokens) { + fn to_tokens(&self, $tokens: &mut ::proc_macro2::TokenStream) { match *self { $($arms)* } diff --git a/src/op.rs b/src/op.rs index b9a8ef7f90..95ba33cbc4 100644 --- a/src/op.rs +++ b/src/op.rs @@ -174,10 +174,11 @@ pub mod parsing { #[cfg(feature = "printing")] mod printing { use super::*; - use quote::{ToTokens, Tokens}; + use quote::ToTokens; + use proc_macro2::TokenStream; impl ToTokens for BinOp { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { match *self { BinOp::Add(ref t) => t.to_tokens(tokens), BinOp::Sub(ref t) => t.to_tokens(tokens), @@ -212,7 +213,7 @@ mod printing { } impl ToTokens for UnOp { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { match *self { UnOp::Deref(ref t) => t.to_tokens(tokens), UnOp::Not(ref t) => t.to_tokens(tokens), diff --git a/src/parsers.rs b/src/parsers.rs index 7691074c35..bdd0c6a049 100644 --- a/src/parsers.rs +++ b/src/parsers.rs @@ -217,7 +217,7 @@ pub fn invoke R>(f: F, t: T) -> R { /// #[macro_use] /// extern crate syn; /// -/// use syn::{Expr, Ident}; +/// use syn::Expr; /// /// /// Parses any expression that does not begin with a `-` minus sign. /// named!(not_negative_expr -> Expr, do_parse!( @@ -811,15 +811,16 @@ macro_rules! tuple_parser { /// ```rust /// #[macro_use] /// extern crate syn; +/// extern crate proc_macro2; /// -/// use syn::Ident; +/// use proc_macro2::{Ident, Span}; /// /// // Parse any identifier token, or the `!` token in which case the /// // identifier is treated as `"BANG"`. /// named!(ident_or_bang -> Ident, alt!( /// syn!(Ident) /// | -/// punct!(!) => { |_| "BANG".into() } +/// punct!(!) => { |_| Ident::new("BANG", Span::call_site()) } /// )); /// # /// # fn main() {} @@ -926,10 +927,10 @@ macro_rules! alt { /// extern crate syn; /// extern crate proc_macro2; /// -/// use syn::Ident; +/// use proc_macro2::Ident; +/// use proc_macro2::TokenStream; /// use syn::token::Paren; /// use syn::synom::Synom; -/// use proc_macro2::TokenStream; /// /// /// Parse a macro invocation that uses `(` `)` parentheses. /// /// diff --git a/src/path.rs b/src/path.rs index 39edd11b0b..a4495a2463 100644 --- a/src/path.rs +++ b/src/path.rs @@ -31,9 +31,11 @@ impl Path { /// ```rust /// extern crate syn; /// extern crate quote; +/// extern crate proc_macro2; /// /// use syn::{QSelf, Path, PathTokens}; -/// use quote::{Tokens, ToTokens}; +/// use proc_macro2::TokenStream; +/// use quote::ToTokens; /// /// struct MyNode { /// qself: Option, @@ -41,7 +43,7 @@ impl Path { /// } /// /// impl ToTokens for MyNode { -/// fn to_tokens(&self, tokens: &mut Tokens) { +/// fn to_tokens(&self, tokens: &mut TokenStream) { /// PathTokens(&self.qself, &self.path).to_tokens(tokens); /// } /// } @@ -416,24 +418,25 @@ pub mod parsing { #[cfg(feature = "printing")] mod printing { use super::*; - use quote::{ToTokens, Tokens}; + use quote::ToTokens; + use proc_macro2::TokenStream; impl ToTokens for Path { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.leading_colon.to_tokens(tokens); self.segments.to_tokens(tokens); } } impl ToTokens for PathSegment { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.ident.to_tokens(tokens); self.arguments.to_tokens(tokens); } } impl ToTokens for PathArguments { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { match *self { PathArguments::None => {} PathArguments::AngleBracketed(ref arguments) => { @@ -448,7 +451,7 @@ mod printing { impl ToTokens for GenericArgument { #[cfg_attr(feature = "cargo-clippy", allow(match_same_arms))] - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { match *self { GenericArgument::Lifetime(ref lt) => lt.to_tokens(tokens), GenericArgument::Type(ref ty) => ty.to_tokens(tokens), @@ -473,7 +476,7 @@ mod printing { } impl ToTokens for AngleBracketedGenericArguments { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.colon2_token.to_tokens(tokens); self.lt_token.to_tokens(tokens); @@ -516,7 +519,7 @@ mod printing { } impl ToTokens for Binding { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.ident.to_tokens(tokens); self.eq_token.to_tokens(tokens); self.ty.to_tokens(tokens); @@ -524,7 +527,7 @@ mod printing { } impl ToTokens for ParenthesizedGenericArguments { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.paren_token.surround(tokens, |tokens| { self.inputs.to_tokens(tokens); }); @@ -533,7 +536,7 @@ mod printing { } impl<'a> ToTokens for PathTokens<'a> { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { let qself = match *self.0 { Some(ref qself) => qself, None => return self.1.to_tokens(tokens), diff --git a/src/punctuated.rs b/src/punctuated.rs index 25bcfc3dd6..8f4144f30c 100644 --- a/src/punctuated.rs +++ b/src/punctuated.rs @@ -762,14 +762,15 @@ where #[cfg(feature = "printing")] mod printing { use super::*; - use quote::{ToTokens, Tokens}; + use quote::{ToTokens, TokenStreamExt}; + use proc_macro2::TokenStream; impl ToTokens for Punctuated where T: ToTokens, P: ToTokens, { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.pairs()) } } @@ -779,7 +780,7 @@ mod printing { T: ToTokens, P: ToTokens, { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { match *self { Pair::Punctuated(ref a, ref b) => { a.to_tokens(tokens); diff --git a/src/spanned.rs b/src/spanned.rs index 2f96581d9f..da45c10895 100644 --- a/src/spanned.rs +++ b/src/spanned.rs @@ -79,7 +79,7 @@ //! error appear in the correct place underlining the right type. use proc_macro2::{Span, TokenStream}; -use quote::{ToTokens, Tokens}; +use quote::ToTokens; /// A trait that can provide the `Span` of the complete contents of a syntax /// tree node. @@ -109,7 +109,7 @@ where { #[cfg(procmacro2_semver_exempt)] fn span(&self) -> Span { - let mut tokens = Tokens::new(); + let mut tokens = TokenStream::empty(); self.to_tokens(&mut tokens); let token_stream = TokenStream::from(tokens); let mut iter = token_stream.into_iter(); @@ -129,7 +129,7 @@ where #[cfg(not(procmacro2_semver_exempt))] fn span(&self) -> Span { - let mut tokens = Tokens::new(); + let mut tokens = TokenStream::empty(); self.to_tokens(&mut tokens); let token_stream = TokenStream::from(tokens); let mut iter = token_stream.into_iter(); diff --git a/src/synom.rs b/src/synom.rs index eb4c801959..5f0bf78d6c 100644 --- a/src/synom.rs +++ b/src/synom.rs @@ -155,6 +155,7 @@ use proc_macro; use proc_macro2; pub use error::{PResult, ParseError}; +use error::parse_error; use buffer::{Cursor, TokenBuffer}; @@ -213,6 +214,33 @@ impl Synom for proc_macro2::TokenStream { } } +impl Synom for proc_macro2::Ident { + fn parse(input: Cursor) -> PResult { + let (term, rest) = match input.ident() { + Some(term) => term, + _ => return parse_error(), + }; + match &term.to_string()[..] { + "_" + // From https://doc.rust-lang.org/grammar.html#keywords + | "abstract" | "alignof" | "as" | "become" | "box" | "break" | "const" + | "continue" | "crate" | "do" | "else" | "enum" | "extern" | "false" | "final" + | "fn" | "for" | "if" | "impl" | "in" | "let" | "loop" | "macro" | "match" + | "mod" | "move" | "mut" | "offsetof" | "override" | "priv" | "proc" | "pub" + | "pure" | "ref" | "return" | "Self" | "self" | "sizeof" | "static" | "struct" + | "super" | "trait" | "true" | "type" | "typeof" | "unsafe" | "unsized" | "use" + | "virtual" | "where" | "while" | "yield" => return parse_error(), + _ => {} + } + + Ok((term, rest)) + } + + fn description() -> Option<&'static str> { + Some("identifier") + } +} + /// Parser that can parse Rust tokens into a particular syntax tree node. /// /// Refer to the [module documentation] for details about parsing in Syn. diff --git a/src/token.rs b/src/token.rs index 1a077ed882..40e2dce102 100644 --- a/src/token.rs +++ b/src/token.rs @@ -97,7 +97,7 @@ //! # fn main() {} //! ``` -use proc_macro2::Span; +use proc_macro2::{Span, Ident}; macro_rules! tokens { ( @@ -111,7 +111,7 @@ macro_rules! tokens { $($keyword:tt pub struct $keyword_name:ident #[$keyword_doc:meta])* } ) => ( - $(token_punct_def! { #[$punct_doc] $punct pub struct $punct_name/$len })* + $(token_punct_def! { #[$punct_doc] pub struct $punct_name/$len })* $(token_punct_parser! { $punct pub struct $punct_name })* $(token_delimiter! { #[$delimiter_doc] $delimiter pub struct $delimiter_name })* $(token_keyword! { #[$keyword_doc] $keyword pub struct $keyword_name })* @@ -119,7 +119,7 @@ macro_rules! tokens { } macro_rules! token_punct_def { - (#[$doc:meta] $s:tt pub struct $name:ident / $len:tt) => { + (#[$doc:meta] pub struct $name:ident / $len:tt) => { #[cfg_attr(feature = "clone-impls", derive(Copy, Clone))] #[$doc] /// @@ -179,7 +179,7 @@ macro_rules! token_punct_parser { ($s:tt pub struct $name:ident) => { #[cfg(feature = "printing")] impl ::quote::ToTokens for $name { - fn to_tokens(&self, tokens: &mut ::quote::Tokens) { + fn to_tokens(&self, tokens: &mut ::proc_macro2::TokenStream) { printing::punct($s, &self.0, tokens); } } @@ -242,7 +242,7 @@ macro_rules! token_keyword { #[cfg(feature = "printing")] impl ::quote::ToTokens for $name { - fn to_tokens(&self, tokens: &mut ::quote::Tokens) { + fn to_tokens(&self, tokens: &mut ::proc_macro2::TokenStream) { printing::keyword($s, &self.0, tokens); } } @@ -306,9 +306,9 @@ macro_rules! token_delimiter { impl $name { #[cfg(feature = "printing")] - pub fn surround(&self, tokens: &mut ::quote::Tokens, f: F) + pub fn surround(&self, tokens: &mut ::proc_macro2::TokenStream, f: F) where - F: FnOnce(&mut ::quote::Tokens), + F: FnOnce(&mut ::proc_macro2::TokenStream), { printing::delim($s, &self.0, tokens, f); } @@ -335,22 +335,28 @@ macro_rules! token_delimiter { token_punct_def! { /// `_` - "_" pub struct Underscore/1 + pub struct Underscore/1 } #[cfg(feature = "printing")] impl ::quote::ToTokens for Underscore { - fn to_tokens(&self, tokens: &mut ::quote::Tokens) { - tokens.append(::proc_macro2::Term::new("_", self.0[0])); + fn to_tokens(&self, tokens: &mut ::proc_macro2::TokenStream) { + use quote::TokenStreamExt; + tokens.append(::proc_macro2::Ident::new("_", self.0[0])); } } #[cfg(feature = "parsing")] impl ::Synom for Underscore { fn parse(input: ::buffer::Cursor) -> ::synom::PResult { - match input.term() { - Some((term, rest)) if term.as_str() == "_" => Ok((Underscore([term.span()]), rest)), - Some(_) => ::parse_error(), + match input.ident() { + Some((term, rest)) => { + if term == "_" { + Ok((Underscore([term.span()]), rest)) + } else { + ::parse_error() + } + } None => parsing::punct("_", input, Underscore), } } @@ -360,6 +366,41 @@ impl ::Synom for Underscore { } } +token_punct_def! { + /// `'` + pub struct Apostrophe/1 +} + +#[cfg(feature = "printing")] +impl ::quote::ToTokens for Apostrophe { + fn to_tokens(&self, tokens: &mut ::proc_macro2::TokenStream) { + use quote::TokenStreamExt; + let mut token = ::proc_macro2::Punct::new('\'', ::proc_macro2::Spacing::Joint); + token.set_span(self.0[0]); + tokens.append(token); + } +} + +#[cfg(feature = "parsing")] +impl ::Synom for Apostrophe { + fn parse(input: ::buffer::Cursor) -> ::synom::PResult { + match input.punct() { + Some((op, rest)) => { + if op.as_char() == '\'' && op.spacing() == ::proc_macro2::Spacing::Joint { + Ok((Apostrophe([op.span()]), rest)) + } else { + ::parse_error() + } + } + None => ::parse_error() + } + } + + fn description() -> Option<&'static str> { + Some("`'`") + } +} + tokens! { punct: { "+" pub struct Add/1 /// `+` @@ -673,6 +714,21 @@ macro_rules! keyword { ($i:expr, yield) => { call!($i, <$crate::token::Yield as $crate::synom::Synom>::parse) }; } +macro_rules! ident_from_token { + ($token:ident) => { + impl From for Ident { + fn from(token: Token![$token]) -> Ident { + Ident::new(stringify!($token), token.0) + } + } + }; +} + +ident_from_token!(self); +ident_from_token!(Self); +ident_from_token!(super); +ident_from_token!(crate); + #[cfg(feature = "parsing")] mod parsing { use proc_macro2::{Delimiter, Spacing, Span}; @@ -712,16 +768,20 @@ mod parsing { let chars = s.chars(); for (i, (ch, slot)) in chars.zip(&mut spans).enumerate() { - match tokens.op() { - Some((op, rest)) if op.op() == ch => { - if i != s.len() - 1 { - match op.spacing() { - Spacing::Joint => {} - _ => return parse_error(), + match tokens.punct() { + Some((op, rest)) => { + if op.as_char() == ch { + if i != s.len() - 1 { + match op.spacing() { + Spacing::Joint => {} + _ => return parse_error(), + } } + *slot = op.span(); + tokens = rest; + } else { + return parse_error() } - *slot = op.span(); - tokens = rest; } _ => return parse_error(), } @@ -734,8 +794,8 @@ mod parsing { tokens: Cursor<'a>, new: fn(Span) -> T, ) -> PResult<'a, T> { - if let Some((term, rest)) = tokens.term() { - if term.as_str() == keyword { + if let Some((term, rest)) = tokens.ident() { + if term == keyword { return Ok((new(term.span()), rest)); } } @@ -776,10 +836,10 @@ mod parsing { #[cfg(feature = "printing")] mod printing { - use proc_macro2::{Delimiter, Group, Op, Spacing, Span, Term}; - use quote::Tokens; + use proc_macro2::{Delimiter, Group, Punct, Spacing, Span, Ident, TokenStream}; + use quote::TokenStreamExt; - pub fn punct(s: &str, spans: &[Span], tokens: &mut Tokens) { + pub fn punct(s: &str, spans: &[Span], tokens: &mut TokenStream) { assert_eq!(s.len(), spans.len()); let mut chars = s.chars(); @@ -787,23 +847,23 @@ mod printing { let ch = chars.next_back().unwrap(); let span = spans.next_back().unwrap(); for (ch, span) in chars.zip(spans) { - let mut op = Op::new(ch, Spacing::Joint); + let mut op = Punct::new(ch, Spacing::Joint); op.set_span(*span); tokens.append(op); } - let mut op = Op::new(ch, Spacing::Alone); + let mut op = Punct::new(ch, Spacing::Alone); op.set_span(*span); tokens.append(op); } - pub fn keyword(s: &str, span: &Span, tokens: &mut Tokens) { - tokens.append(Term::new(s, *span)); + pub fn keyword(s: &str, span: &Span, tokens: &mut TokenStream) { + tokens.append(Ident::new(s, *span)); } - pub fn delim(s: &str, span: &Span, tokens: &mut Tokens, f: F) + pub fn delim(s: &str, span: &Span, tokens: &mut TokenStream, f: F) where - F: FnOnce(&mut Tokens), + F: FnOnce(&mut TokenStream), { let delim = match s { "(" => Delimiter::Parenthesis, @@ -812,7 +872,7 @@ mod printing { " " => Delimiter::None, _ => panic!("unknown delimiter: {}", s), }; - let mut inner = Tokens::new(); + let mut inner = TokenStream::empty(); f(&mut inner); let mut g = Group::new(delim, inner.into()); g.set_span(*span); diff --git a/src/tt.rs b/src/tt.rs index 2443dce705..bde82dcafb 100644 --- a/src/tt.rs +++ b/src/tt.rs @@ -89,8 +89,8 @@ impl<'a> PartialEq for TokenTreeHelper<'a> { } s2.next().is_none() } - (&TokenTree::Op(ref o1), &TokenTree::Op(ref o2)) => { - o1.op() == o2.op() && match (o1.spacing(), o2.spacing()) { + (&TokenTree::Punct(ref o1), &TokenTree::Punct(ref o2)) => { + o1.as_char() == o2.as_char() && match (o1.spacing(), o2.spacing()) { (Spacing::Alone, Spacing::Alone) | (Spacing::Joint, Spacing::Joint) => true, _ => false, } @@ -98,7 +98,7 @@ impl<'a> PartialEq for TokenTreeHelper<'a> { (&TokenTree::Literal(ref l1), &TokenTree::Literal(ref l2)) => { l1.to_string() == l2.to_string() } - (&TokenTree::Term(ref s1), &TokenTree::Term(ref s2)) => s1.as_str() == s2.as_str(), + (&TokenTree::Ident(ref s1), &TokenTree::Ident(ref s2)) => s1 == s2, _ => false, } } @@ -124,16 +124,16 @@ impl<'a> Hash for TokenTreeHelper<'a> { } 0xffu8.hash(h); // terminator w/ a variant we don't normally hash } - TokenTree::Op(ref op) => { + TokenTree::Punct(ref op) => { 1u8.hash(h); - op.op().hash(h); + op.as_char().hash(h); match op.spacing() { Spacing::Alone => 0u8.hash(h), Spacing::Joint => 1u8.hash(h), } } TokenTree::Literal(ref lit) => (2u8, lit.to_string()).hash(h), - TokenTree::Term(ref word) => (3u8, word.as_str()).hash(h), + TokenTree::Ident(ref word) => (3u8, word).hash(h), } } } diff --git a/src/ty.rs b/src/ty.rs index fa3900adb5..3ca817bbee 100644 --- a/src/ty.rs +++ b/src/ty.rs @@ -641,10 +641,11 @@ pub mod parsing { #[cfg(feature = "printing")] mod printing { use super::*; - use quote::{ToTokens, Tokens}; + use quote::ToTokens; + use proc_macro2::TokenStream; impl ToTokens for TypeSlice { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.bracket_token.surround(tokens, |tokens| { self.elem.to_tokens(tokens); }); @@ -652,7 +653,7 @@ mod printing { } impl ToTokens for TypeArray { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.bracket_token.surround(tokens, |tokens| { self.elem.to_tokens(tokens); self.semi_token.to_tokens(tokens); @@ -662,7 +663,7 @@ mod printing { } impl ToTokens for TypePtr { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.star_token.to_tokens(tokens); match self.mutability { Some(ref tok) => tok.to_tokens(tokens), @@ -675,7 +676,7 @@ mod printing { } impl ToTokens for TypeReference { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.and_token.to_tokens(tokens); self.lifetime.to_tokens(tokens); self.mutability.to_tokens(tokens); @@ -684,7 +685,7 @@ mod printing { } impl ToTokens for TypeBareFn { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.lifetimes.to_tokens(tokens); self.unsafety.to_tokens(tokens); self.abi.to_tokens(tokens); @@ -704,13 +705,13 @@ mod printing { } impl ToTokens for TypeNever { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.bang_token.to_tokens(tokens); } } impl ToTokens for TypeTuple { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.paren_token.surround(tokens, |tokens| { self.elems.to_tokens(tokens); }); @@ -718,27 +719,27 @@ mod printing { } impl ToTokens for TypePath { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { PathTokens(&self.qself, &self.path).to_tokens(tokens); } } impl ToTokens for TypeTraitObject { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.dyn_token.to_tokens(tokens); self.bounds.to_tokens(tokens); } } impl ToTokens for TypeImplTrait { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.impl_token.to_tokens(tokens); self.bounds.to_tokens(tokens); } } impl ToTokens for TypeGroup { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.group_token.surround(tokens, |tokens| { self.elem.to_tokens(tokens); }); @@ -746,7 +747,7 @@ mod printing { } impl ToTokens for TypeParen { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.paren_token.surround(tokens, |tokens| { self.elem.to_tokens(tokens); }); @@ -754,25 +755,25 @@ mod printing { } impl ToTokens for TypeInfer { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.underscore_token.to_tokens(tokens); } } impl ToTokens for TypeMacro { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.mac.to_tokens(tokens); } } impl ToTokens for TypeVerbatim { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.tts.to_tokens(tokens); } } impl ToTokens for ReturnType { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { match *self { ReturnType::Default => {} ReturnType::Type(ref arrow, ref ty) => { @@ -784,7 +785,7 @@ mod printing { } impl ToTokens for BareFnArg { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { if let Some((ref name, ref colon)) = self.name { name.to_tokens(tokens); colon.to_tokens(tokens); @@ -794,7 +795,7 @@ mod printing { } impl ToTokens for BareFnArgName { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { match *self { BareFnArgName::Named(ref t) => t.to_tokens(tokens), BareFnArgName::Wild(ref t) => t.to_tokens(tokens), @@ -803,7 +804,7 @@ mod printing { } impl ToTokens for Abi { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.extern_token.to_tokens(tokens); self.name.to_tokens(tokens); } diff --git a/tests/common/mod.rs b/tests/common/mod.rs index cc7039ccdf..508389f765 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -21,8 +21,13 @@ pub mod parse; pub mod respan; pub fn check_min_stack() { - let min_stack_value = env::var("RUST_MIN_STACK") - .expect("RUST_MIN_STACK env var should be set since some tests require it."); + let min_stack_value = match env::var("RUST_MIN_STACK") { + Ok(s) => s, + Err(_) => { + env::set_var("RUST_MIN_STACK", 16000000.to_string()); + return + } + }; let min_stack_value: usize = min_stack_value .parse() .expect("RUST_MIN_STACK env var should be set since some tests require it."); diff --git a/tests/test_derive_input.rs b/tests/test_derive_input.rs index 553871b46d..6e64d20219 100644 --- a/tests/test_derive_input.rs +++ b/tests/test_derive_input.rs @@ -12,8 +12,7 @@ extern crate proc_macro2; extern crate syn; use proc_macro2::Delimiter::{Brace, Parenthesis}; -use proc_macro2::Group; -use proc_macro2::{Delimiter, Literal, Op, Spacing, Span, Term, TokenStream, TokenTree}; +use proc_macro2::*; use syn::*; use std::iter::FromIterator; @@ -22,15 +21,19 @@ use std::iter::FromIterator; mod macros; fn op(c: char) -> TokenTree { - Op::new(c, Spacing::Alone).into() + Punct::new(c, Spacing::Alone).into() } fn lit>(t: T) -> TokenTree { t.into().into() } +fn ident(sym: &str) -> Ident { + Ident::new(sym, Span::call_site()) +} + fn word(sym: &str) -> TokenTree { - Term::new(sym, Span::call_site()).into() + ident(sym).into() } fn delimited(delim: Delimiter, tokens: Vec) -> TokenTree { @@ -42,7 +45,7 @@ fn test_unit() { let raw = "struct Unit;"; let expected = DeriveInput { - ident: "Unit".into(), + ident: ident("Unit"), vis: Visibility::Inherited, attrs: Vec::new(), generics: Generics::default(), @@ -67,7 +70,7 @@ fn test_struct() { "; let expected = DeriveInput { - ident: "Item".into(), + ident: ident("Item"), vis: Visibility::Public(VisPublic { pub_token: Default::default(), }), @@ -75,7 +78,7 @@ fn test_struct() { bracket_token: Default::default(), pound_token: Default::default(), style: AttrStyle::Outer, - path: "derive".into(), + path: ident("derive").into(), tts: TokenStream::from_iter(vec![delimited( Parenthesis, vec![word("Debug"), op(','), word("Clone")], @@ -90,7 +93,7 @@ fn test_struct() { brace_token: Default::default(), named: punctuated![ Field { - ident: Some("ident".into()), + ident: Some(ident("ident")), colon_token: Some(Default::default()), vis: Visibility::Public(VisPublic { pub_token: Default::default(), @@ -98,11 +101,11 @@ fn test_struct() { attrs: Vec::new(), ty: TypePath { qself: None, - path: "Ident".into(), + path: ident("Ident").into(), }.into(), }, Field { - ident: Some("attrs".into()), + ident: Some(ident("attrs")), colon_token: Some(Default::default()), vis: Visibility::Public(VisPublic { pub_token: Default::default(), @@ -113,7 +116,7 @@ fn test_struct() { path: Path { leading_colon: None, segments: punctuated![PathSegment { - ident: "Vec".into(), + ident: ident("Vec"), arguments: PathArguments::AngleBracketed( AngleBracketedGenericArguments { colon2_token: None, @@ -121,7 +124,7 @@ fn test_struct() { args: punctuated![GenericArgument::Type(Type::from( TypePath { qself: None, - path: "Attribute".into(), + path: ident("Attribute").into(), } )),], gt_token: Default::default(), @@ -141,11 +144,11 @@ fn test_struct() { assert_eq!(expected, actual); let expected_meta_item: Meta = MetaList { - ident: "derive".into(), + ident: ident("derive"), paren_token: Default::default(), nested: punctuated![ - NestedMeta::Meta(Meta::Word("Debug".into())), - NestedMeta::Meta(Meta::Word("Clone".into())), + NestedMeta::Meta(Meta::Word(ident("Debug"))), + NestedMeta::Meta(Meta::Word(ident("Clone"))), ], }.into(); @@ -173,7 +176,7 @@ fn test_enum() { "#; let expected = DeriveInput { - ident: "Result".into(), + ident: ident("Result"), vis: Visibility::Public(VisPublic { pub_token: Default::default(), }), @@ -182,7 +185,7 @@ fn test_enum() { bracket_token: Default::default(), pound_token: Default::default(), style: AttrStyle::Outer, - path: "doc".into(), + path: ident("doc").into(), tts: TokenStream::from_iter(vec![ op('='), lit(Literal::string( @@ -195,7 +198,7 @@ fn test_enum() { bracket_token: Default::default(), pound_token: Default::default(), style: AttrStyle::Outer, - path: "must_use".into(), + path: ident("must_use").into(), tts: TokenStream::empty(), is_sugared_doc: false, }, @@ -205,7 +208,7 @@ fn test_enum() { params: punctuated![ GenericParam::Type(TypeParam { attrs: Vec::new(), - ident: "T".into(), + ident: ident("T"), bounds: Default::default(), default: None, colon_token: None, @@ -213,7 +216,7 @@ fn test_enum() { }), GenericParam::Type(TypeParam { attrs: Vec::new(), - ident: "E".into(), + ident: ident("E"), bounds: Default::default(), colon_token: None, eq_token: None, @@ -226,7 +229,7 @@ fn test_enum() { data: Data::Enum(DataEnum { variants: punctuated![ Variant { - ident: "Ok".into(), + ident: ident("Ok"), attrs: Vec::new(), fields: Fields::Unnamed(FieldsUnnamed { paren_token: Default::default(), @@ -237,14 +240,14 @@ fn test_enum() { attrs: Vec::new(), ty: TypePath { qself: None, - path: "T".into(), + path: ident("T").into(), }.into(), },], }), discriminant: None, }, Variant { - ident: "Err".into(), + ident: ident("Err"), attrs: Vec::new(), fields: Fields::Unnamed(FieldsUnnamed { paren_token: Default::default(), @@ -255,14 +258,14 @@ fn test_enum() { attrs: Vec::new(), ty: TypePath { qself: None, - path: "E".into(), + path: ident("E").into(), }.into(), },], }), discriminant: None, }, Variant { - ident: "Surprise".into(), + ident: ident("Surprise"), attrs: Vec::new(), fields: Fields::Unit, discriminant: Some(( @@ -274,7 +277,7 @@ fn test_enum() { )), }, Variant { - ident: "ProcMacroHack".into(), + ident: ident("ProcMacroHack"), attrs: Vec::new(), fields: Fields::Unit, discriminant: Some(( @@ -319,14 +322,14 @@ fn test_enum() { let expected_meta_items = vec![ MetaNameValue { - ident: "doc".into(), + ident: ident("doc"), eq_token: Default::default(), lit: Lit::Str(LitStr::new( " See the std::result module documentation for details.", Span::call_site(), )), }.into(), - Meta::Word("must_use".into()), + Meta::Word(ident("must_use")), ]; let actual_meta_items: Vec<_> = actual @@ -347,7 +350,7 @@ fn test_attr_with_path() { "#; let expected = DeriveInput { - ident: "Dummy".into(), + ident: ident("Dummy"), vis: Visibility::Inherited, attrs: vec![Attribute { bracket_token: Default::default(), @@ -356,8 +359,8 @@ fn test_attr_with_path() { path: Path { leading_colon: Some(Default::default()), segments: punctuated![ - PathSegment::from("attr_args"), - PathSegment::from("identity"), + PathSegment::from(ident("attr_args")), + PathSegment::from(ident("identity")), ], }, tts: TokenStream::from_iter(vec![ @@ -407,7 +410,7 @@ fn test_attr_with_non_mod_style_path() { "#; let expected = DeriveInput { - ident: "S".into(), + ident: ident("S"), vis: Visibility::Inherited, attrs: vec![Attribute { bracket_token: Default::default(), @@ -415,7 +418,7 @@ fn test_attr_with_non_mod_style_path() { style: AttrStyle::Outer, path: Path { leading_colon: None, - segments: punctuated![PathSegment::from("inert")], + segments: punctuated![PathSegment::from(ident("inert"))], }, tts: TokenStream::from_iter(vec![op('<'), word("T"), op('>')]), is_sugared_doc: false, @@ -443,7 +446,7 @@ fn test_attr_with_mod_style_path_with_self() { "#; let expected = DeriveInput { - ident: "S".into(), + ident: ident("S"), vis: Visibility::Inherited, attrs: vec![Attribute { bracket_token: Default::default(), @@ -451,7 +454,10 @@ fn test_attr_with_mod_style_path_with_self() { style: AttrStyle::Outer, path: Path { leading_colon: None, - segments: punctuated![PathSegment::from("foo"), PathSegment::from("self")], + segments: punctuated![ + PathSegment::from(ident("foo")), + PathSegment::from(ident("self")), + ], }, tts: TokenStream::empty(), is_sugared_doc: false, @@ -479,9 +485,9 @@ fn test_pub_restricted() { "#; let expected = DeriveInput { - ident: "Z".into(), + ident: ident("Z"), vis: Visibility::Restricted(VisRestricted { - path: Box::new("m".into()), + path: Box::new(ident("m").into()), in_token: Some(Default::default()), paren_token: Default::default(), pub_token: Default::default(), @@ -496,7 +502,10 @@ fn test_pub_restricted() { vis: Visibility::Restricted(VisRestricted { path: Box::new(Path { leading_colon: None, - segments: punctuated![PathSegment::from("m"), PathSegment::from("n")], + segments: punctuated![ + PathSegment::from(ident("m")), + PathSegment::from(ident("n")), + ], }), in_token: Some(Default::default()), paren_token: Default::default(), @@ -506,7 +515,7 @@ fn test_pub_restricted() { attrs: vec![], ty: TypePath { qself: None, - path: "u8".into(), + path: ident("u8").into(), }.into(), },], }), @@ -527,7 +536,7 @@ fn test_vis_crate() { "#; let expected = DeriveInput { - ident: "S".into(), + ident: ident("S"), vis: Visibility::Crate(VisCrate { crate_token: Default::default(), }), @@ -552,12 +561,12 @@ fn test_pub_restricted_crate() { "#; let expected = DeriveInput { - ident: "S".into(), + ident: ident("S"), vis: Visibility::Restricted(VisRestricted { pub_token: Default::default(), paren_token: Default::default(), in_token: None, - path: Box::new("crate".into()), + path: Box::new(ident("crate").into()), }), attrs: vec![], generics: Generics::default(), @@ -580,9 +589,9 @@ fn test_pub_restricted_super() { "#; let expected = DeriveInput { - ident: "S".into(), + ident: ident("S"), vis: Visibility::Restricted(VisRestricted { - path: Box::new("super".into()), + path: Box::new(ident("super").into()), in_token: None, paren_token: Default::default(), pub_token: Default::default(), @@ -608,9 +617,9 @@ fn test_pub_restricted_in_super() { "#; let expected = DeriveInput { - ident: "S".into(), + ident: ident("S"), vis: Visibility::Restricted(VisRestricted { - path: Box::new("super".into()), + path: Box::new(ident("super").into()), in_token: Some(Default::default()), paren_token: Default::default(), pub_token: Default::default(), @@ -655,7 +664,7 @@ fn test_fields_on_named_struct() { Field { attrs: vec![], vis: Visibility::Inherited, - ident: Some(Ident::from("foo")), + ident: Some(ident("foo")), colon_token: Some(Default::default()), ty: syn::parse_str("i32").unwrap(), }, @@ -664,7 +673,7 @@ fn test_fields_on_named_struct() { vis: Visibility::Public(VisPublic { pub_token: Default::default(), }), - ident: Some(Ident::from("bar")), + ident: Some(ident("bar")), colon_token: Some(Default::default()), ty: syn::parse_str("String").unwrap(), }, diff --git a/tests/test_expr.rs b/tests/test_expr.rs index f761618d08..304ebfbeda 100644 --- a/tests/test_expr.rs +++ b/tests/test_expr.rs @@ -9,7 +9,9 @@ #![cfg(feature = "extra-traits")] extern crate syn; +extern crate proc_macro2; use syn::*; +use proc_macro2::*; macro_rules! assert_let { ($p:pat = $e:expr) => { @@ -116,7 +118,8 @@ fn test_catch_expr() { assert_let!(Stmt::Expr(ref expr) = block.stmts[3]; { assert_let!(Expr::While(ExprWhile { ref cond, .. }) = *expr; { assert_let!(Expr::Path(ExprPath { qself: None, ref path, .. }) = **cond; { - assert_eq!(*path, "catch".into()); + let name = Ident::new("catch", Span::call_site()); + assert_eq!(*path, name.into()); }); }); }); @@ -124,12 +127,14 @@ fn test_catch_expr() { assert_let!(Stmt::Semi(ref expr, _) = block.stmts[5]; { assert_let!(Expr::Assign(ExprAssign { ref left, ref right, .. }) = *expr; { assert_let!(Expr::Path(ExprPath { qself: None, ref path, .. }) = **left; { - assert_eq!(*path, "catch".into()); + let name = Ident::new("catch", Span::call_site()); + assert_eq!(*path, name.into()); }); assert_let!(Expr::If(ExprIf { ref cond, .. }) = **right; { assert_let!(Expr::Path(ExprPath { qself: None, ref path, .. }) = **cond; { - assert_eq!(*path, "catch".into()); + let name = Ident::new("catch", Span::call_site()); + assert_eq!(*path, name.into()); }); }); }); @@ -138,7 +143,8 @@ fn test_catch_expr() { assert_let!(Stmt::Semi(ref expr, _) = block.stmts[7]; { assert_let!(Expr::Match(ExprMatch { ref expr, .. }) = *expr; { assert_let!(Expr::Path(ExprPath { qself: None, ref path, .. }) = **expr; { - assert_eq!(*path, "catch".into()); + let name = Ident::new("catch", Span::call_site()); + assert_eq!(*path, name.into()); }); }); }); diff --git a/tests/test_generics.rs b/tests/test_generics.rs index bdeb1719a1..6dd10afacb 100644 --- a/tests/test_generics.rs +++ b/tests/test_generics.rs @@ -16,13 +16,17 @@ use syn::*; extern crate quote; extern crate proc_macro2; -use proc_macro2::{Span, TokenStream}; +use proc_macro2::{Span, TokenStream, Ident}; #[macro_use] mod macros; mod common; +fn ident(s: &str) -> Ident { + Ident::new(s, Span::call_site()) +} + #[test] fn test_split_for_impl() { // <'a, 'b: 'a, #[may_dangle] T: 'a = ()> where T: Debug @@ -47,11 +51,11 @@ fn test_split_for_impl() { bracket_token: Default::default(), pound_token: Default::default(), style: AttrStyle::Outer, - path: "may_dangle".into(), + path: ident("may_dangle").into(), tts: TokenStream::empty(), is_sugared_doc: false, }], - ident: "T".into(), + ident: ident("T"), bounds: punctuated![TypeParamBound::Lifetime(Lifetime::new( "'a", Span::call_site() @@ -73,13 +77,13 @@ fn test_split_for_impl() { colon_token: Default::default(), bounded_ty: TypePath { qself: None, - path: "T".into(), + path: ident("T").into(), }.into(), bounds: punctuated![TypeParamBound::Trait(TraitBound { paren_token: None, modifier: TraitBoundModifier::None, lifetimes: None, - path: "Debug".into(), + path: ident("Debug").into(), }),], }),], }), @@ -114,6 +118,7 @@ fn test_ty_param_bound() { ); let tokens = quote!('_); + println!("{:?}", tokens); let expected = TypeParamBound::Lifetime(Lifetime::new("'_", Span::call_site())); assert_eq!( expected, @@ -125,7 +130,7 @@ fn test_ty_param_bound() { paren_token: None, modifier: TraitBoundModifier::None, lifetimes: None, - path: "Debug".into(), + path: ident("Debug").into(), }); assert_eq!( expected, @@ -137,7 +142,7 @@ fn test_ty_param_bound() { paren_token: None, modifier: TraitBoundModifier::Maybe(Default::default()), lifetimes: None, - path: "Sized".into(), + path: ident("Sized").into(), }); assert_eq!( expected, diff --git a/tests/test_grouping.rs b/tests/test_grouping.rs index fde9d5a301..a25c99fb90 100644 --- a/tests/test_grouping.rs +++ b/tests/test_grouping.rs @@ -37,17 +37,17 @@ fn lit>(t: T) -> Expr { fn test_grouping() { let raw: TokenStream = vec![ TokenTree::Literal(Literal::i32_suffixed(1)), - TokenTree::Op(Op::new('+', Spacing::Alone)), + TokenTree::Punct(Punct::new('+', Spacing::Alone)), TokenTree::Group(proc_macro2::Group::new( Delimiter::None, vec![ TokenTree::Literal(Literal::i32_suffixed(2)), - TokenTree::Op(Op::new('+', Spacing::Alone)), + TokenTree::Punct(Punct::new('+', Spacing::Alone)), TokenTree::Literal(Literal::i32_suffixed(3)), ].into_iter() .collect(), )), - TokenTree::Op(Op::new('*', Spacing::Alone)), + TokenTree::Punct(Punct::new('*', Spacing::Alone)), TokenTree::Literal(Literal::i32_suffixed(4)), ].into_iter() .collect(); @@ -83,17 +83,17 @@ fn test_grouping() { fn test_invalid_grouping() { let raw: TokenStream = vec![ TokenTree::Literal(Literal::i32_suffixed(1)), - TokenTree::Op(Op::new('+', Spacing::Alone)), + TokenTree::Punct(Punct::new('+', Spacing::Alone)), TokenTree::Group(proc_macro2::Group::new( Delimiter::None, vec![ TokenTree::Literal(Literal::i32_suffixed(2)), - TokenTree::Op(Op::new('+', Spacing::Alone)), + TokenTree::Punct(Punct::new('+', Spacing::Alone)), ].into_iter() .collect(), )), TokenTree::Literal(Literal::i32_suffixed(3)), - TokenTree::Op(Op::new('*', Spacing::Alone)), + TokenTree::Punct(Punct::new('*', Spacing::Alone)), TokenTree::Literal(Literal::i32_suffixed(4)), ].into_iter() .collect(); diff --git a/tests/test_ident.rs b/tests/test_ident.rs index 7a88f5341e..cb0b2f414a 100644 --- a/tests/test_ident.rs +++ b/tests/test_ident.rs @@ -9,10 +9,9 @@ extern crate proc_macro2; extern crate syn; -use proc_macro2::{Span, TokenStream}; +use proc_macro2::{Span, TokenStream, Ident}; use std::str::FromStr; use syn::synom::ParseError; -use syn::Ident; fn parse(s: &str) -> Result { syn::parse2(TokenStream::from_str(s).unwrap()) @@ -68,13 +67,13 @@ fn ident_new_keyword() { } #[test] -#[should_panic(expected = "ident is not allowed to be empty; use Option")] +#[should_panic(expected = "use Option")] fn ident_new_empty() { new(""); } #[test] -#[should_panic(expected = "ident is not allowed to be a lifetime; use syn::Lifetime")] +#[should_panic(expected = "not a valid Ident")] fn ident_new_lifetime() { new("'static"); } @@ -85,13 +84,13 @@ fn ident_new_underscore() { } #[test] -#[should_panic(expected = "ident cannot be a number, use syn::Index instead")] +#[should_panic(expected = "use Literal instead")] fn ident_new_number() { new("255"); } #[test] -#[should_panic(expected = "\"a#\" is not a valid ident")] +#[should_panic(expected = "\"a#\" is not a valid Ident")] fn ident_new_invalid() { new("a#"); } diff --git a/tests/test_lit.rs b/tests/test_lit.rs index b974b3051d..b0e4004e5f 100644 --- a/tests/test_lit.rs +++ b/tests/test_lit.rs @@ -33,7 +33,7 @@ fn strings() { match lit(s) { Lit::Str(lit) => { assert_eq!(lit.value(), value); - let again = lit.into_tokens().to_string(); + let again = lit.into_token_stream().to_string(); if again != s { test_string(&again, value); } @@ -64,7 +64,7 @@ fn byte_strings() { match lit(s) { Lit::ByteStr(lit) => { assert_eq!(lit.value(), value); - let again = lit.into_tokens().to_string(); + let again = lit.into_token_stream().to_string(); if again != s { test_byte_string(&again, value); } @@ -93,7 +93,7 @@ fn bytes() { match lit(s) { Lit::Byte(lit) => { assert_eq!(lit.value(), value); - let again = lit.into_tokens().to_string(); + let again = lit.into_token_stream().to_string(); assert_eq!(again, s); } wrong => panic!("{:?}", wrong), @@ -114,7 +114,7 @@ fn chars() { match lit(s) { Lit::Char(lit) => { assert_eq!(lit.value(), value); - let again = lit.into_tokens().to_string(); + let again = lit.into_token_stream().to_string(); if again != s { test_char(&again, value); } @@ -140,7 +140,7 @@ fn ints() { Lit::Int(lit) => { assert_eq!(lit.value(), value); assert_eq!(lit.suffix(), suffix); - let again = lit.into_tokens().to_string(); + let again = lit.into_token_stream().to_string(); if again != s { test_int(&again, value, suffix); } @@ -178,7 +178,7 @@ fn floats() { Lit::Float(lit) => { assert_eq!(lit.value(), value); assert_eq!(lit.suffix(), suffix); - let again = lit.into_tokens().to_string(); + let again = lit.into_token_stream().to_string(); if again != s { test_float(&again, value, suffix); } diff --git a/tests/test_meta_item.rs b/tests/test_meta_item.rs index 55eefb8e6e..1c2d4e3529 100644 --- a/tests/test_meta_item.rs +++ b/tests/test_meta_item.rs @@ -11,7 +11,7 @@ extern crate proc_macro2; extern crate syn; -use proc_macro2::{Literal, Span, TokenStream}; +use proc_macro2::{Literal, Span, TokenStream, Ident}; use syn::buffer::TokenBuffer; use syn::*; @@ -22,9 +22,13 @@ fn lit>(t: T) -> Lit { Lit::new(t.into()) } +fn ident(s: &str) -> Ident { + Ident::new(s, Span::call_site()) +} + #[test] fn test_meta_item_word() { - run_test("#[foo]", Meta::Word("foo".into())) + run_test("#[foo]", Meta::Word(ident("foo"))) } #[test] @@ -32,7 +36,7 @@ fn test_meta_item_name_value() { run_test( "#[foo = 5]", MetaNameValue { - ident: "foo".into(), + ident: ident("foo").into(), eq_token: Default::default(), lit: lit(Literal::i32_unsuffixed(5)), }, @@ -44,7 +48,7 @@ fn test_meta_item_bool_value() { run_test( "#[foo = true]", MetaNameValue { - ident: "foo".into(), + ident: ident("foo").into(), eq_token: Default::default(), lit: Lit::Bool(LitBool { value: true, @@ -55,7 +59,7 @@ fn test_meta_item_bool_value() { run_test( "#[foo = false]", MetaNameValue { - ident: "foo".into(), + ident: ident("foo").into(), eq_token: Default::default(), lit: Lit::Bool(LitBool { value: false, @@ -70,7 +74,7 @@ fn test_meta_item_list_lit() { run_test( "#[foo(5)]", MetaList { - ident: "foo".into(), + ident: ident("foo").into(), paren_token: Default::default(), nested: punctuated![NestedMeta::Literal(lit(Literal::i32_unsuffixed(5)))], }, @@ -82,9 +86,9 @@ fn test_meta_item_list_word() { run_test( "#[foo(bar)]", MetaList { - ident: "foo".into(), + ident: ident("foo").into(), paren_token: Default::default(), - nested: punctuated![NestedMeta::Meta(Meta::Word("bar".into()))], + nested: punctuated![NestedMeta::Meta(Meta::Word(ident("bar").into()))], }, ) } @@ -94,11 +98,11 @@ fn test_meta_item_list_name_value() { run_test( "#[foo(bar = 5)]", MetaList { - ident: "foo".into(), + ident: ident("foo").into(), paren_token: Default::default(), nested: punctuated![NestedMeta::Meta( MetaNameValue { - ident: "bar".into(), + ident: ident("bar").into(), eq_token: Default::default(), lit: lit(Literal::i32_unsuffixed(5)), }.into(), @@ -112,11 +116,11 @@ fn test_meta_item_list_bool_value() { run_test( "#[foo(bar = true)]", MetaList { - ident: "foo".into(), + ident: ident("foo").into(), paren_token: Default::default(), nested: punctuated![NestedMeta::Meta( MetaNameValue { - ident: "bar".into(), + ident: ident("bar").into(), eq_token: Default::default(), lit: Lit::Bool(LitBool { value: true, @@ -133,31 +137,31 @@ fn test_meta_item_multiple() { run_test( "#[foo(word, name = 5, list(name2 = 6), word2)]", MetaList { - ident: "foo".into(), + ident: ident("foo").into(), paren_token: Default::default(), nested: punctuated![ - NestedMeta::Meta(Meta::Word("word".into())), + NestedMeta::Meta(Meta::Word(ident("word").into())), NestedMeta::Meta( MetaNameValue { - ident: "name".into(), + ident: ident("name").into(), eq_token: Default::default(), lit: lit(Literal::i32_unsuffixed(5)), }.into(), ), NestedMeta::Meta( MetaList { - ident: "list".into(), + ident: ident("list").into(), paren_token: Default::default(), nested: punctuated![NestedMeta::Meta( MetaNameValue { - ident: "name2".into(), + ident: ident("name2").into(), eq_token: Default::default(), lit: lit(Literal::i32_unsuffixed(6)), }.into(), ),], }.into(), ), - NestedMeta::Meta(Meta::Word("word2".into())), + NestedMeta::Meta(Meta::Word(ident("word2").into())), ], }, ) diff --git a/tests/test_round_trip.rs b/tests/test_round_trip.rs index 92e84e5fab..4659be40d8 100644 --- a/tests/test_round_trip.rs +++ b/tests/test_round_trip.rs @@ -120,10 +120,10 @@ fn test_round_trip() { true } else { errorf!( - "=== {}: FAIL\nbefore: {}\nafter: {}\n", + "=== {}: FAIL\nbefore: {:#?}\nafter: {:#?}\n", path.display(), - format!("{:?}", before).replace("\n", ""), - format!("{:?}", after).replace("\n", "") + before, + after, ); false } diff --git a/tests/test_token_trees.rs b/tests/test_token_trees.rs index 1bd9d1c6bb..c385727f6e 100644 --- a/tests/test_token_trees.rs +++ b/tests/test_token_trees.rs @@ -14,15 +14,15 @@ extern crate quote; extern crate syn; use proc_macro2::Delimiter::*; -use proc_macro2::{Delimiter, Group, Op, Spacing, Span, Term, TokenStream, TokenTree}; +use proc_macro2::*; use syn::{AttrStyle, Attribute, Lit}; fn alone(c: char) -> TokenTree { - Op::new(c, Spacing::Alone).into() + Punct::new(c, Spacing::Alone).into() } fn joint(c: char) -> TokenTree { - Op::new(c, Spacing::Joint).into() + Punct::new(c, Spacing::Joint).into() } fn delimited(delim: Delimiter, tokens: Vec) -> TokenTree { @@ -30,7 +30,7 @@ fn delimited(delim: Delimiter, tokens: Vec) -> TokenTree { } fn word(sym: &str) -> TokenTree { - Term::new(sym, Span::call_site()).into() + Ident::new(sym, Span::call_site()).into() } #[test] @@ -80,7 +80,7 @@ fn test_struct() { style: AttrStyle::Outer, pound_token: Default::default(), bracket_token: Default::default(), - path: "test".into(), + path: Ident::new("test", Span::call_site()).into(), tts, is_sugared_doc: false, }