Skip to content

Custom attribute and derive generation order #2968

Open
@kriswuollett

Description

@kriswuollett

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 rust-lang/rust#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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions