gtk-rs / gir

Tool to generate rust bindings and user API for glib-based libraries
https://gtk-rs.org/gir/book/
MIT License
236 stars 107 forks source link

un-copyable struct fields generated with #[derive(Copy)] #716

Closed BrainBlasted closed 5 years ago

BrainBlasted commented 5 years ago

Similar to #534, I'm running into structs that #[derive(Copy)] but are un-copyable, this time because of a generated _trucated_record_marker, causing the build to fail.

Log:

error[E0204]: the trait `Copy` may not be implemented for this type
    --> libdazzle-sys/src/lib.rs:1186:10
     |
1186 | #[derive(Copy, Clone)]
     |          ^^^^
...
1201 |     _truncated_record_marker: c_void,
     |     -------------------------------- this field does not implement `Copy`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0204`.
error: Could not compile `libdazzle-sys`.

Struct:

#[repr(C)]
#[derive(Copy, Clone)]
pub struct DzlPreferencesInterface {
    pub parent_interface: gobject::GTypeInterface,
    pub set_page: Option<unsafe extern "C" fn(*mut DzlPreferences, *const c_char, *mut glib::GHashTable)>,
    pub add_page: Option<unsafe extern "C" fn(*mut DzlPreferences, *const c_char, *const c_char, c_int)>,
    pub add_group: Option<unsafe extern "C" fn(*mut DzlPreferences, *const c_char, *const c_char, *const c_char, c_int)>,
    pub add_list_group: Option<unsafe extern "C" fn(*mut DzlPreferences, *const c_char, *const c_char, *const c_char, gtk::GtkSelectionMode, c_int)>,
    pub add_radio: Option<unsafe extern "C" fn(*mut DzlPreferences, *const c_char, *const c_char, *const c_char, *const c_char, *const c_char, *const c_char, *const c_char, *const c_char, *const c_char, c_int) -> c_uint>,
    pub add_font_button: Option<unsafe extern "C" fn(*mut DzlPreferences, *const c_char, *const c_char, *const c_char, *const c_char, *const c_char, *const c_char, c_int) -> c_uint>,
    pub add_switch: Option<unsafe extern "C" fn(*mut DzlPreferences, *const c_char, *const c_char, *const c_char, *const c_char, *const c_char, *const c_char, *const c_char, *const c_char, *const c_char, c_int) -> c_uint>,
    pub add_spin_button: Option<unsafe extern "C" fn(*mut DzlPreferences, *const c_char, *const c_char, *const c_char, *const c_char, *const c_char, *const c_char, *const c_char, *const c_char, c_int) -> c_uint>,
    pub add_file_chooser: Option<unsafe extern "C" fn(*mut DzlPreferences, *const c_char, *const c_char, *const c_char, *const c_char, *const c_char, *const c_char, *const c_char, gtk::GtkFileChooserAction, *const c_char, c_int) -> c_uint>,
    pub add_custom: Option<unsafe extern "C" fn(*mut DzlPreferences, *const c_char, *const c_char, *mut gtk::GtkWidget, *const c_char, c_int) -> c_uint>,
    pub remove_id: Option<unsafe extern "C" fn(*mut DzlPreferences, c_uint) -> gboolean>,
    pub get_widget: Option<unsafe extern "C" fn(*mut DzlPreferences, c_uint) -> *mut gtk::GtkWidget>,
    _truncated_record_marker: c_void,
    // /*Unimplemented*/Option<unsafe extern "C" fn(*mut DzlPreferences, *const c_char, *const c_char, *mut gtk::GtkWidget, /*Unimplemented*/va_list) -> c_uint>
}
BrainBlasted commented 5 years ago

This is the function that's failing to generate:

guint
dzl_preferences_add_table_row (DzlPreferences *self,
                               const gchar    *page_name,
                               const gchar    *group_name,
                               GtkWidget      *first_widget,
                               ...)
{
  va_list args;
  gint ret;

  g_return_val_if_fail (DZL_IS_PREFERENCES (self), 0);
  g_return_val_if_fail (page_name != NULL, 0);
  g_return_val_if_fail (group_name != NULL, 0);
  g_return_val_if_fail (GTK_IS_WIDGET (first_widget), 0);

  va_start (args, first_widget);
  ret = DZL_PREFERENCES_GET_IFACE (self)->add_table_row_va (self, page_name, group_name, first_widget, args);
  va_end (args);

  return ret;
}

Here's the header file and .c file

sdroege commented 5 years ago

This looks like a bug in gir, or the .gir file is broken. Can you attach the .gir file or paste the relevant part (the struct definition of DzlPreferencesInterface) here?

There's no reason for creating a truncated version of the struct here. It's all pointers, and varargs C functions can be properly expressed in Rust too (you can even call them, just not yet define new ones).

sdroege commented 5 years ago

CC @EPashkin Or maybe you already have an idea about this one?

EPashkin commented 5 years ago

@sdroege, @BrainBlasted Sorry, it sure bug in gir: generating derive(Copy,Clone) for incomplete type, but I don't have time to look at this.

EPashkin commented 5 years ago

Code seems generated in https://github.com/gtk-rs/gir/blob/32d1716ebbb2fcf41000207e246961684aa7d0b8/src/codegen/sys/fields.rs#L38 seems something wrong in implementation of analysis::types::IsIncomplete::is_incomplete, not sure what

BrainBlasted commented 5 years ago

Here's a paste of PreferencesInterface: https://paste.gnome.org/py9qn4ytt

EPashkin commented 5 years ago

Can you add Gir.toml for sys too? Or just link your repo.

BrainBlasted commented 5 years ago
[options]
work_mode = "sys"
library = "Dazzle"
version = "1.0"
min_cfg_version = "1.0"
external_libraries = [
   "Cairo",
   "Pango",
   "GLib",
   "GObject",
   "Gio",
   "Gtk",
   "Gdk",
]
EPashkin commented 5 years ago

@BrainBlasted You pasted only part of .gir file, it interface, but error IMHO in struct PreferencesInterface. Can you just create repo with your generating attempt? I propose use same layout as https://gitlab.freedesktop.org/slomo/gstreamer-rs/tree/master folder "gir-files" in copied to repo, dazzle.gir added to it, to Gir.toml also added

target_path = "."
girs_dir = "gir-files"

to remove need in gir.exe parameters

BrainBlasted commented 5 years ago

Sure. I was trying to avoid pushing until I had a working build.

EPashkin commented 5 years ago

Note, that it different error from https://github.com/gtk-rs/gir/issues/534, by _truncated_record_marker you have incomplete type due va_list function, in #534 just "wrong" type in .gir file.

BrainBlasted commented 5 years ago

https://gitlab.gnome.org/BrainBlasted/libdazzle-rs

EPashkin commented 5 years ago

Note, that it different error from https://github.com/gtk-rs/gir/issues/534, by _truncated_record_marker you have incomplete type due va_list function, in #534 just "wrong" type in .gir file.

EPashkin commented 5 years ago

@BrainBlasted Gir PR merged. gir-files better be not submodule, but just directory, so you can add Dazzle-1.0.gir to it, this fix removes need for parameters in command line

+++ b/libdazzle-sys/gir-libdazzle.toml
 min_cfg_version = "1.0"
+target_path = "."
+girs_dir = "../gir-files"
 external_libraries = [
BrainBlasted commented 5 years ago

How can I use the fix with current code? Will I need to wait until the next release of the gtk-rs series?

EPashkin commented 5 years ago

Gir not packaged so you can just get last master.

BrainBlasted commented 5 years ago

Using current master causes even more build failures: https://gist.github.com/BrainBlasted/fb21d76d3278a3bbc886f1988a27b5e5

EPashkin commented 5 years ago

Oh, seems you need use git version for gtk too or really wait for next release.

EPashkin commented 5 years ago

Or override only gtk-sys with https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#overriding-dependencies