From 0ff94fa620ebc623f28445d75a6ebfaf929544cf Mon Sep 17 00:00:00 2001 From: Dunqing <29533304+Dunqing@users.noreply.github.com> Date: Mon, 25 Nov 2024 04:56:17 +0000 Subject: [PATCH] refactor(isolated-declarations): use `CloneIn` instead of `ast.copy` (#7459) --- crates/oxc_isolated_declarations/src/class.rs | 53 +++++++------------ .../src/declaration.rs | 30 ++++------- crates/oxc_isolated_declarations/src/enum.rs | 7 ++- .../src/formal_parameter_binding_pattern.rs | 5 +- .../oxc_isolated_declarations/src/function.rs | 31 ++++------- .../oxc_isolated_declarations/src/inferrer.rs | 40 ++++++-------- crates/oxc_isolated_declarations/src/lib.rs | 2 +- .../oxc_isolated_declarations/src/module.rs | 41 +++++++------- .../src/return_type.rs | 5 +- crates/oxc_isolated_declarations/src/scope.rs | 4 +- .../src/signatures.rs | 3 +- crates/oxc_isolated_declarations/src/types.rs | 48 +++++------------ 12 files changed, 102 insertions(+), 167 deletions(-) diff --git a/crates/oxc_isolated_declarations/src/class.rs b/crates/oxc_isolated_declarations/src/class.rs index 9218380f10d28..2f03e7a1f390b 100644 --- a/crates/oxc_isolated_declarations/src/class.rs +++ b/crates/oxc_isolated_declarations/src/class.rs @@ -62,8 +62,7 @@ impl<'a> IsolatedDeclarations<'a> { if property.accessibility.map_or(true, |a| !a.is_private()) { if property.type_annotation.is_some() { - // SAFETY: `ast.copy` is unsound! We need to fix. - type_annotations = unsafe { self.ast.copy(&property.type_annotation) }; + type_annotations = property.type_annotation.clone_in(self.ast.allocator); } else if let Some(expr) = property.value.as_ref() { let ts_type = if property.readonly { // `field = 'string'` remain `field = 'string'` instead of `field: 'string'` @@ -75,8 +74,7 @@ impl<'a> IsolatedDeclarations<'a> { .transform_template_to_string(lit) .map(Expression::StringLiteral); } else { - // SAFETY: `ast.copy` is unsound! We need to fix. - value = Some(unsafe { self.ast.copy(expr) }); + value = Some(expr.clone_in(self.ast.allocator)); } None } @@ -96,8 +94,7 @@ impl<'a> IsolatedDeclarations<'a> { property.r#type, property.span, self.ast.vec(), - // SAFETY: `ast.copy` is unsound! We need to fix. - unsafe { self.ast.copy(&property.key) }, + property.key.clone_in(self.ast.allocator), value, property.computed, property.r#static, @@ -122,15 +119,12 @@ impl<'a> IsolatedDeclarations<'a> { let value = self.ast.alloc_function( FunctionType::TSEmptyBodyFunctionExpression, function.span, - // SAFETY: `ast.copy` is unsound! We need to fix. - unsafe { self.ast.copy(&function.id) }, + function.id.clone_in(self.ast.allocator), false, false, false, - // SAFETY: `ast.copy` is unsound! We need to fix. - unsafe { self.ast.copy(&function.type_parameters) }, - // SAFETY: `ast.copy` is unsound! We need to fix. - unsafe { self.ast.copy(&function.this_param) }, + function.type_parameters.clone_in(self.ast.allocator), + function.this_param.clone_in(self.ast.allocator), params, return_type, NONE, @@ -140,8 +134,7 @@ impl<'a> IsolatedDeclarations<'a> { definition.r#type, definition.span, self.ast.vec(), - // SAFETY: `ast.copy` is unsound! We need to fix. - unsafe { self.ast.copy(&definition.key) }, + definition.key.clone_in(self.ast.allocator), value, definition.kind, definition.computed, @@ -221,8 +214,7 @@ impl<'a> IsolatedDeclarations<'a> { self.create_class_property( r#type, method.span, - // SAFETY: `ast.copy` is unsound! We need to fix. - unsafe { self.ast.copy(&method.key) }, + method.key.clone_in(self.ast.allocator), method.r#static, method.r#override, self.transform_accessibility(method.accessibility), @@ -259,8 +251,8 @@ impl<'a> IsolatedDeclarations<'a> { None } else { // transformed params will definitely have type annotation - // SAFETY: `ast.copy` is unsound! We need to fix. - unsafe { self.ast.copy(¶ms.items[index].pattern.type_annotation) } + + params.items[index].pattern.type_annotation.clone_in(self.ast.allocator) }; if let Some(new_element) = self.transform_formal_parameter_to_class_property(param, type_annotation) @@ -499,14 +491,12 @@ impl<'a> IsolatedDeclarations<'a> { property.r#type, property.span, self.ast.vec(), - // SAFETY: `ast.copy` is unsound! We need to fix. - unsafe { self.ast.copy(&property.key) }, + property.key.clone_in(self.ast.allocator), None, property.computed, property.r#static, property.definite, - // SAFETY: `ast.copy` is unsound! We need to fix. - unsafe { self.ast.copy(&property.type_annotation) }, + property.type_annotation.clone_in(self.ast.allocator), property.accessibility, ); elements.push(new_element); @@ -515,8 +505,8 @@ impl<'a> IsolatedDeclarations<'a> { if self.has_internal_annotation(signature.span) { continue; } - // SAFETY: `ast.copy` is unsound! We need to fix. - unsafe { self.ast.copy(element) } + + element.clone_in(self.ast.allocator) }), } } @@ -542,16 +532,11 @@ impl<'a> IsolatedDeclarations<'a> { decl.r#type, decl.span, self.ast.vec(), - // SAFETY: `ast.copy` is unsound! We need to fix. - unsafe { self.ast.copy(&decl.id) }, - // SAFETY: `ast.copy` is unsound! We need to fix. - unsafe { self.ast.copy(&decl.type_parameters) }, - // SAFETY: `ast.copy` is unsound! We need to fix. - unsafe { self.ast.copy(&decl.super_class) }, - // SAFETY: `ast.copy` is unsound! We need to fix. - unsafe { self.ast.copy(&decl.super_type_parameters) }, - // SAFETY: `ast.copy` is unsound! We need to fix. - unsafe { self.ast.copy(&decl.implements) }, + decl.id.clone_in(self.ast.allocator), + decl.type_parameters.clone_in(self.ast.allocator), + decl.super_class.clone_in(self.ast.allocator), + decl.super_type_parameters.clone_in(self.ast.allocator), + decl.implements.clone_in(self.ast.allocator), body, decl.r#abstract, declare.unwrap_or_else(|| self.is_declare()), diff --git a/crates/oxc_isolated_declarations/src/declaration.rs b/crates/oxc_isolated_declarations/src/declaration.rs index 701e1b5da15a2..731f47d19a4f8 100644 --- a/crates/oxc_isolated_declarations/src/declaration.rs +++ b/crates/oxc_isolated_declarations/src/declaration.rs @@ -1,8 +1,6 @@ use std::cell::Cell; -use oxc_allocator::Box; -use oxc_allocator::CloneIn; -use oxc_allocator::Vec; +use oxc_allocator::{Box, CloneIn, Vec}; use oxc_ast::ast::*; use oxc_ast::visit::walk_mut::walk_ts_signatures; use oxc_ast::{Visit, VisitMut}; @@ -76,8 +74,7 @@ impl<'a> IsolatedDeclarations<'a> { init = self.transform_template_to_string(lit).map(Expression::StringLiteral); } else { - // SAFETY: `ast.copy` is unsound! We need to fix. - init = Some(unsafe { self.ast.copy(init_expr) }); + init = Some(init_expr.clone_in(self.ast.allocator)); } } else if !decl.kind.is_const() || !matches!(init_expr, Expression::TemplateLiteral(_)) @@ -94,14 +91,10 @@ impl<'a> IsolatedDeclarations<'a> { } } let id = binding_type.map_or_else( - || { - // SAFETY: `ast.copy` is unsound! We need to fix. - unsafe { self.ast.copy(&decl.id) } - }, + || decl.id.clone_in(self.ast.allocator), |ts_type| { self.ast.binding_pattern( - // SAFETY: `ast.copy` is unsound! We need to fix. - unsafe { self.ast.copy(&decl.id.kind) }, + decl.id.kind.clone_in(self.ast.allocator), Some(self.ast.ts_type_annotation(SPAN, ts_type)), decl.id.optional, ) @@ -128,13 +121,11 @@ impl<'a> IsolatedDeclarations<'a> { decl: &Box<'a, TSModuleDeclaration<'a>>, ) -> Box<'a, TSModuleDeclaration<'a>> { if decl.declare { - // SAFETY: `ast.copy` is unsound! We need to fix. - return unsafe { self.ast.copy(decl) }; + return decl.clone_in(self.ast.allocator); } let Some(body) = &decl.body else { - // SAFETY: `ast.copy` is unsound! We need to fix. - return unsafe { self.ast.copy(decl) }; + return decl.clone_in(self.ast.allocator); }; match body { @@ -142,8 +133,7 @@ impl<'a> IsolatedDeclarations<'a> { let inner = self.transform_ts_module_declaration(decl); self.ast.alloc_ts_module_declaration( decl.span, - // SAFETY: `ast.copy` is unsound! We need to fix. - unsafe { self.ast.copy(&decl.id) }, + decl.id.clone_in(self.ast.allocator), Some(TSModuleDeclarationBody::TSModuleDeclaration(inner)), decl.kind, self.is_declare(), @@ -153,8 +143,7 @@ impl<'a> IsolatedDeclarations<'a> { let body = self.transform_ts_module_block(block); self.ast.alloc_ts_module_declaration( decl.span, - // SAFETY: `ast.copy` is unsound! We need to fix. - unsafe { self.ast.copy(&decl.id) }, + decl.id.clone_in(self.ast.allocator), Some(TSModuleDeclarationBody::TSModuleBlock(body)), decl.kind, self.is_declare(), @@ -232,8 +221,7 @@ impl<'a> IsolatedDeclarations<'a> { } Declaration::TSImportEqualsDeclaration(decl) => { if !check_binding || self.scope.has_reference(&decl.id.name) { - // SAFETY: `ast.copy` is unsound! We need to fix. - Some(Declaration::TSImportEqualsDeclaration(unsafe { self.ast.copy(decl) })) + Some(Declaration::TSImportEqualsDeclaration(decl.clone_in(self.ast.allocator))) } else { None } diff --git a/crates/oxc_isolated_declarations/src/enum.rs b/crates/oxc_isolated_declarations/src/enum.rs index bf48897bff556..53a2f41f2e852 100644 --- a/crates/oxc_isolated_declarations/src/enum.rs +++ b/crates/oxc_isolated_declarations/src/enum.rs @@ -1,5 +1,6 @@ use rustc_hash::FxHashMap; +use oxc_allocator::CloneIn; use oxc_ast::ast::*; use oxc_ecmascript::ToInt32; use oxc_span::{Atom, GetSpan, SPAN}; @@ -54,8 +55,7 @@ impl<'a> IsolatedDeclarations<'a> { let member = self.ast.ts_enum_member( member.span, - // SAFETY: `ast.copy` is unsound! We need to fix. - unsafe { self.ast.copy(&member.id) }, + member.id.clone_in(self.ast.allocator), value.map(|v| match v { ConstantValue::Number(v) => { let is_negative = v < 0.0; @@ -88,8 +88,7 @@ impl<'a> IsolatedDeclarations<'a> { Some(self.ast.declaration_ts_enum( decl.span, - // SAFETY: `ast.copy` is unsound! We need to fix. - unsafe { self.ast.copy(&decl.id) }, + decl.id.clone_in(self.ast.allocator), members, decl.r#const, self.is_declare(), diff --git a/crates/oxc_isolated_declarations/src/formal_parameter_binding_pattern.rs b/crates/oxc_isolated_declarations/src/formal_parameter_binding_pattern.rs index 0e64905631782..faeb91a27a6db 100644 --- a/crates/oxc_isolated_declarations/src/formal_parameter_binding_pattern.rs +++ b/crates/oxc_isolated_declarations/src/formal_parameter_binding_pattern.rs @@ -1,3 +1,4 @@ +use oxc_allocator::CloneIn; use oxc_ast::{ ast::BindingPatternKind, visit::walk_mut::walk_binding_pattern_kind, AstBuilder, VisitMut, }; @@ -9,10 +10,8 @@ pub struct FormalParameterBindingPattern<'a> { impl<'a> VisitMut<'a> for FormalParameterBindingPattern<'a> { fn visit_binding_pattern_kind(&mut self, kind: &mut BindingPatternKind<'a>) { if let BindingPatternKind::AssignmentPattern(assignment) = kind { - // SAFETY: `ast.copy` is unsound! We need to fix. - *kind = unsafe { self.ast.copy(&assignment.left.kind) }; + *kind = assignment.left.kind.clone_in(self.ast.allocator); } - walk_binding_pattern_kind(self, kind); } } diff --git a/crates/oxc_isolated_declarations/src/function.rs b/crates/oxc_isolated_declarations/src/function.rs index 380098817f38d..6b471f39c6934 100644 --- a/crates/oxc_isolated_declarations/src/function.rs +++ b/crates/oxc_isolated_declarations/src/function.rs @@ -1,4 +1,4 @@ -use oxc_allocator::Box; +use oxc_allocator::{Box, CloneIn}; use oxc_ast::{ast::*, NONE}; use oxc_span::{Span, SPAN}; @@ -28,15 +28,12 @@ impl<'a> IsolatedDeclarations<'a> { Some(self.ast.alloc_function( func.r#type, func.span, - // SAFETY: `ast.copy` is unsound! We need to fix. - unsafe { self.ast.copy(&func.id) }, + func.id.clone_in(self.ast.allocator), false, false, declare.unwrap_or_else(|| self.is_declare()), - // SAFETY: `ast.copy` is unsound! We need to fix. - unsafe { self.ast.copy(&func.type_parameters) }, - // SAFETY: `ast.copy` is unsound! We need to fix. - unsafe { self.ast.copy(&func.this_param) }, + func.type_parameters.clone_in(self.ast.allocator), + func.this_param.clone_in(self.ast.allocator), params, return_type, NONE, @@ -62,11 +59,9 @@ impl<'a> IsolatedDeclarations<'a> { let is_assignment_pattern = pattern.kind.is_assignment_pattern(); let mut pattern = if let BindingPatternKind::AssignmentPattern(pattern) = ¶m.pattern.kind { - // SAFETY: `ast.copy` is unsound! We need to fix. - unsafe { self.ast.copy(&pattern.left) } + pattern.left.clone_in(self.ast.allocator) } else { - // SAFETY: `ast.copy` is unsound! We need to fix. - unsafe { self.ast.copy(¶m.pattern) } + param.pattern.clone_in(self.ast.allocator) }; FormalParameterBindingPattern::remove_assignments_from_kind(self.ast, &mut pattern.kind); @@ -75,10 +70,7 @@ impl<'a> IsolatedDeclarations<'a> { let type_annotation = pattern .type_annotation .as_ref() - .map(|type_annotation| { - // SAFETY: `ast.copy` is unsound! We need to fix. - unsafe { self.ast.copy(&type_annotation.type_annotation) } - }) + .map(|type_annotation| type_annotation.type_annotation.clone_in(self.ast.allocator)) .or_else(|| { // report error for has no type annotation let new_type = self.infer_type_from_formal_parameter(param); @@ -112,8 +104,7 @@ impl<'a> IsolatedDeclarations<'a> { }); pattern = self.ast.binding_pattern( - // SAFETY: `ast.copy` is unsound! We need to fix. - unsafe { self.ast.copy(&pattern.kind) }, + pattern.kind.clone_in(self.ast.allocator), type_annotation, // if it's assignment pattern, it's optional pattern.optional || (!is_remaining_params_have_required && is_assignment_pattern), @@ -128,8 +119,7 @@ impl<'a> IsolatedDeclarations<'a> { params: &FormalParameters<'a>, ) -> Box<'a, FormalParameters<'a>> { if params.kind.is_signature() || (params.rest.is_none() && params.items.is_empty()) { - // SAFETY: `ast.copy` is unsound! We need to fix. - return self.ast.alloc(unsafe { self.ast.copy(params) }); + return self.ast.alloc(params.clone_in(self.ast.allocator)); } let items = @@ -151,8 +141,7 @@ impl<'a> IsolatedDeclarations<'a> { params.span, FormalParameterKind::Signature, items, - // SAFETY: `ast.copy` is unsound! We need to fix. - unsafe { self.ast.copy(¶ms.rest) }, + params.rest.clone_in(self.ast.allocator), ) } } diff --git a/crates/oxc_isolated_declarations/src/inferrer.rs b/crates/oxc_isolated_declarations/src/inferrer.rs index 67eca4b22b018..1081578cdbbaa 100644 --- a/crates/oxc_isolated_declarations/src/inferrer.rs +++ b/crates/oxc_isolated_declarations/src/inferrer.rs @@ -1,4 +1,4 @@ -use oxc_allocator::Box; +use oxc_allocator::{Box, CloneIn}; use oxc_ast::ast::{ ArrowFunctionExpression, BindingPatternKind, Expression, FormalParameter, Function, Statement, TSType, TSTypeAnnotation, UnaryExpression, @@ -30,17 +30,11 @@ impl<'a> IsolatedDeclarations<'a> { _ => None, }, Expression::FunctionExpression(func) => { - self.transform_function_to_ts_type(func).map(|x| { - // SAFETY: `ast.copy` is unsound! We need to fix. - unsafe { self.ast.copy(&x) } - }) - } - Expression::ArrowFunctionExpression(func) => { - self.transform_arrow_function_to_ts_type(func).map(|x| { - // SAFETY: `ast.copy` is unsound! We need to fix. - unsafe { self.ast.copy(&x) } - }) + self.transform_function_to_ts_type(func).map(|x| x.clone_in(self.ast.allocator)) } + Expression::ArrowFunctionExpression(func) => self + .transform_arrow_function_to_ts_type(func) + .map(|x| x.clone_in(self.ast.allocator)), Expression::ObjectExpression(expr) => { Some(self.transform_object_expression_to_ts_type(expr, false)) } @@ -52,8 +46,7 @@ impl<'a> IsolatedDeclarations<'a> { if expr.type_annotation.is_const_type_reference() { self.transform_expression_to_ts_type(&expr.expression) } else { - // SAFETY: `ast.copy` is unsound! We need to fix. - Some(unsafe { self.ast.copy(&expr.type_annotation) }) + Some(expr.type_annotation.clone_in(self.ast.allocator)) } } Expression::ClassExpression(expr) => { @@ -70,8 +63,7 @@ impl<'a> IsolatedDeclarations<'a> { self.infer_type_from_expression(&expr.expression) } Expression::TSTypeAssertion(expr) => { - // SAFETY: `ast.copy` is unsound! We need to fix. - Some(unsafe { self.ast.copy(&expr.type_annotation) }) + Some(expr.type_annotation.clone_in(self.ast.allocator)) } Expression::UnaryExpression(expr) => { if Self::can_infer_unary_expression(expr) { @@ -89,15 +81,15 @@ impl<'a> IsolatedDeclarations<'a> { param: &FormalParameter<'a>, ) -> Option> { if param.pattern.type_annotation.is_some() { - param.pattern.type_annotation.as_ref().map(|x| { - // SAFETY: `ast.copy` is unsound! We need to fix. - unsafe { self.ast.copy(&x.type_annotation) } - }); + param + .pattern + .type_annotation + .as_ref() + .map(|x| x.type_annotation.clone_in(self.ast.allocator)); } if let BindingPatternKind::AssignmentPattern(pattern) = ¶m.pattern.kind { if let Some(annotation) = pattern.left.type_annotation.as_ref() { - // SAFETY: `ast.copy` is unsound! We need to fix. - Some(unsafe { self.ast.copy(&annotation.type_annotation) }) + Some(annotation.type_annotation.clone_in(self.ast.allocator)) } else { self.infer_type_from_expression(&pattern.right) } @@ -111,8 +103,7 @@ impl<'a> IsolatedDeclarations<'a> { function: &Function<'a>, ) -> Option>> { if function.return_type.is_some() { - // SAFETY: `ast.copy` is unsound! We need to fix. - return unsafe { self.ast.copy(&function.return_type) }; + return function.return_type.clone_in(self.ast.allocator); } if function.r#async || function.generator { @@ -130,8 +121,7 @@ impl<'a> IsolatedDeclarations<'a> { function: &ArrowFunctionExpression<'a>, ) -> Option>> { if function.return_type.is_some() { - // SAFETY: `ast.copy` is unsound! We need to fix. - return unsafe { self.ast.copy(&function.return_type) }; + return function.return_type.clone_in(self.ast.allocator); } if function.r#async { diff --git a/crates/oxc_isolated_declarations/src/lib.rs b/crates/oxc_isolated_declarations/src/lib.rs index 18c7e5ec369a9..d9f20b1ad4607 100644 --- a/crates/oxc_isolated_declarations/src/lib.rs +++ b/crates/oxc_isolated_declarations/src/lib.rs @@ -21,7 +21,6 @@ mod types; use std::{cell::RefCell, mem}; -use diagnostics::function_with_assigning_properties; use oxc_allocator::{Allocator, CloneIn}; use oxc_ast::{ast::*, AstBuilder, Visit, NONE}; use oxc_diagnostics::OxcDiagnostic; @@ -29,6 +28,7 @@ use oxc_span::{Atom, GetSpan, SourceType, SPAN}; use rustc_hash::{FxHashMap, FxHashSet}; use crate::scope::ScopeTree; +use diagnostics::function_with_assigning_properties; #[derive(Debug, Default, Clone, Copy)] pub struct IsolatedDeclarationsOptions { diff --git a/crates/oxc_isolated_declarations/src/module.rs b/crates/oxc_isolated_declarations/src/module.rs index a905fe717dae6..966405f0504c9 100644 --- a/crates/oxc_isolated_declarations/src/module.rs +++ b/crates/oxc_isolated_declarations/src/module.rs @@ -1,4 +1,5 @@ use oxc_allocator::Box; +use oxc_allocator::CloneIn; use oxc_allocator::Vec; use oxc_ast::ast::*; use oxc_span::{Atom, GetSpan, SPAN}; @@ -44,8 +45,7 @@ impl<'a> IsolatedDeclarations<'a> { .transform_class(decl, Some(false)) .map(|d| (None, ExportDefaultDeclarationKind::ClassDeclaration(d))), ExportDefaultDeclarationKind::TSInterfaceDeclaration(_) => { - // SAFETY: `ast.copy` is unsound! We need to fix. - Some((None, unsafe { self.ast.copy(&decl.declaration) })) + Some((None, decl.declaration.clone_in(self.ast.allocator))) } declaration @ match_expression!(ExportDefaultDeclarationKind) => self .transform_export_expression(declaration.to_expression()) @@ -115,30 +115,33 @@ impl<'a> IsolatedDeclarations<'a> { ) -> Option>> { let specifiers = decl.specifiers.as_ref()?; - // SAFETY: `ast.copy` is unsound! We need to fix. - let mut specifiers = unsafe { self.ast.copy(specifiers) }; - specifiers.retain(|specifier| match specifier { - ImportDeclarationSpecifier::ImportSpecifier(specifier) => { - self.scope.has_reference(&specifier.local.name) - } - ImportDeclarationSpecifier::ImportDefaultSpecifier(specifier) => { - self.scope.has_reference(&specifier.local.name) - } - ImportDeclarationSpecifier::ImportNamespaceSpecifier(_) => { - self.scope.has_reference(&specifier.name()) + let mut new_specifiers = self.ast.vec_with_capacity(specifiers.len()); + specifiers.iter().for_each(|specifier| { + let is_referenced = match specifier { + ImportDeclarationSpecifier::ImportSpecifier(specifier) => { + self.scope.has_reference(&specifier.local.name) + } + ImportDeclarationSpecifier::ImportDefaultSpecifier(specifier) => { + self.scope.has_reference(&specifier.local.name) + } + ImportDeclarationSpecifier::ImportNamespaceSpecifier(_) => { + self.scope.has_reference(&specifier.name()) + } + }; + if is_referenced { + new_specifiers.push(specifier.clone_in(self.ast.allocator)); } }); - if specifiers.is_empty() { + + if new_specifiers.is_empty() { // We don't need to print this import statement None } else { Some(self.ast.alloc_import_declaration( decl.span, - Some(specifiers), - // SAFETY: `ast.copy` is unsound! We need to fix. - unsafe { self.ast.copy(&decl.source) }, - // SAFETY: `ast.copy` is unsound! We need to fix. - unsafe { self.ast.copy(&decl.with_clause) }, + Some(new_specifiers), + decl.source.clone(), + decl.with_clause.clone_in(self.ast.allocator), decl.import_kind, )) } diff --git a/crates/oxc_isolated_declarations/src/return_type.rs b/crates/oxc_isolated_declarations/src/return_type.rs index 55a6ca5154383..c6904ba009fcb 100644 --- a/crates/oxc_isolated_declarations/src/return_type.rs +++ b/crates/oxc_isolated_declarations/src/return_type.rs @@ -1,5 +1,6 @@ use std::cell::Cell; +use oxc_allocator::CloneIn; use oxc_ast::{ ast::{ ArrowFunctionExpression, BindingIdentifier, Expression, Function, FunctionBody, @@ -162,7 +163,7 @@ impl<'a> Visit<'a> for FunctionReturnType<'a> { return; } } - // SAFETY: `ast.copy` is unsound! We need to fix. - self.return_expression = Some(unsafe { self.ast.copy(&stmt.argument) }); + + self.return_expression = Some(stmt.argument.clone_in(self.ast.allocator)); } } diff --git a/crates/oxc_isolated_declarations/src/scope.rs b/crates/oxc_isolated_declarations/src/scope.rs index 51e5cf0dd03fd..1e71c6deec488 100644 --- a/crates/oxc_isolated_declarations/src/scope.rs +++ b/crates/oxc_isolated_declarations/src/scope.rs @@ -1,8 +1,8 @@ -use std::cell::Cell; - use bitflags::bitflags; use rustc_hash::FxHashMap; +use std::cell::Cell; + use oxc_ast::ast::*; use oxc_ast::{visit::walk::*, Visit}; use oxc_span::Atom; diff --git a/crates/oxc_isolated_declarations/src/signatures.rs b/crates/oxc_isolated_declarations/src/signatures.rs index 35ffd361be8d1..562d5a40e7fc4 100644 --- a/crates/oxc_isolated_declarations/src/signatures.rs +++ b/crates/oxc_isolated_declarations/src/signatures.rs @@ -1,7 +1,8 @@ +use rustc_hash::FxHashMap; + use oxc_allocator::{CloneIn, Vec}; use oxc_ast::ast::{TSMethodSignatureKind, TSSignature}; use oxc_span::GetSpan; -use rustc_hash::FxHashMap; use crate::IsolatedDeclarations; diff --git a/crates/oxc_isolated_declarations/src/types.rs b/crates/oxc_isolated_declarations/src/types.rs index 3f5a3b150a86c..c69f814dbc7ce 100644 --- a/crates/oxc_isolated_declarations/src/types.rs +++ b/crates/oxc_isolated_declarations/src/types.rs @@ -1,3 +1,4 @@ +use oxc_allocator::CloneIn; use oxc_ast::{ ast::{ ArrayExpression, ArrayExpressionElement, ArrowFunctionExpression, Expression, Function, @@ -29,10 +30,8 @@ impl<'a> IsolatedDeclarations<'a> { return_type.map(|return_type| { self.ast.ts_type_function_type( func.span, - // SAFETY: `ast.copy` is unsound! We need to fix. - unsafe { self.ast.copy(&func.type_parameters) }, - // SAFETY: `ast.copy` is unsound! We need to fix. - unsafe { self.ast.copy(&func.this_param) }, + func.type_parameters.clone_in(self.ast.allocator), + func.this_param.clone_in(self.ast.allocator), params, return_type, ) @@ -57,8 +56,7 @@ impl<'a> IsolatedDeclarations<'a> { return_type.map(|return_type| { self.ast.ts_type_function_type( func.span, - // SAFETY: `ast.copy` is unsound! We need to fix. - unsafe { self.ast.copy(&func.type_parameters) }, + func.type_parameters.clone_in(self.ast.allocator), NONE, params, return_type, @@ -101,15 +99,12 @@ impl<'a> IsolatedDeclarations<'a> { let params = self.transform_formal_parameters(&function.params); return Some(self.ast.ts_signature_method_signature( object.span, - // SAFETY: `ast.copy` is unsound! We need to fix. - unsafe { self.ast.copy(&object.key) }, + object.key.clone_in(self.ast.allocator), object.computed, false, TSMethodSignatureKind::Method, - // SAFETY: `ast.copy` is unsound! We need to fix. - unsafe { self.ast.copy(&function.type_parameters) }, - // SAFETY: `ast.copy` is unsound! We need to fix. - unsafe { self.ast.copy(&function.this_param) }, + function.type_parameters.clone_in(self.ast.allocator), + function.this_param.clone_in(self.ast.allocator), params, return_type, )); @@ -132,8 +127,7 @@ impl<'a> IsolatedDeclarations<'a> { false, false, is_const, - // SAFETY: `ast.copy` is unsound! We need to fix. - unsafe { self.ast.copy(&object.key) }, + object.key.clone_in(self.ast.allocator), type_annotation.map(|type_annotation| { self.ast.ts_type_annotation(SPAN, type_annotation) }), @@ -188,31 +182,19 @@ impl<'a> IsolatedDeclarations<'a> { match expr { Expression::BooleanLiteral(lit) => Some(self.ast.ts_type_literal_type( SPAN, - TSLiteral::BooleanLiteral({ - // SAFETY: `ast.copy` is unsound! We need to fix. - unsafe { self.ast.copy(lit) } - }), + TSLiteral::BooleanLiteral(lit.clone_in(self.ast.allocator)), )), Expression::NumericLiteral(lit) => Some(self.ast.ts_type_literal_type( SPAN, - TSLiteral::NumericLiteral({ - // SAFETY: `ast.copy` is unsound! We need to fix. - unsafe { self.ast.copy(lit) } - }), + TSLiteral::NumericLiteral(lit.clone_in(self.ast.allocator)), )), Expression::BigIntLiteral(lit) => Some(self.ast.ts_type_literal_type( SPAN, - TSLiteral::BigIntLiteral({ - // SAFETY: `ast.copy` is unsound! We need to fix. - unsafe { self.ast.copy(lit) } - }), + TSLiteral::BigIntLiteral(lit.clone_in(self.ast.allocator)), )), Expression::StringLiteral(lit) => Some(self.ast.ts_type_literal_type( SPAN, - TSLiteral::StringLiteral({ - // SAFETY: `ast.copy` is unsound! We need to fix. - unsafe { self.ast.copy(lit) } - }), + TSLiteral::StringLiteral(lit.clone_in(self.ast.allocator)), )), Expression::NullLiteral(lit) => Some(self.ast.ts_type_null_keyword(lit.span)), Expression::Identifier(ident) => match ident.name.as_str() { @@ -228,8 +210,7 @@ impl<'a> IsolatedDeclarations<'a> { if Self::can_infer_unary_expression(expr) { Some(self.ast.ts_type_literal_type( SPAN, - // SAFETY: `ast.copy` is unsound! We need to fix. - TSLiteral::UnaryExpression(unsafe { self.ast.copy(expr) }), + TSLiteral::UnaryExpression(expr.clone_in(self.ast.allocator)), )) } else { None @@ -249,8 +230,7 @@ impl<'a> IsolatedDeclarations<'a> { if expr.type_annotation.is_const_type_reference() { self.transform_expression_to_ts_type(&expr.expression) } else { - // SAFETY: `ast.copy` is unsound! We need to fix. - Some(unsafe { self.ast.copy(&expr.type_annotation) }) + Some(expr.type_annotation.clone_in(self.ast.allocator)) } } _ => None,