Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 19 additions & 34 deletions crates/oxc_isolated_declarations/src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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'`
Expand All @@ -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
}
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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(&params.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)
Expand Down Expand Up @@ -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);
Expand All @@ -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)
}),
}
}
Expand All @@ -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()),
Expand Down
30 changes: 9 additions & 21 deletions crates/oxc_isolated_declarations/src/declaration.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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(_))
Expand All @@ -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,
)
Expand All @@ -128,22 +121,19 @@ 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 {
TSModuleDeclarationBody::TSModuleDeclaration(decl) => {
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(),
Expand All @@ -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(),
Expand Down Expand Up @@ -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
}
Expand Down
7 changes: 3 additions & 4 deletions crates/oxc_isolated_declarations/src/enum.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use oxc_allocator::CloneIn;
use oxc_ast::{
ast::BindingPatternKind, visit::walk_mut::walk_binding_pattern_kind, AstBuilder, VisitMut,
};
Expand All @@ -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);
}
}
Expand Down
31 changes: 10 additions & 21 deletions crates/oxc_isolated_declarations/src/function.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use oxc_allocator::Box;
use oxc_allocator::{Box, CloneIn};
use oxc_ast::{ast::*, NONE};
use oxc_span::{Span, SPAN};

Expand Down Expand Up @@ -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,
Expand All @@ -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) = &param.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(&param.pattern) }
param.pattern.clone_in(self.ast.allocator)
};

FormalParameterBindingPattern::remove_assignments_from_kind(self.ast, &mut pattern.kind);
Expand All @@ -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);
Expand Down Expand Up @@ -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),
Expand All @@ -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 =
Expand All @@ -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(&params.rest) },
params.rest.clone_in(self.ast.allocator),
)
}
}
Expand Down
Loading