Skip to content

Commit

Permalink
Fix missing case of the one-tuple-struct-inside-unwrapped-newtype-var…
Browse files Browse the repository at this point in the history
…iant bug (#516)
  • Loading branch information
juntyr committed Oct 13, 2023
1 parent d59ec87 commit a6fc5eb
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ While data structures with any of these attributes should generally roundtrip th
- zero-length arrays / tuples / tuple structs / structs / tuple variants / struct variants
- `Option`s with `#[enable(implicit_some)]` must not contain any of these or a unit, unit struct, or an untagged unit variant
- externally tagged tuple variants with just one field (that are not newtype variants)
- tuples or arrays with just one element are not supported inside newtype variants with `#[enable(unwrap_variant_newtypes)]`
- tuples or arrays or tuple structs with just one element are not supported inside newtype variants with `#[enable(unwrap_variant_newtypes)]` (including `Some`)
- a `ron::value::RawValue`
- untagged tuple / struct variants with no fields are not supported
- untagged tuple variants with just one field (that are not newtype variants) are not supported when the `#![enable(unwrap_variant_newtypes)]` extension is enabled
Expand Down
10 changes: 10 additions & 0 deletions fuzz/fuzz_targets/bench/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5398,6 +5398,16 @@ impl<'a> SerdeDataType<'a> {
return false;
}

if fields.len() == 1
&& inside_newtype_variant
&& pretty
.extensions
.contains(Extensions::UNWRAP_VARIANT_NEWTYPES)
{
// BUG: a one-length tuple struct inside an unwrapped variant newtype will be swallowed
return false;
}

fields
.iter()
.all(|field| field.supported_inside_untagged(pretty, false, false))
Expand Down
10 changes: 4 additions & 6 deletions tests/502_known_bugs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -736,21 +736,19 @@ fn one_tuple_inside_unwrapped_newtype_variant_inside_adjacently_tagged() {
#[test]
fn one_tuple_inside_unwrapped_newtype_variant_inside_untagged() {
#[derive(PartialEq, Debug, Serialize, Deserialize)]
enum A {
Newtype((i32,)),
}
struct OneTuple(i32, #[serde(skip)] ());

#[derive(PartialEq, Debug, Serialize, Deserialize)]
#[serde(untagged)]
enum Untagged {
B { ho: i32, a: A },
B { ho: i32, a: Option<OneTuple> },
}

assert_eq!(
check_roundtrip(
&Untagged::B {
ho: 24,
a: A::Newtype((42,))
a: Some(OneTuple(42, ()))
},
PrettyConfig::default()
),
Expand All @@ -760,7 +758,7 @@ fn one_tuple_inside_unwrapped_newtype_variant_inside_untagged() {
check_roundtrip(
&Untagged::B {
ho: 24,
a: A::Newtype((42,))
a: Some(OneTuple(42, ()))
},
PrettyConfig::default().extensions(Extensions::UNWRAP_VARIANT_NEWTYPES)
),
Expand Down

0 comments on commit a6fc5eb

Please sign in to comment.