microsoft / igvm

MIT License
81 stars 17 forks source link

open_enum doesn't respect cfg(feature) because of emitted debug impl #19

Open chris-oo opened 6 months ago

chris-oo commented 6 months ago

Gating say an enum variant behind the unstable feature causes open_enum to gate the derived enum correctly, but not the debug impl. This blocks us from having nice intra-doc links because it's quite annoying to then gate any doclink comments in that enum variant.

For example:

#[open_enum]
#[derive(AsBytes, FromBytes, FromZeroes, Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u32)]
pub enum IgvmVariableHeaderType {
    ....
    #[cfg(feature = "unstable")]
    #[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
    IGVM_VHT_MEMORY_STATE_PARAMETER = 0x313,
}

becomes

    #[cfg(feature = "unstable")]
    #[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
    pub const IGVM_VHT_MEMORY_STATE_PARAMETER: IgvmVariableHeaderType = IgvmVariableHeaderType(787);
    ....
impl ::core::fmt::Debug for IgvmVariableHeaderType {
    fn fmt(&self, fmt: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        #![allow(unreachable_patterns)]
        let s = match *self {
        ....    
            Self::IGVM_VHT_DEVICE_TREE => stringify!(IGVM_VHT_DEVICE_TREE),
            Self::IGVM_VHT_MEMORY_STATE_PARAMETER => stringify!(IGVM_VHT_MEMORY_STATE_PARAMETER),
        ....

which doesn't compile because the type must also be cfg(feature) attr-ed in the emitted debug impl

chris-oo commented 6 months ago

Requires a fix in open_enum which I'll take a look at.