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

Fix incorrect fmt::Pointer implementations attempt two #381

Merged
merged 6 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 6 additions & 1 deletion impl/src/fmt/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,12 @@ impl<'a> Expansion<'a> {
fn generate_body(&self) -> syn::Result<TokenStream> {
if let Some(fmt) = &self.attr.fmt {
return Ok(if let Some((expr, trait_ident)) = fmt.transparent_call() {
let expr = if self.fields.fmt_args_idents().any(|field| expr == field) {
let expr = if self
.fields
.fmt_args_idents()
.into_iter()
.any(|field| expr == field)
{
quote! { #expr }
} else {
quote! { &(#expr) }
Expand Down
17 changes: 10 additions & 7 deletions impl/src/fmt/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,13 +282,16 @@ impl<'a> Expansion<'a> {

quote! { &derive_more::core::format_args!(#fmt, #(#deref_args),*) }
} else if let Some((expr, trait_ident)) = fmt.transparent_call() {
let expr =
if self.fields.fmt_args_idents().any(|field| expr == field)
{
quote! { #expr }
} else {
quote! { &(#expr) }
};
let expr = if self
.fields
.fmt_args_idents()
.into_iter()
.any(|field| expr == field)
{
quote! { #expr }
} else {
quote! { &(#expr) }
};

quote! {
derive_more::core::fmt::#trait_ident::fmt(#expr, __derive_more_f)
Expand Down
24 changes: 15 additions & 9 deletions impl/src/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,13 +290,16 @@ impl FmtAttribute {
})
.collect::<Vec<_>>();

fields.fmt_args_idents().filter_map(move |field_name| {
(used_args.iter().any(|arg| field_name == arg)
&& !self.args.iter().any(|arg| {
arg.alias.as_ref().map_or(false, |(n, _)| n == &field_name)
}))
.then(|| quote! { #field_name = *#field_name })
})
fields
.fmt_args_idents()
.into_iter()
.filter_map(move |field_name| {
(used_args.iter().any(|arg| field_name == arg)
&& !self.args.iter().any(|arg| {
arg.alias.as_ref().map_or(false, |(n, _)| n == &field_name)
}))
.then(|| quote! { #field_name = *#field_name })
})
}

/// Errors in case legacy syntax is encountered: `fmt = "...", (arg),*`.
Expand Down Expand Up @@ -671,14 +674,17 @@ trait FieldsExt {
/// [`FmtAttribute`] as [`FmtArgument`]s or named [`Placeholder`]s.
///
/// [`syn::Ident`]: struct@syn::Ident
fn fmt_args_idents(&self) -> impl Iterator<Item = syn::Ident> + '_;
// TODO: Return `impl Iterator<Item = syn::Ident> + '_` once MSRV is bumped up to 1.75 or
// higher.
tyranron marked this conversation as resolved.
Show resolved Hide resolved
fn fmt_args_idents(&self) -> Vec<syn::Ident>;
}

impl FieldsExt for syn::Fields {
fn fmt_args_idents(&self) -> impl Iterator<Item = syn::Ident> + '_ {
fn fmt_args_idents(&self) -> Vec<syn::Ident> {
self.iter()
.enumerate()
.map(|(i, f)| f.ident.clone().unwrap_or_else(|| format_ident!("_{i}")))
.collect()
}
}

Expand Down