Prefer call site spans

This commit is contained in:
David Tolnay 2018-03-28 10:42:53 +02:00
parent 7c04c98e0e
commit d2b65e0a5d
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
5 changed files with 39 additions and 31 deletions

View File

@ -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,

View File

@ -27,7 +27,7 @@ pub fn expand_derive_deserialize(input: &syn::DeriveInput) -> Result<Tokens, Str
let ident = &cont.ident;
let params = Parameters::new(&cont);
let (de_impl_generics, _, ty_generics, where_clause) = split_with_de_lifetime(&params);
let dummy_const = Ident::new(&format!("_IMPL_DESERIALIZE_FOR_{}", ident), Span::def_site());
let dummy_const = Ident::new(&format!("_IMPL_DESERIALIZE_FOR_{}", ident), Span::call_site());
let body = Stmts(deserialize_body(&cont, &params));
let delife = params.borrowed.de_lifetime();
@ -181,8 +181,8 @@ enum BorrowedLifetimes {
impl BorrowedLifetimes {
fn de_lifetime(&self) -> 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(),
}

View File

@ -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]

View File

@ -27,7 +27,7 @@ pub fn expand_derive_serialize(input: &syn::DeriveInput) -> Result<Tokens, Strin
let ident = &cont.ident;
let params = Parameters::new(&cont);
let (impl_generics, ty_generics, where_clause) = params.generics.split_for_impl();
let dummy_const = Ident::new(&format!("_IMPL_SERIALIZE_FOR_{}", ident), Span::def_site());
let dummy_const = Ident::new(&format!("_IMPL_SERIALIZE_FOR_{}", ident), Span::call_site());
let body = Stmts(serialize_body(&cont, &params));
let impl_block = if let Some(remote) = cont.attrs.remote() {
@ -98,9 +98,9 @@ impl Parameters {
fn new(cont: &Container) -> 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();

View File

@ -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,