Skip to content

--no-derive-debug prevents even manual Debug on enumerations #2076

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
VeaaC opened this issue Jul 20, 2021 · 3 comments · Fixed by #2090
Closed

--no-derive-debug prevents even manual Debug on enumerations #2076

VeaaC opened this issue Jul 20, 2021 · 3 comments · Fixed by #2090

Comments

@VeaaC
Copy link
Contributor

VeaaC commented Jul 20, 2021

Input C/C++ Header

// lots of enums like this
/// <div rustbindgen derive="Clone"></div>
/// <div rustbindgen derive="Debug"></div>
/// <div rustbindgen derive="Copy"></div>
/// <div rustbindgen derive="PartialEq"></div>
/// <div rustbindgen derive="PartialOrd"></div>
enum NeedsDebug { VALUE };

// Lots of structs like this
typedef struct MustNotHaveDebug MustNotHaveDebug;

Bindgen Invocation

bindgen --no-derive-debug --rustified-enum input.h

Actual Results

/* automatically generated by rust-bindgen 0.59.0 */

#[repr(u32)]
#[doc = " <div rustbindgen derive=\"Clone\"></div>"]
#[doc = " <div rustbindgen derive=\"Debug\"></div>"]
#[doc = " <div rustbindgen derive=\"Copy\"></div>"]
#[doc = " <div rustbindgen derive=\"PartialEq\"></div>"]
#[doc = " <div rustbindgen derive=\"PartialOrd\"></div>"]
#[derive(Copy, Clone, Hash, PartialEq, Eq)]
pub enum NeedsDebug {
    VALUE = 0,
}
#[repr(C)]
#[derive(Copy, Clone)]
pub struct MustNotHaveDebug {
    _unused: [u8; 0],
}

Expected Results

I would expect manual overrides to overrule default settings. The main use case here is that I need Debug on all enumerations, a few structs, but not on most other types.

@emilio
Copy link
Contributor

emilio commented Jul 25, 2021

curious, does Default behave like Debug?

@VeaaC
Copy link
Contributor Author

VeaaC commented Jul 27, 2021

I realized that enumeration are not adding any derive directives from annotations at all. It is a simple change to support that, e.g.:

@@ -3003,13 +3004,16 @@ impl CodeGenerator for Enum {
                     DerivableTraits::PARTIAL_EQ |
                     DerivableTraits::EQ,
             );
+            let mut derives: Vec<_> = derives.into();
+            derives.extend(
+                item.annotations().derives().iter().map(String::as_str),
+           );
-            let derives: Vec<_> = derives.into();
             attrs.push(attributes::derives(&derives));

However, the code also talks about backwards compatibility:

            // For backwards compat, enums always derive Clone/Eq/PartialEq/Hash, even
            // if we don't generate those by default.

And it seems like Debug should have most likely also been part of the same backwards compatibility.

I could prepare a PR with a fix / implementation based on what kind of change you would prefer (supporting annotations, adding Debug always, or both).

@VeaaC
Copy link
Contributor Author

VeaaC commented Aug 3, 2021

I created two possible fixes for this: #2089 is kind of a minimal fix, restoring the previous behaviour. #2090 is a more complete fix, restoring the previous behaviour, but also adding full support for annotations on enumerations (which allows the user to opt out of Debug)

emilio pushed a commit that referenced this issue Aug 24, 2021
lightsofapollo pushed a commit to lightsofapollo/rust-bindgen that referenced this issue Oct 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants