Skip to content

emitted enum type changes type depending on usage #3205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
metaspace opened this issue May 7, 2025 · 2 comments
Closed

emitted enum type changes type depending on usage #3205

metaspace opened this issue May 7, 2025 · 2 comments

Comments

@metaspace
Copy link

Hi!

We have observed the following behavior when generating bindings for the Linux kernel:

test_a.h:

enum foo {
  BAR,
  BAZ,
};

bindgen test_a.h:

/* automatically generated by rust-bindgen 0.71.1 */
pub const foo_BAR: foo = 0;
pub const foo_BAZ: foo = 1;
pub type foo = ::std::os::raw::c_uint;

test_b.h

extern enum foo do_something(void);

enum foo {
  BAR,
  BAZ,
};

bindgen test_b.h:

/* automatically generated by rust-bindgen 0.71.1 */

unsafe extern "C" {
    pub fn do_something() -> foo;
}
pub const foo_BAR: foo = 0;
pub const foo_BAZ: foo = 1;
pub type foo = i32;

The type of foo has changed.

This leads to some confusion, because depending on kernel configuration, include files might change ordering. We currently fix this by using as casts when we use constants that are affected:

/// Restart policy for timers.
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[repr(u32)]
pub enum HrTimerRestart {
    /// Timer should not be restarted.
    #[allow(clippy::unnecessary_cast)]
    NoRestart = bindings::hrtimer_restart_HRTIMER_NORESTART as u32,
    /// Timer should be restarted.
    #[allow(clippy::unnecessary_cast)]
    Restart = bindings::hrtimer_restart_HRTIMER_RESTART as u32,
}

Is there anything we can do to prevent this behavior?

@ojeda
Copy link
Contributor

ojeda commented May 7, 2025

I opened #3179 for this.

@metaspace
Copy link
Author

Duplicate of #3179

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants