Skip to content

codegen: Revert some bitfield codegen changes and silence unnecessary_transmutes instead. - #3455

Merged
emilio merged 1 commit into
mainfrom
newtype-enum-bitfield
Sep 7, 2026
Merged

codegen: Revert some bitfield codegen changes and silence unnecessary_transmutes instead.#3455
emilio merged 1 commit into
mainfrom
newtype-enum-bitfield

Conversation

@emilio

@emilio emilio commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

This reverts commits a502187 and 01d65d9, because it breaks when the bitfield is a newtype enum, and adds a test for that.

Fixes #3454.

…_transmutes instead.

This reverts commits a502187 and
01d65d9, because it breaks when the
bitfield is a newtype enum, and adds a test for that.

Fixes #3454.
@emilio
emilio added this pull request to the merge queue Sep 7, 2026
Merged via the queue into main with commit 4348523 Sep 7, 2026
51 checks passed
@ojeda

ojeda commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

This one references unnecessary_transmutes even with --rust-target 1.85, but the lint appeared in Rust 1.88 (c8c074288a27 ("Suggest {to,from}_ne_bytes for transmutations between arrays and integers, etc")), which in turn means users may error on an unknown lint, e.g.

error: unknown lint: `unnecessary_transmutes`
     --> rust/bindings/bindings_generated.rs:68316:13
      |
68316 |     #[allow(unnecessary_transmutes)]
      |             ^^^^^^^^^^^^^^^^^^^^^^

For Linux, we can allow unknown lints, but is that intentional? i.e. should users of bindgen generally allow unknown lints on bindgen's generated code?

@emilio

emilio commented Sep 8, 2026

Copy link
Copy Markdown
Contributor Author

Gah, that's my bad. We should've made this conditional on rust_target, but we don't have the appropriate rust-target hooked in... And maybe instead I should've used #[allow(unknown_lints, unnecessary_transmutes)], if that works that's definitely easier.

But ignoring unknown_lints seems easy enough if you care about older toolchains in this particular case. I guess we don't have tests for "bindings compile warning-free in old toolchains", we probably should...

@ojeda

ojeda commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

Yeah, no worries, for the kernel side, we can work around it -- I did it conditionally to the version to avoid losing the lint in all cases, here (I Cc'd you there, but in case others wonder about it):

https://lore.kernel.org/rust-for-linux/20260908170539.345207-1-ojeda@kernel.org/

But, yeah, a "more local" #[allow(...)] is usually better.

Thanks for the quick reply!

ojeda added a commit to Rust-for-Linux/linux that referenced this pull request Sep 8, 2026
Starting with bindgen 0.73.2 [1], `#[allow(unnecessary_transmutes)]`
are used, even when `--rust-target 1.85` is passed.

However, the lint was introduced in Rust 1.88.0. Thus building with
older Rust versions warns like:

    error: unknown lint: `unnecessary_transmutes`
         --> rust/uapi/uapi_generated.rs:26294:13
          |
    26294 |     #[allow(unnecessary_transmutes)]
          |             ^^^^^^^^^^^^^^^^^^^^^^
          |
          = note: `-D unknown-lints` implied by `-D warnings`
          = help: to override `-D warnings` add `#[allow(unknown_lints)]`

Thus allow `unknown_lints` in the generated bindings -- only when building
with older Rust versions.

I have asked upstream if this is intentional [1], i.e. if we are supposed
to always allow unknown lints in case `bindgen` uses such attributes,
or whether it is an oversight.

[ Emilio said it wasn't intentional -- we will work around it for now
  on the kernel side. - Miguel ]

Cc: stable@vger.kernel.org # Needed in 6.12.y and later (Rust is pinned in older LTSs).
Cc: Emilio Cobos Álvarez <emilio@crisal.io>
Link: rust-lang/rust-bindgen#3455 (comment) [1]
Assisted-by: LLM
Link: https://patch.msgid.link/20260908170539.345207-1-ojeda@kernel.org
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
ojeda added a commit to Rust-for-Linux/linux that referenced this pull request Sep 8, 2026
Starting with bindgen 0.73.2 [1], `#[allow(unnecessary_transmutes)]`
are used, even when `--rust-target 1.85` is passed.

However, the lint was introduced in Rust 1.88.0. Thus building with
older Rust versions warns like:

    error: unknown lint: `unnecessary_transmutes`
         --> rust/uapi/uapi_generated.rs:26294:13
          |
    26294 |     #[allow(unnecessary_transmutes)]
          |             ^^^^^^^^^^^^^^^^^^^^^^
          |
          = note: `-D unknown-lints` implied by `-D warnings`
          = help: to override `-D warnings` add `#[allow(unknown_lints)]`

Thus allow `unknown_lints` in the generated bindings -- only when building
with older Rust versions.

I have asked upstream if this is intentional [1], i.e. if we are supposed
to always allow unknown lints in case `bindgen` uses such attributes,
or whether it is an oversight.

[ Emilio said it wasn't intentional -- we will work around it for now
  on the kernel side. - Miguel ]

Cc: stable@vger.kernel.org # Needed in 6.12.y and later (Rust is pinned in older LTSs).
Cc: Emilio Cobos Álvarez <emilio@crisal.io>
Link: rust-lang/rust-bindgen#3455 (comment) [1]
Assisted-by: LLM
Link: https://patch.msgid.link/20260908170539.345207-1-ojeda@kernel.org
[ Removed the `cfg` for `allow(unnecessary_transmutes)` as suggested by
  Gary. - Miguel ]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
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

Successfully merging this pull request may close these issues.

Bitfield setter for enum-typed field emits invalid as cast (E0605), regression since 0.73.0

2 participants