Skip to content

Commit

Permalink
refactor(span): derive Copy on Atom (#8596)
Browse files Browse the repository at this point in the history
Follow up from
#8543 (comment)

> I agree. https://github.com/oxc-project/backlog/issues/155

> Originally we were considering some form of interning and
reference-counting, so we didn't make it Copy to leave the door open for
that. But now all strings are stored in the arena anyway, so even if we
did decide to intern strings, reference-counting would be irrelevant -
our bump allocator doesn't allow freeing individual allocations anyway.

Most of the changes are done automatically by `just fix` (`cargo clippy
--fix` && `cargo fmt --all`). See the commit list for the manual edits.
  • Loading branch information
branchseer authored Jan 19, 2025
1 parent 066ffe8 commit ac4f98e
Show file tree
Hide file tree
Showing 61 changed files with 210 additions and 248 deletions.
2 changes: 1 addition & 1 deletion crates/oxc_ast/src/ast_builder_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ impl<'a> AstBuilder<'a> {
#[inline]
pub fn use_strict_directive(self) -> Directive<'a> {
let use_strict = Atom::from("use strict");
self.directive(SPAN, self.string_literal(SPAN, use_strict.clone(), None), use_strict)
self.directive(SPAN, self.string_literal(SPAN, use_strict, None), use_strict)
}

/* ---------- Functions ---------- */
Expand Down
30 changes: 15 additions & 15 deletions crates/oxc_ast/src/ast_impl/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ impl<'a> PropertyKey<'a> {
#[allow(missing_docs)]
pub fn private_name(&self) -> Option<Atom<'a>> {
match self {
Self::PrivateIdentifier(ident) => Some(ident.name.clone()),
Self::PrivateIdentifier(ident) => Some(ident.name),
_ => None,
}
}
Expand Down Expand Up @@ -451,7 +451,7 @@ impl<'a> TemplateLiteral<'a> {

/// Get single quasi from `template`
pub fn quasi(&self) -> Option<Atom<'a>> {
self.quasis.first().and_then(|quasi| quasi.value.cooked.clone())
self.quasis.first().and_then(|quasi| quasi.value.cooked)
}
}

Expand Down Expand Up @@ -548,13 +548,13 @@ impl<'a> ComputedMemberExpression<'a> {
#[allow(missing_docs)]
pub fn static_property_name(&self) -> Option<Atom<'a>> {
match &self.expression {
Expression::StringLiteral(lit) => Some(lit.value.clone()),
Expression::StringLiteral(lit) => Some(lit.value),
Expression::TemplateLiteral(lit)
if lit.expressions.is_empty() && lit.quasis.len() == 1 =>
{
Some(lit.quasis[0].value.raw.clone())
Some(lit.quasis[0].value.raw)
}
Expression::RegExpLiteral(lit) => lit.raw.clone(),
Expression::RegExpLiteral(lit) => lit.raw,
_ => None,
}
}
Expand Down Expand Up @@ -953,7 +953,7 @@ impl<'a> BindingPatternKind<'a> {
#[allow(missing_docs)]
pub fn get_identifier(&self) -> Option<Atom<'a>> {
match self {
Self::BindingIdentifier(ident) => Some(ident.name.clone()),
Self::BindingIdentifier(ident) => Some(ident.name),
Self::AssignmentPattern(assign) => assign.left.get_identifier(),
_ => None,
}
Expand Down Expand Up @@ -1026,7 +1026,7 @@ impl<'a> Function<'a> {
/// Returns this [`Function`]'s name, if it has one.
#[inline]
pub fn name(&self) -> Option<Atom<'a>> {
self.id.as_ref().map(|id| id.name.clone())
self.id.as_ref().map(|id| id.name)
}

/// Get the [`SymbolId`] this [`Function`] is bound to.
Expand Down Expand Up @@ -1176,7 +1176,7 @@ impl<'a> Class<'a> {
/// Returns this [`Class`]'s name, if it has one.
#[inline]
pub fn name(&self) -> Option<Atom<'a>> {
self.id.as_ref().map(|id| id.name.clone())
self.id.as_ref().map(|id| id.name)
}

/// `true` if this [`Class`] is an expression.
Expand Down Expand Up @@ -1487,8 +1487,8 @@ impl<'a> ImportAttributeKey<'a> {
#[allow(missing_docs)]
pub fn as_atom(&self) -> Atom<'a> {
match self {
Self::Identifier(identifier) => identifier.name.clone(),
Self::StringLiteral(literal) => literal.value.clone(),
Self::Identifier(identifier) => identifier.name,
Self::StringLiteral(literal) => literal.value,
}
}
}
Expand Down Expand Up @@ -1543,17 +1543,17 @@ impl<'a> ModuleExportName<'a> {
#[allow(missing_docs)]
pub fn name(&self) -> Atom<'a> {
match self {
Self::IdentifierName(identifier) => identifier.name.clone(),
Self::IdentifierReference(identifier) => identifier.name.clone(),
Self::StringLiteral(literal) => literal.value.clone(),
Self::IdentifierName(identifier) => identifier.name,
Self::IdentifierReference(identifier) => identifier.name,
Self::StringLiteral(literal) => literal.value,
}
}

