From 5f9ce738730c0c516b2d3d2f6025897d43860808 Mon Sep 17 00:00:00 2001 From: Tom Dohrmann Date: Sat, 30 Nov 2024 12:17:36 +0100 Subject: [PATCH 1/2] 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. --- derive/src/traits.rs | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/derive/src/traits.rs b/derive/src/traits.rs index 02a0e41..9420a1e 100644 --- a/derive/src/traits.rs +++ b/derive/src/traits.rs @@ -607,19 +607,24 @@ 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,)* } + + #[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 +716,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 +795,22 @@ 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, } + #[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)] From 34fb69612b7b307fdfe2f4052c06fc3e7e817dbd Mon Sep 17 00:00:00 2001 From: Tom Dohrmann Date: Sat, 30 Nov 2024 12:32:16 +0100 Subject: [PATCH 2/2] 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 | 68 ++++++++++++++++++++++++++------------------ 1 file changed, 40 insertions(+), 28 deletions(-) diff --git a/derive/src/traits.rs b/derive/src/traits.rs index 9420a1e..2093561 100644 --- a/derive/src/traits.rs +++ b/derive/src/traits.rs @@ -617,14 +617,17 @@ fn generate_checked_bit_pattern_struct( #(#field_name: <#field_ty as #crate_name::CheckedBitPattern>::Bits,)* } - #[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) + #[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; @@ -801,15 +804,18 @@ fn generate_checked_bit_pattern_enum_with_fields( payload: #variants_union_ident, } - #[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) + #[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)] @@ -818,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)* }, @@ -941,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)* },