Skip to content

Commit

Permalink
hide unexpected_cfgs warnings in proc-macro generated code (Lokatho…
Browse files Browse the repository at this point in the history
…r#287)

* replace #[derive(Debug)] with manual impls

The new impls should be identical, but they have the advantage that we
can move them around. See the next patch for why this is useful.

* allow unexpected_cfgs lint

AFAICT it's not possible to set `#[allow(unexpected_cfgs)]` on a
`#[cfg(...)]` or `#[cfg_attr(...)]` directly. Instead, we have to crate
a new scope and use `#[allow(...)]` on that. This patch wraps all uses
of `target_arch = "spirv"` in an unnamed constant scope and uses
`#[allow(unexpected_cfgs)]` on it.
  • Loading branch information
Freax13 authored Nov 30, 2024
1 parent 9bf993b commit 227d7fe
Showing 1 changed file with 44 additions and 21 deletions.
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

0 comments on commit 227d7fe

Please sign in to comment.