#[allow(missing_docs)]
pub fn identifier_name(&self) -> Option<Atom<'a>> {
match self {
Self::IdentifierName(identifier) => Some(identifier.name.clone()),
Self::IdentifierReference(identifier) => Some(identifier.name.clone()),
Self::IdentifierName(identifier) => Some(identifier.name),
Self::IdentifierReference(identifier) => Some(identifier.name),
Self::StringLiteral(_) => None,
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_ast/src/ast_impl/jsx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ impl<'a> JSXElementName<'a> {
#[allow(missing_docs)]
pub fn get_identifier_name(&self) -> Option<Atom<'a>> {
match self {
Self::Identifier(id) => Some(id.as_ref().name.clone()),
Self::IdentifierReference(id) => Some(id.as_ref().name.clone()),
Self::Identifier(id) => Some(id.as_ref().name),
Self::IdentifierReference(id) => Some(id.as_ref().name),
_ => None,
}
}
Expand Down
8 changes: 4 additions & 4 deletions crates/oxc_ast/src/ast_impl/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ impl<'a> TSEnumMemberName<'a> {
/// Get the name of this enum member.
pub fn static_name(&self) -> Atom<'a> {
match self {
Self::Identifier(ident) => ident.name.clone(),
Self::String(lit) => lit.value.clone(),
Self::Identifier(ident) => ident.name,
Self::String(lit) => lit.value,
}
}
}
Expand Down Expand Up @@ -211,8 +211,8 @@ impl<'a> TSModuleDeclarationName<'a> {
/// Get the static name of this module declaration name.
pub fn name(&self) -> Atom<'a> {
match self {
Self::Identifier(ident) => ident.name.clone(),
Self::StringLiteral(lit) => lit.value.clone(),
Self::Identifier(ident) => ident.name,
Self::StringLiteral(lit) => lit.value,
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions crates/oxc_ast/src/ast_kind_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ impl<'a> AstKind<'a> {

pub fn identifier_name(self) -> Option<Atom<'a>> {
match self {
Self::BindingIdentifier(ident) => Some(ident.name.clone()),
Self::IdentifierReference(ident) => Some(ident.name.clone()),
Self::LabelIdentifier(ident) => Some(ident.name.clone()),
Self::IdentifierName(ident) => Some(ident.name.clone()),
Self::BindingIdentifier(ident) => Some(ident.name),
Self::IdentifierReference(ident) => Some(ident.name),
Self::LabelIdentifier(ident) => Some(ident.name),
Self::IdentifierName(ident) => Some(ident.name),
_ => None,
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_ast/src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ impl Serialize for JSXElementName<'_> {
match self {
Self::Identifier(ident) => ident.serialize(serializer),
Self::IdentifierReference(ident) => {
JSXIdentifier { span: ident.span, name: ident.name.clone() }.serialize(serializer)
JSXIdentifier { span: ident.span, name: ident.name }.serialize(serializer)
}
Self::NamespacedName(name) => name.serialize(serializer),
Self::MemberExpression(expr) => expr.serialize(serializer),
Expand All @@ -322,7 +322,7 @@ impl Serialize for JSXMemberExpressionObject<'_> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self {
Self::IdentifierReference(ident) => {
JSXIdentifier { span: ident.span, name: ident.name.clone() }.serialize(serializer)
JSXIdentifier { span: ident.span, name: ident.name }.serialize(serializer)
}
Self::MemberExpression(expr) => expr.serialize(serializer),
Self::ThisExpression(expr) => {
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_isolated_declarations/src/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl<'a> IsolatedDeclarations<'a> {
TSEnumMemberName::Identifier(id) => &id.name,
TSEnumMemberName::String(str) => &str.value,
};
prev_members.insert(member_name.clone(), value.clone());
prev_members.insert(*member_name, value.clone());
}

let member = self.ast.ts_enum_member(
Expand Down
14 changes: 7 additions & 7 deletions crates/oxc_isolated_declarations/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ impl<'a> IsolatedDeclarations<'a> {
return false;
}
} else {
last_function_name = Some(name.clone());
last_function_name = Some(*name);
}
true
}
Expand All @@ -470,7 +470,7 @@ impl<'a> IsolatedDeclarations<'a> {
return false;
}
} else {
last_function_name = Some(name.clone());
last_function_name = Some(*name);
}
true
} else {
Expand Down Expand Up @@ -546,14 +546,14 @@ impl<'a> IsolatedDeclarations<'a> {
assignable_properties_for_namespace
.entry(&ident.name)
.or_default()
.insert(id.name.clone());
.insert(id.name);
}
}
Some(Declaration::TSEnumDeclaration(decl)) => {
assignable_properties_for_namespace
.entry(&ident.name)
.or_default()
.insert(decl.id.name.clone());
.insert(decl.id.name);
}
_ => {}
}
Expand All @@ -573,7 +573,7 @@ impl<'a> IsolatedDeclarations<'a> {
Some(Declaration::FunctionDeclaration(func)) => {
if func.body.is_some() {
if let Some(id) = func.id.as_ref() {
can_expando_function_names.insert(id.name.clone());
can_expando_function_names.insert(id.name);
}
}
}
Expand All @@ -583,7 +583,7 @@ impl<'a> IsolatedDeclarations<'a> {
&& declarator.init.as_ref().is_some_and(Expression::is_function)
{
if let Some(name) = declarator.id.get_identifier() {
can_expando_function_names.insert(name.clone());
can_expando_function_names.insert(name);
}
}
}
Expand Down Expand Up @@ -617,7 +617,7 @@ impl<'a> IsolatedDeclarations<'a> {
{
if let Some(name) = declarator.id.get_identifier() {
if self.scope.has_reference(&name) {
can_expando_function_names.insert(name.clone());
can_expando_function_names.insert(name);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_isolated_declarations/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl<'a> IsolatedDeclarations<'a> {
// declare const _default: Type
let kind = VariableDeclarationKind::Const;
let name = self.create_unique_name("_default");
let id = self.ast.binding_pattern_kind_binding_identifier(SPAN, &name);
let id = self.ast.binding_pattern_kind_binding_identifier(SPAN, name);
let type_annotation = self
.infer_type_from_expression(expr)
.map(|ts_type| self.ast.ts_type_annotation(SPAN, ts_type));
Expand All @@ -97,7 +97,7 @@ impl<'a> IsolatedDeclarations<'a> {
declarations,
self.is_declare(),
));
Some((Some(variable_statement), self.ast.expression_identifier_reference(SPAN, &name)))
Some((Some(variable_statement), self.ast.expression_identifier_reference(SPAN, name)))
}
}

Expand Down
8 changes: 4 additions & 4 deletions crates/oxc_isolated_declarations/src/return_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ impl<'a> FunctionReturnType<'a> {
if let Some((reference_name, is_value)) = match &expr_type {
TSType::TSTypeReference(type_reference) => {
if let TSTypeName::IdentifierReference(ident) = &type_reference.type_name {
Some((ident.name.clone(), false))
Some((ident.name, false))
} else {
None
}
}
TSType::TSTypeQuery(query) => {
if let TSTypeQueryExprName::IdentifierReference(ident) = &query.expr_name {
Some((ident.name.clone(), true))
Some((ident.name, true))
} else {
None
}
Expand Down Expand Up @@ -132,13 +132,13 @@ impl<'a> Visit<'a> for FunctionReturnType<'a> {

fn visit_binding_identifier(&mut self, ident: &BindingIdentifier<'a>) {
if self.scope_depth == 0 {
self.value_bindings.push(ident.name.clone());
self.value_bindings.push(ident.name);
}
}

fn visit_ts_type_alias_declaration(&mut self, decl: &TSTypeAliasDeclaration<'a>) {
if self.scope_depth == 0 {
self.type_bindings.push(decl.id.name.clone());
self.type_bindings.push(decl.id.name);
}
}

Expand Down
24 changes: 12 additions & 12 deletions crates/oxc_isolated_declarations/src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,19 @@ impl<'a> Visit<'a> for ScopeTree<'a> {
}

fn visit_identifier_reference(&mut self, ident: &IdentifierReference<'a>) {
self.add_reference(ident.name.clone(), KindFlags::Value);
self.add_reference(ident.name, KindFlags::Value);
}

fn visit_binding_pattern(&mut self, pattern: &BindingPattern<'a>) {
if let BindingPatternKind::BindingIdentifier(ident) = &pattern.kind {
self.add_binding(ident.name.clone(), KindFlags::Value);
self.add_binding(ident.name, KindFlags::Value);
}
walk_binding_pattern(self, pattern);
}

fn visit_ts_type_name(&mut self, name: &TSTypeName<'a>) {
if let TSTypeName::IdentifierReference(ident) = name {
self.add_reference(ident.name.clone(), KindFlags::Type);
self.add_reference(ident.name, KindFlags::Type);
} else {
walk_ts_type_name(self, name);
}
Expand All @@ -114,7 +114,7 @@ impl<'a> Visit<'a> for ScopeTree<'a> {
fn visit_ts_type_query(&mut self, ty: &TSTypeQuery<'a>) {
if let Some(type_name) = ty.expr_name.as_ts_type_name() {
let ident = TSTypeName::get_identifier_reference(type_name);
self.add_reference(ident.name.clone(), KindFlags::Value);
self.add_reference(ident.name, KindFlags::Value);
// `typeof Type<Parameters>`
// ^^^^^^^^^^^
if let Some(type_parameters) = &ty.type_parameters {
Expand All @@ -140,7 +140,7 @@ impl<'a> Visit<'a> for ScopeTree<'a> {

fn visit_export_default_declaration(&mut self, decl: &ExportDefaultDeclaration<'a>) {
if let ExportDefaultDeclarationKind::Identifier(ident) = &decl.declaration {
self.add_reference(ident.name.clone(), KindFlags::All);
self.add_reference(ident.name, KindFlags::All);
} else {
walk_export_default_declaration(self, decl);
}
Expand All @@ -153,30 +153,30 @@ impl<'a> Visit<'a> for ScopeTree<'a> {
}
Declaration::FunctionDeclaration(decl) => {
if let Some(id) = decl.id.as_ref() {
self.add_binding(id.name.clone(), KindFlags::Value);
self.add_binding(id.name, KindFlags::Value);
}
}
Declaration::ClassDeclaration(decl) => {
if let Some(id) = decl.id.as_ref() {
self.add_binding(id.name.clone(), KindFlags::Value);
self.add_binding(id.name, KindFlags::Value);
}
}
Declaration::TSTypeAliasDeclaration(decl) => {
self.add_binding(decl.id.name.clone(), KindFlags::Type);
self.add_binding(decl.id.name, KindFlags::Type);
}
Declaration::TSInterfaceDeclaration(decl) => {
self.add_binding(decl.id.name.clone(), KindFlags::Type);
self.add_binding(decl.id.name, KindFlags::Type);
}
Declaration::TSEnumDeclaration(decl) => {
self.add_binding(decl.id.name.clone(), KindFlags::All);
self.add_binding(decl.id.name, KindFlags::All);
}
Declaration::TSModuleDeclaration(decl) => {
if let TSModuleDeclarationName::Identifier(ident) = &decl.id {
self.add_binding(ident.name.clone(), KindFlags::All);
self.add_binding(ident.name, KindFlags::All);
}
}
Declaration::TSImportEqualsDeclaration(decl) => {
self.add_binding(decl.id.name.clone(), KindFlags::Value);
self.add_binding(decl.id.name, KindFlags::Value);
}
}
walk_declaration(self, declaration);
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/ast_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ pub fn extract_regex_flags<'a>(
return None;
}
let flag_arg = match &args[1] {
Argument::StringLiteral(flag_arg) => flag_arg.value.clone(),
Argument::StringLiteral(flag_arg) => flag_arg.value,
Argument::TemplateLiteral(template) if template.is_no_substitution_template() => {
template.quasi().expect("no-substitution templates always have a quasi")
}
Expand Down
Loading

0 comments on commit ac4f98e

Please sign in to comment.