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.45k stars 696 forks source link

Regresion from 0.30.0 to 0.36.0 not valid rust code generated #1306

Closed siscia closed 6 years ago

siscia commented 6 years ago

Input C/C++ Header

https://github.com/RedBeardLab/rediSQL/blob/master/redisql_lib/src/CDeps/Redis/include/redismodule.h

Bindgen Invocation

$ bindgen redismodule.h > bobo.rs 

Actual Results

$ rustc bobo.rs 
error: unexpected close delimiter: `]`
   --> bobo.rs:483:39
    |
483 | # [ derive ( Copy , Clone ) ]; 7usize ] , }# [ test ]
    |                                       ^

and/or

pub type fsblkcnt_t = __fsblkcnt_t;
pub type fsfilcnt_t = __fsfilcnt_t;
pub type pthread_t = ::std::os::raw::c_ulong;
# [ repr ( C ) ]
# [ derive ( Copy , Clone ) ]; 7usize ] , }# [ test ] <-- HERE LINE 483
fn bindgen_test_layout_pthread_attr_t() {
    assert_eq!(::std::mem::size_of::<pthread_attr_t>(),
               56usize,
               concat!("Size of: ", stringify!(pthread_attr_t)));
    assert_eq!(::std::mem::align_of::<pthread_attr_t>(),
               8usize,
               concat!("Alignment of ", stringify!(pthread_attr_t)));
    assert_eq!(unsafe {
        &(*(::std::ptr::null::<pthread_attr_t>())).__size as
        *const _ as usize
    },
               0usize,
               concat!("Offset of field: ",
                       stringify!(pthread_attr_t),
                       "::",
                       stringify!(__size)));
    assert_eq!(unsafe {
        &(*(::std::ptr::null::<pthread_attr_t>())).__align as
        *const _ as usize
    },
               0usize,
               concat!("Offset of field: ",
                       stringify!(pthread_attr_t),
                       "::",
                       stringify!(__align)));
}

Expected Results

Valid Rust Code

Sorry that I wasn't able to minimize your search space by providing a smaller header.

emilio commented 6 years ago

This is a rustfmt bug. either rustfmt_bindings(false), or update rustfmt-nightly. See #1279, etc.

Thanks for the report!

siscia commented 6 years ago

Actually on bindgen 0.35.0 it didn't work.

Am I doing something wrong?

    let bindings =
        bindgen::Builder::default()
            .parse_callbacks(Box::new(SqliteTypeChooser))
            .rustfmt_bindings(false) // see https://github.com/rust-lang-nursery/rust-bindgen/issues/1306#event-1597477817
            .header("redis_dependencies.h")
            .generate()
            .expect("Unable to generate bindings for Redis");
emilio commented 6 years ago

Hmm, that should work indeed, could you upload the generated bindings?

siscia commented 6 years ago

Sorry, it worked perfectly, it was simply that I had two libraries I was using bindgen on, the error was the same but on different libraries.