Skip to content
Myr

Revert "Add checks for conflicts for aliases"

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

This reverts commit 5f9fffa53e8a48d9e1f32b1735542207c42f3ead.

authored by David Tolnay · committed by David Tolnay · parent a20e924 · Browse files · 1 file+1 added−135 removed
This commit

State

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

Diff stats

1 file+1 added−135 removed

  • other:+1 added−135 removed

1 file+1 added−135 removed

modified:serde_derive/src/internals/check.rs+1 added-135 removed
1use crate::internals::ast::{Container, Data, Field, Style, Variant};1use crate::internals::ast::{Container, Data, Field, Style};2use crate::internals::attr::{Default, Identifier, TagType};3use crate::internals::{ungroup, Ctxt, Derive};4use std::collections::btree_map::Entry;5use std::collections::{BTreeMap, BTreeSet};4use syn::{Member, Type};56// Cross-cutting checks that require looking at more than a single attrs object.7// Simpler checks should happen when parsing and building the attrs.
7 unmodified lines
15 check_internal_tag_field_name_conflict(cx, cont);16 check_adjacent_tag_conflict(cx, cont);17 check_transparent(cx, cont, derive);18 check_from_and_try_from(cx, cont);21 check_name_conflicts(cx, cont, derive);19}2021// If some field of a tuple struct is marked #[serde(default)] then all fields22// after it must also be marked with that attribute, or the struct must have a
451 unmodified lines
474 "#[serde(from = \"...\")] and #[serde(try_from = \"...\")] conflict with each other",475 );476 }477}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}