Touch up PR 2553

This commit is contained in:
David Tolnay 2023-08-05 22:12:38 -07:00
parent 73931692b2
commit bd7b0e257e
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
8 changed files with 62 additions and 66 deletions

View File

@ -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));
}
}

View File

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

View File

@ -1,4 +1,4 @@
error: #[serde(default)] can only be used on structs with fields
error: #[serde(default)] can only be used on structs
--> tests/ui/default-attribute/enum.rs:4:9
|
4 | #[serde(default)]

View File

@ -1,4 +1,4 @@
error: #[serde(default = "...")] can only be used on structs with fields
error: #[serde(default = "...")] can only be used on structs
--> tests/ui/default-attribute/enum_path.rs:4:9
|
4 | #[serde(default = "default_e")]

View File

@ -1,46 +1,45 @@
use serde_derive::Deserialize;
/// No errors expected
// No errors expected.
#[derive(Deserialize)]
struct T0(u8, u8);
/// No errors expected:
/// - if both fields are provided, both gets value from data
/// - if only one field is provided, the second gets default value
// No errors expected:
// - If both fields are provided, both get value from data.
// - If only one field is provided, the second gets default value.
#[derive(Deserialize)]
struct T1(u8, #[serde(default)] u8);
/// Errors expected -- the first field can get default value only if sequence is
/// empty, but that mean that all other fields cannot be deserialized without
/// errors, so the `#[serde(default)]` attribute is superfluous
// ERROR: The first field can get default value only if sequence is empty, but
// that mean that all other fields cannot be deserialized without errors.
#[derive(Deserialize)]
struct T2(#[serde(default)] u8, u8, u8);
/// No errors expected:
/// - if both fields are provided, both gets value from data
/// - if only one field is provided, the second gets default value
/// - if none fields are provided, both gets default value
// No errors expected:
// - If both fields are provided, both get value from data.
// - If only one field is provided, the second gets default value.
// - If no fields are provided, both get default value.
#[derive(Deserialize)]
struct T3(#[serde(default)] u8, #[serde(default)] u8);
////////////////////////////////////////////////////////////////////////////////
/// No errors expected -- missing fields gets default values
// No errors expected -- missing fields get default values.
#[derive(Deserialize, Default)]
#[serde(default)]
struct T4(u8, u8);
/// No errors expected -- missing fields gets default values
// No errors expected -- missing fields get default values.
#[derive(Deserialize, Default)]
#[serde(default)]
struct T5(#[serde(default)] u8, u8);
/// No errors expected -- missing fields gets default values
// No errors expected -- missing fields get default values.
#[derive(Deserialize, Default)]
#[serde(default)]
struct T6(u8, #[serde(default)] u8);
/// No errors expected -- missing fields gets default values
// No errors expected -- missing fields get default values.
#[derive(Deserialize, Default)]
#[serde(default)]
struct T7(#[serde(default)] u8, #[serde(default)] u8);

View File

@ -1,11 +1,11 @@
error: struct or field must have #[serde(default)] because previous field 0 have #[serde(default)]
--> tests/ui/default-attribute/tuple_struct.rs:17:33
error: field must have #[serde(default)] because previous field 0 has #[serde(default)]
--> tests/ui/default-attribute/tuple_struct.rs:16:33
|
17 | struct T2(#[serde(default)] u8, u8, u8);
16 | struct T2(#[serde(default)] u8, u8, u8);
| ^^
error: struct or field must have #[serde(default)] because previous field 0 have #[serde(default)]
--> tests/ui/default-attribute/tuple_struct.rs:17:37
error: field must have #[serde(default)] because previous field 0 has #[serde(default)]
--> tests/ui/default-attribute/tuple_struct.rs:16:37
|
17 | struct T2(#[serde(default)] u8, u8, u8);
16 | struct T2(#[serde(default)] u8, u8, u8);
| ^^

View File

@ -4,72 +4,71 @@ fn d<T>() -> T {
unimplemented!()
}
/// No errors expected:
/// - if both fields are provided, both gets value from data
/// - if only one field is provided, the second gets default value
// No errors expected:
// - If both fields are provided, both get value from data.
// - If only one field is provided, the second gets default value.
#[derive(Deserialize)]
struct T1(u8, #[serde(default = "d")] u8);
/// Errors expected -- the first field can get default value only if sequence is
/// empty, but that mean that all other fields cannot be deserialized without
/// errors, so the `#[serde(default)]` attribute is superfluous
// ERROR: The first field can get default value only if sequence is empty, but
// that mean that all other fields cannot be deserialized without errors.
#[derive(Deserialize)]
struct T2(#[serde(default = "d")] u8, u8, u8);
/// No errors expected:
/// - if both fields are provided, both gets value from data
/// - if only one field is provided, the second gets default value
/// - if none fields are provided, both gets default value
// No errors expected:
// - If both fields are provided, both get value from data.
// - If only one field is provided, the second gets default value.
// - If no fields are provided, both get default value.
#[derive(Deserialize)]
struct T3(#[serde(default = "d")] u8, #[serde(default = "d")] u8);
////////////////////////////////////////////////////////////////////////////////
/// No errors expected -- missing fields gets default values
// No errors expected -- missing fields get default values.
#[derive(Deserialize, Default)]
#[serde(default)]
struct T1D(#[serde(default = "d")] u8, u8);
/// No errors expected -- missing fields gets default values
// No errors expected -- missing fields get default values.
#[derive(Deserialize, Default)]
#[serde(default)]
struct T2D(u8, #[serde(default = "d")] u8);
/// No errors expected -- missing fields gets default values
// No errors expected -- missing fields get default values.
#[derive(Deserialize, Default)]
#[serde(default)]
struct T3D(#[serde(default = "d")] u8, #[serde(default = "d")] u8);
////////////////////////////////////////////////////////////////////////////////
/// No errors expected -- missing fields gets default values
// No errors expected -- missing fields get default values.
#[derive(Deserialize)]
#[serde(default = "d")]
struct T1Path(#[serde(default)] u8, u8);
/// No errors expected -- missing fields gets default values
// No errors expected -- missing fields get default values.
#[derive(Deserialize)]
#[serde(default = "d")]
struct T2Path(u8, #[serde(default)] u8);
/// No errors expected -- missing fields gets default values
// No errors expected -- missing fields get default values.
#[derive(Deserialize)]
#[serde(default = "d")]
struct T3Path(#[serde(default)] u8, #[serde(default)] u8);
////////////////////////////////////////////////////////////////////////////////
/// No errors expected -- missing fields gets default values
// No errors expected -- missing fields get default values.
#[derive(Deserialize)]
#[serde(default = "d")]
struct T1PathD(#[serde(default = "d")] u8, u8);
/// No errors expected -- missing fields gets default values
// No errors expected -- missing fields get default values.
#[derive(Deserialize)]
#[serde(default = "d")]
struct T2PathD(u8, #[serde(default = "d")] u8);
/// No errors expected -- missing fields gets default values
// No errors expected -- missing fields get default values.
#[derive(Deserialize)]
#[serde(default = "d")]
struct T3PathD(#[serde(default = "d")] u8, #[serde(default = "d")] u8);

View File

@ -1,11 +1,11 @@
error: struct or field must have #[serde(default)] because previous field 0 have #[serde(default)]
--> tests/ui/default-attribute/tuple_struct_path.rs:17:39
error: field must have #[serde(default)] because previous field 0 has #[serde(default)]
--> tests/ui/default-attribute/tuple_struct_path.rs:16:39
|
17 | struct T2(#[serde(default = "d")] u8, u8, u8);
16 | struct T2(#[serde(default = "d")] u8, u8, u8);
| ^^
error: struct or field must have #[serde(default)] because previous field 0 have #[serde(default)]
--> tests/ui/default-attribute/tuple_struct_path.rs:17:43
error: field must have #[serde(default)] because previous field 0 has #[serde(default)]
--> tests/ui/default-attribute/tuple_struct_path.rs:16:43
|
17 | struct T2(#[serde(default = "d")] u8, u8, u8);
16 | struct T2(#[serde(default = "d")] u8, u8, u8);
| ^^