refactor(ast, regular_expression): shorten ContentEq implementations (#8519)

Shorten codegen-ed implementations of `ContentEq` for enums, matching what Rust produces for `#[derive(PartialEq)]` (inspired by #8517).
This commit is contained in:
overlookmotel 2025-01-17 06:24:08 +00:00
parent 1d4c7a1099
commit 007e8c017c
3 changed files with 808 additions and 2366 deletions

File diff suppressed because it is too large Load diff

View file

@ -27,55 +27,21 @@ impl ContentEq for Alternative<'_> {
impl ContentEq for Term<'_> { impl ContentEq for Term<'_> {
fn content_eq(&self, other: &Self) -> bool { fn content_eq(&self, other: &Self) -> bool {
match self { #[allow(clippy::match_same_arms)]
Self::BoundaryAssertion(it) => match other { match (self, other) {
Self::BoundaryAssertion(other) if ContentEq::content_eq(it, other) => true, (Self::BoundaryAssertion(a), Self::BoundaryAssertion(b)) => a.content_eq(b),
_ => false, (Self::LookAroundAssertion(a), Self::LookAroundAssertion(b)) => a.content_eq(b),
}, (Self::Quantifier(a), Self::Quantifier(b)) => a.content_eq(b),
Self::LookAroundAssertion(it) => match other { (Self::Character(a), Self::Character(b)) => a.content_eq(b),
Self::LookAroundAssertion(other) if ContentEq::content_eq(it, other) => true, (Self::Dot(a), Self::Dot(b)) => a.content_eq(b),
_ => false, (Self::CharacterClassEscape(a), Self::CharacterClassEscape(b)) => a.content_eq(b),
}, (Self::UnicodePropertyEscape(a), Self::UnicodePropertyEscape(b)) => a.content_eq(b),
Self::Quantifier(it) => match other { (Self::CharacterClass(a), Self::CharacterClass(b)) => a.content_eq(b),
Self::Quantifier(other) if ContentEq::content_eq(it, other) => true, (Self::CapturingGroup(a), Self::CapturingGroup(b)) => a.content_eq(b),
_ => false, (Self::IgnoreGroup(a), Self::IgnoreGroup(b)) => a.content_eq(b),
}, (Self::IndexedReference(a), Self::IndexedReference(b)) => a.content_eq(b),
Self::Character(it) => match other { (Self::NamedReference(a), Self::NamedReference(b)) => a.content_eq(b),
Self::Character(other) if ContentEq::content_eq(it, other) => true, _ => false,
_ => false,
},
Self::Dot(it) => match other {
Self::Dot(other) if ContentEq::content_eq(it, other) => true,
_ => false,
},
Self::CharacterClassEscape(it) => match other {
Self::CharacterClassEscape(other) if ContentEq::content_eq(it, other) => true,
_ => false,
},
Self::UnicodePropertyEscape(it) => match other {
Self::UnicodePropertyEscape(other) if ContentEq::content_eq(it, other) => true,
_ => false,
},
Self::CharacterClass(it) => match other {
Self::CharacterClass(other) if ContentEq::content_eq(it, other) => true,
_ => false,
},
Self::CapturingGroup(it) => match other {
Self::CapturingGroup(other) if ContentEq::content_eq(it, other) => true,
_ => false,
},
Self::IgnoreGroup(it) => match other {
Self::IgnoreGroup(other) if ContentEq::content_eq(it, other) => true,
_ => false,
},
Self::IndexedReference(it) => match other {
Self::IndexedReference(other) if ContentEq::content_eq(it, other) => true,
_ => false,
},
Self::NamedReference(it) => match other {
Self::NamedReference(other) if ContentEq::content_eq(it, other) => true,
_ => false,
},
} }
} }
} }
@ -171,31 +137,15 @@ impl ContentEq for CharacterClassContentsKind {
impl ContentEq for CharacterClassContents<'_> { impl ContentEq for CharacterClassContents<'_> {
fn content_eq(&self, other: &Self) -> bool { fn content_eq(&self, other: &Self) -> bool {
match self { #[allow(clippy::match_same_arms)]
Self::CharacterClassRange(it) => match other { match (self, other) {
Self::CharacterClassRange(other) if ContentEq::content_eq(it, other) => true, (Self::CharacterClassRange(a), Self::CharacterClassRange(b)) => a.content_eq(b),
_ => false, (Self::CharacterClassEscape(a), Self::CharacterClassEscape(b)) => a.content_eq(b),
}, (Self::UnicodePropertyEscape(a), Self::UnicodePropertyEscape(b)) => a.content_eq(b),
Self::CharacterClassEscape(it) => match other { (Self::Character(a), Self::Character(b)) => a.content_eq(b),
Self::CharacterClassEscape(other) if ContentEq::content_eq(it, other) => true, (Self::NestedCharacterClass(a), Self::NestedCharacterClass(b)) => a.content_eq(b),
_ => false, (Self::ClassStringDisjunction(a), Self::ClassStringDisjunction(b)) => a.content_eq(b),
}, _ => false,
Self::UnicodePropertyEscape(it) => match other {
Self::UnicodePropertyEscape(other) if ContentEq::content_eq(it, other) => true,
_ => false,
},
Self::Character(it) => match other {
Self::Character(other) if ContentEq::content_eq(it, other) => true,
_ => false,
},
Self::NestedCharacterClass(it) => match other {
Self::NestedCharacterClass(other) if ContentEq::content_eq(it, other) => true,
_ => false,
},
Self::ClassStringDisjunction(it) => match other {
Self::ClassStringDisjunction(other) if ContentEq::content_eq(it, other) => true,
_ => false,
},
} }
} }
} }

View file

@ -53,22 +53,16 @@ fn derive_enum(def: &EnumDef) -> (&str, TokenStream) {
let matches = def.all_variants().map(|var| { let matches = def.all_variants().map(|var| {
let ident = var.ident(); let ident = var.ident();
if var.is_unit() { if var.is_unit() {
quote!(Self :: #ident => matches!(other, Self :: #ident)) quote!( (Self::#ident, Self::#ident) => true )
} else { } else {
quote! { quote!( (Self::#ident(a), Self::#ident(b)) => a.content_eq(b) )
Self :: #ident(it) => {
// NOTE: writing the match expression formats better than using `matches` macro.
match other {
Self :: #ident (other) if ContentEq::content_eq(it, other) => true,
_ => false,
}
}
}
} }
}); });
quote! { quote! {
match self { #[allow(clippy::match_same_arms)]
#(#matches),* match (self, other) {
#(#matches,)*
_ => false,
} }
} }
}; };