Nested packed struct containing incomplete array declaration results in compilation failure due to non-present Debug impl

Aperta
#3,341 0 commenti 1 reazione 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
55/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Ferma
Stack tecnologico
c, rust
Ambito
devtools

Direzione di ricerca

Inizia con example.h e riproduci l'example.rs generato usando bindgen 0.72.1 o master, quindi compilalo con il comando rustc indicato. Esamina i derive generati per nested_struct e _bindgen_ty_1; il lavoro è completato quando il binding viene compilato senza il requisito errato di Debug, mantenendo i controlli di layout riportati.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

example.h:

struct nested_struct {
  char *name;
  unsigned char more_data[];
} __attribute__((packed));

struct {
  struct nested_struct meow;
} cat;

After running bindgen -o example.rs example.h, we get

/* automatically generated by rust-bindgen 0.72.1 */

#[repr(C)]
#[derive(Default)]
pub struct __IncompleteArrayField<T>(::std::marker::PhantomData<T>, [T; 0]);
impl<T> __IncompleteArrayField<T> {
    #[inline]
    pub const fn new() -> Self {
        __IncompleteArrayField(::std::marker::PhantomData, [])
    }
    #[inline]
    pub fn as_ptr(&self) -> *const T {
        self as *const _ as *const T
    }
    #[inline]
    pub fn as_mut_ptr(&mut self) -> *mut T {
        self as *mut _ as *mut T
    }
    #[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")
    }
}
#[repr(C, packed)]
pub struct nested_struct {
    pub name: *mut ::std::os::raw::c_char,
    pub more_data: __IncompleteArrayField<::std::os::raw::c_uchar>,
}
#[allow(clippy::unnecessary_operation, clippy::identity_op)]
const _: () = {
    ["Size of nested_struct"][::std::mem::size_of::<nested_struct>() - 8usize];
    ["Alignment of nested_struct"][::std::mem::align_of::<nested_struct>() - 1usize];
    ["Offset of field: nested_struct::name"][::std::mem::offset_of!(nested_struct, name) - 0usize];
    ["Offset of field: nested_struct::more_data"]
        [::std::mem::offset_of!(nested_struct, more_data) - 8usize];
};
#[repr(C)]
#[derive(Debug)]
pub struct _bindgen_ty_1 {
    pub meow: nested_struct,
}
#[allow(clippy::unnecessary_operation, clippy::identity_op)]
const _: () = {
    ["Size of _bindgen_ty_1"][::std::mem::size_of::<_bindgen_ty_1>() - 8usize];
    ["Alignment of _bindgen_ty_1"][::std::mem::align_of::<_bindgen_ty_1>() - 1usize];
    ["Offset of field: _bindgen_ty_1::meow"][::std::mem::offset_of!(_bindgen_ty_1, meow) - 0usize];
};
unsafe extern "C" {
    pub static mut cat: _bindgen_ty_1;
}

Trying to compile example.rs with rustc --crate-type staticlib -A nonstandard_style example.rs produces

error[E0277]: `nested_struct` doesn't implement `Debug`
  --> example_manual.rs:49:5
   |
47 | #[derive(Debug)]
   |          ----- in this derive macro expansion
48 | pub struct _bindgen_ty_1 {
49 |     pub meow: nested_struct,
   |     ^^^^^^^^^^^^^^^^^^^^^^^ the trait `Debug` is not implemented for `nested_struct`
   |
   = note: add `#[derive(Debug)]` to `nested_struct` or manually `impl Debug for nested_struct`
help: consider annotating `nested_struct` with `#[derive(Debug)]`
   |
34 + #[derive(Debug)]
35 | pub struct nested_struct {
   |

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0277`.

It seems like the combination of the incomplete array field and the struct nesting results in a #[derive(Debug)] line being incorrectly generated for the anonymous struct even though nested_struct lacks it.

Tested with bindgen 0.72.1 and master (74a5f30c14769891e12de53ba275233ef2616819 as of the time of writing) using Rust 1.93

Lingua principale
Rust
Stelle
5.3k
Fork
846
Merge medio
2g 6h
PR unite (30g)
11

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di rust-lang/rust-bindgen

Tutte le issue di rust-lang/rust-bindgen

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.