Merge pull request #1424 from hcpl/spanned-error-messages

Use more spans for error messages
This commit is contained in:
David Tolnay
2018-12-01 23:16:49 -08:00
committed by GitHub
125 changed files with 1285 additions and 449 deletions
+1 -1
View File
@@ -26,7 +26,7 @@ proc-macro = true
[dependencies]
proc-macro2 = "0.4"
quote = "0.6.3"
syn = { version = "0.15", features = ["visit"] }
syn = { version = "0.15.22", features = ["visit"] }
[dev-dependencies]
serde = { version = "1.0", path = "../serde" }
+30 -14
View File
@@ -13,9 +13,12 @@ use try;
use std::collections::BTreeSet;
pub fn expand_derive_deserialize(input: &syn::DeriveInput) -> Result<TokenStream, String> {
pub fn expand_derive_deserialize(input: &syn::DeriveInput) -> Result<TokenStream, Vec<syn::Error>> {
let ctxt = Ctxt::new();
let cont = Container::from_ast(&ctxt, input, Derive::Deserialize);
let cont = match Container::from_ast(&ctxt, input, Derive::Deserialize) {
Some(cont) => cont,
None => return Err(ctxt.check().unwrap_err()),
};
precondition(&ctxt, &cont);
try!(ctxt.check());
@@ -86,7 +89,7 @@ fn precondition_sized(cx: &Ctxt, cont: &Container) {
if let Data::Struct(_, ref fields) = cont.data {
if let Some(last) = fields.last() {
if let syn::Type::Slice(_) = *last.ty {
cx.error("cannot deserialize a dynamically sized struct");
cx.error_spanned_by(cont.original, "cannot deserialize a dynamically sized struct");
}
}
}
@@ -96,7 +99,10 @@ fn precondition_no_de_lifetime(cx: &Ctxt, cont: &Container) {
if let BorrowedLifetimes::Borrowed(_) = borrowed_lifetimes(cont) {
for param in cont.generics.lifetimes() {
if param.lifetime.to_string() == "'de" {
cx.error("cannot deserialize when there is a lifetime parameter called 'de");
cx.error_spanned_by(
&param.lifetime,
"cannot deserialize when there is a lifetime parameter called 'de",
);
return;
}
}
@@ -357,7 +363,10 @@ fn deserialize_transparent(cont: &Container, params: &Parameters) -> Fragment {
let path = match transparent_field.attrs.deserialize_with() {
Some(path) => quote!(#path),
None => quote!(_serde::Deserialize::deserialize),
None => {
let span = transparent_field.original.span();
quote_spanned!(span=> _serde::Deserialize::deserialize)
},
};
let assign = fields.iter().map(|field| {
@@ -797,8 +806,10 @@ fn deserialize_newtype_struct(
let value = match field.attrs.deserialize_with() {
None => {
let span = field.original.span();
let func = quote_spanned!(span=> <#field_ty as _serde::Deserialize>::deserialize);
quote! {
try!(<#field_ty as _serde::Deserialize>::deserialize(__e))
try!(#func(__e))
}
}
Some(path) => {
@@ -1803,10 +1814,10 @@ fn deserialize_externally_tagged_newtype_variant(
match field.attrs.deserialize_with() {
None => {
let field_ty = field.ty;
let span = field.original.span();
let func = quote_spanned!(span=> _serde::de::VariantAccess::newtype_variant::<#field_ty>);
quote_expr! {
_serde::export::Result::map(
_serde::de::VariantAccess::newtype_variant::<#field_ty>(__variant),
#this::#variant_ident)
_serde::export::Result::map(#func(__variant), #this::#variant_ident)
}
}
Some(path) => {
@@ -1831,10 +1842,10 @@ fn deserialize_untagged_newtype_variant(
let field_ty = field.ty;
match field.attrs.deserialize_with() {
None => {
let span = field.original.span();
let func = quote_spanned!(span=> <#field_ty as _serde::Deserialize>::deserialize);
quote_expr! {
_serde::export::Result::map(
<#field_ty as _serde::Deserialize>::deserialize(#deserializer),
#this::#variant_ident)
_serde::export::Result::map(#func(#deserializer), #this::#variant_ident)
}
}
Some(path) => {
@@ -2441,7 +2452,10 @@ fn deserialize_map(
.map(|&(field, ref name)| {
let field_ty = field.ty;
let func = match field.attrs.deserialize_with() {
None => quote!(_serde::de::Deserialize::deserialize),
None => {
let span = field.original.span();
quote_spanned!(span=> _serde::de::Deserialize::deserialize)
},
Some(path) => quote!(#path),
};
quote! {
@@ -2793,7 +2807,9 @@ fn wrap_deserialize_variant_with(
fn expr_is_missing(field: &Field, cattrs: &attr::Container) -> Fragment {
match *field.attrs.default() {
attr::Default::Default => {
return quote_expr!(_serde::export::Default::default());
let span = field.original.span();
let func = quote_spanned!(span=> _serde::export::Default::default);
return quote_expr!(#func());
}
attr::Default::Path(ref path) => {
return quote_expr!(#path());
+9 -3
View File
@@ -17,6 +17,8 @@ pub struct Container<'a> {
pub data: Data<'a>,
/// Any generics on the struct or enum.
pub generics: &'a syn::Generics,
/// Original input.
pub original: &'a syn::DeriveInput,
}
/// The fields of a struct or enum.
@@ -33,6 +35,7 @@ pub struct Variant<'a> {
pub attrs: attr::Variant,
pub style: Style,
pub fields: Vec<Field<'a>>,
pub original: &'a syn::Variant,
}
/// A field of a struct.
@@ -57,7 +60,7 @@ pub enum Style {
impl<'a> Container<'a> {
/// Convert the raw Syn ast into a parsed container object, collecting errors in `cx`.
pub fn from_ast(cx: &Ctxt, item: &'a syn::DeriveInput, derive: Derive) -> Container<'a> {
pub fn from_ast(cx: &Ctxt, item: &'a syn::DeriveInput, derive: Derive) -> Option<Container<'a>> {
let mut attrs = attr::Container::from_ast(cx, item);
let mut data = match item.data {
@@ -69,7 +72,8 @@ impl<'a> Container<'a> {
Data::Struct(style, fields)
}
syn::Data::Union(_) => {
panic!("Serde does not support derive for unions");
cx.error_spanned_by(item, "Serde does not support derive for unions");
return None;
}
};
@@ -105,9 +109,10 @@ impl<'a> Container<'a> {
attrs: attrs,
data: data,
generics: &item.generics,
original: item,
};
check::check(cx, &mut item, derive);
item
Some(item)
}
}
@@ -142,6 +147,7 @@ fn enum_from_ast<'a>(
attrs: attrs,
style: style,
fields: fields,
original: variant,
}
})
.collect()
+290 -152
View File
@@ -1,5 +1,6 @@
use internals::Ctxt;
use proc_macro2::{Group, Span, TokenStream, TokenTree};
use quote::ToTokens;
use std::collections::BTreeSet;
use std::str::FromStr;
use syn;
@@ -19,10 +20,11 @@ use syn::NestedMeta::{Literal, Meta};
pub use internals::case::RenameRule;
#[derive(Copy, Clone)]
#[derive(Clone)]
struct Attr<'c, T> {
cx: &'c Ctxt,
name: &'static str,
tokens: TokenStream,
value: Option<T>,
}
@@ -31,22 +33,28 @@ impl<'c, T> Attr<'c, T> {
Attr {
cx: cx,
name: name,
tokens: TokenStream::new(),
value: None,
}
}
fn set(&mut self, value: T) {
fn set<A: ToTokens>(&mut self, obj: A, value: T) {
let tokens = obj.into_token_stream();
if self.value.is_some() {
self.cx
.error(format!("duplicate serde attribute `{}`", self.name));
self.cx.error_spanned_by(
tokens,
format!("duplicate serde attribute `{}`", self.name),
);
} else {
self.tokens = tokens;
self.value = Some(value);
}
}
fn set_opt(&mut self, value: Option<T>) {
fn set_opt<A: ToTokens>(&mut self, obj: A, value: Option<T>) {
if let Some(value) = value {
self.set(value);
self.set(obj, value);
}
}
@@ -59,6 +67,13 @@ impl<'c, T> Attr<'c, T> {
fn get(self) -> Option<T> {
self.value
}
fn get_with_tokens(self) -> Option<(TokenStream, T)> {
match self.value {
Some(v) => Some((self.tokens, v)),
None => None,
}
}
}
struct BoolAttr<'c>(Attr<'c, ()>);
@@ -68,8 +83,8 @@ impl<'c> BoolAttr<'c> {
BoolAttr(Attr::none(cx, name))
}
fn set_true(&mut self) {
self.0.set(());
fn set_true<A: ToTokens>(&mut self, obj: A) {
self.0.set(obj, ());
}
fn get(&self) -> bool {
@@ -199,16 +214,16 @@ impl Container {
// Parse `#[serde(rename = "foo")]`
Meta(NameValue(ref m)) if m.ident == "rename" => {
if let Ok(s) = get_lit_str(cx, &m.ident, &m.ident, &m.lit) {
ser_name.set(s.value());
de_name.set(s.value());
ser_name.set(&m.ident, s.value());
de_name.set(&m.ident, s.value());
}
}
// Parse `#[serde(rename(serialize = "foo", deserialize = "bar"))]`
Meta(List(ref m)) if m.ident == "rename" => {
if let Ok((ser, de)) = get_renames(cx, &m.nested) {
ser_name.set_opt(ser.map(syn::LitStr::value));
de_name.set_opt(de.map(syn::LitStr::value));
ser_name.set_opt(&m.ident, ser.map(syn::LitStr::value));
de_name.set_opt(&m.ident, de.map(syn::LitStr::value));
}
}
@@ -216,11 +231,11 @@ impl Container {
Meta(NameValue(ref m)) if m.ident == "rename_all" => {
if let Ok(s) = get_lit_str(cx, &m.ident, &m.ident, &m.lit) {
match RenameRule::from_str(&s.value()) {
Ok(rename_rule) => rename_all.set(rename_rule),
Err(()) => cx.error(format!(
Ok(rename_rule) => rename_all.set(&m.ident, rename_rule),
Err(()) => cx.error_spanned_by(s, format!(
"unknown rename rule for #[serde(rename_all \
= {:?})]",
s.value()
s.value(),
)),
}
}
@@ -228,23 +243,33 @@ impl Container {
// Parse `#[serde(transparent)]`
Meta(Word(ref word)) if word == "transparent" => {
transparent.set_true();
transparent.set_true(word);
}
// Parse `#[serde(deny_unknown_fields)]`
Meta(Word(ref word)) if word == "deny_unknown_fields" => {
deny_unknown_fields.set_true();
deny_unknown_fields.set_true(word);
}
// Parse `#[serde(default)]`
Meta(Word(ref word)) if word == "default" => match item.data {
syn::Data::Struct(syn::DataStruct {
fields: syn::Fields::Named(_),
..
}) => {
default.set(Default::Default);
}
_ => cx.error(
syn::Data::Struct(syn::DataStruct { ref fields, .. }) => match *fields {
syn::Fields::Named(_) => {
default.set(word, Default::Default);
}
syn::Fields::Unnamed(_) | syn::Fields::Unit => cx.error_spanned_by(
fields,
"#[serde(default)] can only be used on structs \
with named fields",
)
},
syn::Data::Enum(syn::DataEnum { ref enum_token, .. }) => cx.error_spanned_by(
enum_token,
"#[serde(default)] can only be used on structs \
with named fields",
),
syn::Data::Union(syn::DataUnion { ref union_token, .. }) => cx.error_spanned_by(
union_token,
"#[serde(default)] can only be used on structs \
with named fields",
),
@@ -254,13 +279,23 @@ impl Container {
Meta(NameValue(ref m)) if m.ident == "default" => {
if let Ok(path) = parse_lit_into_expr_path(cx, &m.ident, &m.lit) {
match item.data {
syn::Data::Struct(syn::DataStruct {
fields: syn::Fields::Named(_),
..
}) => {
default.set(Default::Path(path));
}
_ => cx.error(
syn::Data::Struct(syn::DataStruct { ref fields, .. }) => match *fields {
syn::Fields::Named(_) => {
default.set(&m.ident, Default::Path(path));
}
syn::Fields::Unnamed(_) | syn::Fields::Unit => cx.error_spanned_by(
fields,
"#[serde(default = \"...\")] can only be used \
on structs with named fields",
)
},
syn::Data::Enum(syn::DataEnum { ref enum_token, .. }) => cx.error_spanned_by(
enum_token,
"#[serde(default = \"...\")] can only be used \
on structs with named fields",
),
syn::Data::Union(syn::DataUnion { ref union_token, .. }) => cx.error_spanned_by(
union_token,
"#[serde(default = \"...\")] can only be used \
on structs with named fields",
),
@@ -273,26 +308,35 @@ impl Container {
if let Ok(where_predicates) =
parse_lit_into_where(cx, &m.ident, &m.ident, &m.lit)
{
ser_bound.set(where_predicates.clone());
de_bound.set(where_predicates);
ser_bound.set(&m.ident, where_predicates.clone());
de_bound.set(&m.ident, where_predicates);
}
}
// Parse `#[serde(bound(serialize = "...", deserialize = "..."))]`
Meta(List(ref m)) if m.ident == "bound" => {
if let Ok((ser, de)) = get_where_predicates(cx, &m.nested) {
ser_bound.set_opt(ser);
de_bound.set_opt(de);
ser_bound.set_opt(&m.ident, ser);
de_bound.set_opt(&m.ident, de);
}
}
// Parse `#[serde(untagged)]`
Meta(Word(ref word)) if word == "untagged" => match item.data {
syn::Data::Enum(_) => {
untagged.set_true();
untagged.set_true(word);
}
syn::Data::Struct(_) | syn::Data::Union(_) => {
cx.error("#[serde(untagged)] can only be used on enums")
syn::Data::Struct(syn::DataStruct { ref struct_token, .. }) => {
cx.error_spanned_by(
struct_token,
"#[serde(untagged)] can only be used on enums",
);
}
syn::Data::Union(syn::DataUnion { ref union_token, .. }) => {
cx.error_spanned_by(
union_token,
"#[serde(untagged)] can only be used on enums",
);
}
},
@@ -301,11 +345,20 @@ impl Container {
if let Ok(s) = get_lit_str(cx, &m.ident, &m.ident, &m.lit) {
match item.data {
syn::Data::Enum(_) => {
internal_tag.set(s.value());
}
syn::Data::Struct(_) | syn::Data::Union(_) => {
cx.error("#[serde(tag = \"...\")] can only be used on enums")
internal_tag.set(&m.ident, s.value());
}
syn::Data::Struct(syn::DataStruct { ref struct_token, .. }) => {
cx.error_spanned_by(
struct_token,
"#[serde(tag = \"...\")] can only be used on enums",
);
},
syn::Data::Union(syn::DataUnion { ref union_token, .. }) => {
cx.error_spanned_by(
union_token,
"#[serde(tag = \"...\")] can only be used on enums",
);
},
}
}
}
@@ -315,12 +368,20 @@ impl Container {
if let Ok(s) = get_lit_str(cx, &m.ident, &m.ident, &m.lit) {
match item.data {
syn::Data::Enum(_) => {
content.set(s.value());
content.set(&m.ident, s.value());
}
syn::Data::Struct(_) | syn::Data::Union(_) => cx.error(
"#[serde(content = \"...\")] can only be used on \
enums",
),
syn::Data::Struct(syn::DataStruct { ref struct_token, .. }) => {
cx.error_spanned_by(
struct_token,
"#[serde(content = \"...\")] can only be used on enums",
);
},
syn::Data::Union(syn::DataUnion { ref union_token, .. }) => {
cx.error_spanned_by(
union_token,
"#[serde(content = \"...\")] can only be used on enums",
);
},
}
}
}
@@ -328,14 +389,14 @@ impl Container {
// Parse `#[serde(from = "Type")]
Meta(NameValue(ref m)) if m.ident == "from" => {
if let Ok(from_ty) = parse_lit_into_ty(cx, &m.ident, &m.lit) {
type_from.set_opt(Some(from_ty));
type_from.set_opt(&m.ident, Some(from_ty));
}
}
// Parse `#[serde(into = "Type")]
Meta(NameValue(ref m)) if m.ident == "into" => {
if let Ok(into_ty) = parse_lit_into_ty(cx, &m.ident, &m.lit) {
type_into.set_opt(Some(into_ty));
type_into.set_opt(&m.ident, Some(into_ty));
}
}
@@ -343,32 +404,32 @@ impl Container {
Meta(NameValue(ref m)) if m.ident == "remote" => {
if let Ok(path) = parse_lit_into_path(cx, &m.ident, &m.lit) {
if is_primitive_path(&path, "Self") {
remote.set(item.ident.clone().into());
remote.set(&m.ident, item.ident.clone().into());
} else {
remote.set(path);
remote.set(&m.ident, path);
}
}
}
// Parse `#[serde(field_identifier)]`
Meta(Word(ref word)) if word == "field_identifier" => {
field_identifier.set_true();
field_identifier.set_true(word);
}
// Parse `#[serde(variant_identifier)]`
Meta(Word(ref word)) if word == "variant_identifier" => {
variant_identifier.set_true();
variant_identifier.set_true(word);
}
Meta(ref meta_item) => {
cx.error(format!(
cx.error_spanned_by(meta_item.name(), format!(
"unknown serde container attribute `{}`",
meta_item.name()
));
}
Literal(_) => {
cx.error("unexpected literal in serde container attribute");
Literal(ref lit) => {
cx.error_spanned_by(lit, "unexpected literal in serde container attribute");
}
}
}
@@ -385,11 +446,11 @@ impl Container {
rename_all: rename_all.get().unwrap_or(RenameRule::None),
ser_bound: ser_bound.get(),
de_bound: de_bound.get(),
tag: decide_tag(cx, item, &untagged, internal_tag, content),
tag: decide_tag(cx, item, untagged, internal_tag, content),
type_from: type_from.get(),
type_into: type_into.get(),
remote: remote.get(),
identifier: decide_identifier(cx, item, &field_identifier, &variant_identifier),
identifier: decide_identifier(cx, item, field_identifier, variant_identifier),
has_flatten: false,
}
}
@@ -454,14 +515,14 @@ impl Container {
fn decide_tag(
cx: &Ctxt,
item: &syn::DeriveInput,
untagged: &BoolAttr,
untagged: BoolAttr,
internal_tag: Attr<String>,
content: Attr<String>,
) -> EnumTag {
match (untagged.get(), internal_tag.get(), content.get()) {
(false, None, None) => EnumTag::External,
(true, None, None) => EnumTag::None,
(false, Some(tag), None) => {
match (untagged.0.get_with_tokens(), internal_tag.get_with_tokens(), content.get_with_tokens()) {
(None, None, None) => EnumTag::External,
(Some(_), None, None) => EnumTag::None,
(None, Some((_, tag)), None) => {
// Check that there are no tuple variants.
if let syn::Data::Enum(ref data) = item.data {
for variant in &data.variants {
@@ -469,7 +530,8 @@ fn decide_tag(
syn::Fields::Named(_) | syn::Fields::Unit => {}
syn::Fields::Unnamed(ref fields) => {
if fields.unnamed.len() != 1 {
cx.error(
cx.error_spanned_by(
variant,
"#[serde(tag = \"...\")] cannot be used with tuple \
variants",
);
@@ -481,24 +543,52 @@ fn decide_tag(
}
EnumTag::Internal { tag: tag }
}
(true, Some(_), None) => {
cx.error("enum cannot be both untagged and internally tagged");
(Some((untagged_tokens, _)), Some((tag_tokens, _)), None) => {
cx.error_spanned_by(
untagged_tokens,
"enum cannot be both untagged and internally tagged",
);
cx.error_spanned_by(
tag_tokens,
"enum cannot be both untagged and internally tagged",
);
EnumTag::External // doesn't matter, will error
}
(false, None, Some(_)) => {
cx.error("#[serde(tag = \"...\", content = \"...\")] must be used together");
(None, None, Some((content_tokens, _))) => {
cx.error_spanned_by(
content_tokens,
"#[serde(tag = \"...\", content = \"...\")] must be used together",
);
EnumTag::External
}
(true, None, Some(_)) => {
cx.error("untagged enum cannot have #[serde(content = \"...\")]");
(Some((untagged_tokens, _)), None, Some((content_tokens, _))) => {
cx.error_spanned_by(
untagged_tokens,
"untagged enum cannot have #[serde(content = \"...\")]",
);
cx.error_spanned_by(
content_tokens,
"untagged enum cannot have #[serde(content = \"...\")]",
);
EnumTag::External
}
(false, Some(tag), Some(content)) => EnumTag::Adjacent {
(None, Some((_, tag)), Some((_, content))) => EnumTag::Adjacent {
tag: tag,
content: content,
},
(true, Some(_), Some(_)) => {
cx.error("untagged enum cannot have #[serde(tag = \"...\", content = \"...\")]");
(Some((untagged_tokens, _)), Some((tag_tokens, _)), Some((content_tokens, _))) => {
cx.error_spanned_by(
untagged_tokens,
"untagged enum cannot have #[serde(tag = \"...\", content = \"...\")]",
);
cx.error_spanned_by(
tag_tokens,
"untagged enum cannot have #[serde(tag = \"...\", content = \"...\")]",
);
cx.error_spanned_by(
content_tokens,
"untagged enum cannot have #[serde(tag = \"...\", content = \"...\")]",
);
EnumTag::External
}
}
@@ -507,23 +597,50 @@ fn decide_tag(
fn decide_identifier(
cx: &Ctxt,
item: &syn::DeriveInput,
field_identifier: &BoolAttr,
variant_identifier: &BoolAttr,
field_identifier: BoolAttr,
variant_identifier: BoolAttr,
) -> Identifier {
match (&item.data, field_identifier.get(), variant_identifier.get()) {
(_, false, false) => Identifier::No,
(_, true, true) => {
cx.error("`field_identifier` and `variant_identifier` cannot both be set");
match (&item.data, field_identifier.0.get_with_tokens(), variant_identifier.0.get_with_tokens()) {
(_, None, None) => Identifier::No,
(_, Some((field_identifier_tokens, _)), Some((variant_identifier_tokens, _))) => {
cx.error_spanned_by(
field_identifier_tokens,
"#[serde(field_identifier)] and #[serde(variant_identifier)] cannot both be set",
);
cx.error_spanned_by(
variant_identifier_tokens,
"#[serde(field_identifier)] and #[serde(variant_identifier)] cannot both be set",
);
Identifier::No
}
(&syn::Data::Enum(_), true, false) => Identifier::Field,
(&syn::Data::Enum(_), false, true) => Identifier::Variant,
(&syn::Data::Struct(_), true, false) | (&syn::Data::Union(_), true, false) => {
cx.error("`field_identifier` can only be used on an enum");
(&syn::Data::Enum(_), Some(_), None) => Identifier::Field,
(&syn::Data::Enum(_), None, Some(_)) => Identifier::Variant,
(&syn::Data::Struct(syn::DataStruct { ref struct_token, .. }), Some(_), None) => {
cx.error_spanned_by(
struct_token,
"#[serde(field_identifier)] can only be used on an enum",
);
Identifier::No
}
(&syn::Data::Struct(_), false, true) | (&syn::Data::Union(_), false, true) => {
cx.error("`variant_identifier` can only be used on an enum");
(&syn::Data::Union(syn::DataUnion { ref union_token, .. }), Some(_), None) => {
cx.error_spanned_by(
union_token,
"#[serde(field_identifier)] can only be used on an enum",
);
Identifier::No
}
(&syn::Data::Struct(syn::DataStruct { ref struct_token, .. }), None, Some(_)) => {
cx.error_spanned_by(
struct_token,
"#[serde(variant_identifier)] can only be used on an enum",
);
Identifier::No
}
(&syn::Data::Union(syn::DataUnion { ref union_token, .. }), None, Some(_)) => {
cx.error_spanned_by(
union_token,
"#[serde(variant_identifier)] can only be used on an enum",
);
Identifier::No
}
}
@@ -565,16 +682,16 @@ impl Variant {
// Parse `#[serde(rename = "foo")]`
Meta(NameValue(ref m)) if m.ident == "rename" => {
if let Ok(s) = get_lit_str(cx, &m.ident, &m.ident, &m.lit) {
ser_name.set(s.value());
de_name.set(s.value());
ser_name.set(&m.ident, s.value());
de_name.set(&m.ident, s.value());
}
}
// Parse `#[serde(rename(serialize = "foo", deserialize = "bar"))]`
Meta(List(ref m)) if m.ident == "rename" => {
if let Ok((ser, de)) = get_renames(cx, &m.nested) {
ser_name.set_opt(ser.map(syn::LitStr::value));
de_name.set_opt(de.map(syn::LitStr::value));
ser_name.set_opt(&m.ident, ser.map(syn::LitStr::value));
de_name.set_opt(&m.ident, de.map(syn::LitStr::value));
}
}
@@ -582,8 +699,8 @@ impl Variant {
Meta(NameValue(ref m)) if m.ident == "rename_all" => {
if let Ok(s) = get_lit_str(cx, &m.ident, &m.ident, &m.lit) {
match RenameRule::from_str(&s.value()) {
Ok(rename_rule) => rename_all.set(rename_rule),
Err(()) => cx.error(format!(
Ok(rename_rule) => rename_all.set(&m.ident, rename_rule),
Err(()) => cx.error_spanned_by(s, format!(
"unknown rename rule for #[serde(rename_all \
= {:?})]",
s.value()
@@ -594,23 +711,23 @@ impl Variant {
// Parse `#[serde(skip)]`
Meta(Word(ref word)) if word == "skip" => {
skip_serializing.set_true();
skip_deserializing.set_true();
skip_serializing.set_true(word);
skip_deserializing.set_true(word);
}
// Parse `#[serde(skip_deserializing)]`
Meta(Word(ref word)) if word == "skip_deserializing" => {
skip_deserializing.set_true();
skip_deserializing.set_true(word);
}
// Parse `#[serde(skip_serializing)]`
Meta(Word(ref word)) if word == "skip_serializing" => {
skip_serializing.set_true();
skip_serializing.set_true(word);
}
// Parse `#[serde(other)]`
Meta(Word(ref word)) if word == "other" => {
other.set_true();
other.set_true(word);
}
// Parse `#[serde(bound = "T: SomeBound")]`
@@ -618,16 +735,16 @@ impl Variant {
if let Ok(where_predicates) =
parse_lit_into_where(cx, &m.ident, &m.ident, &m.lit)
{
ser_bound.set(where_predicates.clone());
de_bound.set(where_predicates);
ser_bound.set(&m.ident, where_predicates.clone());
de_bound.set(&m.ident, where_predicates);
}
}
// Parse `#[serde(bound(serialize = "...", deserialize = "..."))]`
Meta(List(ref m)) if m.ident == "bound" => {
if let Ok((ser, de)) = get_where_predicates(cx, &m.nested) {
ser_bound.set_opt(ser);
de_bound.set_opt(de);
ser_bound.set_opt(&m.ident, ser);
de_bound.set_opt(&m.ident, de);
}
}
@@ -639,49 +756,55 @@ impl Variant {
.path
.segments
.push(Ident::new("serialize", Span::call_site()).into());
serialize_with.set(ser_path);
serialize_with.set(&m.ident, ser_path);
let mut de_path = path;
de_path
.path
.segments
.push(Ident::new("deserialize", Span::call_site()).into());
deserialize_with.set(de_path);
deserialize_with.set(&m.ident, de_path);
}
}
// Parse `#[serde(serialize_with = "...")]`
Meta(NameValue(ref m)) if m.ident == "serialize_with" => {
if let Ok(path) = parse_lit_into_expr_path(cx, &m.ident, &m.lit) {
serialize_with.set(path);
serialize_with.set(&m.ident, path);
}
}
// Parse `#[serde(deserialize_with = "...")]`
Meta(NameValue(ref m)) if m.ident == "deserialize_with" => {
if let Ok(path) = parse_lit_into_expr_path(cx, &m.ident, &m.lit) {
deserialize_with.set(path);
deserialize_with.set(&m.ident, path);
}
}
// Defer `#[serde(borrow)]` and `#[serde(borrow = "'a + 'b")]`
Meta(ref m) if m.name() == "borrow" => match variant.fields {
syn::Fields::Unnamed(ref fields) if fields.unnamed.len() == 1 => {
borrow.set(m.clone());
borrow.set(m.name(), m.clone());
}
_ => {
cx.error("#[serde(borrow)] may only be used on newtype variants");
cx.error_spanned_by(
variant,
"#[serde(borrow)] may only be used on newtype variants",
);
}
},
Meta(ref meta_item) => {
cx.error(format!(
cx.error_spanned_by(meta_item.name(), format!(
"unknown serde variant attribute `{}`",
meta_item.name()
));
}
Literal(_) => {
cx.error("unexpected literal in serde variant attribute");
Literal(ref lit) => {
cx.error_spanned_by(
lit,
"unexpected literal in serde variant attribute",
);
}
}
}
@@ -837,65 +960,65 @@ impl Field {
// Parse `#[serde(rename = "foo")]`
Meta(NameValue(ref m)) if m.ident == "rename" => {
if let Ok(s) = get_lit_str(cx, &m.ident, &m.ident, &m.lit) {
ser_name.set(s.value());
de_name.set(s.value());
ser_name.set(&m.ident, s.value());
de_name.set(&m.ident, s.value());
}
}
// Parse `#[serde(rename(serialize = "foo", deserialize = "bar"))]`
Meta(List(ref m)) if m.ident == "rename" => {
if let Ok((ser, de)) = get_renames(cx, &m.nested) {
ser_name.set_opt(ser.map(syn::LitStr::value));
de_name.set_opt(de.map(syn::LitStr::value));
ser_name.set_opt(&m.ident, ser.map(syn::LitStr::value));
de_name.set_opt(&m.ident, de.map(syn::LitStr::value));
}
}
// Parse `#[serde(default)]`
Meta(Word(ref word)) if word == "default" => {
default.set(Default::Default);
default.set(word, Default::Default);
}
// Parse `#[serde(default = "...")]`
Meta(NameValue(ref m)) if m.ident == "default" => {
if let Ok(path) = parse_lit_into_expr_path(cx, &m.ident, &m.lit) {
default.set(Default::Path(path));
default.set(&m.ident, Default::Path(path));
}
}
// Parse `#[serde(skip_serializing)]`
Meta(Word(ref word)) if word == "skip_serializing" => {
skip_serializing.set_true();
skip_serializing.set_true(word);
}
// Parse `#[serde(skip_deserializing)]`
Meta(Word(ref word)) if word == "skip_deserializing" => {
skip_deserializing.set_true();
skip_deserializing.set_true(word);
}
// Parse `#[serde(skip)]`
Meta(Word(ref word)) if word == "skip" => {
skip_serializing.set_true();
skip_deserializing.set_true();
skip_serializing.set_true(word);
skip_deserializing.set_true(word);
}
// Parse `#[serde(skip_serializing_if = "...")]`
Meta(NameValue(ref m)) if m.ident == "skip_serializing_if" => {
if let Ok(path) = parse_lit_into_expr_path(cx, &m.ident, &m.lit) {
skip_serializing_if.set(path);
skip_serializing_if.set(&m.ident, path);
}
}
// Parse `#[serde(serialize_with = "...")]`
Meta(NameValue(ref m)) if m.ident == "serialize_with" => {
if let Ok(path) = parse_lit_into_expr_path(cx, &m.ident, &m.lit) {
serialize_with.set(path);
serialize_with.set(&m.ident, path);
}
}
// Parse `#[serde(deserialize_with = "...")]`
Meta(NameValue(ref m)) if m.ident == "deserialize_with" => {
if let Ok(path) = parse_lit_into_expr_path(cx, &m.ident, &m.lit) {
deserialize_with.set(path);
deserialize_with.set(&m.ident, path);
}
}
@@ -907,13 +1030,13 @@ impl Field {
.path
.segments
.push(Ident::new("serialize", Span::call_site()).into());
serialize_with.set(ser_path);
serialize_with.set(&m.ident, ser_path);
let mut de_path = path;
de_path
.path
.segments
.push(Ident::new("deserialize", Span::call_site()).into());
deserialize_with.set(de_path);
deserialize_with.set(&m.ident, de_path);
}
}
@@ -922,39 +1045,39 @@ impl Field {
if let Ok(where_predicates) =
parse_lit_into_where(cx, &m.ident, &m.ident, &m.lit)
{
ser_bound.set(where_predicates.clone());
de_bound.set(where_predicates);
ser_bound.set(&m.ident, where_predicates.clone());
de_bound.set(&m.ident, where_predicates);
}
}
// Parse `#[serde(bound(serialize = "...", deserialize = "..."))]`
Meta(List(ref m)) if m.ident == "bound" => {
if let Ok((ser, de)) = get_where_predicates(cx, &m.nested) {
ser_bound.set_opt(ser);
de_bound.set_opt(de);
ser_bound.set_opt(&m.ident, ser);
de_bound.set_opt(&m.ident, de);
}
}
// Parse `#[serde(borrow)]`
Meta(Word(ref word)) if word == "borrow" => {
if let Ok(borrowable) = borrowable_lifetimes(cx, &ident, &field.ty) {
borrowed_lifetimes.set(borrowable);
if let Ok(borrowable) = borrowable_lifetimes(cx, &ident, field) {
borrowed_lifetimes.set(word, borrowable);
}
}
// Parse `#[serde(borrow = "'a + 'b")]`
Meta(NameValue(ref m)) if m.ident == "borrow" => {
if let Ok(lifetimes) = parse_lit_into_lifetimes(cx, &m.ident, &m.lit) {
if let Ok(borrowable) = borrowable_lifetimes(cx, &ident, &field.ty) {
if let Ok(borrowable) = borrowable_lifetimes(cx, &ident, field) {
for lifetime in &lifetimes {
if !borrowable.contains(lifetime) {
cx.error(format!(
cx.error_spanned_by(field, format!(
"field `{}` does not have lifetime {}",
ident, lifetime
));
}
}
borrowed_lifetimes.set(lifetimes);
borrowed_lifetimes.set(&m.ident, lifetimes);
}
}
}
@@ -962,24 +1085,27 @@ impl Field {
// Parse `#[serde(getter = "...")]`
Meta(NameValue(ref m)) if m.ident == "getter" => {
if let Ok(path) = parse_lit_into_expr_path(cx, &m.ident, &m.lit) {
getter.set(path);
getter.set(&m.ident, path);
}
}
// Parse `#[serde(flatten)]`
Meta(Word(ref word)) if word == "flatten" => {
flatten.set_true();
flatten.set_true(word);
}
Meta(ref meta_item) => {
cx.error(format!(
cx.error_spanned_by(meta_item.name(), format!(
"unknown serde field attribute `{}`",
meta_item.name()
));
}
Literal(_) => {
cx.error("unexpected literal in serde field attribute");
Literal(ref lit) => {
cx.error_spanned_by(
lit,
"unexpected literal in serde field attribute",
);
}
}
}
@@ -1162,18 +1288,18 @@ where
match *meta {
Meta(NameValue(ref meta)) if meta.ident == "serialize" => {
if let Ok(v) = f(cx, &attr_name, &meta.ident, &meta.lit) {
ser_meta.set(v);
ser_meta.set(&meta.ident, v);
}
}
Meta(NameValue(ref meta)) if meta.ident == "deserialize" => {
if let Ok(v) = f(cx, &attr_name, &meta.ident, &meta.lit) {
de_meta.set(v);
de_meta.set(&meta.ident, v);
}
}
_ => {
cx.error(format!(
cx.error_spanned_by(meta, format!(
"malformed {0} attribute, expected `{0}(serialize = ..., \
deserialize = ...)`",
attr_name
@@ -1223,7 +1349,7 @@ fn get_lit_str<'a>(
if let syn::Lit::Str(ref lit) = *lit {
Ok(lit)
} else {
cx.error(format!(
cx.error_spanned_by(lit, format!(
"expected serde {} attribute to be a string: `{} = \"...\"`",
attr_name, meta_item_name
));
@@ -1234,7 +1360,10 @@ fn get_lit_str<'a>(
fn parse_lit_into_path(cx: &Ctxt, attr_name: &Ident, lit: &syn::Lit) -> Result<syn::Path, ()> {
let string = try!(get_lit_str(cx, attr_name, attr_name, lit));
parse_lit_str(string)
.map_err(|_| cx.error(format!("failed to parse path: {:?}", string.value())))
.map_err(|_| cx.error_spanned_by(
lit,
format!("failed to parse path: {:?}", string.value()),
))
}
fn parse_lit_into_expr_path(
@@ -1244,7 +1373,10 @@ fn parse_lit_into_expr_path(
) -> Result<syn::ExprPath, ()> {
let string = try!(get_lit_str(cx, attr_name, attr_name, lit));
parse_lit_str(string)
.map_err(|_| cx.error(format!("failed to parse path: {:?}", string.value())))
.map_err(|_| cx.error_spanned_by(
lit,
format!("failed to parse path: {:?}", string.value()),
))
}
fn parse_lit_into_where(
@@ -1262,14 +1394,14 @@ fn parse_lit_into_where(
parse_lit_str::<syn::WhereClause>(&where_string)
.map(|wh| wh.predicates.into_iter().collect())
.map_err(|err| cx.error(err))
.map_err(|err| cx.error_spanned_by(lit, err))
}
fn parse_lit_into_ty(cx: &Ctxt, attr_name: &Ident, lit: &syn::Lit) -> Result<syn::Type, ()> {
let string = try!(get_lit_str(cx, attr_name, attr_name, lit));
parse_lit_str(string).map_err(|_| {
cx.error(format!(
cx.error_spanned_by(lit, format!(
"failed to parse type: {} = {:?}",
attr_name,
string.value()
@@ -1286,7 +1418,7 @@ fn parse_lit_into_lifetimes(
) -> Result<BTreeSet<syn::Lifetime>, ()> {
let string = try!(get_lit_str(cx, attr_name, attr_name, lit));
if string.value().is_empty() {
cx.error("at least one lifetime must be borrowed");
cx.error_spanned_by(lit, "at least one lifetime must be borrowed");
return Err(());
}
@@ -1302,13 +1434,16 @@ fn parse_lit_into_lifetimes(
let mut set = BTreeSet::new();
for lifetime in lifetimes {
if !set.insert(lifetime.clone()) {
cx.error(format!("duplicate borrowed lifetime `{}`", lifetime));
cx.error_spanned_by(
lit,
format!("duplicate borrowed lifetime `{}`", lifetime),
);
}
}
return Ok(set);
}
cx.error(format!(
cx.error_spanned_by(lit, format!(
"failed to parse borrowed lifetimes: {:?}",
string.value()
));
@@ -1461,12 +1596,15 @@ fn is_primitive_path(path: &syn::Path, primitive: &str) -> bool {
fn borrowable_lifetimes(
cx: &Ctxt,
name: &str,
ty: &syn::Type,
field: &syn::Field,
) -> Result<BTreeSet<syn::Lifetime>, ()> {
let mut lifetimes = BTreeSet::new();
collect_lifetimes(ty, &mut lifetimes);
collect_lifetimes(&field.ty, &mut lifetimes);
if lifetimes.is_empty() {
cx.error(format!("field `{}` has no lifetimes to borrow", name));
cx.error_spanned_by(
field,
format!("field `{}` has no lifetimes to borrow", name),
);
Err(())
} else {
Ok(lifetimes)
+89 -37
View File
@@ -21,12 +21,16 @@ fn check_getter(cx: &Ctxt, cont: &Container) {
match cont.data {
Data::Enum(_) => {
if cont.data.has_getter() {
cx.error("#[serde(getter = \"...\")] is not allowed in an enum");
cx.error_spanned_by(
cont.original,
"#[serde(getter = \"...\")] is not allowed in an enum",
);
}
}
Data::Struct(_, _) => {
if cont.data.has_getter() && cont.attrs.remote().is_none() {
cx.error(
cx.error_spanned_by(
cont.original,
"#[serde(getter = \"...\")] can only be used in structs \
that have #[serde(remote = \"...\")]",
);
@@ -59,26 +63,35 @@ fn check_flatten_field(cx: &Ctxt, style: Style, field: &Field) {
}
match style {
Style::Tuple => {
cx.error("#[serde(flatten)] cannot be used on tuple structs");
cx.error_spanned_by(
field.original,
"#[serde(flatten)] cannot be used on tuple structs",
);
}
Style::Newtype => {
cx.error("#[serde(flatten)] cannot be used on newtype structs");
cx.error_spanned_by(
field.original,
"#[serde(flatten)] cannot be used on newtype structs",
);
}
_ => {}
}
if field.attrs.skip_serializing() {
cx.error(
"#[serde(flatten] can not be combined with \
cx.error_spanned_by(
field.original,
"#[serde(flatten)] can not be combined with \
#[serde(skip_serializing)]",
);
} else if field.attrs.skip_serializing_if().is_some() {
cx.error(
"#[serde(flatten] can not be combined with \
cx.error_spanned_by(
field.original,
"#[serde(flatten)] can not be combined with \
#[serde(skip_serializing_if = \"...\")]",
);
} else if field.attrs.skip_deserializing() {
cx.error(
"#[serde(flatten] can not be combined with \
cx.error_spanned_by(
field.original,
"#[serde(flatten)] can not be combined with \
#[serde(skip_deserializing)]",
);
}
@@ -107,24 +120,36 @@ fn check_identifier(cx: &Ctxt, cont: &Container) {
) {
// The `other` attribute may not be used in a variant_identifier.
(_, Identifier::Variant, true, _) => {
cx.error("#[serde(other)] may not be used on a variant_identifier");
cx.error_spanned_by(
variant.original,
"#[serde(other)] may not be used on a variant identifier",
);
}
// Variant with `other` attribute cannot appear in untagged enum
(_, Identifier::No, true, &EnumTag::None) => {
cx.error("#[serde(other)] cannot appear on untagged enum");
cx.error_spanned_by(
variant.original,
"#[serde(other)] cannot appear on untagged enum",
);
}
// Variant with `other` attribute must be the last one.
(Style::Unit, Identifier::Field, true, _) | (Style::Unit, Identifier::No, true, _) => {
if i < variants.len() - 1 {
cx.error("#[serde(other)] must be the last variant");
cx.error_spanned_by(
variant.original,
"#[serde(other)] must be on the last variant",
);
}
}
// Variant with `other` attribute must be a unit variant.
(_, Identifier::Field, true, _) | (_, Identifier::No, true, _) => {
cx.error("#[serde(other)] must be on a unit variant");
cx.error_spanned_by(
variant.original,
"#[serde(other)] must be on a unit variant",
);
}
// Any sort of variant is allowed if this is not an identifier.
@@ -136,16 +161,25 @@ fn check_identifier(cx: &Ctxt, cont: &Container) {
// The last field is allowed to be a newtype catch-all.
(Style::Newtype, Identifier::Field, false, _) => {
if i < variants.len() - 1 {
cx.error(format!("`{}` must be the last variant", variant.ident));
cx.error_spanned_by(
variant.original,
format!("`{}` must be the last variant", variant.ident),
);
}
}
(_, Identifier::Field, false, _) => {
cx.error("field_identifier may only contain unit variants");
cx.error_spanned_by(
variant.original,
"#[serde(field_identifier)] may only contain unit variants",
);
}
(_, Identifier::Variant, false, _) => {
cx.error("variant_identifier may only contain unit variants");
cx.error_spanned_by(
variant.original,
"#[serde(variant_identifier)] may only contain unit variants",
);
}
}
}
@@ -164,7 +198,7 @@ fn check_variant_skip_attrs(cx: &Ctxt, cont: &Container) {
for variant in variants.iter() {
if variant.attrs.serialize_with().is_some() {
if variant.attrs.skip_serializing() {
cx.error(format!(
cx.error_spanned_by(variant.original, format!(
"variant `{}` cannot have both #[serde(serialize_with)] and \
#[serde(skip_serializing)]",
variant.ident
@@ -175,7 +209,7 @@ fn check_variant_skip_attrs(cx: &Ctxt, cont: &Container) {
let member = member_message(&field.member);
if field.attrs.skip_serializing() {
cx.error(format!(
cx.error_spanned_by(variant.original, format!(
"variant `{}` cannot have both #[serde(serialize_with)] and \
a field {} marked with #[serde(skip_serializing)]",
variant.ident, member
@@ -183,7 +217,7 @@ fn check_variant_skip_attrs(cx: &Ctxt, cont: &Container) {
}
if field.attrs.skip_serializing_if().is_some() {
cx.error(format!(
cx.error_spanned_by(variant.original, format!(
"variant `{}` cannot have both #[serde(serialize_with)] and \
a field {} marked with #[serde(skip_serializing_if)]",
variant.ident, member
@@ -194,7 +228,7 @@ fn check_variant_skip_attrs(cx: &Ctxt, cont: &Container) {
if variant.attrs.deserialize_with().is_some() {
if variant.attrs.skip_deserializing() {
cx.error(format!(
cx.error_spanned_by(variant.original, format!(
"variant `{}` cannot have both #[serde(deserialize_with)] and \
#[serde(skip_deserializing)]",
variant.ident
@@ -205,7 +239,7 @@ fn check_variant_skip_attrs(cx: &Ctxt, cont: &Container) {
if field.attrs.skip_deserializing() {
let member = member_message(&field.member);
cx.error(format!(
cx.error_spanned_by(variant.original, format!(
"variant `{}` cannot have both #[serde(deserialize_with)] \
and a field {} marked with #[serde(skip_deserializing)]",
variant.ident, member
@@ -231,10 +265,10 @@ fn check_internal_tag_field_name_conflict(cx: &Ctxt, cont: &Container) {
EnumTag::External | EnumTag::Adjacent { .. } | EnumTag::None => return,
};
let diagnose_conflict = || {
let message = format!("variant field name `{}` conflicts with internal tag", tag);
cx.error(message);
};
let diagnose_conflict = || cx.error_spanned_by(
cont.original,
format!("variant field name `{}` conflicts with internal tag", tag),
);
for variant in variants {
match variant.style {
@@ -269,11 +303,10 @@ fn check_adjacent_tag_conflict(cx: &Ctxt, cont: &Container) {
};
if type_tag == content_tag {
let message = format!(
cx.error_spanned_by(cont.original, format!(
"enum tags `{}` for type and content conflict with each other",
type_tag
);
cx.error(message);
));
}
}
@@ -284,20 +317,32 @@ fn check_transparent(cx: &Ctxt, cont: &mut Container, derive: Derive) {
}
if cont.attrs.type_from().is_some() {
cx.error("#[serde(transparent)] is not allowed with #[serde(from = \"...\")]");
cx.error_spanned_by(
cont.original,
"#[serde(transparent)] is not allowed with #[serde(from = \"...\")]",
);
}
if cont.attrs.type_into().is_some() {
cx.error("#[serde(transparent)] is not allowed with #[serde(into = \"...\")]");
cx.error_spanned_by(
cont.original,
"#[serde(transparent)] is not allowed with #[serde(into = \"...\")]",
);
}
let fields = match cont.data {
Data::Enum(_) => {
cx.error("#[serde(transparent)] is not allowed on an enum");
cx.error_spanned_by(
cont.original,
"#[serde(transparent)] is not allowed on an enum",
);
return;
}
Data::Struct(Style::Unit, _) => {
cx.error("#[serde(transparent)] is not allowed on a unit struct");
cx.error_spanned_by(
cont.original,
"#[serde(transparent)] is not allowed on a unit struct",
);
return;
}
Data::Struct(_, ref mut fields) => fields,
@@ -308,7 +353,8 @@ fn check_transparent(cx: &Ctxt, cont: &mut Container, derive: Derive) {
for field in fields {
if allow_transparent(field, derive) {
if transparent_field.is_some() {
cx.error(
cx.error_spanned_by(
cont.original,
"#[serde(transparent)] requires struct to have at most one transparent field",
);
return;
@@ -321,10 +367,16 @@ fn check_transparent(cx: &Ctxt, cont: &mut Container, derive: Derive) {
Some(transparent_field) => transparent_field.attrs.mark_transparent(),
None => match derive {
Derive::Serialize => {
cx.error("#[serde(transparent)] requires at least one field that is not skipped");
cx.error_spanned_by(
cont.original,
"#[serde(transparent)] requires at least one field that is not skipped",
);
}
Derive::Deserialize => {
cx.error("#[serde(transparent)] requires at least one field that is neither skipped nor has a default");
cx.error_spanned_by(
cont.original,
"#[serde(transparent)] requires at least one field that is neither skipped nor has a default",
);
}
},
}
@@ -333,7 +385,7 @@ fn check_transparent(cx: &Ctxt, cont: &mut Container, derive: Derive) {
fn member_message(member: &Member) -> String {
match *member {
Member::Named(ref ident) => format!("`{}`", ident),
Member::Unnamed(ref i) => i.index.to_string(),
Member::Unnamed(ref i) => format!("#{}", i.index),
}
}
+12 -15
View File
@@ -1,6 +1,8 @@
use quote::ToTokens;
use std::cell::RefCell;
use std::fmt::Display;
use std::thread;
use syn;
/// A type to collect errors together and format them.
///
@@ -11,7 +13,7 @@ use std::thread;
pub struct Ctxt {
// The contents will be set to `None` during checking. This is so that checking can be
// enforced.
errors: RefCell<Option<Vec<String>>>,
errors: RefCell<Option<Vec<syn::Error>>>,
}
impl Ctxt {
@@ -24,29 +26,24 @@ impl Ctxt {
}
}
/// Add an error to the context object.
pub fn error<T: Display>(&self, msg: T) {
/// Add an error to the context object with a tokenenizable object.
///
/// The object is used for spanning in error messages.
pub fn error_spanned_by<A: ToTokens, T: Display>(&self, obj: A, msg: T) {
self.errors
.borrow_mut()
.as_mut()
.unwrap()
.push(msg.to_string());
// Curb monomorphization from generating too many identical methods.
.push(syn::Error::new_spanned(obj.into_token_stream(), msg));
}
/// Consume this object, producing a formatted error string if there are errors.
pub fn check(self) -> Result<(), String> {
let mut errors = self.errors.borrow_mut().take().unwrap();
pub fn check(self) -> Result<(), Vec<syn::Error>> {
let errors = self.errors.borrow_mut().take().unwrap();
match errors.len() {
0 => Ok(()),
1 => Err(errors.pop().unwrap()),
n => {
let mut msg = format!("{} errors:", n);
for err in errors {
msg.push_str("\n\t# ");
msg.push_str(&err);
}
Err(msg)
}
_ => Err(errors),
}
}
}
+5 -6
View File
@@ -77,7 +77,7 @@ mod try;
pub fn derive_serialize(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
ser::expand_derive_serialize(&input)
.unwrap_or_else(compile_error)
.unwrap_or_else(to_compile_errors)
.into()
}
@@ -85,12 +85,11 @@ pub fn derive_serialize(input: TokenStream) -> TokenStream {
pub fn derive_deserialize(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
de::expand_derive_deserialize(&input)
.unwrap_or_else(compile_error)
.unwrap_or_else(to_compile_errors)
.into()
}
fn compile_error(message: String) -> proc_macro2::TokenStream {
quote! {
compile_error!(#message);
}
fn to_compile_errors(errors: Vec<syn::Error>) -> proc_macro2::TokenStream {
let compile_errors = errors.iter().map(syn::Error::to_compile_error);
quote!(#(#compile_errors)*)
}
+25 -10
View File
@@ -9,9 +9,12 @@ use internals::{attr, Ctxt, Derive};
use pretend;
use try;
pub fn expand_derive_serialize(input: &syn::DeriveInput) -> Result<TokenStream, String> {
pub fn expand_derive_serialize(input: &syn::DeriveInput) -> Result<TokenStream, Vec<syn::Error>> {
let ctxt = Ctxt::new();
let cont = Container::from_ast(&ctxt, input, Derive::Serialize);
let cont = match Container::from_ast(&ctxt, input, Derive::Serialize) {
Some(cont) => cont,
None => return Err(ctxt.check().unwrap_err()),
};
precondition(&ctxt, &cont);
try!(ctxt.check());
@@ -72,10 +75,10 @@ fn precondition(cx: &Ctxt, cont: &Container) {
match cont.attrs.identifier() {
attr::Identifier::No => {}
attr::Identifier::Field => {
cx.error("field identifiers cannot be serialized");
cx.error_spanned_by(cont.original, "field identifiers cannot be serialized");
}
attr::Identifier::Variant => {
cx.error("variant identifiers cannot be serialized");
cx.error_spanned_by(cont.original, "variant identifiers cannot be serialized");
}
}
}
@@ -200,7 +203,10 @@ fn serialize_transparent(cont: &Container, params: &Parameters) -> Fragment {
let path = match transparent_field.attrs.serialize_with() {
Some(path) => quote!(#path),
None => quote!(_serde::Serialize::serialize),
None => {
let span = transparent_field.original.span();
quote_spanned!(span=> _serde::Serialize::serialize)
},
};
quote_block! {
@@ -505,8 +511,10 @@ fn serialize_externally_tagged_variant(
field_expr = wrap_serialize_field_with(params, field.ty, path, &field_expr);
}
let span = field.original.span();
let func = quote_spanned!(span=> _serde::Serializer::serialize_newtype_variant);
quote_expr! {
_serde::Serializer::serialize_newtype_variant(
#func(
__serializer,
#type_name,
#variant_index,
@@ -579,8 +587,10 @@ fn serialize_internally_tagged_variant(
field_expr = wrap_serialize_field_with(params, field.ty, path, &field_expr);
}
let span = field.original.span();
let func = quote_spanned!(span=> _serde::private::ser::serialize_tagged_newtype);
quote_expr! {
_serde::private::ser::serialize_tagged_newtype(
#func(
__serializer,
#enum_ident_str,
#variant_ident_str,
@@ -637,12 +647,14 @@ fn serialize_adjacently_tagged_variant(
field_expr = wrap_serialize_field_with(params, field.ty, path, &field_expr);
}
let span = field.original.span();
let func = quote_spanned!(span=> _serde::ser::SerializeStruct::serialize_field);
return quote_block! {
let mut __struct = try!(_serde::Serializer::serialize_struct(
__serializer, #type_name, 2));
try!(_serde::ser::SerializeStruct::serialize_field(
&mut __struct, #tag, #variant_name));
try!(_serde::ser::SerializeStruct::serialize_field(
try!(#func(
&mut __struct, #content, #field_expr));
_serde::ser::SerializeStruct::end(__struct)
};
@@ -738,8 +750,10 @@ fn serialize_untagged_variant(
field_expr = wrap_serialize_field_with(params, field.ty, path, &field_expr);
}
let span = field.original.span();
let func = quote_spanned!(span=> _serde::Serialize::serialize);
quote_expr! {
_serde::Serialize::serialize(#field_expr, __serializer)
#func(#field_expr, __serializer)
}
}
Style::Tuple => serialize_tuple_variant(TupleVariant::Untagged, params, &variant.fields),
@@ -1079,8 +1093,9 @@ fn serialize_struct_visitor(
let span = field.original.span();
let ser = if field.attrs.flatten() {
let func = quote_spanned!(span=> _serde::Serialize::serialize);
quote! {
try!(_serde::Serialize::serialize(&#field_expr, _serde::private::ser::FlatMapSerializer(&mut __serde_state)));
try!(#func(&#field_expr, _serde::private::ser::FlatMapSerializer(&mut __serde_state)));
}
} else {
let func = struct_trait.serialize_field(span);
@@ -1,8 +1,8 @@
error: failed to parse borrowed lifetimes: "zzz"
--> $DIR/bad_lifetimes.rs:4:10
--> $DIR/bad_lifetimes.rs:6:22
|
4 | #[derive(Deserialize)]
| ^^^^^^^^^^^
6 | #[serde(borrow = "zzz")]
| ^^^^^
error: aborting due to previous error
@@ -1,8 +1,8 @@
error: duplicate borrowed lifetime `'a`
--> $DIR/duplicate_lifetime.rs:4:10
--> $DIR/duplicate_lifetime.rs:6:22
|
4 | #[derive(Deserialize)]
| ^^^^^^^^^^^
6 | #[serde(borrow = "'a + 'a")]
| ^^^^^^^^^
error: aborting due to previous error
@@ -1,8 +1,8 @@
error: duplicate serde attribute `borrow`
--> $DIR/duplicate_variant.rs:7:10
--> $DIR/duplicate_variant.rs:9:13
|
7 | #[derive(Deserialize)]
| ^^^^^^^^^^^
9 | #[serde(borrow)]
| ^^^^^^
error: aborting due to previous error
@@ -1,8 +1,8 @@
error: at least one lifetime must be borrowed
--> $DIR/empty_lifetimes.rs:4:10
--> $DIR/empty_lifetimes.rs:6:22
|
4 | #[derive(Deserialize)]
| ^^^^^^^^^^^
6 | #[serde(borrow = "")]
| ^^
error: aborting due to previous error
@@ -1,8 +1,9 @@
error: field `s` has no lifetimes to borrow
--> $DIR/no_lifetimes.rs:4:10
--> $DIR/no_lifetimes.rs:6:5
|
4 | #[derive(Deserialize)]
| ^^^^^^^^^^^
6 | / #[serde(borrow)]
7 | | s: String,
| |_____________^
error: aborting due to previous error
@@ -1,8 +1,9 @@
error: #[serde(borrow)] may only be used on newtype variants
--> $DIR/struct_variant.rs:7:10
|
7 | #[derive(Deserialize)]
| ^^^^^^^^^^^
--> $DIR/struct_variant.rs:9:5
|
9 | / #[serde(borrow)]
10 | | S { s: Str<'a> },
| |____________________^
error: aborting due to previous error
@@ -1,8 +1,9 @@
error: field `s` does not have lifetime 'b
--> $DIR/wrong_lifetime.rs:4:10
--> $DIR/wrong_lifetime.rs:6:5
|
4 | #[derive(Deserialize)]
| ^^^^^^^^^^^
6 | / #[serde(borrow = "'b")]
7 | | s: &'a str,
| |______________^
error: aborting due to previous error
@@ -1,8 +1,12 @@
error: enum tags `conflict` for type and content conflict with each other
--> $DIR/adjacent-tag.rs:4:10
--> $DIR/adjacent-tag.rs:5:1
|
4 | #[derive(Serialize)]
| ^^^^^^^^^
5 | / #[serde(tag = "conflict", content = "conflict")]
6 | | enum E {
7 | | A,
8 | | B,
9 | | }
| |_^
error: aborting due to previous error
@@ -1,8 +1,8 @@
error: #[serde(flatten)] cannot be used on newtype structs
--> $DIR/flatten-newtype-struct.rs:4:10
--> $DIR/flatten-newtype-struct.rs:5:12
|
4 | #[derive(Serialize)]
| ^^^^^^^^^
5 | struct Foo(#[serde(flatten)] HashMap<String, String>);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to previous error
@@ -1,8 +1,9 @@
error: #[serde(flatten] can not be combined with #[serde(skip_deserializing)]
--> $DIR/flatten-skip-deserializing.rs:4:10
error: #[serde(flatten)] can not be combined with #[serde(skip_deserializing)]
--> $DIR/flatten-skip-deserializing.rs:6:5
|
4 | #[derive(Deserialize)]
| ^^^^^^^^^^^
6 | / #[serde(flatten, skip_deserializing)]
7 | | other: Other,
| |________________^
error: aborting due to previous error
@@ -1,8 +1,9 @@
error: #[serde(flatten] can not be combined with #[serde(skip_serializing_if = "...")]
--> $DIR/flatten-skip-serializing-if.rs:4:10
error: #[serde(flatten)] can not be combined with #[serde(skip_serializing_if = "...")]
--> $DIR/flatten-skip-serializing-if.rs:6:5
|
4 | #[derive(Serialize)]
| ^^^^^^^^^
6 | / #[serde(flatten, skip_serializing_if = "Option::is_none")]
7 | | other: Option<Other>,
| |________________________^
error: aborting due to previous error
@@ -1,8 +1,9 @@
error: #[serde(flatten] can not be combined with #[serde(skip_serializing)]
--> $DIR/flatten-skip-serializing.rs:4:10
error: #[serde(flatten)] can not be combined with #[serde(skip_serializing)]
--> $DIR/flatten-skip-serializing.rs:6:5
|
4 | #[derive(Serialize)]
| ^^^^^^^^^
6 | / #[serde(flatten, skip_serializing)]
7 | | other: Other,
| |________________^
error: aborting due to previous error
@@ -1,8 +1,8 @@
error: #[serde(flatten)] cannot be used on tuple structs
--> $DIR/flatten-tuple-struct.rs:4:10
--> $DIR/flatten-tuple-struct.rs:5:17
|
4 | #[derive(Serialize)]
| ^^^^^^^^^
5 | struct Foo(u32, #[serde(flatten)] HashMap<String, String>);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to previous error
@@ -1,8 +1,14 @@
error: variant field name `conflict` conflicts with internal tag
--> $DIR/internal-tag.rs:4:10
|
4 | #[derive(Serialize)]
| ^^^^^^^^^
--> $DIR/internal-tag.rs:5:1
|
5 | / #[serde(tag = "conflict")]
6 | | enum E {
7 | | A {
8 | | #[serde(rename = "conflict")]
9 | | x: (),
10 | | },
11 | | }
| |_^
error: aborting due to previous error
@@ -1,8 +1,8 @@
error: #[serde(default)] can only be used on structs with named fields
--> $DIR/enum.rs:4:10
--> $DIR/enum.rs:6:1
|
4 | #[derive(Deserialize)]
| ^^^^^^^^^^^
6 | enum E {
| ^^^^
error: aborting due to previous error
@@ -0,0 +1,8 @@
#[macro_use]
extern crate serde_derive;
#[derive(Deserialize)]
#[serde(default = "default_e")]
enum E {
S { f: u8 },
}
@@ -0,0 +1,8 @@
error: #[serde(default = "...")] can only be used on structs with named fields
--> $DIR/enum_path.rs:6:1
|
6 | enum E {
| ^^^^
error: aborting due to previous error
@@ -1,8 +1,8 @@
error: #[serde(default)] can only be used on structs with named fields
--> $DIR/nameless_struct_fields.rs:4:10
--> $DIR/nameless_struct_fields.rs:6:9
|
4 | #[derive(Deserialize)]
| ^^^^^^^^^^^
6 | struct T(u8, u8);
| ^^^^^^^^
error: aborting due to previous error
@@ -0,0 +1,8 @@
#[macro_use]
extern crate serde_derive;
#[derive(Deserialize)]
#[serde(default = "default_t")]
struct T(u8, u8);
fn main() {}
@@ -0,0 +1,8 @@
error: #[serde(default = "...")] can only be used on structs with named fields
--> $DIR/nameless_struct_fields_path.rs:6:9
|
6 | struct T(u8, u8);
| ^^^^^^^^
error: aborting due to previous error
@@ -1,8 +1,8 @@
error: unknown serde field attribute `serialize`
--> $DIR/rename-and-ser.rs:4:10
--> $DIR/rename-and-ser.rs:6:27
|
4 | #[derive(Serialize)]
| ^^^^^^^^^
6 | #[serde(rename = "x", serialize = "y")]
| ^^^^^^^^^
error: aborting due to previous error
@@ -1,8 +1,8 @@
error: duplicate serde attribute `rename`
--> $DIR/rename-rename-de.rs:4:10
--> $DIR/rename-rename-de.rs:7:13
|
4 | #[derive(Serialize)]
| ^^^^^^^^^
7 | #[serde(rename(deserialize = "y"))]
| ^^^^^^
error: aborting due to previous error
@@ -1,8 +1,8 @@
error: duplicate serde attribute `rename`
--> $DIR/rename-ser-rename-ser.rs:4:10
--> $DIR/rename-ser-rename-ser.rs:6:38
|
4 | #[derive(Serialize)]
| ^^^^^^^^^
6 | #[serde(rename(serialize = "x"), rename(serialize = "y"))]
| ^^^^^^
error: aborting due to previous error
@@ -1,8 +1,8 @@
error: duplicate serde attribute `rename`
--> $DIR/rename-ser-rename.rs:4:10
--> $DIR/rename-ser-rename.rs:7:13
|
4 | #[derive(Serialize)]
| ^^^^^^^^^
7 | #[serde(rename = "y")]
| ^^^^^^
error: aborting due to previous error
@@ -1,8 +1,8 @@
error: duplicate serde attribute `rename`
--> $DIR/rename-ser-ser.rs:4:10
--> $DIR/rename-ser-ser.rs:6:37
|
4 | #[derive(Serialize)]
| ^^^^^^^^^
6 | #[serde(rename(serialize = "x", serialize = "y"))]
| ^^^^^^^^^
error: aborting due to previous error
@@ -1,8 +1,8 @@
error: duplicate serde attribute `rename`
--> $DIR/two-rename-ser.rs:4:10
--> $DIR/two-rename-ser.rs:7:13
|
4 | #[derive(Serialize)]
| ^^^^^^^^^
7 | #[serde(rename(serialize = "y"))]
| ^^^^^^
error: aborting due to previous error
@@ -1,8 +1,8 @@
error: duplicate serde attribute `serialize_with`
--> $DIR/with-and-serialize-with.rs:4:10
--> $DIR/with-and-serialize-with.rs:6:25
|
4 | #[derive(Serialize)]
| ^^^^^^^^^
6 | #[serde(with = "w", serialize_with = "s")]
| ^^^^^^^^^^^^^^
error: aborting due to previous error
@@ -0,0 +1,11 @@
#[macro_use]
extern crate serde_derive;
#[derive(Serialize)]
#[serde(content = "c")]
enum E {
A(u8),
B(String),
}
fn main() {}
@@ -0,0 +1,8 @@
error: #[serde(tag = "...", content = "...")] must be used together
--> $DIR/content-no-tag.rs:5:9
|
5 | #[serde(content = "c")]
| ^^^^^^^
error: aborting due to previous error
@@ -1,8 +1,8 @@
error: #[serde(tag = "...")] cannot be used with tuple variants
--> $DIR/internal-tuple-variant.rs:4:10
--> $DIR/internal-tuple-variant.rs:7:5
|
4 | #[derive(Serialize)]
| ^^^^^^^^^
7 | Tuple(u8, u8),
| ^^^^^^^^^^^^^
error: aborting due to previous error
@@ -1,8 +1,8 @@
error: #[serde(tag = "...")] can only be used on enums
--> $DIR/internally-tagged-struct.rs:4:10
--> $DIR/internally-tagged-struct.rs:6:1
|
4 | #[derive(Serialize)]
| ^^^^^^^^^
6 | struct S;
| ^^^^^^
error: aborting due to previous error
@@ -0,0 +1,12 @@
#[macro_use]
extern crate serde_derive;
#[derive(Serialize)]
#[serde(untagged)]
#[serde(tag = "t", content = "c")]
enum E {
A(u8),
B(String),
}
fn main() {}
@@ -0,0 +1,20 @@
error: untagged enum cannot have #[serde(tag = "...", content = "...")]
--> $DIR/untagged-and-adjacent.rs:5:9
|
5 | #[serde(untagged)]
| ^^^^^^^^
error: untagged enum cannot have #[serde(tag = "...", content = "...")]
--> $DIR/untagged-and-adjacent.rs:6:9
|
6 | #[serde(tag = "t", content = "c")]
| ^^^
error: untagged enum cannot have #[serde(tag = "...", content = "...")]
--> $DIR/untagged-and-adjacent.rs:6:20
|
6 | #[serde(tag = "t", content = "c")]
| ^^^^^^^
error: aborting due to 3 previous errors
@@ -0,0 +1,12 @@
#[macro_use]
extern crate serde_derive;
#[derive(Serialize)]
#[serde(untagged)]
#[serde(content = "c")]
enum E {
A(u8),
B(String),
}
fn main() {}
@@ -0,0 +1,14 @@
error: untagged enum cannot have #[serde(content = "...")]
--> $DIR/untagged-and-content.rs:5:9
|
5 | #[serde(untagged)]
| ^^^^^^^^
error: untagged enum cannot have #[serde(content = "...")]
--> $DIR/untagged-and-content.rs:6:9
|
6 | #[serde(content = "c")]
| ^^^^^^^
error: aborting due to 2 previous errors
@@ -1,8 +1,14 @@
error: enum cannot be both untagged and internally tagged
--> $DIR/untagged-and-internal.rs:4:10
--> $DIR/untagged-and-internal.rs:5:9
|
4 | #[derive(Serialize)]
| ^^^^^^^^^
5 | #[serde(untagged)]
| ^^^^^^^^
error: aborting due to previous error
error: enum cannot be both untagged and internally tagged
--> $DIR/untagged-and-internal.rs:6:9
|
6 | #[serde(tag = "type")]
| ^^^
error: aborting due to 2 previous errors
@@ -1,8 +1,8 @@
error: #[serde(untagged)] can only be used on enums
--> $DIR/untagged-struct.rs:4:10
--> $DIR/untagged-struct.rs:6:1
|
4 | #[derive(Serialize)]
| ^^^^^^^^^
6 | struct S;
| ^^^^^^
error: aborting due to previous error
@@ -0,0 +1,10 @@
#[macro_use]
extern crate serde_derive;
#[derive(Serialize)]
struct S {
#[serde(rename = true)]
boolean: (),
}
fn main() {}
@@ -0,0 +1,8 @@
error: expected serde rename attribute to be a string: `rename = "..."`
--> $DIR/boolean.rs:6:22
|
6 | #[serde(rename = true)]
| ^^^^
error: aborting due to previous error
@@ -0,0 +1,10 @@
#[macro_use]
extern crate serde_derive;
#[derive(Serialize)]
struct S {
#[serde(rename = b'a')]
byte_character: (),
}
fn main() {}
@@ -0,0 +1,8 @@
error: expected serde rename attribute to be a string: `rename = "..."`
--> $DIR/byte_character.rs:6:22
|
6 | #[serde(rename = b'a')]
| ^^^^
error: aborting due to previous error
@@ -0,0 +1,10 @@
#[macro_use]
extern crate serde_derive;
#[derive(Serialize)]
struct S {
#[serde(rename = b"byte string")]
byte_string: (),
}
fn main() {}
@@ -0,0 +1,8 @@
error: expected serde rename attribute to be a string: `rename = "..."`
--> $DIR/byte_string.rs:6:22
|
6 | #[serde(rename = b"byte string")]
| ^^^^^^^^^^^^^^
error: aborting due to previous error
@@ -0,0 +1,10 @@
#[macro_use]
extern crate serde_derive;
#[derive(Serialize)]
struct S {
#[serde(rename = 'a')]
character: (),
}
fn main() {}
@@ -0,0 +1,8 @@
error: expected serde rename attribute to be a string: `rename = "..."`
--> $DIR/character.rs:6:22
|
6 | #[serde(rename = 'a')]
| ^^^
error: aborting due to previous error
@@ -0,0 +1,10 @@
#[macro_use]
extern crate serde_derive;
#[derive(Serialize)]
struct S {
#[serde(rename = 3.14)]
float: (),
}
fn main() {}
@@ -0,0 +1,8 @@
error: expected serde rename attribute to be a string: `rename = "..."`
--> $DIR/float.rs:6:22
|
6 | #[serde(rename = 3.14)]
| ^^^^
error: aborting due to previous error
@@ -0,0 +1,10 @@
#[macro_use]
extern crate serde_derive;
#[derive(Serialize)]
struct S {
#[serde(rename = 100)]
integer: (),
}
fn main() {}
@@ -0,0 +1,8 @@
error: expected serde rename attribute to be a string: `rename = "..."`
--> $DIR/integer.rs:6:22
|
6 | #[serde(rename = 100)]
| ^^^
error: aborting due to previous error
+11 -5
View File
@@ -1,8 +1,14 @@
error: `field_identifier` and `variant_identifier` cannot both be set
--> $DIR/both.rs:4:10
error: #[serde(field_identifier)] and #[serde(variant_identifier)] cannot both be set
--> $DIR/both.rs:5:9
|
4 | #[derive(Deserialize)]
| ^^^^^^^^^^^
5 | #[serde(field_identifier, variant_identifier)]
| ^^^^^^^^^^^^^^^^
error: aborting due to previous error
error: #[serde(field_identifier)] and #[serde(variant_identifier)] cannot both be set
--> $DIR/both.rs:5:27
|
5 | #[serde(field_identifier, variant_identifier)]
| ^^^^^^^^^^^^^^^^^^
error: aborting due to 2 previous errors
@@ -1,8 +1,8 @@
error: `field_identifier` can only be used on an enum
--> $DIR/field_struct.rs:4:10
error: #[serde(field_identifier)] can only be used on an enum
--> $DIR/field_struct.rs:6:1
|
4 | #[derive(Deserialize)]
| ^^^^^^^^^^^
6 | struct S;
| ^^^^^^
error: aborting due to previous error
@@ -1,8 +1,8 @@
error: field_identifier may only contain unit variants
--> $DIR/field_tuple.rs:4:10
error: #[serde(field_identifier)] may only contain unit variants
--> $DIR/field_tuple.rs:8:5
|
4 | #[derive(Deserialize)]
| ^^^^^^^^^^^
8 | B(u8, u8),
| ^^^^^^^^^
error: aborting due to previous error
@@ -1,8 +1,8 @@
error: `Other` must be the last variant
--> $DIR/newtype_not_last.rs:4:10
--> $DIR/newtype_not_last.rs:8:5
|
4 | #[derive(Deserialize)]
| ^^^^^^^^^^^
8 | Other(String),
| ^^^^^^^^^^^^^
error: aborting due to previous error
@@ -1,8 +1,9 @@
error: #[serde(other)] must be on a unit variant
--> $DIR/not_unit.rs:4:10
--> $DIR/not_unit.rs:8:5
|
4 | #[derive(Deserialize)]
| ^^^^^^^^^^^
8 | / #[serde(other)]
9 | | Other(u8, u8),
| |_________________^
error: aborting due to previous error
@@ -1,8 +1,9 @@
error: #[serde(other)] must be the last variant
--> $DIR/other_not_last.rs:4:10
error: #[serde(other)] must be on the last variant
--> $DIR/other_not_last.rs:8:5
|
4 | #[derive(Deserialize)]
| ^^^^^^^^^^^
8 | / #[serde(other)]
9 | | Other,
| |_________^
error: aborting due to previous error
@@ -0,0 +1,11 @@
#[macro_use]
extern crate serde_derive;
#[derive(Deserialize)]
#[serde(untagged)]
enum F {
#[serde(other)]
Other,
}
fn main() {}
@@ -0,0 +1,9 @@
error: #[serde(other)] cannot appear on untagged enum
--> $DIR/other_untagged.rs:7:5
|
7 | / #[serde(other)]
8 | | Other,
| |_________^
error: aborting due to previous error
@@ -0,0 +1,11 @@
#[macro_use]
extern crate serde_derive;
#[derive(Deserialize)]
#[serde(variant_identifier)]
enum F {
#[serde(other)]
Other,
}
fn main() {}
@@ -0,0 +1,9 @@
error: #[serde(other)] may not be used on a variant identifier
--> $DIR/other_variant.rs:7:5
|
7 | / #[serde(other)]
8 | | Other,
| |_________^
error: aborting due to previous error
@@ -1,8 +0,0 @@
error: field identifiers cannot be serialized
--> $DIR/serialize.rs:4:10
|
4 | #[derive(Serialize)]
| ^^^^^^^^^
error: aborting due to previous error
@@ -1,8 +1,8 @@
error: `variant_identifier` can only be used on an enum
--> $DIR/variant_struct.rs:4:10
error: #[serde(variant_identifier)] can only be used on an enum
--> $DIR/variant_struct.rs:6:1
|
4 | #[derive(Deserialize)]
| ^^^^^^^^^^^
6 | struct S;
| ^^^^^^
error: aborting due to previous error
@@ -1,8 +1,8 @@
error: variant_identifier may only contain unit variants
--> $DIR/variant_tuple.rs:4:10
error: #[serde(variant_identifier)] may only contain unit variants
--> $DIR/variant_tuple.rs:8:5
|
4 | #[derive(Deserialize)]
| ^^^^^^^^^^^
8 | B(u8, u8),
| ^^^^^^^^^
error: aborting due to previous error
+10
View File
@@ -0,0 +1,10 @@
#[macro_use]
extern crate serde_derive;
#[derive(Serialize)]
struct S {
#[serde(bound(unknown))]
x: (),
}
fn main() {}
@@ -0,0 +1,8 @@
error: malformed bound attribute, expected `bound(serialize = ..., deserialize = ...)`
--> $DIR/bound.rs:6:19
|
6 | #[serde(bound(unknown))]
| ^^^^^^^
error: aborting due to previous error
+10
View File
@@ -0,0 +1,10 @@
#[macro_use]
extern crate serde_derive;
#[derive(Serialize)]
struct S {
#[serde(rename(unknown))]
x: (),
}
fn main() {}
@@ -0,0 +1,8 @@
error: malformed rename attribute, expected `rename(serialize = ..., deserialize = ...)`
--> $DIR/rename.rs:6:20
|
6 | #[serde(rename(unknown))]
| ^^^^^^^
error: aborting due to previous error
@@ -1,8 +1,8 @@
error: cannot deserialize when there is a lifetime parameter called 'de
--> $DIR/deserialize_de_lifetime.rs:4:10
--> $DIR/deserialize_de_lifetime.rs:5:10
|
4 | #[derive(Deserialize)]
| ^^^^^^^^^^^
5 | struct S<'de> {
| ^^^
error: aborting due to previous error
@@ -1,8 +1,11 @@
error: cannot deserialize a dynamically sized struct
--> $DIR/deserialize_dst.rs:4:10
--> $DIR/deserialize_dst.rs:5:1
|
4 | #[derive(Deserialize)]
| ^^^^^^^^^^^
5 | / struct S {
6 | | string: String,
7 | | slice: [u8],
8 | | }
| |_^
error: aborting due to previous error
@@ -0,0 +1,12 @@
error: field identifiers cannot be serialized
--> $DIR/serialize_field_identifier.rs:5:1
|
5 | / #[serde(field_identifier)]
6 | | enum F {
7 | | A,
8 | | B,
9 | | }
| |_^
error: aborting due to previous error
@@ -0,0 +1,11 @@
#[macro_use]
extern crate serde_derive;
#[derive(Serialize)]
#[serde(variant_identifier)]
enum F {
A,
B,
}
fn main() {}
@@ -0,0 +1,12 @@
error: variant identifiers cannot be serialized
--> $DIR/serialize_variant_identifier.rs:5:1
|
5 | / #[serde(variant_identifier)]
6 | | enum F {
7 | | A,
8 | | B,
9 | | }
| |_^
error: aborting due to previous error
+3 -3
View File
@@ -1,8 +1,8 @@
error: failed to parse path: "~~~"
--> $DIR/bad_getter.rs:10:10
--> $DIR/bad_getter.rs:13:22
|
10 | #[derive(Serialize)]
| ^^^^^^^^^
13 | #[serde(getter = "~~~")]
| ^^^^^
error: aborting due to previous error
+3 -3
View File
@@ -1,8 +1,8 @@
error: failed to parse path: "~~~"
--> $DIR/bad_remote.rs:10:10
--> $DIR/bad_remote.rs:11:18
|
10 | #[derive(Serialize)]
| ^^^^^^^^^
11 | #[serde(remote = "~~~")]
| ^^^^^
error: aborting due to previous error
@@ -1,8 +1,14 @@
error: #[serde(getter = "...")] is not allowed in an enum
--> $DIR/enum_getter.rs:10:10
--> $DIR/enum_getter.rs:11:1
|
10 | #[derive(Serialize)]
| ^^^^^^^^^
11 | / #[serde(remote = "remote::E")]
12 | | pub enum E {
13 | | A {
14 | | #[serde(getter = "get_a")]
15 | | a: u8,
16 | | },
17 | | }
| |_^
error: aborting due to previous error
@@ -1,8 +1,11 @@
error: #[serde(getter = "...")] can only be used in structs that have #[serde(remote = "...")]
--> $DIR/nonremote_getter.rs:4:10
--> $DIR/nonremote_getter.rs:5:1
|
4 | #[derive(Serialize)]
| ^^^^^^^^^
5 | / struct S {
6 | | #[serde(getter = "S::get")]
7 | | a: u8,
8 | | }
| |_^
error: aborting due to previous error
@@ -0,0 +1,12 @@
#[macro_use]
extern crate serde_derive;
#[derive(Serialize)]
#[serde(rename_all = "abc")]
struct S {
name: u8,
long_name: u8,
very_long_name: u8,
}
fn main() {}
@@ -0,0 +1,8 @@
error: unknown rename rule for #[serde(rename_all = "abc")]
--> $DIR/container_unknown_rename_rule.rs:5:22
|
5 | #[serde(rename_all = "abc")]
| ^^^^^
error: aborting due to previous error
@@ -0,0 +1,14 @@
#[macro_use]
extern crate serde_derive;
#[derive(Serialize)]
enum S {
#[serde(rename_all = "abc")]
V {
name: u8,
long_name: u8,
very_long_name: u8,
},
}
fn main() {}
@@ -0,0 +1,8 @@
error: unknown rename rule for #[serde(rename_all = "abc")]
--> $DIR/variant_unknown_rename_rule.rs:6:26
|
6 | #[serde(rename_all = "abc")]
| ^^^^^
error: aborting due to previous error
@@ -1,8 +1,12 @@
error: #[serde(transparent)] requires struct to have at most one transparent field
--> $DIR/at_most_one.rs:4:10
--> $DIR/at_most_one.rs:5:1
|
4 | #[derive(Serialize)]
| ^^^^^^^^^
5 | / #[serde(transparent)]
6 | | struct S {
7 | | a: u8,
8 | | b: u8,
9 | | }
| |_^
error: aborting due to previous error
@@ -1,8 +1,14 @@
error: #[serde(transparent)] requires at least one field that is neither skipped nor has a default
--> $DIR/de_at_least_one.rs:4:10
|
4 | #[derive(Deserialize)]
| ^^^^^^^^^^^
--> $DIR/de_at_least_one.rs:5:1
|
5 | / #[serde(transparent)]
6 | | struct S {
7 | | #[serde(skip)]
8 | | a: u8,
9 | | #[serde(default)]
10 | | b: u8,
11 | | }
| |_^
error: aborting due to previous error
+8
View File
@@ -0,0 +1,8 @@
#[macro_use]
extern crate serde_derive;
#[derive(Serialize)]
#[serde(transparent)]
enum E {}
fn main() {}
@@ -0,0 +1,9 @@
error: #[serde(transparent)] is not allowed on an enum
--> $DIR/enum.rs:5:1
|
5 | / #[serde(transparent)]
6 | | enum E {}
| |_________^
error: aborting due to previous error
@@ -1,8 +1,12 @@
error: #[serde(transparent)] requires at least one field that is not skipped
--> $DIR/ser_at_least_one.rs:4:10
--> $DIR/ser_at_least_one.rs:5:1
|
4 | #[derive(Serialize)]
| ^^^^^^^^^
5 | / #[serde(transparent)]
6 | | struct S {
7 | | #[serde(skip)]
8 | | a: u8,
9 | | }
| |_^
error: aborting due to previous error
@@ -0,0 +1,8 @@
#[macro_use]
extern crate serde_derive;
#[derive(Serialize)]
#[serde(transparent)]
struct S;
fn main() {}
@@ -0,0 +1,9 @@
error: #[serde(transparent)] is not allowed on a unit struct
--> $DIR/unit_struct.rs:5:1
|
5 | / #[serde(transparent)]
6 | | struct S;
| |_________^
error: aborting due to previous error
@@ -0,0 +1,10 @@
#[macro_use]
extern crate serde_derive;
#[derive(Serialize)]
#[serde(transparent, from = "u64")]
struct S {
a: u8,
}
fn main() {}
@@ -0,0 +1,11 @@
error: #[serde(transparent)] is not allowed with #[serde(from = "...")]
--> $DIR/with_from.rs:5:1
|
5 | / #[serde(transparent, from = "u64")]
6 | | struct S {
7 | | a: u8,
8 | | }
| |_^
error: aborting due to previous error
@@ -0,0 +1,10 @@
#[macro_use]
extern crate serde_derive;
#[derive(Serialize)]
#[serde(transparent, into = "u64")]
struct S {
a: u8,
}
fn main() {}
@@ -0,0 +1,11 @@
error: #[serde(transparent)] is not allowed with #[serde(into = "...")]
--> $DIR/with_into.rs:5:1
|
5 | / #[serde(transparent, into = "u64")]
6 | | struct S {
7 | | a: u8,
8 | | }
| |_^
error: aborting due to previous error
@@ -1,8 +1,8 @@
error: failed to parse type: from = "Option<T"
--> $DIR/from.rs:4:10
--> $DIR/from.rs:5:16
|
4 | #[derive(Deserialize)]
| ^^^^^^^^^^^
5 | #[serde(from = "Option<T")]
| ^^^^^^^^^^
error: aborting due to previous error

Some files were not shown because too many files have changed in this diff Show More