Merge pull request #2553 from Mingun/default-on-tuples

Allow `#[serde(default)]` on tuple structs
This commit is contained in:
David Tolnay
2023-08-05 22:12:10 -07:00
committed by GitHub
13 changed files with 230 additions and 60 deletions
+10 -10
View File
@@ -396,20 +396,20 @@ impl Container {
if let Some(path) = parse_lit_into_expr_path(cx, DEFAULT, &meta)? {
match &item.data {
syn::Data::Struct(syn::DataStruct { fields, .. }) => match fields {
syn::Fields::Named(_) => {
syn::Fields::Named(_) | syn::Fields::Unnamed(_) => {
default.set(&meta.path, Default::Path(path));
}
syn::Fields::Unnamed(_) | syn::Fields::Unit => {
let msg = "#[serde(default = \"...\")] can only be used on structs with named fields";
syn::Fields::Unit => {
let msg = "#[serde(default = \"...\")] can only be used on structs with fields";
cx.syn_error(meta.error(msg));
}
},
syn::Data::Enum(_) => {
let msg = "#[serde(default = \"...\")] can only be used on structs with named fields";
let msg = "#[serde(default = \"...\")] can only be used on structs with fields";
cx.syn_error(meta.error(msg));
}
syn::Data::Union(_) => {
let msg = "#[serde(default = \"...\")] can only be used on structs with named fields";
let msg = "#[serde(default = \"...\")] can only be used on structs with fields";
cx.syn_error(meta.error(msg));
}
}
@@ -418,20 +418,20 @@ impl Container {
// #[serde(default)]
match &item.data {
syn::Data::Struct(syn::DataStruct { fields, .. }) => match fields {
syn::Fields::Named(_) => {
syn::Fields::Named(_) | syn::Fields::Unnamed(_) => {
default.set(meta.path, Default::Default);
}
syn::Fields::Unnamed(_) | syn::Fields::Unit => {
let msg = "#[serde(default)] can only be used on structs with named fields";
syn::Fields::Unit => {
let msg = "#[serde(default)] can only be used on structs with fields";
cx.error_spanned_by(fields, msg);
}
},
syn::Data::Enum(_) => {
let msg = "#[serde(default)] can only be used on structs with named fields";
let msg = "#[serde(default)] can only be used on structs with fields";
cx.syn_error(meta.error(msg));
}
syn::Data::Union(_) => {
let msg = "#[serde(default)] can only be used on structs with named fields";
let msg = "#[serde(default)] can only be used on structs with fields";
cx.syn_error(meta.error(msg));
}
}
+37 -1
View File
@@ -1,11 +1,12 @@
use crate::internals::ast::{Container, Data, Field, Style};
use crate::internals::attr::{Identifier, TagType};
use crate::internals::attr::{Default, Identifier, TagType};
use crate::internals::{ungroup, Ctxt, Derive};
use syn::{Member, Type};
// Cross-cutting checks that require looking at more than a single attrs object.
// Simpler checks should happen when parsing and building the attrs.
pub fn check(cx: &Ctxt, cont: &mut Container, derive: Derive) {
check_default_on_tuple(cx, cont);
check_remote_generic(cx, cont);
check_getter(cx, cont);
check_flatten(cx, cont);
@@ -17,6 +18,41 @@ 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.
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
if field.attrs.skip_deserializing() {
continue;
}
if let Default::None = field.attrs.default() {
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),
);
}
continue;
}
if let None = first_default_index {
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
// remote type:
//