rust-lang / rust-lang/rust-bindgen

Custom attribute and derive generation order

Open
#2,968 2 comments 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

The interface and/or implementation of bindgen::callbacks::ParseCallbacks may need some changes to address a compile warning that may become an error in the future, see https://github.com/rust-lang/rust/issues/79202. I'm guessing that the very least derives need to appear before other attributes?

I wish I remember what library it was, but I believe I already encountered a derive macro in the past that was very sensitive to order of everything and needed to be the very first one listed as well otherwise it would cause a compile error. If I come across it again, I'll be sure to update this issue.

Details

From a header file with a type like:

typedef enum {
    color_carmine_red = 0,
    color_cornflower_blue = 1,
    color_lime_green = 2,
} color_t;

Using the item_name, add_derives, add_attributes, and enum_variant_name parse callbacks:

/* automatically generated by rust-bindgen 0.70.1 */

#[repr(u32)]
#[non_exhaustive]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[strum(serialize_all = "SCREAMING_SNAKE_CASE")]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, strum :: AsRefStr, strum :: IntoStaticStr)]
pub enum Color {
    CarmineRed = 0,
    CornflowerBlue = 1,
    LimeGreen = 2,
}

Which causes a warning for derive helper being used before it is introduced,
rust-lang/rust#79202:

warning: derive helper attribute is used before it is introduced
 --> src/bindings.rs:6:3
  |
6 | #[strum(serialize_all = "SCREAMING_SNAKE_CASE")]
  |   ^^^^^
7 | #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, strum :: AsRefStr, strum :: IntoStaticStr)]
  |                                                   ----------------- the attribute is introduced here
  |
  = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
  = note: for more information, see issue #79202 <https://github.com/rust-lang/rust/issues/79202>
  = note: `#[warn(legacy_derive_helpers)]` on by default

An example project is at https://github.com/kriswuollett/example-bindgen-derive-ordering/tree/0130dcd8fa3f8d0820ed53c711375152faa0cec2.

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

Start by inspecting bindgen::callbacks::ParseCallbacks and the generated enum shown in the issue, then reproduce the warning with the linked example project. Trace how item_name, add_derives, add_attributes, and enum_variant_name contribute attributes. Done means generated bindings order derive-related attributes without triggering the legacy_derive_helpers warning.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.