rust-lang / rust-lang/rust-bindgen

Broken bitfields behavior

Open
#1,998 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
5.3k
Forks
829
Avg merge
1d 1h
Merged PRs (30d)
15

Description

Bindgen up to 0.55.1 generates the correct bitflags for

typedef enum
{
	PANEL_DISABLED          = 0,          /**< PANEL is completely off */
	PANEL_ENABLED           = 1 << 1,     /**< PANEL is on, if any other flag is set panel is treated as active */
    PANEL_EVENT_NO_HANDLING = 1 << 2,      /**< PANEL is on and could be drawn but it does not handle pointer events,
	                                        * it is good to use in pair with SetManualPanelUpdates(int enable, iv_panelupdateshandler handler) 
	                                        * @see SetManualPanelUpdates(int enable, iv_panelupdateshandler handler) 
	                                        */
    PANEL_NO_FB_OFFSET = 1 << 3,
}

using .bitfield_enum("PanelType"), that looks like

impl PanelType {
    pub const DISABLED: PanelType = PanelType(0);
}
impl PanelType {
    pub const ENABLED: PanelType = PanelType(2);
}
impl PanelType {
    pub const EVENT_NO_HANDLING: PanelType = PanelType(4);
}
impl PanelType {
    pub const NO_FB_OFFSET: PanelType = PanelType(8);
}
...
impl ::core::ops::BitAndAssign for PanelType {
    #[inline]
    fn bitand_assign(&mut self, rhs: PanelType) {
        self.0 &= rhs.0;
    }
}
#[repr(transparent)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct PanelType(pub c_types::c_uint);

But if I set bindgen version to 0.56.0 or greater the output will be

#[repr(u32)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum PanelType {
    DISABLED = 0,
    ENABLED = 2,
    EVENT_NO_HANDLING = 4,
    NO_FB_OFFSET = 8,
}```

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the shown C typedef enum with .bitfield_enum("PanelType") using bindgen 0.55.1 and 0.56.0, then compare the generated Rust. Trace the bitfield enum handling from that entry point; done means the generated output preserves the intended bitfield wrapper behavior rather than switching to a plain repr(u32) enum.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, rust
Domain
compilers, devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.