Skip to content

Commit a398237

Browse files
committed
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.
1 parent b63c65d commit a398237

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

serde_derive/src/internals/ast.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,7 @@ fn enum_from_ast<'a>(
140140
variants: &'a Punctuated<syn::Variant, Token![,]>,
141141
container_default: &attr::Default,
142142
) -> Vec<Variant<'a>> {
143-
let mut seen_untagged = false;
144-
variants
143+
let variants: Vec<Variant> = variants
145144
.iter()
146145
.map(|variant| {
147146
let attrs = attr::Variant::from_ast(cx, variant);
@@ -154,12 +153,21 @@ fn enum_from_ast<'a>(
154153
fields,
155154
original: variant,
156155
}
157-
}).inspect(|variant| {
158-
if !variant.attrs.untagged() && seen_untagged {
156+
})
157+
.collect();
158+
159+
let index_of_last_tagged_variant = variants
160+
.iter()
161+
.rposition(|variant| !variant.attrs.untagged());
162+
if let Some(index_of_last_tagged_variant) = index_of_last_tagged_variant {
163+
for variant in &variants[..index_of_last_tagged_variant] {
164+
if variant.attrs.untagged() {
159165
cx.error_spanned_by(&variant.ident, "all variants with the #[serde(untagged)] attribute must be placed at the end of the enum");
160166
}
161-
seen_untagged = variant.attrs.untagged();
162-
}).collect()
167+
}
168+
}
169+
170+
variants
163171
}
164172

165173
fn struct_from_ast<'a>(
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: all variants with the #[serde(untagged)] attribute must be placed at the end of the enum
2-
--> tests/ui/enum-representation/partially_tagged_wrong_order.rs:7:5
2+
--> tests/ui/enum-representation/partially_tagged_wrong_order.rs:6:5
33
|
4-
7 | B(String),
4+
6 | A(u8),
55
| ^

0 commit comments

Comments
 (0)