From 45333bc8d2976ba7cbfc44d29716e6c09714e898 Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Fri, 25 Oct 2024 21:23:28 +0000 Subject: [PATCH] refactor(ast_tools): shorten code by hoisting imports (#6903) Pure refactor. --- tasks/ast_tools/src/derives/mod.rs | 11 ++--- tasks/ast_tools/src/markers.rs | 4 +- tasks/ast_tools/src/output/rust.rs | 2 +- tasks/ast_tools/src/schema/defs.rs | 5 ++- tasks/ast_tools/src/schema/get_generics.rs | 4 +- tasks/ast_tools/src/schema/get_ident.rs | 10 +++-- tasks/ast_tools/src/schema/mod.rs | 50 ++++++++++------------ tasks/ast_tools/src/schema/to_type.rs | 22 +++++----- 8 files changed, 54 insertions(+), 54 deletions(-) diff --git a/tasks/ast_tools/src/derives/mod.rs b/tasks/ast_tools/src/derives/mod.rs index c9921c2295e50..4a3ac9b6a9f4e 100644 --- a/tasks/ast_tools/src/derives/mod.rs +++ b/tasks/ast_tools/src/derives/mod.rs @@ -1,7 +1,9 @@ use convert_case::{Case, Casing}; use itertools::Itertools; use proc_macro2::TokenStream; +use quote::quote; use rustc_hash::{FxHashMap, FxHashSet}; +use syn::{parse_str, ItemUse}; use crate::{ codegen::LateCtx, @@ -49,15 +51,14 @@ pub trait Derive { .chain(it.strip_suffix("::mod").unwrap_or(it).split("::").skip(1)) .chain(["*"]) .join("::"); - let use_module: syn::ItemUse = - syn::parse_str(format!("use {local_path};").as_str()).unwrap(); - quote::quote! { + let use_module: ItemUse = parse_str(format!("use {local_path};").as_str()).unwrap(); + quote! { ///@@line_break #use_module } }); - quote::quote! { + quote! { #prelude #(#use_modules)* @@ -103,7 +104,7 @@ pub trait Derive { tokens: Self::template( modules, streams.into_iter().fold(TokenStream::new(), |mut acc, it| { - acc.extend(quote::quote! { + acc.extend(quote! { ///@@line_break }); acc.extend(it); diff --git a/tasks/ast_tools/src/markers.rs b/tasks/ast_tools/src/markers.rs index 903c236c24d7a..dc89a0470d6f7 100644 --- a/tasks/ast_tools/src/markers.rs +++ b/tasks/ast_tools/src/markers.rs @@ -5,7 +5,7 @@ use syn::{ parenthesized, parse::{Parse, ParseStream}, parse2, - punctuated::Punctuated, + punctuated::{self, Punctuated}, spanned::Spanned, token, Attribute, Expr, Ident, LitStr, Meta, MetaNameValue, Token, }; @@ -38,7 +38,7 @@ impl Parse for VisitArg { pub struct VisitArgs(Punctuated); impl IntoIterator for VisitArgs { - type IntoIter = syn::punctuated::IntoIter; + type IntoIter = punctuated::IntoIter; type Item = VisitArg; fn into_iter(self) -> Self::IntoIter { diff --git a/tasks/ast_tools/src/output/rust.rs b/tasks/ast_tools/src/output/rust.rs index 012869cb48025..ebf2a82ea6bcf 100644 --- a/tasks/ast_tools/src/output/rust.rs +++ b/tasks/ast_tools/src/output/rust.rs @@ -28,7 +28,7 @@ fn generate_header(generator_path: &str) -> TokenStream { // TODO: Add generation date, AST source hash, etc here. let edit_comment = format!("@ To edit this generated file you have to edit `{generator_path}`"); - quote::quote! { + quote! { //!@ Auto-generated code, DO NOT EDIT DIRECTLY! #![doc = #edit_comment] //!@@line_break diff --git a/tasks/ast_tools/src/schema/defs.rs b/tasks/ast_tools/src/schema/defs.rs index 1494a30cc9b5f..45a77dac7f120 100644 --- a/tasks/ast_tools/src/schema/defs.rs +++ b/tasks/ast_tools/src/schema/defs.rs @@ -1,4 +1,5 @@ use serde::Serialize; +use syn::Ident; use super::{with_either, TypeName}; use crate::{ @@ -120,7 +121,7 @@ pub struct VariantDef { } impl VariantDef { - pub fn ident(&self) -> syn::Ident { + pub fn ident(&self) -> Ident { self.name.to_ident() } @@ -176,7 +177,7 @@ impl From<&syn::Visibility> for Visibility { } impl FieldDef { - pub fn ident(&self) -> Option { + pub fn ident(&self) -> Option { self.name.as_ref().map(ToIdent::to_ident) } } diff --git a/tasks/ast_tools/src/schema/get_generics.rs b/tasks/ast_tools/src/schema/get_generics.rs index 11de9c075d22e..cdd3ad8de3e91 100644 --- a/tasks/ast_tools/src/schema/get_generics.rs +++ b/tasks/ast_tools/src/schema/get_generics.rs @@ -1,4 +1,4 @@ -use syn::parse_quote; +use syn::{parse_quote, Generics}; use super::{ defs::{EnumDef, StructDef, TypeDef}, @@ -10,7 +10,7 @@ pub trait GetGenerics { false } - fn generics(&self) -> Option { + fn generics(&self) -> Option { if self.has_lifetime() { Some(parse_quote!(<'a>)) } else { diff --git a/tasks/ast_tools/src/schema/get_ident.rs b/tasks/ast_tools/src/schema/get_ident.rs index 35967dd99572b..6022d4cfd77fa 100644 --- a/tasks/ast_tools/src/schema/get_ident.rs +++ b/tasks/ast_tools/src/schema/get_ident.rs @@ -1,3 +1,5 @@ +use syn::Ident; + use super::{ defs::{EnumDef, StructDef, TypeDef}, with_either, @@ -5,23 +7,23 @@ use super::{ use crate::util::ToIdent; pub trait GetIdent { - fn ident(&self) -> syn::Ident; + fn ident(&self) -> Ident; } impl GetIdent for TypeDef { - fn ident(&self) -> syn::Ident { + fn ident(&self) -> Ident { with_either!(self, it => it.ident()) } } impl GetIdent for StructDef { - fn ident(&self) -> syn::Ident { + fn ident(&self) -> Ident { self.name.to_ident() } } impl GetIdent for EnumDef { - fn ident(&self) -> syn::Ident { + fn ident(&self) -> Ident { self.name.to_ident() } } diff --git a/tasks/ast_tools/src/schema/mod.rs b/tasks/ast_tools/src/schema/mod.rs index 802d26cfdfee2..06419861ae615 100644 --- a/tasks/ast_tools/src/schema/mod.rs +++ b/tasks/ast_tools/src/schema/mod.rs @@ -3,9 +3,13 @@ use std::fmt; use quote::ToTokens; use rustc_hash::FxHashSet; use serde::Serialize; +use syn::{ + punctuated::Punctuated, Attribute, Expr, ExprLit, Field, Ident, Lit, Meta, MetaNameValue, + Token, Type, Variant, +}; use crate::{ - codegen, + codegen::EarlyCtx, layout::KnownLayout, markers::{ get_derive_attributes, get_estree_attribute, get_scope_attribute, get_scope_markers, @@ -121,18 +125,18 @@ impl<'a> IntoIterator for &'a Schema { } } -fn parse_struct_outer_markers(attrs: &Vec) -> Result { +fn parse_struct_outer_markers(attrs: &Vec) -> Result { Ok(StructOuterMarkers { scope: get_scope_attribute(attrs).transpose()?, estree: get_estree_attribute(attrs).transpose()?, }) } -fn parse_enum_outer_markers(attrs: &Vec) -> Result { +fn parse_enum_outer_markers(attrs: &Vec) -> Result { Ok(EnumOuterMarkers { estree: get_estree_attribute(attrs).transpose()?.unwrap_or_default() }) } -fn parse_inner_markers(attrs: &Vec) -> Result { +fn parse_inner_markers(attrs: &Vec) -> Result { Ok(InnerMarkers { span: attrs.iter().any(|a| a.path().is_ident("span")), visit: get_visit_markers(attrs)?, @@ -142,7 +146,7 @@ fn parse_inner_markers(attrs: &Vec) -> Result { } // lower `AstType` to `TypeDef`. -pub fn lower_ast_types(ctx: &codegen::EarlyCtx) -> Schema { +pub fn lower_ast_types(ctx: &EarlyCtx) -> Schema { let defs = ctx .mods() .borrow() @@ -153,7 +157,7 @@ pub fn lower_ast_types(ctx: &codegen::EarlyCtx) -> Schema { Schema { defs } } -fn lower_ast_type(ty: &rust::AstType, ctx: &codegen::EarlyCtx) -> TypeDef { +fn lower_ast_type(ty: &rust::AstType, ctx: &EarlyCtx) -> TypeDef { match ty { rust::AstType::Enum(it) => TypeDef::Enum(lower_ast_enum(it, ctx)), rust::AstType::Struct(it) => TypeDef::Struct(lower_ast_struct(it, ctx)), @@ -161,7 +165,7 @@ fn lower_ast_type(ty: &rust::AstType, ctx: &codegen::EarlyCtx) -> TypeDef { } } -fn lower_ast_enum(it @ rust::Enum { item, meta }: &rust::Enum, ctx: &codegen::EarlyCtx) -> EnumDef { +fn lower_ast_enum(it @ rust::Enum { item, meta }: &rust::Enum, ctx: &EarlyCtx) -> EnumDef { let (size_64, align_64, offsets_64) = meta .layout_64 .clone() @@ -199,10 +203,7 @@ fn lower_ast_enum(it @ rust::Enum { item, meta }: &rust::Enum, ctx: &codegen::Ea } } -fn lower_ast_struct( - it @ rust::Struct { item, meta }: &rust::Struct, - ctx: &codegen::EarlyCtx, -) -> StructDef { +fn lower_ast_struct(it @ rust::Struct { item, meta }: &rust::Struct, ctx: &EarlyCtx) -> StructDef { let (size_64, align_64, offsets_64) = meta .layout_64 .clone() @@ -234,7 +235,7 @@ fn lower_ast_struct( } } -fn lower_variant(variant: &syn::Variant, enum_dbg_name: F, ctx: &codegen::EarlyCtx) -> VariantDef +fn lower_variant(variant: &Variant, enum_dbg_name: F, ctx: &EarlyCtx) -> VariantDef where F: Fn() -> String, { @@ -243,7 +244,7 @@ where discriminant: variant.discriminant.as_ref().map_or_else( || panic!("expected explicit enum discriminants on {}", enum_dbg_name()), |(_, disc)| match disc { - syn::Expr::Lit(syn::ExprLit { lit: syn::Lit::Int(lit), .. }) => { + Expr::Lit(ExprLit { lit: Lit::Int(lit), .. }) => { lit.base10_parse().expect("invalid base10 enum discriminant") } _ => panic!("invalid enum discriminant {:?} on {}", disc, enum_dbg_name()), @@ -254,7 +255,7 @@ where } } -fn lower_inherit(inherit: &rust::Inherit, ctx: &codegen::EarlyCtx) -> InheritDef { +fn lower_inherit(inherit: &rust::Inherit, ctx: &EarlyCtx) -> InheritDef { match inherit { rust::Inherit::Linked { super_, variants } => InheritDef { super_: create_type_ref(super_, ctx), @@ -269,7 +270,7 @@ fn lower_inherit(inherit: &rust::Inherit, ctx: &codegen::EarlyCtx) -> InheritDef } } -fn lower_field(field: &syn::Field, ctx: &codegen::EarlyCtx) -> FieldDef { +fn lower_field(field: &Field, ctx: &EarlyCtx) -> FieldDef { FieldDef { name: field .ident @@ -282,7 +283,7 @@ fn lower_field(field: &syn::Field, ctx: &codegen::EarlyCtx) -> FieldDef { } } -fn create_type_ref(ty: &syn::Type, ctx: &codegen::EarlyCtx) -> TypeRef { +fn create_type_ref(ty: &Type, ctx: &EarlyCtx) -> TypeRef { let ident = ty.get_ident(); let id = ident.as_ident().and_then(|id| ctx.type_id(&id.to_string())); let transparent_id = ctx.type_id(&ident.inner_ident().to_string()); @@ -296,21 +297,16 @@ fn create_type_ref(ty: &syn::Type, ctx: &codegen::EarlyCtx) -> TypeRef { } } -fn get_docs(attrs: &[syn::Attribute]) -> Vec { +fn get_docs(attrs: &[Attribute]) -> Vec { attrs .iter() .filter_map(|attr| { - if let syn::Meta::NameValue(syn::MetaNameValue { - path, - value: syn::Expr::Lit(lit), - .. - }) = &attr.meta - { + if let Meta::NameValue(MetaNameValue { path, value: Expr::Lit(lit), .. }) = &attr.meta { if !path.is_ident("doc") { return None; } match &lit.lit { - syn::Lit::Str(lit) => Some(lit.value().trim().to_string()), + Lit::Str(lit) => Some(lit.value().trim().to_string()), _ => None, } } else { @@ -320,15 +316,15 @@ fn get_docs(attrs: &[syn::Attribute]) -> Vec { .collect() } -fn parse_generate_derive(attrs: &[syn::Attribute]) -> Vec { +fn parse_generate_derive(attrs: &[Attribute]) -> Vec { let mut derives = FxHashSet::default(); for attr in attrs { if !attr.path().is_ident("generate_derive") { continue; } - let args: syn::punctuated::Punctuated = - attr.parse_args_with(syn::punctuated::Punctuated::parse_terminated).unwrap(); + let args: Punctuated = + attr.parse_args_with(Punctuated::parse_terminated).unwrap(); for arg in args { derives.insert(arg.to_string()); diff --git a/tasks/ast_tools/src/schema/to_type.rs b/tasks/ast_tools/src/schema/to_type.rs index 5cf18ba130a22..0b7e2c5c8bab6 100644 --- a/tasks/ast_tools/src/schema/to_type.rs +++ b/tasks/ast_tools/src/schema/to_type.rs @@ -1,6 +1,6 @@ use proc_macro2::TokenStream; use quote::ToTokens; -use syn::parse_quote; +use syn::{parse_quote, parse_str, Type}; use super::{ defs::{EnumDef, StructDef, TypeDef, TypeRef}, @@ -8,21 +8,21 @@ use super::{ }; pub trait ToType { - fn to_type(&self) -> syn::Type; - fn to_type_elide(&self) -> syn::Type; - fn to_type_with_explicit_generics(&self, generics: TokenStream) -> syn::Type; + fn to_type(&self) -> Type; + fn to_type_elide(&self) -> Type; + fn to_type_with_explicit_generics(&self, generics: TokenStream) -> Type; } impl ToType for TypeRef { - fn to_type(&self) -> syn::Type { - syn::parse_str(self.raw()).unwrap() + fn to_type(&self) -> Type { + parse_str(self.raw()).unwrap() } - fn to_type_elide(&self) -> syn::Type { + fn to_type_elide(&self) -> Type { self.to_type_with_explicit_generics(proc_macro2::TokenStream::default()) } - fn to_type_with_explicit_generics(&self, generics: proc_macro2::TokenStream) -> syn::Type { + fn to_type_with_explicit_generics(&self, generics: proc_macro2::TokenStream) -> Type { let ident = self.name().first_ident(); parse_quote!(#ident #generics) } @@ -38,15 +38,15 @@ macro_rules! auto_impl_to_type { ($($ty:ty,)+) => ( $( impl ToType for $ty { - fn to_type(&self) -> syn::Type { + fn to_type(&self) -> Type { self.to_type_with_explicit_generics(self.generics().to_token_stream()) } - fn to_type_elide(&self) -> syn::Type { + fn to_type_elide(&self) -> Type { self.to_type_with_explicit_generics(TokenStream::default()) } - fn to_type_with_explicit_generics(&self, generics: TokenStream) -> syn::Type { + fn to_type_with_explicit_generics(&self, generics: TokenStream) -> Type { let name = self.ident(); parse_quote!(#name #generics) }