rust-lang / rust-bindgen

Automatically generates Rust FFI bindings to C (and some C++) libraries.
https://rust-lang.github.io/rust-bindgen/
BSD 3-Clause "New" or "Revised" License
4.51k stars 703 forks source link

Custom attribute and derive generation order #2968

Open kriswuollett opened 1 month ago

kriswuollett commented 1 month ago

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.

pvdrz commented 1 day ago

Hi @kriswuollett, I tried to add a test to our suite so we could catch any regressions but I wasn't able to reproduce it: https://github.com/rust-lang/rust-bindgen/pull/3034.

Check the expectation file specifically where the derive attributes are always generated before the other custom attributes. I disabled formatting to be sure it wasn't interfering with the output.

I also checked the source code and here's the relevant section. The derive attributes are always pushed before the custom attributes.