Skip to content

Commit 227d7fe

Browse files
authored
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.
1 parent 9bf993b commit 227d7fe

File tree

1 file changed

+44
-21
lines changed

1 file changed

+44
-21
lines changed

derive/src/traits.rs

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -607,19 +607,27 @@ fn generate_checked_bit_pattern_struct(
607607
let field_name = &field_names[..];
608608
let field_ty = &field_tys[..];
609609

610-
let derive_dbg =
611-
quote!(#[cfg_attr(not(target_arch = "spirv"), derive(Debug))]);
612-
613610
Ok((
614611
quote! {
615612
#[doc = #GENERATED_TYPE_DOCUMENTATION]
616613
#repr
617614
#[derive(Clone, Copy, #crate_name::AnyBitPattern)]
618-
#derive_dbg
619615
#[allow(missing_docs)]
620616
pub struct #bits_ty {
621617
#(#field_name: <#field_ty as #crate_name::CheckedBitPattern>::Bits,)*
622618
}
619+
620+
#[allow(unexpected_cfgs)]
621+
const _: () = {
622+
#[cfg(not(target_arch = "spirv"))]
623+
impl ::core::fmt::Debug for #bits_ty {
624+
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
625+
let mut debug_struct = ::core::fmt::Formatter::debug_struct(f, ::core::stringify!(#bits_ty));
626+
#(::core::fmt::DebugStruct::field(&mut debug_struct, ::core::stringify!(#field_name), &self.#field_name);)*
627+
::core::fmt::DebugStruct::finish(&mut debug_struct)
628+
}
629+
}
630+
};
623631
},
624632
quote! {
625633
type Bits = #bits_ty;
@@ -711,9 +719,6 @@ fn generate_checked_bit_pattern_enum_with_fields(
711719
let representation = get_repr(&input.attrs)?;
712720
let vis = &input.vis;
713721

714-
let derive_dbg =
715-
quote!(#[cfg_attr(not(target_arch = "spirv"), derive(Debug))]);
716-
717722
match representation.repr {
718723
Repr::Rust => unreachable!(),
719724
repr @ (Repr::C | Repr::CWithDiscriminant(_)) => {
@@ -793,27 +798,42 @@ fn generate_checked_bit_pattern_enum_with_fields(
793798
quote! {
794799
#[doc = #GENERATED_TYPE_DOCUMENTATION]
795800
#[derive(::core::clone::Clone, ::core::marker::Copy, #crate_name::AnyBitPattern)]
796-
#derive_dbg
797801
#bits_repr
798802
#vis struct #bits_ty_ident {
799803
tag: #integer,
800804
payload: #variants_union_ident,
801805
}
802806

807+
#[allow(unexpected_cfgs)]
808+
const _: () = {
809+
#[cfg(not(target_arch = "spirv"))]
810+
impl ::core::fmt::Debug for #bits_ty_ident {
811+
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
812+
let mut debug_struct = ::core::fmt::Formatter::debug_struct(f, ::core::stringify!(#bits_ty_ident));
813+
::core::fmt::DebugStruct::field(&mut debug_struct, "tag", &self.tag);
814+
::core::fmt::DebugStruct::field(&mut debug_struct, "payload", &self.payload);
815+
::core::fmt::DebugStruct::finish(&mut debug_struct)
816+
}
817+
}
818+
};
819+
803820
#[derive(::core::clone::Clone, ::core::marker::Copy, #crate_name::AnyBitPattern)]
804821
#[repr(C)]
805822
#[allow(non_snake_case)]
806823
#vis union #variants_union_ident {
807824
#(#union_fields,)*
808825
}
809826

810-
#[cfg(not(target_arch = "spirv"))]
811-
impl ::core::fmt::Debug for #variants_union_ident {
812-
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
813-
let mut debug_struct = ::core::fmt::Formatter::debug_struct(f, ::core::stringify!(#variants_union_ident));
814-
::core::fmt::DebugStruct::finish_non_exhaustive(&mut debug_struct)
827+
#[allow(unexpected_cfgs)]
828+
const _: () = {
829+
#[cfg(not(target_arch = "spirv"))]
830+
impl ::core::fmt::Debug for #variants_union_ident {
831+
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
832+
let mut debug_struct = ::core::fmt::Formatter::debug_struct(f, ::core::stringify!(#variants_union_ident));
833+
::core::fmt::DebugStruct::finish_non_exhaustive(&mut debug_struct)
834+
}
815835
}
816-
}
836+
};
817837

818838
#(#variant_struct_definitions)*
819839
},
@@ -930,14 +950,17 @@ fn generate_checked_bit_pattern_enum_with_fields(
930950
#(#union_fields,)*
931951
}
932952

933-
#[cfg(not(target_arch = "spirv"))]
934-
impl ::core::fmt::Debug for #bits_ty_ident {
935-
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
936-
let mut debug_struct = ::core::fmt::Formatter::debug_struct(f, ::core::stringify!(#bits_ty_ident));
937-
::core::fmt::DebugStruct::field(&mut debug_struct, "tag", unsafe { &self.__tag });
938-
::core::fmt::DebugStruct::finish_non_exhaustive(&mut debug_struct)
953+
#[allow(unexpected_cfgs)]
954+
const _: () = {
955+
#[cfg(not(target_arch = "spirv"))]
956+
impl ::core::fmt::Debug for #bits_ty_ident {
957+
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
958+
let mut debug_struct = ::core::fmt::Formatter::debug_struct(f, ::core::stringify!(#bits_ty_ident));
959+
::core::fmt::DebugStruct::field(&mut debug_struct, "tag", unsafe { &self.__tag });
960+
::core::fmt::DebugStruct::finish_non_exhaustive(&mut debug_struct)
961+
}
939962
}
940-
}
963+
};
941964

942965
#(#variant_struct_definitions)*
943966
},

0 commit comments

Comments
 (0)