mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-04-22 19:28:01 +00:00
Touch up PR 2553
This commit is contained in:
@@ -400,16 +400,16 @@ impl Container {
|
||||
default.set(&meta.path, Default::Path(path));
|
||||
}
|
||||
syn::Fields::Unit => {
|
||||
let msg = "#[serde(default = \"...\")] can only be used on structs with fields";
|
||||
let msg = "#[serde(default = \"...\")] can only be used on structs that have fields";
|
||||
cx.syn_error(meta.error(msg));
|
||||
}
|
||||
},
|
||||
syn::Data::Enum(_) => {
|
||||
let msg = "#[serde(default = \"...\")] can only be used on structs with fields";
|
||||
let msg = "#[serde(default = \"...\")] can only be used on structs";
|
||||
cx.syn_error(meta.error(msg));
|
||||
}
|
||||
syn::Data::Union(_) => {
|
||||
let msg = "#[serde(default = \"...\")] can only be used on structs with fields";
|
||||
let msg = "#[serde(default = \"...\")] can only be used on structs";
|
||||
cx.syn_error(meta.error(msg));
|
||||
}
|
||||
}
|
||||
@@ -422,16 +422,16 @@ impl Container {
|
||||
default.set(meta.path, Default::Default);
|
||||
}
|
||||
syn::Fields::Unit => {
|
||||
let msg = "#[serde(default)] can only be used on structs with fields";
|
||||
let msg = "#[serde(default)] can only be used on structs that have fields";
|
||||
cx.error_spanned_by(fields, msg);
|
||||
}
|
||||
},
|
||||
syn::Data::Enum(_) => {
|
||||
let msg = "#[serde(default)] can only be used on structs with fields";
|
||||
let msg = "#[serde(default)] can only be used on structs";
|
||||
cx.syn_error(meta.error(msg));
|
||||
}
|
||||
syn::Data::Union(_) => {
|
||||
let msg = "#[serde(default)] can only be used on structs with fields";
|
||||
let msg = "#[serde(default)] can only be used on structs";
|
||||
cx.syn_error(meta.error(msg));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,19 +18,19 @@ pub fn check(cx: &Ctxt, cont: &mut Container, derive: Derive) {
|
||||
check_from_and_try_from(cx, cont);
|
||||
}
|
||||
|
||||
/// If some field of tuple is marked as `#[serde(default)]` then all subsequent
|
||||
/// fields also should be marked with that attribute or the struct itself should
|
||||
/// have this attribute. This is because using default value for a field is
|
||||
/// possible only if the sequence is exhausted that means that all subsequent
|
||||
/// fields will fail to deserialize and should provide a default value if we want
|
||||
/// the successful deserialization.
|
||||
// If some field of a tuple struct is marked #[serde(default)] then all fields
|
||||
// after it must also be marked with that attribute, or the struct must have a
|
||||
// container-level serde(default) attribute. A field's default value is only
|
||||
// used for tuple fields if the sequence is exhausted at that point; that means
|
||||
// all subsequent fields will fail to deserialize if they don't have their own
|
||||
// default.
|
||||
fn check_default_on_tuple(cx: &Ctxt, cont: &Container) {
|
||||
if let Default::None = cont.attrs.default() {
|
||||
if let Data::Struct(Style::Tuple, fields) = &cont.data {
|
||||
let mut first_default_index = None;
|
||||
for (i, field) in fields.iter().enumerate() {
|
||||
// Skipped fields automatically get the #[serde(default)] attribute
|
||||
// We interested only on non-skipped fields here
|
||||
// Skipped fields automatically get the #[serde(default)]
|
||||
// attribute. We are interested only on non-skipped fields here.
|
||||
if field.attrs.skip_deserializing() {
|
||||
continue;
|
||||
}
|
||||
@@ -38,19 +38,17 @@ fn check_default_on_tuple(cx: &Ctxt, cont: &Container) {
|
||||
if let Some(first) = first_default_index {
|
||||
cx.error_spanned_by(
|
||||
field.ty,
|
||||
format!("struct or field must have #[serde(default)] because previous field {} have #[serde(default)]", first),
|
||||
format!("field must have #[serde(default)] because previous field {} has #[serde(default)]", first),
|
||||
);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if let None = first_default_index {
|
||||
if first_default_index.is_none() {
|
||||
first_default_index = Some(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// TODO: Warn if container has default and all fields also marked with default
|
||||
// when warnings in proc-macro become available
|
||||
}
|
||||
|
||||
// Remote derive definition type must have either all of the generics of the
|
||||
|
||||
Reference in New Issue
Block a user