From 73768c17a0d3639a9e9c0f5a5fbd282e8d66e109 Mon Sep 17 00:00:00 2001 From: IWANABETHATGUY Date: Wed, 22 Jan 2025 17:15:55 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=F0=9F=A4=96=20refactor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/oxc_allocator/src/lib.rs | 7 +------ crates/oxc_semantic/src/symbol.rs | 11 ++++++----- tasks/ast_tools/src/derives/clone_in.rs | 19 ++++++++++++------- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/crates/oxc_allocator/src/lib.rs b/crates/oxc_allocator/src/lib.rs index d8214848b713f..add3cbd7f8cd9 100644 --- a/crates/oxc_allocator/src/lib.rs +++ b/crates/oxc_allocator/src/lib.rs @@ -236,6 +236,7 @@ pub use vec::Vec; pub struct Allocator { bump: Bump, } + impl Allocator { /// Create a new [`Allocator`] with no initial capacity. /// @@ -464,12 +465,6 @@ impl Allocator { } } -impl Allocator { - /// Create a new bump-allocated memory arena. - pub fn with_capacity(size: usize) -> Self { - Self { bump: Bump::with_capacity(size) } - } -} /// SAFETY: Not actually safe, but for enabling `Send` for downstream crates. unsafe impl Send for Allocator {} /// SAFETY: Not actually safe, but for enabling `Sync` for downstream crates. diff --git a/crates/oxc_semantic/src/symbol.rs b/crates/oxc_semantic/src/symbol.rs index ce88a2c100d2f..6cf6d1add1031 100644 --- a/crates/oxc_semantic/src/symbol.rs +++ b/crates/oxc_semantic/src/symbol.rs @@ -326,7 +326,8 @@ impl SymbolTable { /// use another `Allocator` to avoid memory leak #[must_use] pub fn clone_with_another_arena(&self) -> SymbolTable { - let allocator = self.inner.with_dependent(|alloc, _| Allocator::with_capacity(0)); + let allocator = + self.inner.with_dependent(|alloc, _| Allocator::with_capacity(alloc.capacity())); Self { spans: self.spans.clone(), flags: self.flags.clone(), @@ -336,10 +337,10 @@ impl SymbolTable { references: self.references.clone(), inner: SymbolTableCell::new(allocator, |allocator| { self.inner.with_dependent(|_allocator, inner| { - let names = inner.names.clone_in(&allocator); - let redeclaration_spans = inner.redeclaration_spans.clone_in(&allocator); - let resolved_references = inner.resolved_references.clone_in(&allocator); - SymbolTableInner { names, redeclaration_spans, resolved_references } + let names = inner.names.clone_in(allocator); + let redeclaration_spans = inner.redeclaration_spans.clone_in(allocator); + let resolved_references = inner.resolved_references.clone_in(allocator); + SymbolTableInner { names, resolved_references, redeclaration_spans } }) }), } diff --git a/tasks/ast_tools/src/derives/clone_in.rs b/tasks/ast_tools/src/derives/clone_in.rs index 3faef87d04ed2..532a3370e2134 100644 --- a/tasks/ast_tools/src/derives/clone_in.rs +++ b/tasks/ast_tools/src/derives/clone_in.rs @@ -68,11 +68,12 @@ fn derive_enum(def: &EnumDef) -> TokenStream { fn derive_struct(def: &StructDef) -> TokenStream { let ty_ident = def.ident(); let needs_override_clone_in_with_semantic_ids = Cell::new(false); - let (alloc_ident, clone_in_body, clone_in_with_semantic_ids_body) = if def.fields.is_empty() { + let (alloc_ident, clone_in_body, clone_in_with_semantic_ids_function) = if def.fields.is_empty() + { (format_ident!("_"), quote!(#ty_ident), quote!()) } else { let alloc_ident = format_ident!("allocator"); - let fields = def + let clone_in_fields = def .fields .iter() .map(|field| { @@ -104,7 +105,11 @@ fn derive_struct(def: &StructDef) -> TokenStream { } else { quote!() }; - (alloc_ident, quote!(#ty_ident { #(#fields),* }), clone_in_with_semantic_ids_token_stream) + ( + alloc_ident, + quote!(#ty_ident { #(#clone_in_fields),* }), + clone_in_with_semantic_ids_token_stream, + ) }; impl_clone_in( @@ -112,7 +117,7 @@ fn derive_struct(def: &StructDef) -> TokenStream { def.has_lifetime, &alloc_ident, &clone_in_body, - &clone_in_with_semantic_ids_body, + &clone_in_with_semantic_ids_function, ) } @@ -121,7 +126,7 @@ fn impl_clone_in( has_lifetime: bool, alloc_ident: &Ident, clone_in_body: &TokenStream, - clone_in_with_semantic_ids_body: &TokenStream, + clone_in_with_semantic_ids_function: &TokenStream, ) -> TokenStream { if has_lifetime { quote! { @@ -131,7 +136,7 @@ fn impl_clone_in( #clone_in_body } - #clone_in_with_semantic_ids_body + #clone_in_with_semantic_ids_function } } } else { @@ -142,7 +147,7 @@ fn impl_clone_in( #clone_in_body } - #clone_in_with_semantic_ids_body + #clone_in_with_semantic_ids_function } } }