From a3982379302ea6111a2ac219ad5aa78c57ba1a6a Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Wed, 7 Jun 2023 21:48:16 -0700 Subject: [PATCH] Point out serde(untagged) variants which are out of order Previously if someone wrote an enum containing: - `A` (untagged) - `B` (tagged) - `C` (tagged) - `D` (untagged) - `E` (tagged) - `F` (untagged) serde_derive would produce errors referring to B and E only, saying you're supposed to put untagged variants at the end. The choice of B and E for this error doesn't make a lot of sense because in order to resolve the issue, the user must either: - move A and D down or: - move B, C, and E up. This commit changes the error to appear on A and D instead. --- serde_derive/src/internals/ast.rs | 20 +++++++++++++------ .../partially_tagged_wrong_order.stderr | 4 ++-- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/serde_derive/src/internals/ast.rs b/serde_derive/src/internals/ast.rs index b654f7749..8bcb0ecd1 100644 --- a/serde_derive/src/internals/ast.rs +++ b/serde_derive/src/internals/ast.rs @@ -140,8 +140,7 @@ fn enum_from_ast<'a>( variants: &'a Punctuated, container_default: &attr::Default, ) -> Vec> { - let mut seen_untagged = false; - variants + let variants: Vec = variants .iter() .map(|variant| { let attrs = attr::Variant::from_ast(cx, variant); @@ -154,12 +153,21 @@ fn enum_from_ast<'a>( fields, original: variant, } - }).inspect(|variant| { - if !variant.attrs.untagged() && seen_untagged { + }) + .collect(); + + let index_of_last_tagged_variant = variants + .iter() + .rposition(|variant| !variant.attrs.untagged()); + if let Some(index_of_last_tagged_variant) = index_of_last_tagged_variant { + for variant in &variants[..index_of_last_tagged_variant] { + if variant.attrs.untagged() { cx.error_spanned_by(&variant.ident, "all variants with the #[serde(untagged)] attribute must be placed at the end of the enum"); } - seen_untagged = variant.attrs.untagged(); - }).collect() + } + } + + variants } fn struct_from_ast<'a>( diff --git a/test_suite/tests/ui/enum-representation/partially_tagged_wrong_order.stderr b/test_suite/tests/ui/enum-representation/partially_tagged_wrong_order.stderr index 17d3c3f93..08ee26663 100644 --- a/test_suite/tests/ui/enum-representation/partially_tagged_wrong_order.stderr +++ b/test_suite/tests/ui/enum-representation/partially_tagged_wrong_order.stderr @@ -1,5 +1,5 @@ error: all variants with the #[serde(untagged)] attribute must be placed at the end of the enum - --> tests/ui/enum-representation/partially_tagged_wrong_order.rs:7:5 + --> tests/ui/enum-representation/partially_tagged_wrong_order.rs:6:5 | -7 | B(String), +6 | A(u8), | ^