From 54e78d9ea36a1e61574b6df174695abdc95bc352 Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Mon, 28 Oct 2024 00:47:45 +0000 Subject: [PATCH] refactor(ast_tools): reduce macro usage (#6956) Pure refactor. Expand simple macros. --- tasks/ast_tools/src/generators/ast_builder.rs | 36 ++++-------------- tasks/ast_tools/src/rust_ast.rs | 38 +++++++++---------- tasks/ast_tools/src/schema/defs.rs | 27 ++++++++++--- tasks/ast_tools/src/schema/get_generics.rs | 10 ++--- tasks/ast_tools/src/schema/get_ident.rs | 10 ++--- tasks/ast_tools/src/schema/mod.rs | 11 ------ tasks/ast_tools/src/schema/to_type.rs | 14 +++---- 7 files changed, 64 insertions(+), 82 deletions(-) diff --git a/tasks/ast_tools/src/generators/ast_builder.rs b/tasks/ast_tools/src/generators/ast_builder.rs index b2e2d2116a6a2..3cfa71f78e841 100644 --- a/tasks/ast_tools/src/generators/ast_builder.rs +++ b/tasks/ast_tools/src/generators/ast_builder.rs @@ -2,10 +2,8 @@ use std::{borrow::Cow, stringify}; use convert_case::{Case, Casing}; use itertools::Itertools; -use lazy_static::lazy_static; use proc_macro2::TokenStream; use quote::{format_ident, quote, ToTokens}; -use rustc_hash::FxHashMap; use syn::{parse_quote, Ident, Type}; use crate::{ @@ -231,25 +229,13 @@ fn generate_enum_from_variant_builder_fn( } fn default_init_field(field: &FieldDef) -> bool { - macro_rules! field { - ($ident:ident: $ty:ty) => { - (stringify!($ident), stringify!($ty)) - }; - } - lazy_static! { - static ref DEFAULT_FIELDS: FxHashMap<&'static str, &'static str> = FxHashMap::from_iter([ - field!(scope_id: Cell>), - field!(symbol_id: Cell>), - field!(reference_id: Cell>), - ]); - } - let ident = field.ident().expect("expected named field"); - if let Some(&default_type) = DEFAULT_FIELDS.get(ident.to_string().as_str()) { - default_type == field.typ.raw() - } else { - false - } + matches!( + (ident.to_string().as_str(), field.typ.raw()), + ("scope_id", "Cell>") + | ("symbol_id", "Cell>") + | ("reference_id", "Cell>") + ) } /// Generate builder function for struct. @@ -543,25 +529,19 @@ fn article_for>(word: S) -> &'static str { impl ToTokens for DocComment<'_> { fn to_tokens(&self, tokens: &mut TokenStream) { - macro_rules! newline { - () => { - tokens.extend(quote!( #[doc = ""])); - }; - } - let summary = &self.summary; tokens.extend(quote!( #[doc = #summary])); // print description for line in &self.description { // extra newline needed to create a new paragraph - newline!(); + tokens.extend(quote!( #[doc = ""])); tokens.extend(quote!( #[doc = #line])); } // print docs for function parameters if !self.params.is_empty() { - newline!(); + tokens.extend(quote!( #[doc = ""])); tokens.extend(quote!( #[doc = " ## Parameters"])); for param in self.params { let docs = param.docs.first(); diff --git a/tasks/ast_tools/src/rust_ast.rs b/tasks/ast_tools/src/rust_ast.rs index d097968f73052..9e987b016532f 100644 --- a/tasks/ast_tools/src/rust_ast.rs +++ b/tasks/ast_tools/src/rust_ast.rs @@ -201,17 +201,17 @@ impl AstType { } pub fn set_visitable(&mut self, value: bool) -> Result<()> { - macro_rules! assign { - ($it:ident) => {{ - debug_assert!($it.meta.ast, "only ast types can be visitable!"); - $it.meta.visitable = value; - }}; - } match self { - AstType::Enum(it) => assign!(it), - AstType::Struct(it) => assign!(it), - AstType::Macro(it) => return Err(unexpanded_macro_err(&it.item)), - } + AstType::Enum(enum_) => { + debug_assert!(enum_.meta.ast, "only AST types can be visitable!"); + enum_.meta.visitable = value; + } + AstType::Struct(struct_) => { + debug_assert!(struct_.meta.ast, "only AST types can be visitable!"); + struct_.meta.visitable = value; + } + AstType::Macro(macro_) => return Err(unexpanded_macro_err(¯o_.item)), + }; Ok(()) } @@ -245,16 +245,16 @@ impl AstType { } pub fn set_layout(&mut self, layout_64: Layout, layout_32: Layout) -> Result<()> { - macro_rules! assign { - ($it:ident) => {{ - $it.meta.layout_32 = layout_32; - $it.meta.layout_64 = layout_64; - }}; - } match self { - AstType::Enum(it) => assign!(it), - AstType::Struct(it) => assign!(it), - AstType::Macro(it) => return Err(unexpanded_macro_err(&it.item)), + AstType::Enum(enum_) => { + enum_.meta.layout_32 = layout_32; + enum_.meta.layout_64 = layout_64; + } + AstType::Struct(struct_) => { + struct_.meta.layout_32 = layout_32; + struct_.meta.layout_64 = layout_64; + } + AstType::Macro(macro_) => return Err(unexpanded_macro_err(¯o_.item)), } Ok(()) } diff --git a/tasks/ast_tools/src/schema/defs.rs b/tasks/ast_tools/src/schema/defs.rs index 5688a1b9d7b9e..eba9806d264b9 100644 --- a/tasks/ast_tools/src/schema/defs.rs +++ b/tasks/ast_tools/src/schema/defs.rs @@ -10,7 +10,7 @@ use crate::{ TypeId, }; -use super::{with_either, TypeName}; +use super::TypeName; #[derive(Debug, Serialize)] #[serde(untagged)] @@ -21,19 +21,31 @@ pub enum TypeDef { impl TypeDef { pub fn id(&self) -> TypeId { - with_either!(self, it => it.id) + match self { + TypeDef::Struct(def) => def.id, + TypeDef::Enum(def) => def.id, + } } pub fn name(&self) -> &str { - with_either!(self, it => &it.name) + match self { + TypeDef::Struct(def) => &def.name, + TypeDef::Enum(def) => &def.name, + } } pub fn visitable(&self) -> bool { - with_either!(self, it => it.visitable) + match self { + TypeDef::Struct(def) => def.visitable, + TypeDef::Enum(def) => def.visitable, + } } pub fn generated_derives(&self) -> &Vec { - with_either!(self, it => &it.generated_derives) + match self { + TypeDef::Struct(def) => &def.generated_derives, + TypeDef::Enum(def) => &def.generated_derives, + } } pub fn generates_derive(&self, derive: &str) -> bool { @@ -42,7 +54,10 @@ impl TypeDef { } pub fn module_path(&self) -> &str { - with_either!(self, it => &it.module_path) + match self { + TypeDef::Struct(def) => &def.module_path, + TypeDef::Enum(def) => &def.module_path, + } } } diff --git a/tasks/ast_tools/src/schema/get_generics.rs b/tasks/ast_tools/src/schema/get_generics.rs index cdd3ad8de3e91..797d6e1ed5fd3 100644 --- a/tasks/ast_tools/src/schema/get_generics.rs +++ b/tasks/ast_tools/src/schema/get_generics.rs @@ -1,9 +1,6 @@ use syn::{parse_quote, Generics}; -use super::{ - defs::{EnumDef, StructDef, TypeDef}, - with_either, -}; +use super::defs::{EnumDef, StructDef, TypeDef}; pub trait GetGenerics { fn has_lifetime(&self) -> bool { @@ -21,7 +18,10 @@ pub trait GetGenerics { impl GetGenerics for TypeDef { fn has_lifetime(&self) -> bool { - with_either!(self, it => it.has_lifetime()) + match self { + TypeDef::Struct(def) => def.has_lifetime(), + TypeDef::Enum(def) => def.has_lifetime(), + } } } diff --git a/tasks/ast_tools/src/schema/get_ident.rs b/tasks/ast_tools/src/schema/get_ident.rs index a0dde5f300868..1f1f5f8769085 100644 --- a/tasks/ast_tools/src/schema/get_ident.rs +++ b/tasks/ast_tools/src/schema/get_ident.rs @@ -2,10 +2,7 @@ use syn::Ident; use crate::util::ToIdent; -use super::{ - defs::{EnumDef, StructDef, TypeDef}, - with_either, -}; +use super::defs::{EnumDef, StructDef, TypeDef}; pub trait GetIdent { fn ident(&self) -> Ident; @@ -13,7 +10,10 @@ pub trait GetIdent { impl GetIdent for TypeDef { fn ident(&self) -> Ident { - with_either!(self, it => it.ident()) + match self { + TypeDef::Struct(def) => def.ident(), + TypeDef::Enum(def) => def.ident(), + } } } diff --git a/tasks/ast_tools/src/schema/mod.rs b/tasks/ast_tools/src/schema/mod.rs index df1bcdff386fe..4a314de4848ba 100644 --- a/tasks/ast_tools/src/schema/mod.rs +++ b/tasks/ast_tools/src/schema/mod.rs @@ -333,14 +333,3 @@ fn parse_generate_derive(attrs: &[Attribute]) -> Vec { } Vec::from_iter(derives) } - -macro_rules! with_either { - ($def:expr, $it:ident => $body:expr) => { - match $def { - TypeDef::Struct($it) => $body, - TypeDef::Enum($it) => $body, - } - }; -} - -use with_either; diff --git a/tasks/ast_tools/src/schema/to_type.rs b/tasks/ast_tools/src/schema/to_type.rs index 0b7e2c5c8bab6..2aa6c180f15d1 100644 --- a/tasks/ast_tools/src/schema/to_type.rs +++ b/tasks/ast_tools/src/schema/to_type.rs @@ -28,13 +28,7 @@ impl ToType for TypeRef { } } -auto_impl_to_type! { - TypeDef, - EnumDef, - StructDef, -} - -macro_rules! auto_impl_to_type { +macro_rules! impl_to_type { ($($ty:ty,)+) => ( $( impl ToType for $ty { @@ -55,4 +49,8 @@ macro_rules! auto_impl_to_type { ) } -use auto_impl_to_type; +impl_to_type! { + TypeDef, + EnumDef, + StructDef, +}