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

hide unexpected_cfgs warnings in proc-macro generated code #287

Merged
merged 2 commits into from
Nov 30, 2024
Merged
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
65 changes: 44 additions & 21 deletions derive/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -607,19 +607,27 @@ fn generate_checked_bit_pattern_struct(
let field_name = &field_names[..];
let field_ty = &field_tys[..];

let derive_dbg =
quote!(#[cfg_attr(not(target_arch = "spirv"), derive(Debug))]);

Ok((
quote! {
#[doc = #GENERATED_TYPE_DOCUMENTATION]
#repr
#[derive(Clone, Copy, #crate_name::AnyBitPattern)]
#derive_dbg
#[allow(missing_docs)]
pub struct #bits_ty {
#(#field_name: <#field_ty as #crate_name::CheckedBitPattern>::Bits,)*
}

#[allow(unexpected_cfgs)]
const _: () = {
#[cfg(not(target_arch = "spirv"))]
impl ::core::fmt::Debug for #bits_ty {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
let mut debug_struct = ::core::fmt::Formatter::debug_struct(f, ::core::stringify!(#bits_ty));
#(::core::fmt::DebugStruct::field(&mut debug_struct, ::core::stringify!(#field_name), &self.#field_name);)*
::core::fmt::DebugStruct::finish(&mut debug_struct)
}
}
};
},
quote! {
type Bits = #bits_ty;
Expand Down Expand Up @@ -711,9 +719,6 @@ fn generate_checked_bit_pattern_enum_with_fields(
let representation = get_repr(&input.attrs)?;
let vis = &input.vis;

let derive_dbg =
quote!(#[cfg_attr(not(target_arch = "spirv"), derive(Debug))]);

match representation.repr {
Repr::Rust => unreachable!(),
repr @ (Repr::C | Repr::CWithDiscriminant(_)) => {
Expand Down Expand Up @@ -793,27 +798,42 @@ fn generate_checked_bit_pattern_enum_with_fields(
quote! {
#[doc = #GENERATED_TYPE_DOCUMENTATION]
#[derive(::core::clone::Clone, ::core::marker::Copy, #crate_name::AnyBitPattern)]
#derive_dbg
#bits_repr
#vis struct #bits_ty_ident {
tag: #integer,
payload: #variants_union_ident,
}

#[allow(unexpected_cfgs)]
const _: () = {
#[cfg(not(target_arch = "spirv"))]
impl ::core::fmt::Debug for #bits_ty_ident {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
let mut debug_struct = ::core::fmt::Formatter::debug_struct(f, ::core::stringify!(#bits_ty_ident));
::core::fmt::DebugStruct::field(&mut debug_struct, "tag", &self.tag);
::core::fmt::DebugStruct::field(&mut debug_struct, "payload", &self.payload);
::core::fmt::DebugStruct::finish(&mut debug_struct)
}
}
};

#[derive(::core::clone::Clone, ::core::marker::Copy, #crate_name::AnyBitPattern)]
#[repr(C)]
#[allow(non_snake_case)]
#vis union #variants_union_ident {
#(#union_fields,)*
}

#[cfg(not(target_arch = "spirv"))]
impl ::core::fmt::Debug for #variants_union_ident {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
let mut debug_struct = ::core::fmt::Formatter::debug_struct(f, ::core::stringify!(#variants_union_ident));
::core::fmt::DebugStruct::finish_non_exhaustive(&mut debug_struct)
#[allow(unexpected_cfgs)]
const _: () = {
#[cfg(not(target_arch = "spirv"))]
impl ::core::fmt::Debug for #variants_union_ident {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
let mut debug_struct = ::core::fmt::Formatter::debug_struct(f, ::core::stringify!(#variants_union_ident));
::core::fmt::DebugStruct::finish_non_exhaustive(&mut debug_struct)
}
}
}
};

#(#variant_struct_definitions)*
},
Expand Down Expand Up @@ -930,14 +950,17 @@ fn generate_checked_bit_pattern_enum_with_fields(
#(#union_fields,)*
}

#[cfg(not(target_arch = "spirv"))]
impl ::core::fmt::Debug for #bits_ty_ident {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
let mut debug_struct = ::core::fmt::Formatter::debug_struct(f, ::core::stringify!(#bits_ty_ident));
::core::fmt::DebugStruct::field(&mut debug_struct, "tag", unsafe { &self.__tag });
::core::fmt::DebugStruct::finish_non_exhaustive(&mut debug_struct)
#[allow(unexpected_cfgs)]
const _: () = {
#[cfg(not(target_arch = "spirv"))]
impl ::core::fmt::Debug for #bits_ty_ident {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
let mut debug_struct = ::core::fmt::Formatter::debug_struct(f, ::core::stringify!(#bits_ty_ident));
::core::fmt::DebugStruct::field(&mut debug_struct, "tag", unsafe { &self.__tag });
::core::fmt::DebugStruct::finish_non_exhaustive(&mut debug_struct)
}
}
}
};

#(#variant_struct_definitions)*
},
Expand Down