From d2b65e0a5d65b0b5b5dbcb0b560e4e7ed9817002 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Wed, 28 Mar 2018 10:42:53 +0200 Subject: [PATCH] Prefer call site spans --- serde_derive/src/bound.rs | 2 +- serde_derive/src/de.rs | 22 +++++++++++----------- serde_derive/src/lib.rs | 8 ++++++++ serde_derive/src/ser.rs | 22 +++++++++++----------- serde_derive_internals/src/attr.rs | 16 ++++++++-------- 5 files changed, 39 insertions(+), 31 deletions(-) diff --git a/serde_derive/src/bound.rs b/serde_derive/src/bound.rs index 0cb3e4ec..d6346bfa 100644 --- a/serde_derive/src/bound.rs +++ b/serde_derive/src/bound.rs @@ -249,7 +249,7 @@ pub fn with_self_bound( } pub fn with_lifetime_bound(generics: &syn::Generics, lifetime: &str) -> syn::Generics { - let bound = syn::Lifetime::new(Term::intern(lifetime), Span::def_site()); + let bound = syn::Lifetime::new(Term::intern(lifetime), Span::call_site()); let def = syn::LifetimeDef { attrs: Vec::new(), lifetime: bound, diff --git a/serde_derive/src/de.rs b/serde_derive/src/de.rs index 09b2ce9f..cbdeb0a1 100644 --- a/serde_derive/src/de.rs +++ b/serde_derive/src/de.rs @@ -27,7 +27,7 @@ pub fn expand_derive_deserialize(input: &syn::DeriveInput) -> Result syn::Lifetime { match *self { - BorrowedLifetimes::Borrowed(_) => syn::Lifetime::new(Term::intern("'de"), Span::def_site()), - BorrowedLifetimes::Static => syn::Lifetime::new(Term::intern("'static"), Span::def_site()), + BorrowedLifetimes::Borrowed(_) => syn::Lifetime::new(Term::intern("'de"), Span::call_site()), + BorrowedLifetimes::Static => syn::Lifetime::new(Term::intern("'static"), Span::call_site()), } } @@ -190,7 +190,7 @@ impl BorrowedLifetimes { match *self { BorrowedLifetimes::Borrowed(ref bounds) => Some(syn::LifetimeDef { attrs: Vec::new(), - lifetime: syn::Lifetime::new(Term::intern("'de"), Span::def_site()), + lifetime: syn::Lifetime::new(Term::intern("'de"), Span::call_site()), colon_token: None, bounds: bounds.iter().cloned().collect(), }), @@ -549,7 +549,7 @@ fn deserialize_seq( let visit = match field.attrs.deserialize_with() { None => { let field_ty = &field.ty; - let span = Span::def_site().located_at(field.original.span()); + let span = Span::call_site().located_at(field.original.span()); let func = quote_spanned!(span=> _serde::de::SeqAccess::next_element::<#field_ty>); quote!(try!(#func(&mut __seq))) } @@ -2193,7 +2193,7 @@ fn deserialize_map( let visit = match field.attrs.deserialize_with() { None => { let field_ty = &field.ty; - let span = Span::def_site().located_at(field.original.span()); + let span = Span::call_site().located_at(field.original.span()); let func = quote_spanned!(span=> _serde::de::MapAccess::next_value::<#field_ty>); quote! { try!(#func(&mut __map)) @@ -2522,7 +2522,7 @@ fn deserialize_map_in_place( } fn field_i(i: usize) -> Ident { - Ident::new(&format!("__field{}", i), Span::def_site()) + Ident::new(&format!("__field{}", i), Span::call_site()) } /// This function wraps the expression in `#[serde(deserialize_with = "...")]` @@ -2638,7 +2638,7 @@ fn expr_is_missing(field: &Field, cattrs: &attr::Container) -> Fragment { let name = field.attrs.name().deserialize_name(); match field.attrs.deserialize_with() { None => { - let span = Span::def_site().located_at(field.original.span()); + let span = Span::call_site().located_at(field.original.span()); let func = quote_spanned!(span=> _serde::private::de::missing_field); quote_expr! { try!(#func(#name)) @@ -2721,7 +2721,7 @@ impl<'a> ToTokens for DeTypeGenerics<'a> { if self.0.borrowed.de_lifetime_def().is_some() { let def = syn::LifetimeDef { attrs: Vec::new(), - lifetime: syn::Lifetime::new(Term::intern("'de"), Span::def_site()), + lifetime: syn::Lifetime::new(Term::intern("'de"), Span::call_site()), colon_token: None, bounds: Punctuated::new(), }; @@ -2747,7 +2747,7 @@ impl<'a> ToTokens for InPlaceTypeGenerics<'a> { if self.0.borrowed.de_lifetime_def().is_some() { let def = syn::LifetimeDef { attrs: Vec::new(), - lifetime: syn::Lifetime::new(Term::intern("'de"), Span::def_site()), + lifetime: syn::Lifetime::new(Term::intern("'de"), Span::call_site()), colon_token: None, bounds: Punctuated::new(), }; @@ -2772,7 +2772,7 @@ impl<'a> DeTypeGenerics<'a> { fn place_lifetime() -> syn::LifetimeDef { syn::LifetimeDef { attrs: Vec::new(), - lifetime: syn::Lifetime::new(Term::intern("'place"), Span::def_site()), + lifetime: syn::Lifetime::new(Term::intern("'place"), Span::call_site()), colon_token: None, bounds: Punctuated::new(), } diff --git a/serde_derive/src/lib.rs b/serde_derive/src/lib.rs index 3387f3f8..81323081 100644 --- a/serde_derive/src/lib.rs +++ b/serde_derive/src/lib.rs @@ -41,6 +41,14 @@ extern crate proc_macro2; use proc_macro::TokenStream; use syn::DeriveInput; +// Quote's default is def_site but it appears call_site is likely to stabilize +// before def_site. Thus we try to use only call_site. +macro_rules! quote { + ($($tt:tt)*) => { + quote_spanned!($crate::proc_macro2::Span::call_site()=> $($tt)*) + } +} + #[macro_use] mod bound; #[macro_use] diff --git a/serde_derive/src/ser.rs b/serde_derive/src/ser.rs index 2b85c125..8cdc84eb 100644 --- a/serde_derive/src/ser.rs +++ b/serde_derive/src/ser.rs @@ -27,7 +27,7 @@ pub fn expand_derive_serialize(input: &syn::DeriveInput) -> Result Self { let is_remote = cont.attrs.remote().is_some(); let self_var = if is_remote { - Ident::new("__self", Span::def_site()) + Ident::new("__self", Span::call_site()) } else { - Ident::new("self", Span::def_site()) + Ident::new("self", Span::call_site()) }; let this = match cont.attrs.remote() { @@ -212,7 +212,7 @@ fn serialize_newtype_struct( field_expr = wrap_serialize_field_with(params, field.ty, path, &field_expr); } - let span = Span::def_site().located_at(field.original.span()); + let span = Span::call_site().located_at(field.original.span()); let func = quote_spanned!(span=> _serde::Serializer::serialize_newtype_struct); quote_expr! { #func(__serializer, #type_name, #field_expr) @@ -386,7 +386,7 @@ fn serialize_variant( } Style::Tuple => { let field_names = - (0..variant.fields.len()).map(|i| Ident::new(&format!("__field{}", i), Span::def_site())); + (0..variant.fields.len()).map(|i| Ident::new(&format!("__field{}", i), Span::call_site())); quote! { #this::#variant_ident(#(ref #field_names),*) } @@ -619,9 +619,9 @@ fn serialize_adjacently_tagged_variant( unreachable!() } } - Style::Newtype => vec![Ident::new("__field0", Span::def_site())], + Style::Newtype => vec![Ident::new("__field0", Span::call_site())], Style::Tuple => (0..variant.fields.len()) - .map(|i| Ident::new(&format!("__field{}", i), Span::def_site())) + .map(|i| Ident::new(&format!("__field{}", i), Span::call_site())) .collect(), Style::Struct => variant .fields @@ -862,7 +862,7 @@ fn serialize_tuple_struct_visitor( .enumerate() .map(|(i, field)| { let mut field_expr = if is_enum { - let id = Ident::new(&format!("__field{}", i), Span::def_site()); + let id = Ident::new(&format!("__field{}", i), Span::call_site()); quote!(#id) } else { get_member(params, field, &Member::Unnamed(Index { @@ -880,7 +880,7 @@ fn serialize_tuple_struct_visitor( field_expr = wrap_serialize_field_with(params, field.ty, path, &field_expr); } - let span = Span::def_site().located_at(field.original.span()); + let span = Span::call_site().located_at(field.original.span()); let func = tuple_trait.serialize_element(span); let ser = quote! { try!(#func(&mut __serde_state, #field_expr)); @@ -923,7 +923,7 @@ fn serialize_struct_visitor( field_expr = wrap_serialize_field_with(params, field.ty, path, &field_expr); } - let span = Span::def_site().located_at(field.original.span()); + let span = Span::call_site().located_at(field.original.span()); let ser = if field.attrs.flatten() { quote! { try!(_serde::Serialize::serialize(&#field_expr, _serde::private::ser::FlatMapSerializer(&mut __serde_state))); @@ -981,7 +981,7 @@ fn wrap_serialize_variant_with( .map(|(i, field)| { let id = field .ident - .unwrap_or_else(|| Ident::new(&format!("__field{}", i), Span::def_site())); + .unwrap_or_else(|| Ident::new(&format!("__field{}", i), Span::call_site())); quote!(#id) }) .collect(); diff --git a/serde_derive_internals/src/attr.rs b/serde_derive_internals/src/attr.rs index f6c5babf..b061b8cb 100644 --- a/serde_derive_internals/src/attr.rs +++ b/serde_derive_internals/src/attr.rs @@ -939,10 +939,10 @@ impl Field { leading_colon: None, segments: Punctuated::new(), }; - path.segments.push(Ident::new("_serde", Span::def_site()).into()); - path.segments.push(Ident::new("private", Span::def_site()).into()); - path.segments.push(Ident::new("de", Span::def_site()).into()); - path.segments.push(Ident::new("borrow_cow_str", Span::def_site()).into()); + path.segments.push(Ident::new("_serde", Span::call_site()).into()); + path.segments.push(Ident::new("private", Span::call_site()).into()); + path.segments.push(Ident::new("de", Span::call_site()).into()); + path.segments.push(Ident::new("borrow_cow_str", Span::call_site()).into()); let expr = syn::ExprPath { attrs: Vec::new(), qself: None, @@ -954,10 +954,10 @@ impl Field { leading_colon: None, segments: Punctuated::new(), }; - path.segments.push(Ident::new("_serde", Span::def_site()).into()); - path.segments.push(Ident::new("private", Span::def_site()).into()); - path.segments.push(Ident::new("de", Span::def_site()).into()); - path.segments.push(Ident::new("borrow_cow_bytes", Span::def_site()).into()); + path.segments.push(Ident::new("_serde", Span::call_site()).into()); + path.segments.push(Ident::new("private", Span::call_site()).into()); + path.segments.push(Ident::new("de", Span::call_site()).into()); + path.segments.push(Ident::new("borrow_cow_bytes", Span::call_site()).into()); let expr = syn::ExprPath { attrs: Vec::new(), qself: None,