mozilla / cbindgen

A project for generating C bindings from Rust code
Mozilla Public License 2.0
2.27k stars 294 forks source link

`defines` is not applied for impl const #955

Open youknowone opened 1 month ago

youknowone commented 1 month ago

source (x.rs):

#[repr(C)]
pub struct Foo {
    a: i32,
    #[cfg(feature = "feat")]
    b: i32,
}

impl Foo {
    pub const ZEROED: Self = Self {
        a: 0,
        #[cfg(feature = "feat")]
        b: 0,
    };
}

cbindgen.toml:

[export]
include=["Foo"]

[defines]
"feature = feat" = "FEAT"

command:

cbindgen --config cbindgen.toml x.rs

actual result:

#include <cstdarg>
#include <cstdint>
#include <cstdlib>
#include <ostream>
#include <new>

struct Foo {
  int32_t a;
#if defined(FEAT)
  int32_t b
#endif
  ;
};
constexpr static const Foo Foo_ZEROED = Foo{ /* .a = */ 0, /* .b = */ 0 };

expected result:

#include <cstdarg>
#include <cstdint>
#include <cstdlib>
#include <ostream>
#include <new>

struct Foo {
  int32_t a;
#if defined(FEAT)
  int32_t b
#endif
  ;
};
constexpr static const Foo Foo_ZEROED = Foo{
  /* .a = */ 0,
#if defined(FEAT)
  /* .b = */ 0
#endif
};
youknowone commented 1 month ago

maybe related to #937