From 227d7fe6e195587c15afd8bf334d0e5957f247ac Mon Sep 17 00:00:00 2001 From: Tom Dohrmann Date: Sat, 30 Nov 2024 17:07:02 +0100 Subject: [PATCH] hide `unexpected_cfgs` warnings in proc-macro generated code (#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. --- derive/src/traits.rs | 65 ++++++++++++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 21 deletions(-) diff --git a/derive/src/traits.rs b/derive/src/traits.rs index 02a0e41..2093561 100644 --- a/derive/src/traits.rs +++ b/derive/src/traits.rs @@ -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; @@ -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(_)) => { @@ -793,13 +798,25 @@ 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)] @@ -807,13 +824,16 @@ fn generate_checked_bit_pattern_enum_with_fields( #(#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)* }, @@ -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)* },