Skip to content
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

Incorrect derive partial eq/Hash for struct contains incomplete array field #1093

Closed
photoszzt opened this issue Oct 23, 2017 · 5 comments
Closed

Comments

@photoszzt
Copy link
Contributor

Input C/C++ Header

struct test {
     int a;
     char data[0];
 };

Bindgen Invocation

$ bindgen --with-derive-partialeq --with-derive-hash test.h > test.rs

Actual Results

error[E0277]: the trait bound `__IncompleteArrayField<i8>: std::hash::Hash` is not satisfied
  --> test.rs:40:5
   |
40 |     pub data: __IncompleteArrayField<::std::os::raw::c_char>,
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::hash::Hash` is not implemented for `__IncompleteArrayField<i8>`
   |
   = note: required by `std::hash::Hash::hash`

error[E0369]: binary operation `==` cannot be applied to type `__IncompleteArrayField<i8>`
  --> test.rs:40:5
   |
40 |     pub data: __IncompleteArrayField<::std::os::raw::c_char>,
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: an implementation of `std::cmp::PartialEq` might be missing for `__IncompleteArrayField<i8>`

error[E0369]: binary operation `!=` cannot be applied to type `__IncompleteArrayField<i8>`
  --> test.rs:40:5
   |
40 |     pub data: __IncompleteArrayField<::std::os::raw::c_char>,
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: an implementation of `std::cmp::PartialEq` might be missing for `__IncompleteArrayField<i8>`

#[repr(C)]
 #[derive(Default)]
 pub struct __IncompleteArrayField<T>(::std::marker::PhantomData<T>);
 impl <T> __IncompleteArrayField<T> {
     #[inline]
     pub fn new() -> Self {
         __IncompleteArrayField(::std::marker::PhantomData)
     }
     #[inline]
     pub unsafe fn as_ptr(&self) -> *const T { ::std::mem::transmute(self) }
     #[inline]
     pub unsafe fn as_mut_ptr(&mut self) -> *mut T {
         ::std::mem::transmute(self)
     }
     #[inline]
     pub unsafe fn as_slice(&self, len: usize) -> &[T] {
         ::std::slice::from_raw_parts(self.as_ptr(), len)
     }
     #[inline]
     pub unsafe fn as_mut_slice(&mut self, len: usize) -> &mut [T] {
         ::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len)
     }
 }
 impl <T> ::std::fmt::Debug for __IncompleteArrayField<T> {
     fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
         fmt.write_str("__IncompleteArrayField")
     }
 }
 impl <T> ::std::clone::Clone for __IncompleteArrayField<T> {
     #[inline]
     fn clone(&self) -> Self { Self::new() }
 }
 impl <T> ::std::marker::Copy for __IncompleteArrayField<T> { }
 #[repr(C)]
 #[derive(Debug, Hash, PartialEq)]
 pub struct test {
     pub a: ::std::os::raw::c_int,
     pub data: __IncompleteArrayField<::std::os::raw::c_char>,
 }

Expected Results

Either implement Hash and PartialEq for IncompleteArrayField or don't derive Hash and PartialEq for the struct.

@emilio
Copy link
Contributor

emilio commented Oct 23, 2017

Either implement Hash and PartialEq for IncompleteArrayField or don't derive Hash and PartialEq for the struct.

Yeah, I don't think there's a good way of deriving PartialEq for these, the array field may have a different meaning depending on how the user uses it...

@fitzgen
Copy link
Member

fitzgen commented Oct 23, 2017

I thought we were already treating incomplete and zero-sized arrays as not-derive-able, but I guess not...

Thanks for the bug report @photoszzt :)

@photoszzt
Copy link
Contributor Author

photoszzt commented Oct 24, 2017

@fitzgen I think the fixed version hasn't published. The version that has this problem is 0.30 which is what I get from crates.io. I try the master version, it doesn't have the problem for partial eq but fails for hash.

@photoszzt
Copy link
Contributor Author

@highfive assign me.

@highfive
Copy link

Hey @photoszzt! Thanks for your interest in working on this issue. It's now assigned to you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants