pull_number
int64
2.56k
2.8k
test_patch
stringclasses
2 values
issue_numbers
listlengths
1
2
instance_id
stringclasses
2 values
problem_statement
stringclasses
2 values
version
stringclasses
2 values
base_commit
stringclasses
2 values
patch
stringclasses
2 values
repo
stringclasses
1 value
created_at
stringclasses
2 values
hints_text
stringclasses
2 values
environment_setup_commit
stringclasses
2 values
FAIL_TO_PASS
listlengths
1
3
PASS_TO_PASS
listlengths
471
513
FAIL_TO_FAIL
listlengths
0
0
PASS_TO_FAIL
listlengths
0
0
2,562
diff --git a/test_suite/tests/ui/conflict/alias-enum.rs b/test_suite/tests/ui/conflict/alias-enum.rs new file mode 100644 index 000000000..52af74b97 --- /dev/null +++ b/test_suite/tests/ui/conflict/alias-enum.rs @@ -0,0 +1,79 @@ +#![allow(non_camel_case_types)] + +use serde_derive::Deserialize; + +#[derive(Deserialize)] +enum E { + S1 { + /// Expected error on "alias b", because this is a name of other field + /// Error on "alias a" is not expected because this is a name of this field + /// Error on "alias c" is not expected because field `c` is skipped + #[serde(alias = "a", alias = "b", alias = "c")] + a: (), + + /// Expected error on "alias c", because it is already used as alias of `a` + #[serde(alias = "c")] + b: (), + + #[serde(skip_deserializing)] + c: (), + }, + + S2 { + /// Expected error on "alias c", because this is a name of other field after + /// applying rename rules + #[serde(alias = "b", alias = "c")] + a: (), + + #[serde(rename = "c")] + b: (), + }, + + #[serde(rename_all = "UPPERCASE")] + S3 { + /// Expected error on "alias B", because this is a name of field after + /// applying rename rules + #[serde(alias = "B", alias = "c")] + a: (), + b: (), + }, +} + +#[derive(Deserialize)] +enum E1 { + /// Expected error on "alias b", because this is a name of other variant + /// Error on "alias a" is not expected because this is a name of this variant + /// Error on "alias c" is not expected because variant `c` is skipped + #[serde(alias = "a", alias = "b", alias = "c")] + a, + + /// Expected error on "alias c", because it is already used as alias of `a` + #[serde(alias = "c")] + b, + + #[serde(skip_deserializing)] + c, +} + +#[derive(Deserialize)] +enum E2 { + /// Expected error on "alias c", because this is a name of other variant after + /// applying rename rules + #[serde(alias = "b", alias = "c")] + a, + + #[serde(rename = "c")] + b, +} + +#[derive(Deserialize)] +#[serde(rename_all = "UPPERCASE")] +enum E3 { + /// Expected error on "alias B", because this is a name of variant after + /// applying rename rules + #[serde(alias = "B", alias = "c")] + a, + b, +} + +fn main() {} diff --git a/test_suite/tests/ui/conflict/alias-enum.stderr b/test_suite/tests/ui/conflict/alias-enum.stderr new file mode 100644 index 000000000..36e036f65 --- /dev/null +++ b/test_suite/tests/ui/conflict/alias-enum.stderr @@ -0,0 +1,71 @@ +error: alias `b` conflicts with deserialization name of other field + --> tests/ui/conflict/alias-enum.rs:8:9 + | +8 | / /// Expected error on "alias b", because this is a name of other field +9 | | /// Error on "alias a" is not expected because this is a name of this field +10 | | /// Error on "alias c" is not expected because field `c` is skipped +11 | | #[serde(alias = "a", alias = "b", alias = "c")] +12 | | a: (), + | |_____________^ + +error: alias `c` already used by field a + --> tests/ui/conflict/alias-enum.rs:14:9 + | +14 | / /// Expected error on "alias c", because it is already used as alias of `a` +15 | | #[serde(alias = "c")] +16 | | b: (), + | |_____________^ + +error: alias `c` conflicts with deserialization name of other field + --> tests/ui/conflict/alias-enum.rs:23:9 + | +23 | / /// Expected error on "alias c", because this is a name of other field after +24 | | /// applying rename rules +25 | | #[serde(alias = "b", alias = "c")] +26 | | a: (), + | |_____________^ + +error: alias `B` conflicts with deserialization name of other field + --> tests/ui/conflict/alias-enum.rs:34:9 + | +34 | / /// Expected error on "alias B", because this is a name of field after +35 | | /// applying rename rules +36 | | #[serde(alias = "B", alias = "c")] +37 | | a: (), + | |_____________^ + +error: alias `b` conflicts with deserialization name of other variant + --> tests/ui/conflict/alias-enum.rs:44:5 + | +44 | / /// Expected error on "alias b", because this is a name of other variant +45 | | /// Error on "alias a" is not expected because this is a name of this variant +46 | | /// Error on "alias c" is not expected because variant `c` is skipped +47 | | #[serde(alias = "a", alias = "b", alias = "c")] +48 | | a, + | |_____^ + +error: alias `c` already used by variant a + --> tests/ui/conflict/alias-enum.rs:50:5 + | +50 | / /// Expected error on "alias c", because it is already used as alias of `a` +51 | | #[serde(alias = "c")] +52 | | b, + | |_____^ + +error: alias `c` conflicts with deserialization name of other variant + --> tests/ui/conflict/alias-enum.rs:60:5 + | +60 | / /// Expected error on "alias c", because this is a name of other variant after +61 | | /// applying rename rules +62 | | #[serde(alias = "b", alias = "c")] +63 | | a, + | |_____^ + +error: alias `B` conflicts with deserialization name of other variant + --> tests/ui/conflict/alias-enum.rs:72:5 + | +72 | / /// Expected error on "alias B", because this is a name of variant after +73 | | /// applying rename rules +74 | | #[serde(alias = "B", alias = "c")] +75 | | a, + | |_____^ diff --git a/test_suite/tests/ui/conflict/alias.rs b/test_suite/tests/ui/conflict/alias.rs new file mode 100644 index 000000000..f52a90586 --- /dev/null +++ b/test_suite/tests/ui/conflict/alias.rs @@ -0,0 +1,40 @@ +use serde_derive::Deserialize; + +#[derive(Deserialize)] +struct S1 { + /// Expected error on "alias b", because this is a name of other field + /// Error on "alias a" is not expected because this is a name of this field + /// Error on "alias c" is not expected because field `c` is skipped + #[serde(alias = "a", alias = "b", alias = "c")] + a: (), + + /// Expected error on "alias c", because it is already used as alias of `a` + #[serde(alias = "c")] + b: (), + + #[serde(skip_deserializing)] + c: (), +} + +#[derive(Deserialize)] +struct S2 { + /// Expected error on "alias c", because this is a name of other field after + /// applying rename rules + #[serde(alias = "b", alias = "c")] + a: (), + + #[serde(rename = "c")] + b: (), +} + +#[derive(Deserialize)] +#[serde(rename_all = "UPPERCASE")] +struct S3 { + /// Expected error on "alias B", because this is a name of field after + /// applying rename rules + #[serde(alias = "B", alias = "c")] + a: (), + b: (), +} + +fn main() {} diff --git a/test_suite/tests/ui/conflict/alias.stderr b/test_suite/tests/ui/conflict/alias.stderr new file mode 100644 index 000000000..2115b21b1 --- /dev/null +++ b/test_suite/tests/ui/conflict/alias.stderr @@ -0,0 +1,35 @@ +error: alias `b` conflicts with deserialization name of other field + --> tests/ui/conflict/alias.rs:5:5 + | +5 | / /// Expected error on "alias b", because this is a name of other field +6 | | /// Error on "alias a" is not expected because this is a name of this field +7 | | /// Error on "alias c" is not expected because field `c` is skipped +8 | | #[serde(alias = "a", alias = "b", alias = "c")] +9 | | a: (), + | |_________^ + +error: alias `c` already used by field a + --> tests/ui/conflict/alias.rs:11:5 + | +11 | / /// Expected error on "alias c", because it is already used as alias of `a` +12 | | #[serde(alias = "c")] +13 | | b: (), + | |_________^ + +error: alias `c` conflicts with deserialization name of other field + --> tests/ui/conflict/alias.rs:21:5 + | +21 | / /// Expected error on "alias c", because this is a name of other field after +22 | | /// applying rename rules +23 | | #[serde(alias = "b", alias = "c")] +24 | | a: (), + | |_________^ + +error: alias `B` conflicts with deserialization name of other field + --> tests/ui/conflict/alias.rs:33:5 + | +33 | / /// Expected error on "alias B", because this is a name of field after +34 | | /// applying rename rules +35 | | #[serde(alias = "B", alias = "c")] +36 | | a: (), + | |_________^
[ "2308", "2551" ]
serde-rs__serde-2562
No unreachable warning when duplicate field names on enum after rename attributes Looking into the rename feature I discovered that rustc does not gives an unreachable warning with serde rename collisions with `enums` whereas it does on `structs` as mentioned in #754 Would you still be open to a PR to make clashing renames an error? If so I'm happy to give it a go. ## Example ```rust use serde::{Deserialize}; #[derive(Deserialize)] enum Message { #[serde(rename = "Response")] Request { id: String}, #[serde(rename = "Response")] Response { id: String}, } fn main() { let json = "{\"Response\": {\"id\": \"...\"}}"; let parsed: Message = match serde_json::from_str(&json) { Ok(contact) => contact, Err(err) => { println!("{:?}", err); unimplemented!() } }; match parsed { Message::Request { id } => println!("request {}", id), Message::Response { id } => println!("response {}", id) } } ``` ### Output `request ...` with no compiler warnings playgrounds link https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=c6a787d51f1290af999a0e36b9a6d366 Field/variant aliases are not checked for uniqueness [The code](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=0551945af3b0581fefd8c0c9684e4182) ```rust use serde::Deserialize; // 1.0.171; use serde_json; // 1.0.102; #[derive(Deserialize, Debug)] #[serde(deny_unknown_fields)] pub struct Thing { pub w: u8, #[serde(alias = "z", alias = "x")] pub y: u8, #[serde(alias = "same", alias = "other", alias = "same", alias = "x", alias = "y")] pub same: u8, } fn main() { let j = r#" {"j":null} "#; println!("{}", serde_json::from_str::<Thing>(j).unwrap_err()); } ``` gives the following output: ``` unknown field `j`, expected one of `w`, `x`, `z`, `y`, `other`, `same`, `x`, `y` at line 1 column 5 ```
1.21
58a8d229315553c4ae0a8d7eee8e382fbae4b4bf
diff --git a/serde_derive/src/internals/check.rs b/serde_derive/src/internals/check.rs index 52b0f379f..df5d63f01 100644 --- a/serde_derive/src/internals/check.rs +++ b/serde_derive/src/internals/check.rs @@ -1,6 +1,8 @@ -use crate::internals::ast::{Container, Data, Field, Style}; +use crate::internals::ast::{Container, Data, Field, Style, Variant}; use crate::internals::attr::{Default, Identifier, TagType}; use crate::internals::{ungroup, Ctxt, Derive}; +use std::collections::btree_map::Entry; +use std::collections::{BTreeMap, BTreeSet}; use syn::{Member, Type}; // Cross-cutting checks that require looking at more than a single attrs object. @@ -16,6 +18,7 @@ pub fn check(cx: &Ctxt, cont: &mut Container, derive: Derive) { check_adjacent_tag_conflict(cx, cont); check_transparent(cx, cont, derive); check_from_and_try_from(cx, cont); + check_name_conflicts(cx, cont, derive); } // If some field of a tuple struct is marked #[serde(default)] then all fields @@ -475,3 +478,134 @@ fn check_from_and_try_from(cx: &Ctxt, cont: &mut Container) { ); } } + +// Checks that aliases does not repeated +fn check_name_conflicts(cx: &Ctxt, cont: &Container, derive: Derive) { + if let Derive::Deserialize = derive { + match &cont.data { + Data::Enum(variants) => check_variant_name_conflicts(cx, &variants), + Data::Struct(Style::Struct, fields) => check_field_name_conflicts(cx, fields), + _ => {} + } + } +} + +// All renames already applied +fn check_variant_name_conflicts(cx: &Ctxt, variants: &[Variant]) { + let names: BTreeSet<_> = variants + .iter() + .filter_map(|variant| { + if variant.attrs.skip_deserializing() { + None + } else { + Some(variant.attrs.name().deserialize_name().to_owned()) + } + }) + .collect(); + let mut alias_owners = BTreeMap::new(); + + for variant in variants { + let name = variant.attrs.name().deserialize_name(); + + for alias in variant.attrs.aliases().intersection(&names) { + // Aliases contains variant names, so filter them out + if alias == name { + continue; + } + + // TODO: report other variant location when this become possible + cx.error_spanned_by( + variant.original, + format!( + "alias `{}` conflicts with deserialization name of other variant", + alias + ), + ); + } + + for alias in variant.attrs.aliases() { + // Aliases contains variant names, so filter them out + if alias == name { + continue; + } + + match alias_owners.entry(alias) { + Entry::Vacant(e) => { + e.insert(variant); + } + Entry::Occupied(e) => { + // TODO: report other variant location when this become possible + cx.error_spanned_by( + variant.original, + format!( + "alias `{}` already used by variant {}", + alias, + e.get().original.ident + ), + ); + } + } + } + + check_field_name_conflicts(cx, &variant.fields); + } +} + +// All renames already applied +fn check_field_name_conflicts(cx: &Ctxt, fields: &[Field]) { + let names: BTreeSet<_> = fields + .iter() + .filter_map(|field| { + if field.attrs.skip_deserializing() { + None + } else { + Some(field.attrs.name().deserialize_name().to_owned()) + } + }) + .collect(); + let mut alias_owners = BTreeMap::new(); + + for field in fields { + let name = field.attrs.name().deserialize_name(); + + for alias in field.attrs.aliases().intersection(&names) { + // Aliases contains field names, so filter them out + if alias == name { + continue; + } + + // TODO: report other field location when this become possible + cx.error_spanned_by( + field.original, + format!( + "alias `{}` conflicts with deserialization name of other field", + alias + ), + ); + } + + for alias in field.attrs.aliases() { + // Aliases contains field names, so filter them out + if alias == name { + continue; + } + + match alias_owners.entry(alias) { + Entry::Vacant(e) => { + e.insert(field); + } + Entry::Occupied(e) => { + // TODO: report other field location when this become possible + cx.error_spanned_by( + field.original, + format!( + "alias `{}` already used by field {}", + alias, + e.get().original.ident.as_ref().unwrap() + ), + ); + } + } + } + } +}
serde-rs/serde
2023-08-08T17:44:27Z
or maybe instead of an error it should follow rustc and display a warning
49e11ce1bae9fbb9128c9144c4e1051daf7a29ed
[ "tests/ui/conflict/alias-enum.rs", "tests/ui/conflict/alias.rs", "ui" ]
[ "ser::impls::test_format_u8", "internals::case::rename_fields", "internals::case::rename_variants", "tests/ui/borrow/bad_lifetimes.rs", "tests/ui/borrow/duplicate_lifetime.rs", "tests/ui/borrow/duplicate_variant.rs", "tests/ui/borrow/empty_lifetimes.rs", "tests/ui/borrow/no_lifetimes.rs", "tests/ui/...
[]
[]
2,802
diff --git a/test_suite/tests/test_annotations.rs b/test_suite/tests/test_annotations.rs index 1174be6d2..63cb3dd1b 100644 --- a/test_suite/tests/test_annotations.rs +++ b/test_suite/tests/test_annotations.rs @@ -1815,6 +1815,32 @@ fn test_flatten_unit() { ); } +#[test] +fn test_flatten_unit_struct() { + #[derive(Debug, PartialEq, Serialize, Deserialize)] + struct Response<T> { + #[serde(flatten)] + data: T, + status: usize, + } + + #[derive(Debug, PartialEq, Serialize, Deserialize)] + struct Unit; + + assert_tokens( + &Response { + data: Unit, + status: 0, + }, + &[ + Token::Map { len: None }, + Token::Str("status"), + Token::U64(0), + Token::MapEnd, + ], + ); +} + #[test] fn test_flatten_unsupported_type() { #[derive(Debug, PartialEq, Serialize, Deserialize)]
[ "2801" ]
serde-rs__serde-2802
Flattening a unit struct should work like flattening unit itself https://github.com/serde-rs/serde/pull/1874 landed support for flattening the unit type after a report in https://github.com/serde-rs/serde/issues/1873. However, it did not override `deserialize_unit_struct` and `serialize_unit_struct`, so while flattening `()` now works, flattening `struct Unit;` does not. It's not clear whether this was intentional or not, though it seems reasonable to support unit structs the same as unit here.
1.20
1b4da41f970555e111f471633205bbcb4dadbc63
diff --git a/serde/src/private/de.rs b/serde/src/private/de.rs index c402d2c66..c146539ee 100644 --- a/serde/src/private/de.rs +++ b/serde/src/private/de.rs @@ -2710,6 +2710,17 @@ where visitor.visit_unit() } + fn deserialize_unit_struct<V>( + self, + _name: &'static str, + visitor: V, + ) -> Result<V::Value, Self::Error> + where + V: Visitor<'de>, + { + visitor.visit_unit() + } + fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>, @@ -2734,7 +2745,6 @@ where deserialize_string() deserialize_bytes() deserialize_byte_buf() - deserialize_unit_struct(&'static str) deserialize_seq() deserialize_tuple(usize) deserialize_tuple_struct(&'static str, usize) diff --git a/serde/src/private/ser.rs b/serde/src/private/ser.rs index 9570629e9..ebfeba97e 100644 --- a/serde/src/private/ser.rs +++ b/serde/src/private/ser.rs @@ -51,8 +51,6 @@ enum Unsupported { String, ByteArray, Optional, - #[cfg(any(feature = "std", feature = "alloc"))] - UnitStruct, Sequence, Tuple, TupleStruct, @@ -69,8 +67,6 @@ impl Display for Unsupported { Unsupported::String => formatter.write_str("a string"), Unsupported::ByteArray => formatter.write_str("a byte array"), Unsupported::Optional => formatter.write_str("an optional"), - #[cfg(any(feature = "std", feature = "alloc"))] - Unsupported::UnitStruct => formatter.write_str("unit struct"), Unsupported::Sequence => formatter.write_str("a sequence"), Unsupported::Tuple => formatter.write_str("a tuple"), Unsupported::TupleStruct => formatter.write_str("a tuple struct"), @@ -1092,7 +1088,7 @@ where } fn serialize_unit_struct(self, _: &'static str) -> Result<Self::Ok, Self::Error> { - Err(Self::bad_type(Unsupported::UnitStruct)) + Ok(()) } fn serialize_unit_variant(
serde-rs/serde
2024-08-15T13:51:17Z
1b4da41f970555e111f471633205bbcb4dadbc63
[ "test_flatten_unit_struct" ]
[ "ser::impls::test_format_u8", "internals::case::rename_fields", "internals::case::rename_variants", "tests/ui/borrow/bad_lifetimes.rs", "tests/ui/borrow/duplicate_lifetime.rs", "tests/ui/borrow/duplicate_variant.rs", "tests/ui/borrow/empty_lifetimes.rs", "tests/ui/borrow/no_lifetimes.rs", "tests/ui/...
[]
[]