diff --git a/Cargo.toml b/Cargo.toml index 2eb0a2152f..674c176af5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,8 +27,9 @@ clone-impls = [] extra-traits = [] [dependencies] -quote = { version = "0.4", optional = true } -proc-macro2 = "0.2" +proc-macro2 = "0.3" +#quote = { version = "0.5", optional = true } +quote = { git = 'https://github.com/dtolnay/quote', optional = true } unicode-xid = "0.1" [dev-dependencies] diff --git a/codegen/Cargo.toml b/codegen/Cargo.toml index 93ebe16674..db4c2a6261 100644 --- a/codegen/Cargo.toml +++ b/codegen/Cargo.toml @@ -7,7 +7,8 @@ publish = false # this is an internal crate which should never be published [dependencies] syn = { path = "..", features = ["full", "extra-traits"] } -quote = "0.4" +#quote = "0.5" +quote = { git = 'https://github.com/dtolnay/quote' } failure = "0.1" inflections = "1.1" -proc-macro2 = "0.2" +proc-macro2 = "0.3" diff --git a/codegen/src/main.rs b/codegen/src/main.rs index ac02cbcff1..ac5a426225 100644 --- a/codegen/src/main.rs +++ b/codegen/src/main.rs @@ -1035,7 +1035,8 @@ pub trait Fold {{ macro_rules! fold_span_only {{ ($f:ident : $t:ident) => {{ pub fn $f(_visitor: &mut V, mut _i: $t) -> $t {{ - _i.span = _visitor.fold_span(_i.span); + let span = _visitor.fold_span(_i.span()); + _i.set_span(span); _i }} }} diff --git a/examples/heapsize/heapsize_derive/Cargo.toml b/examples/heapsize/heapsize_derive/Cargo.toml index 706b515456..1ebf4ae524 100644 --- a/examples/heapsize/heapsize_derive/Cargo.toml +++ b/examples/heapsize/heapsize_derive/Cargo.toml @@ -9,4 +9,5 @@ proc-macro = true [dependencies] syn = { path = "../../.." } -quote = "0.4" +#quote = "0.5" +quote = { git = 'https://github.com/dtolnay/quote' } diff --git a/examples/heapsize/heapsize_derive/src/lib.rs b/examples/heapsize/heapsize_derive/src/lib.rs index acf1e27083..60577c09c4 100644 --- a/examples/heapsize/heapsize_derive/src/lib.rs +++ b/examples/heapsize/heapsize_derive/src/lib.rs @@ -1,4 +1,6 @@ extern crate proc_macro; + +#[macro_use] extern crate syn; #[macro_use] @@ -40,8 +42,7 @@ pub fn derive_heap_size(input: TokenStream) -> TokenStream { fn add_trait_bounds(mut generics: Generics) -> Generics { for param in &mut generics.params { if let GenericParam::Type(ref mut type_param) = *param { - let bound = syn::parse_str("::heapsize::HeapSize").unwrap(); - type_param.bounds.push(bound); + type_param.bounds.push(parse_quote!(::heapsize::HeapSize)); } } generics diff --git a/examples/heapsize2/example/src/main.rs b/examples/heapsize2/example/src/main.rs index 628639ae00..1c66692c90 100644 --- a/examples/heapsize2/example/src/main.rs +++ b/examples/heapsize2/example/src/main.rs @@ -1,10 +1,8 @@ #[macro_use] extern crate heapsize_derive; -// Demonstrate that hygiene is working correctly by having no `extern crate -// heapsize` in scope here. The macro-generated impl is not for something like -// `::heapsize::HeapSize` like is common in Macros 1.1. It is for the `HeapSize` -// trait brought into scope by the procedural macro definition. +extern crate heapsize; + #[derive(HeapSize)] struct Demo<'a, T: ?Sized> { a: Box, @@ -13,22 +11,12 @@ struct Demo<'a, T: ?Sized> { d: HeapSize, } -// This derive is going to generate `impl HeapSize for HeapSize` which is fine -// because the two `HeapSize` tokens have different hygiene context. The first -// one resolves to the `HeapSize` trait brough into scope by the procedural -// macro definition, and the second one refers to this struct. -// -// Also demonstrate that even though both `impl HeapSize for Demo` and `impl -// HeapSize for HeapSize` spit out a `mod scope`, they do not conflict because -// of hygiene. #[derive(HeapSize)] struct HeapSize { e: String, } fn main() { - extern crate heapsize; - let demo = Demo { a: b"bytestring".to_vec().into_boxed_slice(), b: 255, diff --git a/examples/heapsize2/heapsize_derive/Cargo.toml b/examples/heapsize2/heapsize_derive/Cargo.toml index 75316a3c0f..7132002f5e 100644 --- a/examples/heapsize2/heapsize_derive/Cargo.toml +++ b/examples/heapsize2/heapsize_derive/Cargo.toml @@ -9,5 +9,6 @@ proc-macro = true [dependencies] syn = { path = "../../.." } -quote = "0.4" -proc-macro2 = { version = "0.2", features = ["nightly"] } +#quote = "0.5" +quote = { git = 'https://github.com/dtolnay/quote' } +proc-macro2 = { version = "0.3", features = ["nightly"] } diff --git a/examples/heapsize2/heapsize_derive/src/lib.rs b/examples/heapsize2/heapsize_derive/src/lib.rs index 30ea1b401c..8db8504c0e 100644 --- a/examples/heapsize2/heapsize_derive/src/lib.rs +++ b/examples/heapsize2/heapsize_derive/src/lib.rs @@ -30,19 +30,10 @@ pub fn derive_heap_size(input: TokenStream) -> TokenStream { let sum = heap_size_sum(&input.data, &var); let expanded = quote! { - mod scope { - extern crate heapsize; - use self::heapsize::HeapSize; - - // The generated impl. Okay to use `HeapSize` unqualified here, it - // is guaranteed to resolve to the import on the previous line. This - // works even in edge cases like the user's struct having the name - // `HeapSize` as demonstrated in main.rs, in which case the - // generated code looks like `impl HeapSize for HeapSize`. - impl #impl_generics HeapSize for #name #ty_generics #where_clause { - fn heap_size_of_children(&#var) -> usize { - #sum - } + // The generated impl. + impl #impl_generics ::heapsize::HeapSize for #name #ty_generics #where_clause { + fn heap_size_of_children(&#var) -> usize { + #sum } } }; @@ -55,7 +46,7 @@ pub fn derive_heap_size(input: TokenStream) -> TokenStream { fn add_trait_bounds(mut generics: Generics) -> Generics { for param in &mut generics.params { if let GenericParam::Type(ref mut type_param) = *param { - type_param.bounds.push(parse_quote!(HeapSize)); + type_param.bounds.push(parse_quote!(::heapsize::HeapSize)); } } generics @@ -63,7 +54,6 @@ fn add_trait_bounds(mut generics: Generics) -> Generics { // Generate an expression to sum up the heap size of each field. fn heap_size_sum(data: &Data, var: &Tokens) -> Tokens { - let def_site = Span::def_site(); let call_site = Span::call_site(); match *data { @@ -83,9 +73,8 @@ fn heap_size_sum(data: &Data, var: &Tokens) -> Tokens { let recurse = fields.named.iter().map(|f| { let name = f.ident; let access = quote_spanned!(call_site=> #var.#name); - let span = f.span().resolved_at(def_site); - quote_spanned! {span=> - HeapSize::heap_size_of_children(&#access) + quote_spanned! {f.span()=> + ::heapsize::HeapSize::heap_size_of_children(&#access) } }); quote! { @@ -99,9 +88,8 @@ fn heap_size_sum(data: &Data, var: &Tokens) -> Tokens { let recurse = fields.unnamed.iter().enumerate().map(|(i, f)| { let index = Index { index: i as u32, span: call_site }; let access = quote_spanned!(call_site=> #var.#index); - let span = f.span().resolved_at(def_site); - quote_spanned! {span=> - HeapSize::heap_size_of_children(&#access) + quote_spanned! {f.span()=> + ::heapsize::HeapSize::heap_size_of_children(&#access) } }); quote! { diff --git a/examples/lazy-static/lazy-static/Cargo.toml b/examples/lazy-static/lazy-static/Cargo.toml index fc573a7475..e602cfc618 100644 --- a/examples/lazy-static/lazy-static/Cargo.toml +++ b/examples/lazy-static/lazy-static/Cargo.toml @@ -9,5 +9,6 @@ proc-macro = true [dependencies] syn = { path = "../../../", features = ["full"] } -quote = "0.4" -proc-macro2 = { version = "0.2", features = ["nightly"] } +#quote = "0.5" +quote = { git = 'https://github.com/dtolnay/quote' } +proc-macro2 = { version = "0.3", features = ["nightly"] } diff --git a/examples/lazy-static/lazy-static/src/lib.rs b/examples/lazy-static/lazy-static/src/lib.rs index 1a9dece305..0ae911442c 100644 --- a/examples/lazy-static/lazy-static/src/lib.rs +++ b/examples/lazy-static/lazy-static/src/lib.rs @@ -6,13 +6,11 @@ extern crate syn; #[macro_use] extern crate quote; extern crate proc_macro; -extern crate proc_macro2; use syn::{Visibility, Ident, Type, Expr}; use syn::synom::Synom; use syn::spanned::Spanned; use proc_macro::TokenStream; -use proc_macro2::Span; /// Parses the following syntax, which aligns with the input of the real /// `lazy_static` crate. @@ -51,7 +49,6 @@ impl Synom for LazyStatic { #[proc_macro] pub fn lazy_static(input: TokenStream) -> TokenStream { let LazyStatic { visibility, name, ty, init } = syn::parse(input).unwrap(); - let def_site = Span::def_site(); // The warning looks like this. // @@ -84,20 +81,15 @@ pub fn lazy_static(input: TokenStream) -> TokenStream { // Assert that the static type implements Sync. If not, user sees an error // message like the following. We span this assertion with the field type's - // line/column so that the error message appears in the correct place, but - // resolve it at the def site so that we are guaranteed it is checking the - // correct Sync. If the `Sync` token were resolved at the call site, the - // user could circumvent the check by defining their own Sync trait that is - // implemented for their type. + // line/column so that the error message appears in the correct place. // // error[E0277]: the trait bound `*const (): std::marker::Sync` is not satisfied // --> src/main.rs:10:21 // | // 10 | static ref PTR: *const () = &(); // | ^^^^^^^^^ `*const ()` cannot be shared between threads safely - let ty_span = ty.span().resolved_at(def_site); - let assert_sync = quote_spanned! {ty_span=> - struct _AssertSync where #ty: Sync; + let assert_sync = quote_spanned! {ty.span()=> + struct _AssertSync where #ty: ::std::marker::Sync; }; // Check for Sized. Not vital to check here, but the error message is less @@ -109,28 +101,25 @@ pub fn lazy_static(input: TokenStream) -> TokenStream { // | // 10 | static ref A: str = ""; // | ^^^ `str` does not have a constant size known at compile-time - let assert_sized = quote_spanned! {ty_span=> - struct _AssertSized where #ty: Sized; + let assert_sized = quote_spanned! {ty.span()=> + struct _AssertSized where #ty: ::std::marker::Sized; }; - let init_span = init.span().resolved_at(def_site); - let init_ptr = quote_spanned! {init_span=> + let init_ptr = quote_spanned! {init.span()=> Box::into_raw(Box::new(#init)) }; let expanded = quote! { - extern crate std; - #visibility struct #name; - impl std::ops::Deref for #name { + impl ::std::ops::Deref for #name { type Target = #ty; fn deref(&self) -> &#ty { #assert_sync #assert_sized - static ONCE: std::sync::Once = std::sync::ONCE_INIT; + static ONCE: ::std::sync::Once = ::std::sync::ONCE_INIT; static mut VALUE: *mut #ty = 0 as *mut #ty; unsafe { diff --git a/examples/trace-var/trace-var/Cargo.toml b/examples/trace-var/trace-var/Cargo.toml index ff04107b26..c48f4afe52 100644 --- a/examples/trace-var/trace-var/Cargo.toml +++ b/examples/trace-var/trace-var/Cargo.toml @@ -9,5 +9,6 @@ proc-macro = true [dependencies] syn = { path = "../../../", features = ["full", "fold"] } -quote = "0.4" -proc-macro2 = { version = "0.2", features = ["nightly"] } +#quote = "0.5" +quote = { git = 'https://github.com/dtolnay/quote' } +proc-macro2 = { version = "0.3", features = ["nightly"] } diff --git a/src/attr.rs b/src/attr.rs index 57accef6de..600302ffff 100644 --- a/src/attr.rs +++ b/src/attr.rs @@ -11,7 +11,7 @@ use punctuated::Punctuated; use std::iter; -use proc_macro2::{Delimiter, Spacing, TokenNode, TokenStream, TokenTree}; +use proc_macro2::{Delimiter, Spacing, TokenStream, TokenTree}; #[cfg(feature = "extra-traits")] use std::hash::{Hash, Hasher}; @@ -113,104 +113,104 @@ impl Attribute { let tts = self.tts.clone().into_iter().collect::>(); if tts.len() == 1 { - if let TokenNode::Group(Delimiter::Parenthesis, ref ts) = tts[0].kind { - let tokens = ts.clone().into_iter().collect::>(); - if let Some(nested_meta_items) = list_of_nested_meta_items_from_tokens(&tokens) { - return Some(Meta::List(MetaList { - paren_token: token::Paren(tts[0].span), - ident: *name, - nested: nested_meta_items, - })); - } + if let Some(meta) = Attribute::extract_meta_list(*name, &tts[0]) { + return Some(meta); } } if tts.len() == 2 { - if let TokenNode::Op('=', Spacing::Alone) = tts[0].kind { - if let TokenNode::Literal(ref lit) = tts[1].kind { - if !lit.to_string().starts_with('/') { - return Some(Meta::NameValue(MetaNameValue { - ident: *name, - eq_token: Token![=]([tts[0].span]), - lit: Lit::new(lit.clone(), tts[1].span), - })); - } - } else if let TokenNode::Term(ref term) = tts[1].kind { - match term.as_str() { - v @ "true" | v @ "false" => { - return Some(Meta::NameValue(MetaNameValue { - ident: *name, - eq_token: Token![=]([tts[0].span]), - lit: Lit::Bool(LitBool { value: v == "true", span: tts[1].span }), - })); - }, - _ => {} - } - } + if let Some(meta) = Attribute::extract_name_value(*name, &tts[0], &tts[1]) { + return Some(meta); } } None } + + fn extract_meta_list(ident: Ident, tt: &TokenTree) -> Option { + let g = match *tt { + TokenTree::Group(ref g) => g, + _ => return None, + }; + if g.delimiter() != Delimiter::Parenthesis { + return None + } + let tokens = g.stream().clone().into_iter().collect::>(); + let nested = match list_of_nested_meta_items_from_tokens(&tokens) { + Some(n) => n, + None => return None, + }; + Some(Meta::List(MetaList { + paren_token: token::Paren(g.span()), + ident: ident, + nested: nested, + })) + } + + fn extract_name_value(ident: Ident, a: &TokenTree, b: &TokenTree) -> Option { + let a = match *a { + TokenTree::Op(ref o) => o, + _ => return None, + }; + if a.spacing() != Spacing::Alone { + return None + } + if a.op() != '=' { + return None + } + + match *b { + TokenTree::Literal(ref l) if !l.to_string().starts_with('/') => { + Some(Meta::NameValue(MetaNameValue { + ident: ident, + eq_token: Token![=]([a.span()]), + lit: Lit::new(l.clone()), + })) + } + TokenTree::Term(ref term) => { + match term.as_str() { + v @ "true" | v @ "false" => { + Some(Meta::NameValue(MetaNameValue { + ident: ident, + eq_token: Token![=]([a.span()]), + lit: Lit::Bool(LitBool { + value: v == "true", + span: b.span(), + }), + })) + }, + _ => None , + } + } + _ => None, + } + } } fn nested_meta_item_from_tokens(tts: &[TokenTree]) -> Option<(NestedMeta, &[TokenTree])> { assert!(!tts.is_empty()); - match tts[0].kind { - TokenNode::Literal(ref lit) => { + match tts[0] { + TokenTree::Literal(ref lit) => { if lit.to_string().starts_with('/') { None } else { - let lit = Lit::new(lit.clone(), tts[0].span); + let lit = Lit::new(lit.clone()); Some((NestedMeta::Literal(lit), &tts[1..])) } } - TokenNode::Term(sym) => { - let ident = Ident::new(sym.as_str(), tts[0].span); + TokenTree::Term(sym) => { + let ident = Ident::new(sym.as_str(), sym.span()); if tts.len() >= 3 { - if let TokenNode::Op('=', Spacing::Alone) = tts[1].kind { - if let TokenNode::Literal(ref lit) = tts[2].kind { - if !lit.to_string().starts_with('/') { - let pair = MetaNameValue { - ident: Ident::new(sym.as_str(), tts[0].span), - eq_token: Token![=]([tts[1].span]), - lit: Lit::new(lit.clone(), tts[2].span), - }; - return Some((Meta::NameValue(pair).into(), &tts[3..])); - } - } else if let TokenNode::Term(ref term) = tts[2].kind { - match term.as_str() { - v @ "true" | v @ "false" => { - let pair = MetaNameValue { - ident: Ident::new(sym.as_str(), tts[0].span), - eq_token: Token![=]([tts[1].span]), - lit: Lit::Bool(LitBool { value: v == "true", span: tts[2].span }), - }; - return Some((Meta::NameValue(pair).into(), &tts[3..])); - }, - _ => {} - } - } + if let Some(meta) = Attribute::extract_name_value(ident, &tts[1], &tts[2]) { + return Some((NestedMeta::Meta(meta), &tts[3..])) } } if tts.len() >= 2 { - if let TokenNode::Group(Delimiter::Parenthesis, ref inner_tts) = tts[1].kind { - let inner_tts = inner_tts.clone().into_iter().collect::>(); - return match list_of_nested_meta_items_from_tokens(&inner_tts) { - Some(nested_meta_items) => { - let list = MetaList { - ident: ident, - paren_token: token::Paren(tts[1].span), - nested: nested_meta_items, - }; - Some((Meta::List(list).into(), &tts[2..])) - } - - None => None, - }; + if let Some(meta) = Attribute::extract_meta_list(ident, &tts[1]) { + return Some((NestedMeta::Meta(meta), &tts[2..])) } } @@ -231,8 +231,14 @@ fn list_of_nested_meta_items_from_tokens( let prev_comma = if first { first = false; None - } else if let TokenNode::Op(',', Spacing::Alone) = tts[0].kind { - let tok = Token![,]([tts[0].span]); + } else if let TokenTree::Op(ref op) = tts[0] { + if op.spacing() != Spacing::Alone { + return None + } + if op.op() != ',' { + return None + } + let tok = Token![,]([op.span()]); tts = &tts[1..]; if tts.is_empty() { break; @@ -396,13 +402,12 @@ pub mod parsing { use buffer::Cursor; use parse_error; use synom::PResult; - use proc_macro2::{Literal, Spacing, Span, TokenNode, TokenTree}; + use proc_macro2::{Literal, Spacing, Span, TokenTree, Op}; fn eq(span: Span) -> TokenTree { - TokenTree { - span: span, - kind: TokenNode::Op('=', Spacing::Alone), - } + let mut op = Op::new('=', Spacing::Alone); + op.set_span(span); + op.into() } impl Attribute { @@ -431,7 +436,7 @@ pub mod parsing { map!( call!(lit_doc_comment, Comment::Inner), |lit| { - let span = lit.span; + let span = lit.span(); Attribute { style: AttrStyle::Inner(::new(span)), path: Ident::new("doc", span).into(), @@ -471,7 +476,7 @@ pub mod parsing { map!( call!(lit_doc_comment, Comment::Outer), |lit| { - let span = lit.span; + let span = lit.span(); Attribute { style: AttrStyle::Outer, path: Ident::new("doc", span).into(), @@ -495,20 +500,16 @@ pub mod parsing { fn lit_doc_comment(input: Cursor, style: Comment) -> PResult { match input.literal() { - Some((span, lit, rest)) => { + Some((lit, rest)) => { let string = lit.to_string(); let ok = match style { Comment::Inner => string.starts_with("//!") || string.starts_with("/*!"), Comment::Outer => string.starts_with("///") || string.starts_with("/**"), }; if ok { - Ok(( - TokenTree { - span: span, - kind: TokenNode::Literal(Literal::string(&string)), - }, - rest, - )) + let mut new = Literal::string(&string); + new.set_span(lit.span()); + Ok((new.into(), rest)) } else { parse_error() } @@ -522,25 +523,9 @@ pub mod parsing { mod printing { use super::*; use quote::{ToTokens, Tokens}; - use proc_macro2::Literal; impl ToTokens for Attribute { fn to_tokens(&self, tokens: &mut Tokens) { - // If this was a sugared doc, emit it in its original form instead of `#[doc = "..."]` - if self.is_sugared_doc { - if let Some(Meta::NameValue(ref pair)) = self.interpret_meta() { - if pair.ident == "doc" { - if let Lit::Str(ref comment) = pair.lit { - tokens.append(TokenTree { - span: comment.span, - kind: TokenNode::Literal(Literal::doccomment(&comment.value())), - }); - return; - } - } - } - } - self.pound_token.to_tokens(tokens); if let AttrStyle::Inner(ref b) = self.style { b.to_tokens(tokens); diff --git a/src/buffer.rs b/src/buffer.rs index 6b05d5010d..10ac5f6349 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -128,7 +128,8 @@ // 100% safe but the implementation is fragile internally. use proc_macro as pm; -use proc_macro2::{Delimiter, Literal, Spacing, Span, Term, TokenNode, TokenStream, TokenTree}; +use proc_macro2::{Delimiter, Literal, Span, Term, TokenStream}; +use proc_macro2::{Group, TokenTree, Op}; use std::ptr; use std::marker::PhantomData; @@ -141,9 +142,9 @@ use std::fmt::{self, Debug}; enum Entry { // Mimicking types from proc-macro. Group(Span, Delimiter, TokenBuffer), - Term(Span, Term), - Op(Span, char, Spacing), - Literal(Span, Literal), + Term(Term), + Op(Op), + Literal(Literal), // End entries contain a raw pointer to the entry from the containing // token tree, or null if this is the outermost level. End(*const Entry), @@ -174,20 +175,20 @@ impl TokenBuffer { let mut entries = Vec::new(); let mut seqs = Vec::new(); for tt in stream { - match tt.kind { - TokenNode::Term(sym) => { - entries.push(Entry::Term(tt.span, sym)); + match tt { + TokenTree::Term(sym) => { + entries.push(Entry::Term(sym)); } - TokenNode::Op(chr, ok) => { - entries.push(Entry::Op(tt.span, chr, ok)); + TokenTree::Op(op) => { + entries.push(Entry::Op(op)); } - TokenNode::Literal(lit) => { - entries.push(Entry::Literal(tt.span, lit)); + TokenTree::Literal(l) => { + entries.push(Entry::Literal(l)); } - TokenNode::Group(delim, seq_stream) => { + TokenTree::Group(g) => { // Record the index of the interesting entry, and store an // `End(null)` there temporarially. - seqs.push((entries.len(), tt.span, delim, seq_stream)); + seqs.push((entries.len(), g.span(), g.delimiter(), g.stream().clone())); entries.push(Entry::End(ptr::null())); } } @@ -364,30 +365,30 @@ impl<'a> Cursor<'a> { /// If the cursor is pointing at a `Term`, returns it along with a cursor /// pointing at the next `TokenTree`. - pub fn term(mut self) -> Option<(Span, Term, Cursor<'a>)> { + pub fn term(mut self) -> Option<(Term, Cursor<'a>)> { self.ignore_none(); match *self.entry() { - Entry::Term(span, term) => Some((span, term, unsafe { self.bump() })), + Entry::Term(term) => Some((term, unsafe { self.bump() })), _ => None, } } /// If the cursor is pointing at an `Op`, returns it along with a cursor /// pointing at the next `TokenTree`. - pub fn op(mut self) -> Option<(Span, char, Spacing, Cursor<'a>)> { + pub fn op(mut self) -> Option<(Op, Cursor<'a>)> { self.ignore_none(); match *self.entry() { - Entry::Op(span, op, spacing) => Some((span, op, spacing, unsafe { self.bump() })), + Entry::Op(op) => Some((op, unsafe { self.bump() })), _ => None, } } /// If the cursor is pointing at a `Literal`, return it along with a cursor /// pointing at the next `TokenTree`. - pub fn literal(mut self) -> Option<(Span, Literal, Cursor<'a>)> { + pub fn literal(mut self) -> Option<(Literal, Cursor<'a>)> { self.ignore_none(); match *self.entry() { - Entry::Literal(span, ref lit) => Some((span, lit.clone(), unsafe { self.bump() })), + Entry::Literal(ref lit) => Some((lit.clone(), unsafe { self.bump() })), _ => None, } } @@ -415,23 +416,13 @@ impl<'a> Cursor<'a> { let tree = match *self.entry() { Entry::Group(span, delim, ref buf) => { let stream = buf.begin().token_stream(); - TokenTree { - span: span, - kind: TokenNode::Group(delim, stream), - } + let mut g = Group::new(delim, stream); + g.set_span(span); + TokenTree::from(g) } - Entry::Literal(span, ref lit) => TokenTree { - span: span, - kind: TokenNode::Literal(lit.clone()), - }, - Entry::Term(span, sym) => TokenTree { - span: span, - kind: TokenNode::Term(sym), - }, - Entry::Op(span, chr, spacing) => TokenTree { - span: span, - kind: TokenNode::Op(chr, spacing), - }, + Entry::Literal(ref lit) => lit.clone().into(), + Entry::Term(term) => term.into(), + Entry::Op(op) => op.into(), Entry::End(..) => { return None; } @@ -444,10 +435,10 @@ impl<'a> Cursor<'a> { /// cursor points to eof. pub fn span(self) -> Span { match *self.entry() { - Entry::Group(span, ..) - | Entry::Literal(span, ..) - | Entry::Term(span, ..) - | Entry::Op(span, ..) => span, + Entry::Group(span, ..) => span, + Entry::Literal(ref l) => l.span(), + Entry::Term(t) => t.span(), + Entry::Op(o) => o.span(), Entry::End(..) => Span::call_site(), } } diff --git a/src/expr.rs b/src/expr.rs index d83167028a..2d85e50093 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -639,7 +639,7 @@ impl From for Index { assert!(index < std::u32::MAX as usize); Index { index: index as u32, - span: Span::def_site(), + span: Span::call_site(), } } } @@ -2617,7 +2617,7 @@ pub mod parsing { lit: syn!(LitInt) >> ({ if let IntSuffix::None = lit.suffix() { - Index { index: lit.value() as u32, span: lit.span } + Index { index: lit.value() as u32, span: lit.span() } } else { return parse_error(); } @@ -2813,7 +2813,7 @@ mod printing { #[cfg(feature = "full")] use attr::FilterAttrs; use quote::{ToTokens, Tokens}; - use proc_macro2::{Literal, TokenNode, TokenTree}; + use proc_macro2::Literal; // If the given expression is a bare `ExprStruct`, wraps it in parenthesis // before appending it to `Tokens`. @@ -3182,10 +3182,9 @@ mod printing { impl ToTokens for Index { fn to_tokens(&self, tokens: &mut Tokens) { - tokens.append(TokenTree { - span: self.span, - kind: TokenNode::Literal(Literal::integer(i64::from(self.index))), - }); + let mut lit = Literal::i64_unsuffixed(i64::from(self.index)); + lit.set_span(self.span); + tokens.append(lit); } } diff --git a/src/gen/fold.rs b/src/gen/fold.rs index 46ac7b4da3..46e94b20bc 100644 --- a/src/gen/fold.rs +++ b/src/gen/fold.rs @@ -419,7 +419,8 @@ fn fold_where_predicate(&mut self, i: WherePredicate) -> WherePredicate { fold_w macro_rules! fold_span_only { ($f:ident : $t:ident) => { pub fn $f(_visitor: &mut V, mut _i: $t) -> $t { - _i.span = _visitor.fold_span(_i.span); + let span = _visitor.fold_span(_i.span()); + _i.set_span(span); _i } } @@ -2038,7 +2039,6 @@ pub fn fold_lit_bool(_visitor: &mut V, _i: LitBool) -> LitBool pub fn fold_lit_verbatim(_visitor: &mut V, _i: LitVerbatim) -> LitVerbatim { LitVerbatim { token: _i . token, - span: _visitor.fold_span(_i . span), } } # [ cfg ( any ( feature = "full" , feature = "derive" ) ) ] # [ cfg ( feature = "full" ) ] diff --git a/src/gen/visit.rs b/src/gen/visit.rs index a5dfef4994..1add246184 100644 --- a/src/gen/visit.rs +++ b/src/gen/visit.rs @@ -1241,7 +1241,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; - _visitor.visit_span(& _i . span); } # [ cfg ( feature = "full" ) ] pub fn visit_impl_item<'ast, V: Visit<'ast> + ?Sized>(_visitor: &mut V, _i: &'ast ImplItem) { @@ -1542,7 +1541,6 @@ 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; - _visitor.visit_span(& _i . span); } # [ cfg ( any ( feature = "full" , feature = "derive" ) ) ] pub fn visit_lifetime_def<'ast, V: Visit<'ast> + ?Sized>(_visitor: &mut V, _i: &'ast LifetimeDef) { @@ -1588,37 +1586,30 @@ pub fn visit_lit_bool<'ast, V: Visit<'ast> + ?Sized>(_visitor: &mut V, _i: &'ast # [ cfg ( any ( feature = "full" , feature = "derive" ) ) ] pub fn visit_lit_byte<'ast, V: Visit<'ast> + ?Sized>(_visitor: &mut V, _i: &'ast LitByte) { // Skipped field _i . token; - _visitor.visit_span(& _i . span); } # [ cfg ( any ( feature = "full" , feature = "derive" ) ) ] pub fn visit_lit_byte_str<'ast, V: Visit<'ast> + ?Sized>(_visitor: &mut V, _i: &'ast LitByteStr) { // Skipped field _i . token; - _visitor.visit_span(& _i . span); } # [ cfg ( any ( feature = "full" , feature = "derive" ) ) ] pub fn visit_lit_char<'ast, V: Visit<'ast> + ?Sized>(_visitor: &mut V, _i: &'ast LitChar) { // Skipped field _i . token; - _visitor.visit_span(& _i . span); } # [ cfg ( any ( feature = "full" , feature = "derive" ) ) ] pub fn visit_lit_float<'ast, V: Visit<'ast> + ?Sized>(_visitor: &mut V, _i: &'ast LitFloat) { // Skipped field _i . token; - _visitor.visit_span(& _i . span); } # [ cfg ( any ( feature = "full" , feature = "derive" ) ) ] pub fn visit_lit_int<'ast, V: Visit<'ast> + ?Sized>(_visitor: &mut V, _i: &'ast LitInt) { // Skipped field _i . token; - _visitor.visit_span(& _i . span); } # [ cfg ( any ( feature = "full" , feature = "derive" ) ) ] pub fn visit_lit_str<'ast, V: Visit<'ast> + ?Sized>(_visitor: &mut V, _i: &'ast LitStr) { // Skipped field _i . token; - _visitor.visit_span(& _i . span); } # [ cfg ( any ( feature = "full" , feature = "derive" ) ) ] pub fn visit_lit_verbatim<'ast, V: Visit<'ast> + ?Sized>(_visitor: &mut V, _i: &'ast LitVerbatim) { // Skipped field _i . token; - _visitor.visit_span(& _i . span); } # [ cfg ( any ( feature = "full" , feature = "derive" ) ) ] # [ cfg ( feature = "full" ) ] pub fn visit_local<'ast, V: Visit<'ast> + ?Sized>(_visitor: &mut V, _i: &'ast Local) { diff --git a/src/gen/visit_mut.rs b/src/gen/visit_mut.rs index 75971e545a..1bd9586f55 100644 --- a/src/gen/visit_mut.rs +++ b/src/gen/visit_mut.rs @@ -1242,7 +1242,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; - _visitor.visit_span_mut(& mut _i . span); } # [ cfg ( feature = "full" ) ] pub fn visit_impl_item_mut(_visitor: &mut V, _i: &mut ImplItem) { @@ -1543,7 +1542,6 @@ 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; - _visitor.visit_span_mut(& mut _i . span); } # [ cfg ( any ( feature = "full" , feature = "derive" ) ) ] pub fn visit_lifetime_def_mut(_visitor: &mut V, _i: &mut LifetimeDef) { @@ -1589,37 +1587,30 @@ pub fn visit_lit_bool_mut(_visitor: &mut V, _i: &mut LitBo # [ cfg ( any ( feature = "full" , feature = "derive" ) ) ] pub fn visit_lit_byte_mut(_visitor: &mut V, _i: &mut LitByte) { // Skipped field _i . token; - _visitor.visit_span_mut(& mut _i . span); } # [ cfg ( any ( feature = "full" , feature = "derive" ) ) ] pub fn visit_lit_byte_str_mut(_visitor: &mut V, _i: &mut LitByteStr) { // Skipped field _i . token; - _visitor.visit_span_mut(& mut _i . span); } # [ cfg ( any ( feature = "full" , feature = "derive" ) ) ] pub fn visit_lit_char_mut(_visitor: &mut V, _i: &mut LitChar) { // Skipped field _i . token; - _visitor.visit_span_mut(& mut _i . span); } # [ cfg ( any ( feature = "full" , feature = "derive" ) ) ] pub fn visit_lit_float_mut(_visitor: &mut V, _i: &mut LitFloat) { // Skipped field _i . token; - _visitor.visit_span_mut(& mut _i . span); } # [ cfg ( any ( feature = "full" , feature = "derive" ) ) ] pub fn visit_lit_int_mut(_visitor: &mut V, _i: &mut LitInt) { // Skipped field _i . token; - _visitor.visit_span_mut(& mut _i . span); } # [ cfg ( any ( feature = "full" , feature = "derive" ) ) ] pub fn visit_lit_str_mut(_visitor: &mut V, _i: &mut LitStr) { // Skipped field _i . token; - _visitor.visit_span_mut(& mut _i . span); } # [ cfg ( any ( feature = "full" , feature = "derive" ) ) ] pub fn visit_lit_verbatim_mut(_visitor: &mut V, _i: &mut LitVerbatim) { // Skipped field _i . token; - _visitor.visit_span_mut(& mut _i . span); } # [ cfg ( any ( feature = "full" , feature = "derive" ) ) ] # [ cfg ( feature = "full" ) ] pub fn visit_local_mut(_visitor: &mut V, _i: &mut Local) { diff --git a/src/ident.rs b/src/ident.rs index 84024204b5..7b0e532840 100644 --- a/src/ident.rs +++ b/src/ident.rs @@ -99,7 +99,6 @@ use proc_macro2::Span; #[derive(Copy, Clone, Debug)] pub struct Ident { term: Term, - pub span: Span, } impl Ident { @@ -145,15 +144,22 @@ impl Ident { } Ident { - term: Term::intern(s), - span: span, + 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::def_site()) + Ident::new(s, Span::call_site()) } } @@ -183,13 +189,13 @@ impl From for Ident { impl<'a> From> for Ident { fn from(s: Cow<'a, str>) -> Self { - Ident::new(&s, Span::def_site()) + Ident::new(&s, Span::call_site()) } } impl From for Ident { fn from(s: String) -> Self { - Ident::new(&s, Span::def_site()) + Ident::new(&s, Span::call_site()) } } @@ -244,7 +250,7 @@ pub mod parsing { impl Synom for Ident { fn parse(input: Cursor) -> PResult { - let (span, term, rest) = match input.term() { + let (term, rest) = match input.term() { Some(term) => term, _ => return parse_error(), }; @@ -265,7 +271,6 @@ pub mod parsing { Ok(( Ident { - span: span, term: term, }, rest, @@ -282,14 +287,10 @@ pub mod parsing { mod printing { use super::*; use quote::{ToTokens, Tokens}; - use proc_macro2::{TokenNode, TokenTree}; impl ToTokens for Ident { fn to_tokens(&self, tokens: &mut Tokens) { - tokens.append(TokenTree { - span: self.span, - kind: TokenNode::Term(self.term), - }) + tokens.append(self.term); } } } diff --git a/src/lifetime.rs b/src/lifetime.rs index 40c1e2019f..321336d905 100644 --- a/src/lifetime.rs +++ b/src/lifetime.rs @@ -30,11 +30,10 @@ use unicode_xid::UnicodeXID; #[derive(Copy, Clone)] pub struct Lifetime { term: Term, - pub span: Span, } impl Lifetime { - pub fn new(term: Term, span: Span) -> Self { + pub fn new(term: Term) -> Self { let s = term.as_str(); if !s.starts_with('\'') { @@ -69,9 +68,16 @@ impl Lifetime { Lifetime { term: term, - span: 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 { @@ -116,7 +122,7 @@ pub mod parsing { impl Synom for Lifetime { fn parse(input: Cursor) -> PResult { - let (span, term, rest) = match input.term() { + let (term, rest) = match input.term() { Some(term) => term, _ => return parse_error(), }; @@ -127,7 +133,6 @@ pub mod parsing { Ok(( Lifetime { term: term, - span: span, }, rest, )) @@ -143,14 +148,10 @@ pub mod parsing { mod printing { use super::*; use quote::{ToTokens, Tokens}; - use proc_macro2::{TokenNode, TokenTree}; impl ToTokens for Lifetime { fn to_tokens(&self, tokens: &mut Tokens) { - tokens.append(TokenTree { - span: self.span, - kind: TokenNode::Term(self.term), - }) + self.term.to_tokens(tokens); } } } diff --git a/src/lit.rs b/src/lit.rs index 4234d0b493..b49fa11e8d 100644 --- a/src/lit.rs +++ b/src/lit.rs @@ -6,7 +6,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use proc_macro2::{Literal, Span, TokenNode}; +use proc_macro2::{Literal, Span}; use std::str; #[cfg(feature = "printing")] @@ -17,7 +17,7 @@ use proc_macro2::TokenStream; #[cfg(feature = "parsing")] use {ParseError, Synom}; -#[cfg(any(feature = "printing", feature = "parsing"))] +#[cfg(any(feature = "printing", feature = "parsing", feature = "derive"))] use proc_macro2::TokenTree; #[cfg(feature = "extra-traits")] @@ -41,7 +41,6 @@ ast_enum_of_structs! { /// `"full"` feature.* pub Str(LitStr #manual_extra_traits { token: Literal, - pub span: Span, }), /// A byte string literal: `b"foo"`. @@ -50,7 +49,6 @@ ast_enum_of_structs! { /// `"full"` feature.* pub ByteStr(LitByteStr #manual_extra_traits { token: Literal, - pub span: Span, }), /// A byte literal: `b'f'`. @@ -59,7 +57,6 @@ ast_enum_of_structs! { /// `"full"` feature.* pub Byte(LitByte #manual_extra_traits { token: Literal, - pub span: Span, }), /// A character literal: `'a'`. @@ -68,7 +65,6 @@ ast_enum_of_structs! { /// `"full"` feature.* pub Char(LitChar #manual_extra_traits { token: Literal, - pub span: Span, }), /// An integer literal: `1` or `1u16`. @@ -80,7 +76,6 @@ ast_enum_of_structs! { /// `"full"` feature.* pub Int(LitInt #manual_extra_traits { token: Literal, - pub span: Span, }), /// A floating point literal: `1f64` or `1.0e10f64`. @@ -91,7 +86,6 @@ ast_enum_of_structs! { /// `"full"` feature.* pub Float(LitFloat #manual_extra_traits { token: Literal, - pub span: Span, }), /// A boolean literal: `true` or `false`. @@ -110,16 +104,16 @@ ast_enum_of_structs! { /// `"full"` feature.* pub Verbatim(LitVerbatim #manual_extra_traits { pub token: Literal, - pub span: Span, }), } } impl LitStr { pub fn new(value: &str, span: Span) -> Self { + let mut lit = Literal::string(value); + lit.set_span(span); LitStr { - token: Literal::string(value), - span: span, + token: lit, } } @@ -132,11 +126,13 @@ impl LitStr { /// All spans in the syntax tree will point to the span of this `LitStr`. #[cfg(feature = "parsing")] pub fn parse(&self) -> Result { + use proc_macro2::Group; + // Parse string literal into a token stream with every span equal to the // original literal's span. fn spanned_tokens(s: &LitStr) -> Result { let stream = ::parse_str(&s.value())?; - Ok(respan_token_stream(stream, s.span)) + Ok(respan_token_stream(stream, s.span())) } // Token stream with every span replaced by the given one. @@ -145,81 +141,109 @@ impl LitStr { } // Token tree with every span replaced by the given one. - fn respan_token_tree(token: TokenTree, span: Span) -> TokenTree { - TokenTree { - span: span, - kind: match token.kind { - TokenNode::Group(delimiter, nested) => { - TokenNode::Group(delimiter, respan_token_stream(nested, span)) - } - other => other, - }, + fn respan_token_tree(mut token: TokenTree, span: Span) -> TokenTree { + match token { + TokenTree::Group(ref mut g) => { + let stream = respan_token_stream(g.stream().clone(), span); + *g = Group::new(g.delimiter(), stream); + g.set_span(span); + } + ref mut other => other.set_span(span), } + token } spanned_tokens(self).and_then(::parse2) } + + pub fn span(&self) -> Span { + self.token.span() + } + + pub fn set_span(&mut self, span: Span) { + self.token.set_span(span) + } } impl LitByteStr { pub fn new(value: &[u8], span: Span) -> Self { - LitByteStr { - token: Literal::byte_string(value), - span: span, - } + let mut token = Literal::byte_string(value); + token.set_span(span); + LitByteStr { token: token } } pub fn value(&self) -> Vec { value::parse_lit_byte_str(&self.token.to_string()) } + + pub fn span(&self) -> Span { + self.token.span() + } + + pub fn set_span(&mut self, span: Span) { + self.token.set_span(span) + } } impl LitByte { pub fn new(value: u8, span: Span) -> Self { - LitByte { - token: Literal::byte_char(value), - span: span, - } + let mut token = Literal::u8_suffixed(value); + token.set_span(span); + LitByte { token: token } } pub fn value(&self) -> u8 { value::parse_lit_byte(&self.token.to_string()) } + + pub fn span(&self) -> Span { + self.token.span() + } + + pub fn set_span(&mut self, span: Span) { + self.token.set_span(span) + } } impl LitChar { pub fn new(value: char, span: Span) -> Self { - LitChar { - token: Literal::character(value), - span: span, - } + let mut token = Literal::character(value); + token.set_span(span); + LitChar { token: token } } pub fn value(&self) -> char { value::parse_lit_char(&self.token.to_string()) } + + pub fn span(&self) -> Span { + self.token.span() + } + + pub fn set_span(&mut self, span: Span) { + self.token.set_span(span) + } } impl LitInt { pub fn new(value: u64, suffix: IntSuffix, span: Span) -> Self { - LitInt { - token: match suffix { - IntSuffix::Isize => Literal::isize(value as isize), - IntSuffix::I8 => Literal::i8(value as i8), - IntSuffix::I16 => Literal::i16(value as i16), - IntSuffix::I32 => Literal::i32(value as i32), - IntSuffix::I64 => Literal::i64(value as i64), - IntSuffix::I128 => value::to_literal(&format!("{}i128", value)), - IntSuffix::Usize => Literal::usize(value as usize), - IntSuffix::U8 => Literal::u8(value as u8), - IntSuffix::U16 => Literal::u16(value as u16), - IntSuffix::U32 => Literal::u32(value as u32), - IntSuffix::U64 => Literal::u64(value), - IntSuffix::U128 => value::to_literal(&format!("{}u128", value)), - IntSuffix::None => Literal::integer(value as i64), - }, - span: span, - } + let mut token = match suffix { + IntSuffix::Isize => Literal::isize_suffixed(value as isize), + IntSuffix::I8 => Literal::i8_suffixed(value as i8), + IntSuffix::I16 => Literal::i16_suffixed(value as i16), + IntSuffix::I32 => Literal::i32_suffixed(value as i32), + IntSuffix::I64 => Literal::i64_suffixed(value as i64), + IntSuffix::I128 => value::to_literal(&format!("{}i128", value)), + IntSuffix::Usize => Literal::usize_suffixed(value as usize), + IntSuffix::U8 => Literal::u8_suffixed(value as u8), + IntSuffix::U16 => Literal::u16_suffixed(value as u16), + IntSuffix::U32 => Literal::u32_suffixed(value as u32), + IntSuffix::U64 => Literal::u64_suffixed(value), + IntSuffix::U128 => value::to_literal(&format!("{}u128", value)), + IntSuffix::None => Literal::u64_unsuffixed(value), + }; + token.set_span(span); + LitInt { token: token } } pub fn value(&self) -> u64 { @@ -248,18 +272,25 @@ impl LitInt { } IntSuffix::None } + + pub fn span(&self) -> Span { + self.token.span() + } + + pub fn set_span(&mut self, span: Span) { + self.token.set_span(span) + } } impl LitFloat { pub fn new(value: f64, suffix: FloatSuffix, span: Span) -> Self { - LitFloat { - token: match suffix { - FloatSuffix::F32 => Literal::f32(value as f32), - FloatSuffix::F64 => Literal::f64(value), - FloatSuffix::None => Literal::float(value), - }, - span: span, - } + let mut token = match suffix { + FloatSuffix::F32 => Literal::f32_suffixed(value as f32), + FloatSuffix::F64 => Literal::f64_suffixed(value), + FloatSuffix::None => Literal::f64_unsuffixed(value), + }; + token.set_span(span); + LitFloat { token: token } } pub fn value(&self) -> f64 { @@ -275,6 +306,14 @@ impl LitFloat { } FloatSuffix::None } + + pub fn span(&self) -> Span { + self.token.span() + } + + pub fn set_span(&mut self, span: Span) { + self.token.set_span(span) + } } macro_rules! lit_extra_traits { @@ -301,6 +340,16 @@ macro_rules! lit_extra_traits { } } +impl LitVerbatim { + pub fn span(&self) -> Span { + self.token.span() + } + + pub fn set_span(&mut self, span: Span) { + self.token.set_span(span) + } +} + lit_extra_traits!(LitStr, token); lit_extra_traits!(LitByteStr, token); lit_extra_traits!(LitByte, token); @@ -372,16 +421,16 @@ pub mod parsing { impl Synom for Lit { fn parse(input: Cursor) -> PResult { match input.literal() { - Some((span, lit, rest)) => { + Some((lit, rest)) => { if lit.to_string().starts_with('/') { // Doc comment literal which is not a Syn literal parse_error() } else { - Ok((Lit::new(lit, span), rest)) + Ok((Lit::new(lit), rest)) } } _ => match input.term() { - Some((span, term, rest)) => Ok(( + Some((term, rest)) => Ok(( Lit::Bool(LitBool { value: if term.as_str() == "true" { true @@ -390,7 +439,7 @@ pub mod parsing { } else { return parse_error(); }, - span: span, + span: term.span(), }), rest, )), @@ -461,73 +510,50 @@ mod printing { impl ToTokens for LitStr { fn to_tokens(&self, tokens: &mut Tokens) { - tokens.append(TokenTree { - span: self.span, - kind: TokenNode::Literal(self.token.clone()), - }); + self.token.to_tokens(tokens); } } impl ToTokens for LitByteStr { fn to_tokens(&self, tokens: &mut Tokens) { - tokens.append(TokenTree { - span: self.span, - kind: TokenNode::Literal(self.token.clone()), - }); + self.token.to_tokens(tokens); } } impl ToTokens for LitByte { fn to_tokens(&self, tokens: &mut Tokens) { - tokens.append(TokenTree { - span: self.span, - kind: TokenNode::Literal(self.token.clone()), - }); + self.token.to_tokens(tokens); } } impl ToTokens for LitChar { fn to_tokens(&self, tokens: &mut Tokens) { - tokens.append(TokenTree { - span: self.span, - kind: TokenNode::Literal(self.token.clone()), - }); + self.token.to_tokens(tokens); } } impl ToTokens for LitInt { fn to_tokens(&self, tokens: &mut Tokens) { - tokens.append(TokenTree { - span: self.span, - kind: TokenNode::Literal(self.token.clone()), - }); + self.token.to_tokens(tokens); } } impl ToTokens for LitFloat { fn to_tokens(&self, tokens: &mut Tokens) { - tokens.append(TokenTree { - span: self.span, - kind: TokenNode::Literal(self.token.clone()), - }); + self.token.to_tokens(tokens); } } impl ToTokens for LitBool { fn to_tokens(&self, tokens: &mut Tokens) { - tokens.append(TokenTree { - span: self.span, - kind: TokenNode::Term(Term::intern(if self.value { "true" } else { "false" })), - }); + let s = if self.value { "true" } else { "false" }; + tokens.append(Term::new(s, self.span)); } } impl ToTokens for LitVerbatim { fn to_tokens(&self, tokens: &mut Tokens) { - tokens.append(TokenTree { - span: self.span, - kind: TokenNode::Literal(self.token.clone()), - }); + self.token.to_tokens(tokens); } } } @@ -550,27 +576,24 @@ mod value { /// # Panics /// /// Panics if the input is a doc comment literal. - pub fn new(token: Literal, span: Span) -> Self { + pub fn new(token: Literal) -> Self { let value = token.to_string(); match value::byte(&value, 0) { b'"' | b'r' => { return Lit::Str(LitStr { token: token, - span: span, }) } b'b' => match value::byte(&value, 1) { b'"' | b'r' => { return Lit::ByteStr(LitByteStr { token: token, - span: span, }) } b'\'' => { return Lit::Byte(LitByte { token: token, - span: span, }) } _ => {} @@ -578,30 +601,26 @@ mod value { b'\'' => { return Lit::Char(LitChar { token: token, - span: span, }) } b'0'...b'9' => if number_is_int(&value) { return Lit::Int(LitInt { token: token, - span: span, }); } else if number_is_float(&value) { return Lit::Float(LitFloat { token: token, - span: span, }); } else { // number overflow return Lit::Verbatim(LitVerbatim { token: token, - span: span, }); }, _ => if value == "true" || value == "false" { return Lit::Bool(LitBool { value: value == "true", - span: span, + span: token.span(), }); }, } @@ -1020,8 +1039,8 @@ mod value { pub fn to_literal(s: &str) -> Literal { let stream = s.parse::().unwrap(); - match stream.into_iter().next().unwrap().kind { - TokenNode::Literal(l) => l, + match stream.into_iter().next().unwrap() { + TokenTree::Literal(l) => l, _ => unreachable!(), } } diff --git a/src/spanned.rs b/src/spanned.rs index 84c50d5dba..2f96581d9f 100644 --- a/src/spanned.rs +++ b/src/spanned.rs @@ -48,9 +48,7 @@ //! # let ty = get_a_type(); //! /* ... */ //! -//! let def_site = Span::def_site(); -//! let ty_span = ty.span().resolved_at(def_site); -//! let assert_sync = quote_spanned! {ty_span=> +//! let assert_sync = quote_spanned! {ty.span()=> //! struct _AssertSync where #ty: Sync; //! }; //! @@ -78,13 +76,7 @@ //! ``` //! //! In this technique, using the `Type`'s span for the error message makes the -//! error appear in the correct place underlining the right type. But it is -//! **incredibly important** that the span for the assertion is **resolved** at -//! the procedural macro definition site rather than at the `Type`'s span. This -//! way we guarantee that it refers to the `Sync` trait that we expect. If the -//! assertion were **resolved** at the same place that `ty` is resolved, the -//! user could circumvent the check by defining their own `Sync` trait that is -//! implemented for their type. +//! error appear in the correct place underlining the right type. use proc_macro2::{Span, TokenStream}; use quote::{ToTokens, Tokens}; @@ -122,13 +114,13 @@ where let token_stream = TokenStream::from(tokens); let mut iter = token_stream.into_iter(); let mut span = match iter.next() { - Some(tt) => tt.span, + Some(tt) => tt.span(), None => { return Span::call_site(); } }; for tt in iter { - if let Some(joined) = span.join(tt.span) { + if let Some(joined) = span.join(tt.span()) { span = joined; } } @@ -145,7 +137,7 @@ where // We can't join spans without procmacro2_semver_exempt so just grab the // first one. match iter.next() { - Some(tt) => tt.span, + Some(tt) => tt.span(), None => Span::call_site(), } } diff --git a/src/token.rs b/src/token.rs index d3511c4c68..1c2ee6aac8 100644 --- a/src/token.rs +++ b/src/token.rs @@ -136,7 +136,7 @@ macro_rules! token_punct { impl ::std::default::Default for $name { fn default() -> Self { - $name([Span::def_site(); $len]) + $name([Span::call_site(); $len]) } } @@ -203,7 +203,7 @@ macro_rules! token_keyword { impl ::std::default::Default for $name { fn default() -> Self { - $name(Span::def_site()) + $name(Span::call_site()) } } @@ -265,7 +265,7 @@ macro_rules! token_delimiter { impl ::std::default::Default for $name { fn default() -> Self { - $name(Span::def_site()) + $name(Span::call_site()) } } @@ -663,20 +663,20 @@ mod parsing { where T: FromSpans, { - let mut spans = [Span::def_site(); 3]; + let mut spans = [Span::call_site(); 3]; assert!(s.len() <= spans.len()); let chars = s.chars(); for (i, (ch, slot)) in chars.zip(&mut spans).enumerate() { match tokens.op() { - Some((span, op, kind, rest)) if op == ch => { + Some((op, rest)) if op.op() == ch => { if i != s.len() - 1 { - match kind { + match op.spacing() { Spacing::Joint => {} _ => return parse_error(), } } - *slot = span; + *slot = op.span(); tokens = rest; } _ => return parse_error(), @@ -686,9 +686,9 @@ mod parsing { } pub fn keyword<'a, T>(keyword: &str, tokens: Cursor<'a>, new: fn(Span) -> T) -> PResult<'a, T> { - if let Some((span, term, rest)) = tokens.term() { + if let Some((term, rest)) = tokens.term() { if term.as_str() == keyword { - return Ok((new(span), rest)); + return Ok((new(term.span()), rest)); } } parse_error() @@ -728,7 +728,7 @@ mod parsing { #[cfg(feature = "printing")] mod printing { - use proc_macro2::{Delimiter, Spacing, Span, Term, TokenNode, TokenTree}; + use proc_macro2::{Delimiter, Spacing, Span, Term, Op, Group}; use quote::Tokens; pub fn punct(s: &str, spans: &[Span], tokens: &mut Tokens) { @@ -739,23 +739,18 @@ mod printing { let ch = chars.next_back().unwrap(); let span = spans.next_back().unwrap(); for (ch, span) in chars.zip(spans) { - tokens.append(TokenTree { - span: *span, - kind: TokenNode::Op(ch, Spacing::Joint), - }); + let mut op = Op::new(ch, Spacing::Joint); + op.set_span(*span); + tokens.append(op); } - tokens.append(TokenTree { - span: *span, - kind: TokenNode::Op(ch, Spacing::Alone), - }); + let mut op = Op::new(ch, Spacing::Alone); + op.set_span(*span); + tokens.append(op); } pub fn keyword(s: &str, span: &Span, tokens: &mut Tokens) { - tokens.append(TokenTree { - span: *span, - kind: TokenNode::Term(Term::intern(s)), - }); + tokens.append(Term::new(s, *span)); } pub fn delim(s: &str, span: &Span, tokens: &mut Tokens, f: F) @@ -771,9 +766,8 @@ mod printing { }; let mut inner = Tokens::new(); f(&mut inner); - tokens.append(TokenTree { - span: *span, - kind: TokenNode::Group(delim, inner.into()), - }); + let mut g = Group::new(delim, inner.into()); + g.set_span(*span); + tokens.append(g); } } diff --git a/src/tt.rs b/src/tt.rs index 19ebedd3f1..5fb95bafa3 100644 --- a/src/tt.rs +++ b/src/tt.rs @@ -19,56 +19,42 @@ use {parse_error, MacroDelimiter}; use std::hash::{Hash, Hasher}; #[cfg(any(feature = "parsing", feature = "extra-traits"))] -use proc_macro2::{Delimiter, TokenNode, TokenStream, TokenTree}; +use proc_macro2::{Delimiter, TokenStream, TokenTree}; #[cfg(feature = "parsing")] pub fn delimited(input: Cursor) -> PResult<(MacroDelimiter, TokenStream)> { - match input.token_tree() { - Some(( - TokenTree { - span, - kind: TokenNode::Group(delimiter, tts), - }, - rest, - )) => { - let delimiter = match delimiter { - Delimiter::Parenthesis => MacroDelimiter::Paren(Paren(span)), - Delimiter::Brace => MacroDelimiter::Brace(Brace(span)), - Delimiter::Bracket => MacroDelimiter::Bracket(Bracket(span)), - Delimiter::None => return parse_error(), - }; - Ok(((delimiter, tts), rest)) - } - _ => parse_error(), + if let Some((TokenTree::Group(g), rest)) = input.token_tree() { + let span = g.span(); + let delimiter = match g.delimiter() { + Delimiter::Parenthesis => MacroDelimiter::Paren(Paren(span)), + Delimiter::Brace => MacroDelimiter::Brace(Brace(span)), + Delimiter::Bracket => MacroDelimiter::Bracket(Bracket(span)), + Delimiter::None => return parse_error(), + }; + + return Ok(((delimiter, g.stream().clone()), rest)) } + parse_error() } #[cfg(all(feature = "full", feature = "parsing"))] pub fn braced(input: Cursor) -> PResult<(Brace, TokenStream)> { - match input.token_tree() { - Some(( - TokenTree { - span, - kind: TokenNode::Group(Delimiter::Brace, tts), - }, - rest, - )) => Ok(((Brace(span), tts), rest)), - _ => parse_error(), + if let Some((TokenTree::Group(g), rest)) = input.token_tree() { + if g.delimiter() == Delimiter::Brace { + return Ok(((Brace(g.span()), g.stream().clone()), rest)) + } } + parse_error() } #[cfg(all(feature = "full", feature = "parsing"))] pub fn parenthesized(input: Cursor) -> PResult<(Paren, TokenStream)> { - match input.token_tree() { - Some(( - TokenTree { - span, - kind: TokenNode::Group(Delimiter::Parenthesis, tts), - }, - rest, - )) => Ok(((Paren(span), tts), rest)), - _ => parse_error(), + if let Some((TokenTree::Group(g), rest)) = input.token_tree() { + if g.delimiter() == Delimiter::Parenthesis { + return Ok(((Paren(g.span()), g.stream().clone()), rest)) + } } + parse_error() } #[cfg(feature = "extra-traits")] @@ -79,9 +65,9 @@ impl<'a> PartialEq for TokenTreeHelper<'a> { fn eq(&self, other: &Self) -> bool { use proc_macro2::Spacing; - match (&self.0.kind, &other.0.kind) { - (&TokenNode::Group(d1, ref s1), &TokenNode::Group(d2, ref s2)) => { - match (d1, d2) { + match (self.0, other.0) { + (&TokenTree::Group(ref g1), &TokenTree::Group(ref g2)) => { + match (g1.delimiter(), g2.delimiter()) { (Delimiter::Parenthesis, Delimiter::Parenthesis) | (Delimiter::Brace, Delimiter::Brace) | (Delimiter::Bracket, Delimiter::Bracket) @@ -89,8 +75,8 @@ impl<'a> PartialEq for TokenTreeHelper<'a> { _ => return false, } - let s1 = s1.clone().into_iter(); - let mut s2 = s2.clone().into_iter(); + let s1 = g1.stream().clone().into_iter(); + let mut s2 = g2.stream().clone().into_iter(); for item1 in s1 { let item2 = match s2.next() { @@ -103,16 +89,16 @@ impl<'a> PartialEq for TokenTreeHelper<'a> { } s2.next().is_none() } - (&TokenNode::Op(o1, k1), &TokenNode::Op(o2, k2)) => { - o1 == o2 && match (k1, k2) { + (&TokenTree::Op(ref o1), &TokenTree::Op(ref o2)) => { + o1.op() == o2.op() && match (o1.spacing(), o2.spacing()) { (Spacing::Alone, Spacing::Alone) | (Spacing::Joint, Spacing::Joint) => true, _ => false, } } - (&TokenNode::Literal(ref l1), &TokenNode::Literal(ref l2)) => { + (&TokenTree::Literal(ref l1), &TokenTree::Literal(ref l2)) => { l1.to_string() == l2.to_string() } - (&TokenNode::Term(ref s1), &TokenNode::Term(ref s2)) => s1.as_str() == s2.as_str(), + (&TokenTree::Term(ref s1), &TokenTree::Term(ref s2)) => s1.as_str() == s2.as_str(), _ => false, } } @@ -123,31 +109,31 @@ impl<'a> Hash for TokenTreeHelper<'a> { fn hash(&self, h: &mut H) { use proc_macro2::Spacing; - match self.0.kind { - TokenNode::Group(delim, ref stream) => { + match *self.0 { + TokenTree::Group(ref g) => { 0u8.hash(h); - match delim { + match g.delimiter() { Delimiter::Parenthesis => 0u8.hash(h), Delimiter::Brace => 1u8.hash(h), Delimiter::Bracket => 2u8.hash(h), Delimiter::None => 3u8.hash(h), } - for item in stream.clone() { + for item in g.stream().clone() { TokenTreeHelper(&item).hash(h); } 0xffu8.hash(h); // terminator w/ a variant we don't normally hash } - TokenNode::Op(op, kind) => { + TokenTree::Op(ref op) => { 1u8.hash(h); - op.hash(h); - match kind { + op.op().hash(h); + match op.spacing() { Spacing::Alone => 0u8.hash(h), Spacing::Joint => 1u8.hash(h), } } - TokenNode::Literal(ref lit) => (2u8, lit.to_string()).hash(h), - TokenNode::Term(ref word) => (3u8, word.as_str()).hash(h), + TokenTree::Literal(ref lit) => (2u8, lit.to_string()).hash(h), + TokenTree::Term(ref word) => (3u8, word.as_str()).hash(h), } } } diff --git a/tests/common/respan.rs b/tests/common/respan.rs index 6d1e75a857..a969ae2237 100644 --- a/tests/common/respan.rs +++ b/tests/common/respan.rs @@ -186,6 +186,7 @@ impl Folder for Respanner { fn fold_attribute(&mut self, mut at: Attribute) -> Option { at.id.0 = 0; + at.is_sugared_doc = false; fold::noop_fold_attribute(at, self) } diff --git a/tests/test_derive_input.rs b/tests/test_derive_input.rs index a9fe242e46..bd25652348 100644 --- a/tests/test_derive_input.rs +++ b/tests/test_derive_input.rs @@ -12,7 +12,8 @@ extern crate proc_macro2; extern crate syn; use syn::*; -use proc_macro2::{Delimiter, Literal, Spacing, Span, Term, TokenNode, TokenStream, TokenTree}; +use proc_macro2::{Delimiter, Literal, Spacing, Span, Term, Op, TokenStream, TokenTree}; +use proc_macro2::Group; use proc_macro2::Delimiter::{Brace, Parenthesis}; use std::iter::FromIterator; @@ -21,31 +22,19 @@ use std::iter::FromIterator; mod macros; fn op(c: char) -> TokenTree { - proc_macro2::TokenTree { - span: Span::def_site(), - kind: TokenNode::Op(c, Spacing::Alone), - } + Op::new(c, Spacing::Alone).into() } fn lit>(t: T) -> TokenTree { - proc_macro2::TokenTree { - span: Span::def_site(), - kind: TokenNode::Literal(t.into()), - } + t.into().into() } fn word(sym: &str) -> TokenTree { - proc_macro2::TokenTree { - span: Span::def_site(), - kind: TokenNode::Term(Term::intern(sym)), - } + Term::new(sym, Span::call_site()).into() } fn delimited(delim: Delimiter, tokens: Vec) -> TokenTree { - proc_macro2::TokenTree { - span: Span::def_site(), - kind: TokenNode::Group(delim, tokens.into_iter().collect()), - } + Group::new(delim, tokens.into_iter().collect()).into() } #[test] @@ -284,7 +273,7 @@ fn test_enum() { Default::default(), Expr::Lit(ExprLit { attrs: Vec::new(), - lit: Lit::Int(LitInt::new(0, IntSuffix::Isize, Span::def_site())), + lit: Lit::Int(LitInt::new(0, IntSuffix::Isize, Span::call_site())), }), )), }, @@ -305,19 +294,19 @@ fn test_enum() { lit: Lit::Int(LitInt::new( 0, IntSuffix::None, - Span::def_site() + Span::call_site() )), }), Expr::Lit(ExprLit { attrs: Vec::new(), - lit: Lit::Str(LitStr::new("data", Span::def_site())), + lit: Lit::Str(LitStr::new("data", Span::call_site())), }), ], })), dot_token: Default::default(), member: Member::Unnamed(Index { index: 0, - span: Span::def_site(), + span: Span::call_site(), }), }), )), @@ -338,7 +327,7 @@ fn test_enum() { eq_token: Default::default(), lit: Lit::Str(LitStr::new( "/// See the std::result module documentation for details.", - Span::def_site(), + Span::call_site(), )), }.into(), Meta::Word("must_use".into()), diff --git a/tests/test_generics.rs b/tests/test_generics.rs index b96d6524ee..a86a41e8ec 100644 --- a/tests/test_generics.rs +++ b/tests/test_generics.rs @@ -32,14 +32,14 @@ fn test_split_for_impl() { params: punctuated![ GenericParam::Lifetime(LifetimeDef { attrs: Default::default(), - lifetime: Lifetime::new(Term::intern("'a"), Span::def_site()), + lifetime: Lifetime::new(Term::new("'a", Span::call_site())), bounds: Default::default(), colon_token: None, }), GenericParam::Lifetime(LifetimeDef { attrs: Default::default(), - lifetime: Lifetime::new(Term::intern("'b"), Span::def_site()), - bounds: punctuated![Lifetime::new(Term::intern("'a"), Span::def_site())], + lifetime: Lifetime::new(Term::new("'b", Span::call_site())), + bounds: punctuated![Lifetime::new(Term::new("'a", Span::call_site()))], colon_token: Some(token::Colon::default()), }), GenericParam::Type(TypeParam { @@ -55,7 +55,7 @@ fn test_split_for_impl() { ], ident: "T".into(), bounds: punctuated![ - TypeParamBound::Lifetime(Lifetime::new(Term::intern("'a"), Span::def_site())), + TypeParamBound::Lifetime(Lifetime::new(Term::new("'a", Span::call_site()))), ], default: Some( TypeTuple { @@ -111,14 +111,14 @@ fn test_split_for_impl() { #[test] fn test_ty_param_bound() { let tokens = quote!('a); - let expected = TypeParamBound::Lifetime(Lifetime::new(Term::intern("'a"), Span::def_site())); + let expected = TypeParamBound::Lifetime(Lifetime::new(Term::new("'a", Span::call_site()))); assert_eq!( expected, common::parse::syn::(tokens.into()) ); let tokens = quote!('_); - let expected = TypeParamBound::Lifetime(Lifetime::new(Term::intern("'_"), Span::def_site())); + let expected = TypeParamBound::Lifetime(Lifetime::new(Term::new("'_", Span::call_site()))); assert_eq!( expected, common::parse::syn::(tokens.into()) diff --git a/tests/test_grouping.rs b/tests/test_grouping.rs index 8e00068547..ca686f6940 100644 --- a/tests/test_grouping.rs +++ b/tests/test_grouping.rs @@ -22,13 +22,6 @@ mod macros; mod common; -fn tt(k: TokenNode) -> TokenTree { - TokenTree { - span: Span::def_site(), - kind: k, - } -} - fn expr>(t: T) -> Expr { t.into() } @@ -36,26 +29,26 @@ fn expr>(t: T) -> Expr { fn lit>(t: T) -> Expr { Expr::Lit(ExprLit { attrs: Vec::new(), - lit: Lit::new(t.into(), Span::def_site()), + lit: Lit::new(t.into()), }) } #[test] fn test_grouping() { let raw: TokenStream = vec![ - tt(TokenNode::Literal(Literal::i32(1))), - tt(TokenNode::Op('+', Spacing::Alone)), - tt(TokenNode::Group( + TokenTree::Literal(Literal::i32_suffixed(1)), + TokenTree::Op(Op::new('+', Spacing::Alone)), + TokenTree::Group(proc_macro2::Group::new( Delimiter::None, vec![ - tt(TokenNode::Literal(Literal::i32(2))), - tt(TokenNode::Op('+', Spacing::Alone)), - tt(TokenNode::Literal(Literal::i32(3))), + TokenTree::Literal(Literal::i32_suffixed(2)), + TokenTree::Op(Op::new('+', Spacing::Alone)), + TokenTree::Literal(Literal::i32_suffixed(3)), ].into_iter() .collect(), )), - tt(TokenNode::Op('*', Spacing::Alone)), - tt(TokenNode::Literal(Literal::i32(4))), + TokenTree::Op(Op::new('*', Spacing::Alone)), + TokenTree::Literal(Literal::i32_suffixed(4)), ].into_iter() .collect(); @@ -65,7 +58,7 @@ fn test_grouping() { common::parse::syn::(raw), expr(ExprBinary { attrs: Vec::new(), - left: Box::new(lit(Literal::i32(1))), + left: Box::new(lit(Literal::i32_suffixed(1))), op: BinOp::Add(::default()), right: Box::new(expr(ExprBinary { attrs: Vec::new(), @@ -74,13 +67,13 @@ fn test_grouping() { group_token: Group::default(), expr: Box::new(expr(ExprBinary { attrs: Vec::new(), - left: Box::new(lit(Literal::i32(2))), + left: Box::new(lit(Literal::i32_suffixed(2))), op: BinOp::Add(::default()), - right: Box::new(lit(Literal::i32(3))), + right: Box::new(lit(Literal::i32_suffixed(3))), })), })), op: BinOp::Mul(::default()), - right: Box::new(lit(Literal::i32(4))), + right: Box::new(lit(Literal::i32_suffixed(4))), })), }) ); @@ -89,19 +82,19 @@ fn test_grouping() { #[test] fn test_invalid_grouping() { let raw: TokenStream = vec![ - tt(TokenNode::Literal(Literal::i32(1))), - tt(TokenNode::Op('+', Spacing::Alone)), - tt(TokenNode::Group( + TokenTree::Literal(Literal::i32_suffixed(1)), + TokenTree::Op(Op::new('+', Spacing::Alone)), + TokenTree::Group(proc_macro2::Group::new( Delimiter::None, vec![ - tt(TokenNode::Literal(Literal::i32(2))), - tt(TokenNode::Op('+', Spacing::Alone)), + TokenTree::Literal(Literal::i32_suffixed(2)), + TokenTree::Op(Op::new('+', Spacing::Alone)), ].into_iter() .collect(), )), - tt(TokenNode::Literal(Literal::i32(3))), - tt(TokenNode::Op('*', Spacing::Alone)), - tt(TokenNode::Literal(Literal::i32(4))), + TokenTree::Literal(Literal::i32_suffixed(3)), + TokenTree::Op(Op::new('*', Spacing::Alone)), + TokenTree::Literal(Literal::i32_suffixed(4)), ].into_iter() .collect(); @@ -113,16 +106,16 @@ fn test_invalid_grouping() { attrs: Vec::new(), left: Box::new(expr(ExprBinary { attrs: Vec::new(), - left: Box::new(lit(Literal::i32(1))), + left: Box::new(lit(Literal::i32_suffixed(1))), op: BinOp::Add(::default()), - right: Box::new(lit(Literal::i32(2))), + right: Box::new(lit(Literal::i32_suffixed(2))), })), op: BinOp::Add(::default()), right: Box::new(expr(ExprBinary { attrs: Vec::new(), - left: Box::new(lit(Literal::i32(3))), + left: Box::new(lit(Literal::i32_suffixed(3))), op: BinOp::Mul(::default()), - right: Box::new(lit(Literal::i32(4))), + right: Box::new(lit(Literal::i32_suffixed(4))), })), }) ); diff --git a/tests/test_lit.rs b/tests/test_lit.rs index ad6f246860..79a060671f 100644 --- a/tests/test_lit.rs +++ b/tests/test_lit.rs @@ -12,7 +12,7 @@ extern crate syn; use syn::{FloatSuffix, IntSuffix, Lit}; use quote::ToTokens; -use proc_macro2::{Span, TokenNode, TokenStream}; +use proc_macro2::{TokenTree, TokenStream}; use std::str::FromStr; fn lit(s: &str) -> Lit { @@ -21,9 +21,8 @@ fn lit(s: &str) -> Lit { .into_iter() .next() .unwrap() - .kind { - TokenNode::Literal(lit) => Lit::new(lit, Span::def_site()), + TokenTree::Literal(lit) => Lit::new(lit), _ => panic!(), } } diff --git a/tests/test_meta_item.rs b/tests/test_meta_item.rs index 0fff6d295b..bd2c1a556b 100644 --- a/tests/test_meta_item.rs +++ b/tests/test_meta_item.rs @@ -19,7 +19,7 @@ use proc_macro2::{Literal, Span, TokenStream}; mod macros; fn lit>(t: T) -> Lit { - Lit::new(t.into(), Span::def_site()) + Lit::new(t.into()) } #[test] @@ -34,7 +34,7 @@ fn test_meta_item_name_value() { MetaNameValue { ident: "foo".into(), eq_token: Default::default(), - lit: lit(Literal::integer(5)), + lit: lit(Literal::i32_unsuffixed(5)), }, ) } @@ -46,7 +46,7 @@ fn test_meta_item_bool_value() { MetaNameValue { ident: "foo".into(), eq_token: Default::default(), - lit: Lit::Bool(LitBool { value: true, span: Span::def_site() }), + lit: Lit::Bool(LitBool { value: true, span: Span::call_site() }), }, ); run_test( @@ -54,7 +54,7 @@ fn test_meta_item_bool_value() { MetaNameValue { ident: "foo".into(), eq_token: Default::default(), - lit: Lit::Bool(LitBool { value: false, span: Span::def_site() }), + lit: Lit::Bool(LitBool { value: false, span: Span::call_site() }), }, ) } @@ -66,7 +66,7 @@ fn test_meta_item_list_lit() { MetaList { ident: "foo".into(), paren_token: Default::default(), - nested: punctuated![NestedMeta::Literal(lit(Literal::integer(5)))], + nested: punctuated![NestedMeta::Literal(lit(Literal::i32_unsuffixed(5)))], }, ) } @@ -95,7 +95,7 @@ fn test_meta_item_list_name_value() { MetaNameValue { ident: "bar".into(), eq_token: Default::default(), - lit: lit(Literal::integer(5)), + lit: lit(Literal::i32_unsuffixed(5)), }.into(), ), ], @@ -115,7 +115,7 @@ fn test_meta_item_list_bool_value() { MetaNameValue { ident: "bar".into(), eq_token: Default::default(), - lit: Lit::Bool(LitBool { value: true, span: Span::def_site() }), + lit: Lit::Bool(LitBool { value: true, span: Span::call_site() }), }.into(), ), ], @@ -136,7 +136,7 @@ fn test_meta_item_multiple() { MetaNameValue { ident: "name".into(), eq_token: Default::default(), - lit: lit(Literal::integer(5)), + lit: lit(Literal::i32_unsuffixed(5)), }.into(), ), NestedMeta::Meta( @@ -148,7 +148,7 @@ fn test_meta_item_multiple() { MetaNameValue { ident: "name2".into(), eq_token: Default::default(), - lit: lit(Literal::integer(6)), + lit: lit(Literal::i32_unsuffixed(6)), }.into(), ), ], diff --git a/tests/test_round_trip.rs b/tests/test_round_trip.rs index 3313acb862..a6013ce243 100644 --- a/tests/test_round_trip.rs +++ b/tests/test_round_trip.rs @@ -79,7 +79,7 @@ fn test_round_trip() { }; let back = quote!(#krate).to_string(); - let equal = panic::catch_unwind(|| { + let equal = panic::catch_unwind(|| syntax::with_globals(|| { let sess = ParseSess::new(FilePathMapping::empty()); let before = match libsyntax_parse(content, &sess) { Ok(before) => before, @@ -125,7 +125,7 @@ fn test_round_trip() { ); false } - }); + })); match equal { Err(_) => errorf!("=== {}: ignoring libsyntax panic\n", path.display()), Ok(true) => {} @@ -146,7 +146,5 @@ fn test_round_trip() { fn libsyntax_parse(content: String, sess: &ParseSess) -> PResult { let name = FileName::Custom("test_round_trip".to_string()); - syntax::with_globals(|| { - parse::parse_crate_from_source_str(name, content, sess).map(common::respan::respan_crate) - }) + parse::parse_crate_from_source_str(name, content, sess).map(common::respan::respan_crate) } diff --git a/tests/test_token_trees.rs b/tests/test_token_trees.rs index 0047ae2873..6fbb4aea5e 100644 --- a/tests/test_token_trees.rs +++ b/tests/test_token_trees.rs @@ -14,35 +14,23 @@ extern crate quote; extern crate syn; use syn::{AttrStyle, Attribute, Lit}; -use proc_macro2::{Delimiter, Spacing, Span, Term, TokenNode, TokenStream, TokenTree}; +use proc_macro2::{Delimiter, Spacing, Span, Term, Op, Group, TokenStream, TokenTree}; use proc_macro2::Delimiter::*; fn alone(c: char) -> TokenTree { - proc_macro2::TokenTree { - span: Span::def_site(), - kind: TokenNode::Op(c, Spacing::Alone), - } + Op::new(c, Spacing::Alone).into() } fn joint(c: char) -> TokenTree { - proc_macro2::TokenTree { - span: Span::def_site(), - kind: TokenNode::Op(c, Spacing::Joint), - } + Op::new(c, Spacing::Joint).into() } fn delimited(delim: Delimiter, tokens: Vec) -> TokenTree { - proc_macro2::TokenTree { - span: Span::def_site(), - kind: TokenNode::Group(delim, tokens.into_iter().collect()), - } + Group::new(delim, tokens.into_iter().collect()).into() } fn word(sym: &str) -> TokenTree { - proc_macro2::TokenTree { - span: Span::def_site(), - kind: TokenNode::Term(Term::intern(sym)), - } + Term::new(sym, Span::call_site()).into() } #[test]