Skip to content
Myr

Add checks for conflicts for aliases

Before adoption: this commit came in with the history the project adopted, so no landing records it.

- Check that alias is not the same as name of other field (it still can be the name of owning field/variant) - Check that aliases are unique, i. e. two different fields does not use the same alias

authored by Mingun · committed by Mingun · parent 58a8d22 · Browse files · 5 files+360 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+360 added−1 removed

  • tests:+225 added−0 removed
  • other:+135 added−1 removed

5 files+360 added−1 removed

modified:serde_derive/src/internals/check.rs+135 added-1 removed
1use crate::internals::ast::{Container, Data, Field, Style};1use crate::internals::ast::{Container, Data, Field, Style, Variant};2use crate::internals::attr::{Default, Identifier, TagType};3use crate::internals::{ungroup, Ctxt, Derive};4use std::collections::btree_map::Entry;5use std::collections::{BTreeMap, BTreeSet};6use syn::{Member, Type};78// Cross-cutting checks that require looking at more than a single attrs object.9// Simpler checks should happen when parsing and building the attrs.
7 unmodified lines
17 check_internal_tag_field_name_conflict(cx, cont);18 check_adjacent_tag_conflict(cx, cont);19 check_transparent(cx, cont, derive);20 check_from_and_try_from(cx, cont);21 check_name_conflicts(cx, cont, derive);22}2324// If some field of a tuple struct is marked #[serde(default)] then all fields25// after it must also be marked with that attribute, or the struct must have a
451 unmodified lines
477 "#[serde(from = \"...\")] and #[serde(try_from = \"...\")] conflict with each other",478 );479 }480}481482// Checks that aliases does not repeated483fn check_name_conflicts(cx: &Ctxt, cont: &Container, derive: Derive) {484 if let Derive::Deserialize = derive {485 match &cont.data {486 Data::Enum(variants) => check_variant_name_conflicts(cx, &variants),487 Data::Struct(Style::Struct, fields) => check_field_name_conflicts(cx, fields),488 _ => {}489 }490 }491}492493// All renames already applied494fn check_variant_name_conflicts(cx: &Ctxt, variants: &[Variant]) {495 let names: BTreeSet<_> = variants496 .iter()497 .filter_map(|variant| {498 if variant.attrs.skip_deserializing() {499 None500 } else {501 Some(variant.attrs.name().deserialize_name().to_owned())502 }503 })504 .collect();505 let mut alias_owners = BTreeMap::new();506507 for variant in variants {508 let name = variant.attrs.name().deserialize_name();509510 for alias in variant.attrs.aliases().intersection(&names) {511 // Aliases contains variant names, so filter them out512 if alias == name {513 continue;514 }515516 // TODO: report other variant location when this become possible517 cx.error_spanned_by(518 variant.original,519 format!(520 "alias `{}` conflicts with deserialization name of other variant",521 alias522 ),523 );524 }525526 for alias in variant.attrs.aliases() {527 // Aliases contains variant names, so filter them out528 if alias == name {529 continue;530 }531532 match alias_owners.entry(alias) {533 Entry::Vacant(e) => {534 e.insert(variant);535 }536 Entry::Occupied(e) => {537 // TODO: report other variant location when this become possible538 cx.error_spanned_by(539 variant.original,540 format!(541 "alias `{}` already used by variant {}",542 alias,543 e.get().original.ident544 ),545 );546 }547 }548 }549550 check_field_name_conflicts(cx, &variant.fields);551 }552}553554// All renames already applied555fn check_field_name_conflicts(cx: &Ctxt, fields: &[Field]) {556 let names: BTreeSet<_> = fields557 .iter()558 .filter_map(|field| {559 if field.attrs.skip_deserializing() {560 None561 } else {562 Some(field.attrs.name().deserialize_name().to_owned())563 }564 })565 .collect();566 let mut alias_owners = BTreeMap::new();567568 for field in fields {569 let name = field.attrs.name().deserialize_name();570571 for alias in field.attrs.aliases().intersection(&names) {572 // Aliases contains field names, so filter them out573 if alias == name {574 continue;575 }576577 // TODO: report other field location when this become possible578 cx.error_spanned_by(579 field.original,580 format!(581 "alias `{}` conflicts with deserialization name of other field",582 alias583 ),584 );585 }586587 for alias in field.attrs.aliases() {588 // Aliases contains field names, so filter them out589 if alias == name {590 continue;591 }592593 match alias_owners.entry(alias) {594 Entry::Vacant(e) => {595 e.insert(field);596 }597 Entry::Occupied(e) => {598 // TODO: report other field location when this become possible599 cx.error_spanned_by(600 field.original,601 format!(602 "alias `{}` already used by field {}",603 alias,604 e.get().original.ident.as_ref().unwrap()605 ),606 );607 }608 }609 }610 }611}
added:test_suite/tests/ui/conflict/alias-enum.rs+79 added
1#![allow(non_camel_case_types)]23use serde_derive::Deserialize;45#[derive(Deserialize)]6enum E {7 S1 {8 /// Expected error on "alias b", because this is a name of other field9 /// Error on "alias a" is not expected because this is a name of this field10 /// Error on "alias c" is not expected because field `c` is skipped11 #[serde(alias = "a", alias = "b", alias = "c")]12 a: (),1314 /// Expected error on "alias c", because it is already used as alias of `a`15 #[serde(alias = "c")]16 b: (),1718 #[serde(skip_deserializing)]19 c: (),20 },2122 S2 {23 /// Expected error on "alias c", because this is a name of other field after24 /// applying rename rules25 #[serde(alias = "b", alias = "c")]26 a: (),2728 #[serde(rename = "c")]29 b: (),30 },3132 #[serde(rename_all = "UPPERCASE")]33 S3 {34 /// Expected error on "alias B", because this is a name of field after35 /// applying rename rules36 #[serde(alias = "B", alias = "c")]37 a: (),38 b: (),39 },40}4142#[derive(Deserialize)]43enum E1 {44 /// Expected error on "alias b", because this is a name of other variant45 /// Error on "alias a" is not expected because this is a name of this variant46 /// Error on "alias c" is not expected because variant `c` is skipped47 #[serde(alias = "a", alias = "b", alias = "c")]48 a,4950 /// Expected error on "alias c", because it is already used as alias of `a`51 #[serde(alias = "c")]52 b,5354 #[serde(skip_deserializing)]55 c,56}5758#[derive(Deserialize)]59enum E2 {60 /// Expected error on "alias c", because this is a name of other variant after61 /// applying rename rules62 #[serde(alias = "b", alias = "c")]63 a,6465 #[serde(rename = "c")]66 b,67}6869#[derive(Deserialize)]70#[serde(rename_all = "UPPERCASE")]71enum E3 {72 /// Expected error on "alias B", because this is a name of variant after73 /// applying rename rules74 #[serde(alias = "B", alias = "c")]75 a,76 b,77}7879fn main() {}
added:test_suite/tests/ui/conflict/alias-enum.stderr+71 added
1error: alias `b` conflicts with deserialization name of other field2 --> tests/ui/conflict/alias-enum.rs:8:93 |48 | / /// Expected error on "alias b", because this is a name of other field59 | | /// Error on "alias a" is not expected because this is a name of this field610 | | /// Error on "alias c" is not expected because field `c` is skipped711 | | #[serde(alias = "a", alias = "b", alias = "c")]812 | | a: (),9 | |_____________^1011error: alias `c` already used by field a12 --> tests/ui/conflict/alias-enum.rs:14:913 |1414 | / /// Expected error on "alias c", because it is already used as alias of `a`1515 | | #[serde(alias = "c")]1616 | | b: (),17 | |_____________^1819error: alias `c` conflicts with deserialization name of other field20 --> tests/ui/conflict/alias-enum.rs:23:921 |2223 | / /// Expected error on "alias c", because this is a name of other field after2324 | | /// applying rename rules2425 | | #[serde(alias = "b", alias = "c")]2526 | | a: (),26 | |_____________^2728error: alias `B` conflicts with deserialization name of other field29 --> tests/ui/conflict/alias-enum.rs:34:930 |3134 | / /// Expected error on "alias B", because this is a name of field after3235 | | /// applying rename rules3336 | | #[serde(alias = "B", alias = "c")]3437 | | a: (),35 | |_____________^3637error: alias `b` conflicts with deserialization name of other variant38 --> tests/ui/conflict/alias-enum.rs:44:539 |4044 | / /// Expected error on "alias b", because this is a name of other variant4145 | | /// Error on "alias a" is not expected because this is a name of this variant4246 | | /// Error on "alias c" is not expected because variant `c` is skipped4347 | | #[serde(alias = "a", alias = "b", alias = "c")]4448 | | a,45 | |_____^4647error: alias `c` already used by variant a48 --> tests/ui/conflict/alias-enum.rs:50:549 |5050 | / /// Expected error on "alias c", because it is already used as alias of `a`5151 | | #[serde(alias = "c")]5252 | | b,53 | |_____^5455error: alias `c` conflicts with deserialization name of other variant56 --> tests/ui/conflict/alias-enum.rs:60:557 |5860 | / /// Expected error on "alias c", because this is a name of other variant after5961 | | /// applying rename rules6062 | | #[serde(alias = "b", alias = "c")]6163 | | a,62 | |_____^6364error: alias `B` conflicts with deserialization name of other variant65 --> tests/ui/conflict/alias-enum.rs:72:566 |6772 | / /// Expected error on "alias B", because this is a name of variant after6873 | | /// applying rename rules6974 | | #[serde(alias = "B", alias = "c")]7075 | | a,71 | |_____^
added:test_suite/tests/ui/conflict/alias.rs+40 added
1use serde_derive::Deserialize;23#[derive(Deserialize)]4struct S1 {5 /// Expected error on "alias b", because this is a name of other field6 /// Error on "alias a" is not expected because this is a name of this field7 /// Error on "alias c" is not expected because field `c` is skipped8 #[serde(alias = "a", alias = "b", alias = "c")]9 a: (),1011 /// Expected error on "alias c", because it is already used as alias of `a`12 #[serde(alias = "c")]13 b: (),1415 #[serde(skip_deserializing)]16 c: (),17}1819#[derive(Deserialize)]20struct S2 {21 /// Expected error on "alias c", because this is a name of other field after22 /// applying rename rules23 #[serde(alias = "b", alias = "c")]24 a: (),2526 #[serde(rename = "c")]27 b: (),28}2930#[derive(Deserialize)]31#[serde(rename_all = "UPPERCASE")]32struct S3 {33 /// Expected error on "alias B", because this is a name of field after34 /// applying rename rules35 #[serde(alias = "B", alias = "c")]36 a: (),37 b: (),38}3940fn main() {}
added:test_suite/tests/ui/conflict/alias.stderr+35 added
1error: alias `b` conflicts with deserialization name of other field2 --> tests/ui/conflict/alias.rs:5:53 |45 | / /// Expected error on "alias b", because this is a name of other field56 | | /// Error on "alias a" is not expected because this is a name of this field67 | | /// Error on "alias c" is not expected because field `c` is skipped78 | | #[serde(alias = "a", alias = "b", alias = "c")]89 | | a: (),9 | |_________^1011error: alias `c` already used by field a12 --> tests/ui/conflict/alias.rs:11:513 |1411 | / /// Expected error on "alias c", because it is already used as alias of `a`1512 | | #[serde(alias = "c")]1613 | | b: (),17 | |_________^1819error: alias `c` conflicts with deserialization name of other field20 --> tests/ui/conflict/alias.rs:21:521 |2221 | / /// Expected error on "alias c", because this is a name of other field after2322 | | /// applying rename rules2423 | | #[serde(alias = "b", alias = "c")]2524 | | a: (),26 | |_________^2728error: alias `B` conflicts with deserialization name of other field29 --> tests/ui/conflict/alias.rs:33:530 |3133 | / /// Expected error on "alias B", because this is a name of field after3234 | | /// applying rename rules3335 | | #[serde(alias = "B", alias = "c")]3436 | | a: (),35 | |_________^