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

Avoid name clashes with field names #15

Merged
merged 9 commits into from
Jan 9, 2024
76 changes: 37 additions & 39 deletions src/trait_handlers/clone/clone_enum.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use quote::quote;
use quote::{format_ident, quote};
use syn::{punctuated::Punctuated, Data, DeriveInput, Field, Fields, Ident, Meta, Type, Variant};

use super::models::{FieldAttribute, FieldAttributeBuilder, TypeAttributeBuilder};
Expand Down Expand Up @@ -74,7 +74,7 @@
if variants.is_empty() {
if !contains_copy {
clone_token_stream.extend(quote!(unreachable!()));
clone_from_token_stream.extend(quote!(let _ = v_source_;));
clone_from_token_stream.extend(quote!(let _ = source;));
}
} else {
let mut clone_variants_token_stream = proc_macro2::TokenStream::new();
Expand All @@ -90,58 +90,56 @@
});
clone_from_variants_token_stream.extend(quote! {
Self::#variant_ident => {
if let Self::#variant_ident = v_source_ {
if let Self::#variant_ident = source {
// same
} else {
*self = ::core::clone::Clone::clone(v_source_);
*self = ::core::clone::Clone::clone(source);
}
},
});
},
Fields::Named(_) => {
let mut pattern_token_stream = proc_macro2::TokenStream::new();
let mut pattern2_token_stream = proc_macro2::TokenStream::new();
let mut fields_token_stream = proc_macro2::TokenStream::new();
let mut body_token_stream = proc_macro2::TokenStream::new();
let mut pattern_src_token_stream = proc_macro2::TokenStream::new();
let mut pattern_dst_token_stream = proc_macro2::TokenStream::new();
let mut cl_fields_token_stream = proc_macro2::TokenStream::new();
let mut cf_body_token_stream = proc_macro2::TokenStream::new();

for (field, field_attribute) in variant_fields {
let field_name = field.ident.as_ref().unwrap();

pattern_token_stream.extend(quote!(#field_name,));
let field_name_real = field.ident.as_ref().unwrap();
let field_name_src = format_ident!("_s_{}", field_name_real);

Check warning on line 109 in src/trait_handlers/clone/clone_enum.rs

View workflow job for this annotation

GitHub Actions / rustfmt

Diff in /home/runner/work/educe/educe/src/trait_handlers/clone/clone_enum.rs
let field_name_dst = format_ident!("_d_{}", field_name_real);

let field_name2: Ident =
syn::parse_str(&format!("_{}", field_name)).unwrap();

pattern2_token_stream.extend(quote!(#field_name: #field_name2,));
pattern_src_token_stream.extend(quote!(#field_name_real: #field_name_src,));
pattern_dst_token_stream.extend(quote!(#field_name_real: #field_name_dst,));

if let Some(clone) = field_attribute.method.as_ref() {
fields_token_stream.extend(quote! {
#field_name: #clone(#field_name),
cl_fields_token_stream.extend(quote! {
#field_name_real: #clone(#field_name_src),

Check warning on line 117 in src/trait_handlers/clone/clone_enum.rs

View workflow job for this annotation

GitHub Actions / rustfmt

Diff in /home/runner/work/educe/educe/src/trait_handlers/clone/clone_enum.rs
});
body_token_stream
.extend(quote!(*#field_name = #clone(#field_name2);));
cf_body_token_stream
.extend(quote!(*#field_name_dst = #clone(#field_name_src);));
} else {
clone_types.push(&field.ty);

fields_token_stream.extend(quote! {
#field_name: ::core::clone::Clone::clone(#field_name),
cl_fields_token_stream.extend(quote! {
#field_name_real: ::core::clone::Clone::clone(#field_name_src),
});
body_token_stream.extend(
quote!( ::core::clone::Clone::clone_from(#field_name, #field_name2); ),
cf_body_token_stream.extend(
quote!( ::core::clone::Clone::clone_from(#field_name_dst, #field_name_src); ),
);
}
}

clone_variants_token_stream.extend(quote! {
Self::#variant_ident { #pattern_token_stream } => Self::#variant_ident { #fields_token_stream },
Self::#variant_ident { #pattern_src_token_stream } => Self::#variant_ident { #cl_fields_token_stream },
});

clone_from_variants_token_stream.extend(quote! {
Self::#variant_ident { #pattern_token_stream } => {
if let Self::#variant_ident { #pattern2_token_stream } = v_source_ {
#body_token_stream
Self::#variant_ident { #pattern_dst_token_stream } => {
if let Self::#variant_ident { #pattern_src_token_stream } = source {
#cf_body_token_stream
} else {
*self = ::core::clone::Clone::clone(v_source_);
*self = ::core::clone::Clone::clone(source);
}
},
});
Expand Down Expand Up @@ -182,18 +180,18 @@
}

clone_variants_token_stream.extend(quote! {
Self::#variant_ident ( #pattern_token_stream ) => Self::#variant_ident ( #fields_token_stream ),
});
Self::#variant_ident ( #pattern_token_stream ) => Self::#variant_ident ( #fields_token_stream ),
});

clone_from_variants_token_stream.extend(quote! {
Self::#variant_ident ( #pattern_token_stream ) => {
if let Self::#variant_ident ( #pattern2_token_stream ) = v_source_ {
#body_token_stream
} else {
*self = ::core::clone::Clone::clone(v_source_);
}
},
});
Self::#variant_ident ( #pattern_token_stream ) => {
if let Self::#variant_ident ( #pattern2_token_stream ) = source {
#body_token_stream
} else {
*self = ::core::clone::Clone::clone(source);
}
},
});
},
}
}
Expand Down Expand Up @@ -231,7 +229,7 @@
} else {
Some(quote! {
#[inline]
fn clone_from(&mut self, v_source_: &Self) {
fn clone_from(&mut self, source: &Self) {
#clone_from_token_stream
}
})
Expand Down
20 changes: 10 additions & 10 deletions src/trait_handlers/clone/clone_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ impl TraitHandler for CloneStructHandler {
Fields::Unit => {
if !contains_copy {
clone_token_stream.extend(quote!(Self));
clone_from_token_stream.extend(quote!(let _ = v_source_;));
clone_from_token_stream.extend(quote!(let _ = source;));
}
},
Fields::Named(_) => {
let mut fields_token_stream = proc_macro2::TokenStream::new();
let mut clone_from_body_token_stream = proc_macro2::TokenStream::new();

if fields.is_empty() {
clone_from_body_token_stream.extend(quote!(let _ = v_source_;));
clone_from_body_token_stream.extend(quote!(let _ = source;));
} else {
for (field, field_attribute) in fields {
let field_name = field.ident.as_ref().unwrap();
Expand All @@ -73,7 +73,7 @@ impl TraitHandler for CloneStructHandler {
});

clone_from_body_token_stream.extend(
quote!(self.#field_name = #clone(&v_source_.#field_name);),
quote!(self.#field_name = #clone(&source.#field_name);),
);
} else {
clone_types.push(&field.ty);
Expand All @@ -83,8 +83,8 @@ impl TraitHandler for CloneStructHandler {
});

clone_from_body_token_stream.extend(
quote!( ::core::clone::Clone::clone_from(&mut self.#field_name, &v_source_.#field_name); ),
);
quote!( ::core::clone::Clone::clone_from(&mut self.#field_name, &source.#field_name); ),
);
}
}
}
Expand All @@ -104,7 +104,7 @@ impl TraitHandler for CloneStructHandler {
let mut clone_from_body_token_stream = proc_macro2::TokenStream::new();

if fields.is_empty() {
clone_from_body_token_stream.extend(quote!(let _ = v_source_;));
clone_from_body_token_stream.extend(quote!(let _ = source;));
} else {
for (index, (field, field_attribute)) in fields.into_iter().enumerate() {
let field_name = Index::from(index);
Expand All @@ -113,7 +113,7 @@ impl TraitHandler for CloneStructHandler {
fields_token_stream.extend(quote!(#clone(&self.#field_name),));

clone_from_body_token_stream.extend(
quote!(self.#field_name = #clone(&v_source_.#field_name);),
quote!(self.#field_name = #clone(&source.#field_name);),
);
} else {
clone_types.push(&field.ty);
Expand All @@ -123,8 +123,8 @@ impl TraitHandler for CloneStructHandler {
);

clone_from_body_token_stream.extend(
quote!( ::core::clone::Clone::clone_from(&mut self.#field_name, &v_source_.#field_name); ),
);
quote!( ::core::clone::Clone::clone_from(&mut self.#field_name, &source.#field_name); ),
);
}
}
}
Expand Down Expand Up @@ -154,7 +154,7 @@ impl TraitHandler for CloneStructHandler {
} else {
Some(quote! {
#[inline]
fn clone_from(&mut self, v_source_: &Self) {
fn clone_from(&mut self, source: &Self) {
#clone_from_token_stream
}
})
Expand Down
10 changes: 5 additions & 5 deletions src/trait_handlers/debug/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ pub(crate) fn create_debug_map_builder() -> proc_macro2::TokenStream {

impl ::core::fmt::Debug for RawString {
#[inline]
fn fmt(&self, v_formatter_: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
v_formatter_.write_str(self.0)
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
f.write_str(self.0)
}
}

let mut builder = v_formatter_.debug_map();
let mut builder = f.debug_map();
)
}

Expand Down Expand Up @@ -52,8 +52,8 @@ pub(crate) fn create_format_arg(

impl<'a, #filtered_params> ::core::fmt::Debug for MyDebug<'a, #filtered_params> {
#[inline]
fn fmt(&self, v_formatter_: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
#format_method(self.0, v_formatter_)
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
#format_method(self.0, f)
}
}

Expand Down
Loading
Loading