Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(ast_tools): improve correctness and performance of idents #8895

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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tasks/ast_tools/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ indexmap = { workspace = true }
itertools = { workspace = true }
lazy_static = { workspace = true }
oxc_index = { workspace = true }
phf = { workspace = true, features = ["macros"] }
prettyplease = { workspace = true }
proc-macro2 = { workspace = true }
quote = { workspace = true }
Expand Down
5 changes: 3 additions & 2 deletions tasks/ast_tools/src/derives/clone_in.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
//! Derive for `CloneIn` trait.

use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use quote::quote;
use syn::Ident;

use crate::{
schema::{Def, EnumDef, FieldDef, Schema, StructDef, TypeDef},
utils::create_safe_ident,
Result,
};

Expand Down Expand Up @@ -151,7 +152,7 @@ fn generate_impl(
has_lifetime: bool,
uses_allocator: bool,
) -> TokenStream {
let alloc_ident = format_ident!("{}", if uses_allocator { "allocator" } else { "_" });
let alloc_ident = create_safe_ident(if uses_allocator { "allocator" } else { "_" });

if has_lifetime {
quote! {
Expand Down
21 changes: 11 additions & 10 deletions tasks/ast_tools/src/derives/content_eq.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
//! Derive for `ContentEq` trait.

use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use quote::quote;

use crate::{
schema::{Def, EnumDef, Schema, StructDef, TypeDef},
utils::create_safe_ident,
Result,
};

Expand Down Expand Up @@ -69,11 +70,11 @@ impl Derive for DeriveContentEq {
}

fn derive_struct(struct_def: &StructDef, schema: &Schema) -> TokenStream {
let mut other_name = "other";
let mut uses_other = true;

let body = if struct_def.content_eq.skip {
// Struct has `#[content_eq(skip)]` attr. So `content_eq` always returns true.
other_name = "_";
uses_other = false;
quote!(true)
} else {
let fields = struct_def
Expand All @@ -96,20 +97,20 @@ fn derive_struct(struct_def: &StructDef, schema: &Schema) -> TokenStream {
let mut body = quote!( #(#fields)&&* );
if body.is_empty() {
body = quote!(true);
other_name = "_";
uses_other = false;
};
body
};

generate_impl(&struct_def.ty_anon(schema), other_name, &body)
generate_impl(&struct_def.ty_anon(schema), &body, uses_other)
}

fn derive_enum(enum_def: &EnumDef, schema: &Schema) -> TokenStream {
let mut other_name = "other";
let mut uses_other = true;

let body = if enum_def.content_eq.skip {
// Enum has `#[content_eq(skip)]` attr. So `content_eq` always returns true.
other_name = "_";
uses_other = false;
quote!(true)
} else if enum_def.is_fieldless() {
// We assume fieldless enums implement `PartialEq`
Expand All @@ -132,11 +133,11 @@ fn derive_enum(enum_def: &EnumDef, schema: &Schema) -> TokenStream {
}
};

generate_impl(&enum_def.ty_anon(schema), other_name, &body)
generate_impl(&enum_def.ty_anon(schema), &body, uses_other)
}

fn generate_impl(ty: &TokenStream, other_name: &str, body: &TokenStream) -> TokenStream {
let other_ident = format_ident!("{other_name}");
fn generate_impl(ty: &TokenStream, body: &TokenStream, uses_other: bool) -> TokenStream {
let other_ident = create_safe_ident(if uses_other { "other" } else { "_" });
quote! {
impl ContentEq for #ty {
fn content_eq(&self, #other_ident: &Self) -> bool {
Expand Down
37 changes: 17 additions & 20 deletions tasks/ast_tools/src/derives/get_span.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
//! Derive for `GetSpan` trait.

use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use quote::quote;
use syn::Ident;

use crate::{
schema::{Def, EnumDef, Schema, StructDef},
utils::create_safe_ident,
Result,
};

Expand Down Expand Up @@ -54,15 +55,17 @@ impl Derive for DeriveGetSpan {
}

fn derive(&self, type_def: StructOrEnum, schema: &Schema) -> TokenStream {
let trait_ident = create_safe_ident("GetSpan");
let method_ident = create_safe_ident("span");
let self_ty = quote!(&self);
let result_ty = quote!(Span);
let result_expr = quote!(self.span);
let reference = quote!( & );

derive_type(
type_def,
"GetSpan",
"span",
&trait_ident,
&method_ident,
&self_ty,
&result_ty,
&result_expr,
Expand Down Expand Up @@ -96,15 +99,17 @@ impl Derive for DeriveGetSpanMut {
}

fn derive(&self, type_def: StructOrEnum, schema: &Schema) -> TokenStream {
let trait_ident = create_safe_ident("GetSpanMut");
let method_ident = create_safe_ident("span_mut");
let self_ty = quote!(&mut self);
let result_ty = quote!(&mut Span);
let result_expr = quote!(&mut self.span);
let reference = quote!( &mut );

derive_type(
type_def,
"GetSpanMut",
"span_mut",
&trait_ident,
&method_ident,
&self_ty,
&result_ty,
&result_expr,
Expand All @@ -118,36 +123,28 @@ impl Derive for DeriveGetSpanMut {
#[expect(clippy::too_many_arguments)]
fn derive_type(
type_def: StructOrEnum,
trait_name: &str,
method_name: &str,
trait_ident: &Ident,
method_ident: &Ident,
self_ty: &TokenStream,
result_ty: &TokenStream,
result_expr: &TokenStream,
reference: &TokenStream,
schema: &Schema,
) -> TokenStream {
let trait_ident = format_ident!("{trait_name}");
let method_ident = format_ident!("{method_name}");
match type_def {
StructOrEnum::Struct(struct_def) => derive_struct(
struct_def,
&trait_ident,
&method_ident,
trait_ident,
method_ident,
self_ty,
result_ty,
result_expr,
reference,
schema,
),
StructOrEnum::Enum(enum_def) => derive_enum(
enum_def,
&trait_ident,
&method_ident,
self_ty,
result_ty,
reference,
schema,
),
StructOrEnum::Enum(enum_def) => {
derive_enum(enum_def, trait_ident, method_ident, self_ty, result_ty, reference, schema)
}
}
}

Expand Down
9 changes: 5 additions & 4 deletions tasks/ast_tools/src/generators/ast_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use syn::Ident;
use crate::{
output::{output_path, Output},
schema::{Def, EnumDef, FieldDef, Schema, StructDef, TypeDef, VariantDef},
utils::is_reserved_name,
utils::{create_safe_ident, is_reserved_name},
Codegen, Generator, AST_CRATE_PATH,
};

Expand Down Expand Up @@ -276,11 +276,11 @@ fn get_struct_params<'s>(
TypeDef::Primitive(primitive_def) => match primitive_def.name() {
"Atom" if !has_atom_generic => {
has_atom_generic = true;
Some(format_ident!("A"))
Some(create_safe_ident("A"))
}
"&str" if !has_str_generic => {
has_str_generic = true;
Some(format_ident!("S"))
Some(create_safe_ident("S"))
}
_ => None,
},
Expand Down Expand Up @@ -450,7 +450,8 @@ fn struct_builder_name(snake_name: &str, does_alloc: bool) -> Ident {
} else if is_reserved_name(snake_name) {
format_ident!("{snake_name}_")
} else {
format_ident!("{snake_name}")
// We just checked name is not a reserved word
create_safe_ident(snake_name)
}
}

Expand Down
Loading
Loading