linux/rust/kernel
Miguel Ojeda 8cbc95f983 rust: workaround bindgen issue with forward references to enum types
`bindgen` currently generates the wrong type for an `enum` when there
is a forward reference to it. For instance:

    enum E;
    enum E { A };

generates:

    pub const E_A: E = 0;
    pub type E = i32;

instead of the expected:

    pub const E_A: E = 0;
    pub type E = ffi::c_uint;

The issue was reported to upstream `bindgen` [1].

Now, both GCC and Clang support silently these forward references to
`enum` types, unless `-Wpedantic` is passed, and it turns out that some
headers in the kernel depend on them.

Thus, depending on how the headers are included, which in turn may depend
on the kernel configuration or the architecture, we may get a different
type on the Rust side for a given C `enum`.

That can be quite confusing, to say the least, especially since
developers may only notice issues when building for other architectures
like in [2]. In particular, they may end up forcing a cast and adding
an `#[allow(clippy::unnecessary_cast)]` like it was done in commit
94e05a66ea ("rust: hrtimer: allow timer restart from timer handler"),
which isn't great.

Instead, let's have a section at the top of our `bindings_helper.h` that
`#include`s the headers with the affected types -- hopefully there are
not many cases and there is a single ordering that covers all cases.

This allows us to remove the cast and the `#[allow]`, thus keeping the
correct code in the source files. When the issue gets resolved in upstream
`bindgen` (and we update our minimum `bindgen` version), we can easily
remove this section at the top.

Link: https://github.com/rust-lang/rust-bindgen/issues/3179 [1]
Link: https://lore.kernel.org/rust-for-linux/87tt7md1s6.fsf@kernel.org/ [2]
Acked-by: Andreas Hindborg <a.hindborg@kernel.org>
Link: https://lore.kernel.org/r/20250325184309.97170-1-ojeda@kernel.org
[ Added extra paragraph on the comment to clarify that the workaround may
  not be possible in some cases. - Miguel ]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
2025-05-22 15:39:16 +02:00
..
alloc rust: remove unneeded Rust 1.87.0 allow(clippy::ptr_eq) 2025-05-22 11:46:50 +02:00
block rust: convert raw URLs to Markdown autolinks in comments 2025-05-12 00:20:25 +02:00
fs Rust changes for v6.15 2025-03-30 17:03:26 -07:00
list rust: use derive(CoercePointee) on rustc >= 1.84.0 2025-01-13 23:45:30 +01:00
net rust: module: introduce authors key 2025-03-10 15:12:17 +01:00
sync Rust xarray API for v6.16 2025-05-18 20:36:56 +02:00
time rust: workaround bindgen issue with forward references to enum types 2025-05-22 15:39:16 +02:00
.gitignore rust: jump_label: skip formatting generated file 2024-11-20 13:32:42 -05:00
alloc.rs rust: use derive(CoercePointee) on rustc >= 1.84.0 2025-01-13 23:45:30 +01:00
block.rs
build_assert.rs rust: add build_error! to the prelude 2025-01-10 00:19:09 +01:00
cred.rs cred,rust: mark Credential methods inline 2025-03-04 17:07:49 -05:00
device.rs rust: replace rustdoc references to alloc::format 2025-05-12 00:20:25 +02:00
device_id.rs rust: implement IdArray, IdTable and RawDeviceId 2024-12-20 17:19:25 +01:00
devres.rs rust: io: fix devres test with new io accessor functions 2025-02-25 07:29:48 +01:00
dma.rs rust: dma: add Send implementation for CoherentAllocation 2025-03-24 20:14:10 +01:00
driver.rs rust: make pin-init its own crate 2025-03-16 21:59:19 +01:00
error.rs Rust changes for v6.15 2025-03-30 17:03:26 -07:00
faux.rs rust/kernel/faux: mark Registration methods inline 2025-03-11 10:42:23 +01:00
firmware.rs rust: firmware: Use ffi::c_char type in FwFunc 2025-04-14 14:13:23 +02:00
fs.rs
generated_arch_static_branch_asm.rs.S rust: jump_label: skip formatting generated file 2024-11-20 13:32:42 -05:00
init.rs Rust changes for v6.15 2025-03-30 17:03:26 -07:00
io.rs rust: io: rename io::Io accessors 2025-02-22 15:44:19 +01:00
ioctl.rs
jump_label.rs rust: jump_label: skip formatting generated file 2024-11-20 13:32:42 -05:00
kunit.rs rust: kunit: allow to know if we are in a test 2025-03-20 12:27:05 +01:00
lib.rs Rust xarray API for v6.16 2025-05-18 20:36:56 +02:00
list.rs rust: list: Add examples for linked list 2025-05-22 12:00:52 +02:00
miscdevice.rs rust: types: add ForeignOwnable::PointedTo 2025-05-01 11:35:49 +02:00
net.rs
of.rs rust: of: add of::DeviceId abstraction 2024-12-20 17:21:04 +01:00
page.rs rust: page: optimize rust symbol generation for Page 2025-05-12 00:20:25 +02:00
pci.rs rust: types: add ForeignOwnable::PointedTo 2025-05-01 11:35:49 +02:00
pid_namespace.rs
platform.rs rust: types: add ForeignOwnable::PointedTo 2025-05-01 11:35:49 +02:00
prelude.rs rust: make pin-init its own crate 2025-03-16 21:59:19 +01:00
print.rs rust: replace rustdoc references to alloc::format 2025-05-12 00:20:25 +02:00
rbtree.rs rust: rbtree: fix comments referring to Box instead of KBox 2025-03-23 19:43:02 +01:00
revocable.rs rust: add Revocable type 2024-12-20 17:19:26 +01:00
security.rs lsm,rust: reword "destroy" -> "release" in SecurityCtx 2025-03-04 15:44:46 -05:00
seq_file.rs Rust changes for v6.15 2025-03-30 17:03:26 -07:00
sizes.rs
static_assert.rs rust: static_assert: add optional message 2025-05-12 00:20:25 +02:00
std_vendor.rs rust: convert raw URLs to Markdown autolinks in comments 2025-05-12 00:20:25 +02:00
str.rs rust: str: fix typo in comment 2025-05-20 22:57:27 +02:00
sync.rs Rust changes for v6.15 2025-03-30 17:03:26 -07:00
task.rs Rust changes for v6.15 2025-03-30 17:03:26 -07:00
time.rs rust: time: Introduce Instant type 2025-04-29 15:31:07 +02:00
tracepoint.rs
transmute.rs
types.rs rust: types: add ForeignOwnable::PointedTo 2025-05-01 11:35:49 +02:00
uaccess.rs Alloc changes for v6.16 2025-05-18 20:56:03 +02:00
workqueue.rs Locking changes for v6.15: 2025-03-24 20:55:03 -07:00
xarray.rs rust: xarray: Add an abstraction for XArray 2025-05-01 11:37:59 +02:00