cbindgen icon indicating copy to clipboard operation
cbindgen copied to clipboard

Can't handle bitflags bits method calls correctly

Open CorrM opened this issue 8 months ago • 5 comments

bitflags! {
    #[repr(transparent)]
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
    pub struct MemoryProtection: u32 {
        const NONE = 0;
        /// Execute
        const X = 1 << 0;
        /// Read
        const R = 1 << 1;
        /// Write
        const W = 1 << 2;
        /// Copy
        const C = 1 << 3;
        /// Execute and Read
        const XR = Self::X.bits() | Self::R.bits();
        /// Execute, Read and Write
        const XRW = Self::X.bits() | Self::R.bits() | Self::W.bits();
        /// Execute, Write and Copy
        const XWC = Self::X.bits() | Self::W.bits() | Self::C.bits();
        /// Read and Write
        const RW = Self::R.bits() | Self::W.bits();
        /// Write and Copy
        const WC = Self::W.bits() | Self::C.bits();
        /// Read and Copy
        const RC = Self::R.bits() | Self::C.bits();
        /// Read, Write and Copy
        const RWC = Self::R.bits() | Self::W.bits() | Self::C.bits();
    }
}

Generates:

using MemoryProtection = uint32_t;
constexpr static const MemoryProtection MemoryProtection_NONE = (uint32_t)0;
/// Execute
constexpr static const MemoryProtection MemoryProtection_X = (uint32_t)(1 << 0);
/// Read
constexpr static const MemoryProtection MemoryProtection_R = (uint32_t)(1 << 1);
/// Write
constexpr static const MemoryProtection MemoryProtection_W = (uint32_t)(1 << 2);
/// Copy
constexpr static const MemoryProtection MemoryProtection_C = (uint32_t)(1 << 3);
/// Execute and Read
constexpr static const MemoryProtection MemoryProtection_XR = (uint32_t)((MemoryProtection_X).bits | (MemoryProtection_R).bits);
/// Execute, Read and Write
constexpr static const MemoryProtection MemoryProtection_XRW = (uint32_t)(((MemoryProtection_X).bits | (MemoryProtection_R).bits) | (MemoryProtection_W).bits);
/// Execute, Write and Copy
constexpr static const MemoryProtection MemoryProtection_XWC = (uint32_t)(((MemoryProtection_X).bits | (MemoryProtection_W).bits) | (MemoryProtection_C).bits);
/// Read and Write
constexpr static const MemoryProtection MemoryProtection_RW = (uint32_t)((MemoryProtection_R).bits | (MemoryProtection_W).bits);
/// Write and Copy
constexpr static const MemoryProtection MemoryProtection_WC = (uint32_t)((MemoryProtection_W).bits | (MemoryProtection_C).bits);
/// Read and Copy
constexpr static const MemoryProtection MemoryProtection_RC = (uint32_t)((MemoryProtection_R).bits | (MemoryProtection_C).bits);
/// Read, Write and Copy
constexpr static const MemoryProtection MemoryProtection_RWC = (uint32_t)(((MemoryProtection_R).bits | (MemoryProtection_W).bits) | (MemoryProtection_C).bits);

The Problem: Prints .bits

mostly related to #881

CorrM avatar Aug 02 '25 04:08 CorrM

This is because of the repr(transparent) right? repr(C) works right?

emilio avatar Aug 02 '25 10:08 emilio

This is because of the repr(transparent) right? repr(C) works right?

Yup, that was the case, using repr(C) works. but repr(transparent) should be supported correct?

CorrM avatar Aug 02 '25 18:08 CorrM

@emilio

CorrM avatar Aug 07 '25 22:08 CorrM

Well, yeah, I'd consider this a bug. But it's a more general bug, not to do with bitflags in particular. Any repr(transparent) struct would hit this.

emilio avatar Aug 08 '25 08:08 emilio

@emilio can you assign me this issue? I would love to solve it.

Aditya-PS-05 avatar Oct 22 '25 15:10 Aditya-PS-05