From bd7b0e257e4cb4ab59833ea39ba010edf5e132bb Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 5 Aug 2023 22:12:38 -0700 Subject: [PATCH] Touch up PR 2553 --- serde_derive/src/internals/attr.rs | 12 +++--- serde_derive/src/internals/check.rs | 22 +++++------ .../tests/ui/default-attribute/enum.stderr | 2 +- .../ui/default-attribute/enum_path.stderr | 2 +- .../ui/default-attribute/tuple_struct.rs | 29 +++++++-------- .../ui/default-attribute/tuple_struct.stderr | 12 +++--- .../ui/default-attribute/tuple_struct_path.rs | 37 +++++++++---------- .../tuple_struct_path.stderr | 12 +++--- 8 files changed, 62 insertions(+), 66 deletions(-) diff --git a/serde_derive/src/internals/attr.rs b/serde_derive/src/internals/attr.rs index 0810114b..895ea7ee 100644 --- a/serde_derive/src/internals/attr.rs +++ b/serde_derive/src/internals/attr.rs @@ -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)); } } diff --git a/serde_derive/src/internals/check.rs b/serde_derive/src/internals/check.rs index ea70e630..b01a8ca4 100644 --- a/serde_derive/src/internals/check.rs +++ b/serde_derive/src/internals/check.rs @@ -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 diff --git a/test_suite/tests/ui/default-attribute/enum.stderr b/test_suite/tests/ui/default-attribute/enum.stderr index 9f5b429e..27fbc146 100644 --- a/test_suite/tests/ui/default-attribute/enum.stderr +++ b/test_suite/tests/ui/default-attribute/enum.stderr @@ -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)] diff --git a/test_suite/tests/ui/default-attribute/enum_path.stderr b/test_suite/tests/ui/default-attribute/enum_path.stderr index 8dde0e00..65ef3ac4 100644 --- a/test_suite/tests/ui/default-attribute/enum_path.stderr +++ b/test_suite/tests/ui/default-attribute/enum_path.stderr @@ -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")] diff --git a/test_suite/tests/ui/default-attribute/tuple_struct.rs b/test_suite/tests/ui/default-attribute/tuple_struct.rs index 66f2d430..486abba7 100644 --- a/test_suite/tests/ui/default-attribute/tuple_struct.rs +++ b/test_suite/tests/ui/default-attribute/tuple_struct.rs @@ -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); diff --git a/test_suite/tests/ui/default-attribute/tuple_struct.stderr b/test_suite/tests/ui/default-attribute/tuple_struct.stderr index 92dfea75..7af857e6 100644 --- a/test_suite/tests/ui/default-attribute/tuple_struct.stderr +++ b/test_suite/tests/ui/default-attribute/tuple_struct.stderr @@ -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); | ^^ diff --git a/test_suite/tests/ui/default-attribute/tuple_struct_path.rs b/test_suite/tests/ui/default-attribute/tuple_struct_path.rs index 76cfedf5..48b07cbd 100644 --- a/test_suite/tests/ui/default-attribute/tuple_struct_path.rs +++ b/test_suite/tests/ui/default-attribute/tuple_struct_path.rs @@ -4,72 +4,71 @@ fn d() -> 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); diff --git a/test_suite/tests/ui/default-attribute/tuple_struct_path.stderr b/test_suite/tests/ui/default-attribute/tuple_struct_path.stderr index 6fb23eb2..92bf8cf2 100644 --- a/test_suite/tests/ui/default-attribute/tuple_struct_path.stderr +++ b/test_suite/tests/ui/default-attribute/tuple_struct_path.stderr @@ -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); | ^^