Skip to content

Builder.derive_partialord(true) maybe should not derive PartialOrd for function pointers #3360

@koutheir

Description

@koutheir

Consider the following C declaration:

struct avc_memory_callback {
	void *(*func_malloc)(size_t size);
	/* other fields... */
};

Running bindgen on that, with a call to Builder.derive_partialord(true) (and other options) generates the following Rust declaration:

#[repr(C)]
#[derive(Debug, Copy, Clone, PartialOrd, Ord, PartialEq, Eq)]
pub struct avc_memory_callback {
    pub func_malloc:
        ::std::option::Option<unsafe extern "C" fn(size: usize) -> *mut ::std::os::raw::c_void>,
    /* other fields... */
}

The interesting point here is the #[derive(PartialOrd)], which causes the following rustc warning:

warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
    --> <...>/selinux-sys/target/debug/build/selinux-sys-2e09f76ca98757af/out/selinux-sys.rs:1004:5
     |
1002 |   #[derive(Debug, Copy, Clone, PartialOrd, Ord, PartialEq, Eq)]
     |                                ---------- in this derive macro expansion
1003 |   pub struct avc_memory_callback {
1004 | /     pub func_malloc:
1005 | |         ::std::option::Option<unsafe extern "C" fn(size: usize) -> *mut ::std::os::raw::c_void>,
     | |_______________________________________________________________________________________________^
     |
     = note: the address of the same function can vary between different codegen units
     = note: furthermore, different functions could have the same address after being merged together
     = note: for more information visit <https://doc.rust-lang.org/nightly/core/ptr/fn.fn_addr_eq.html>
     = note: `#[warn(unpredictable_function_pointer_comparisons)]` on by default

The documentation of Builder::derive_partialord says:

Set whether the PartialOrd trait should be derived when possible.

And while this is technically possible, I think rustc here is requesting, politely, to avoid it. I agree with the assessment that rustc is showing. And my question is: should Builder.derive_partialord(true) still do it?

In the meantime, my fix for this issue is to call builder.no_partialeq("^avc_([_a-zA-Z0-9]+)_callback$") to manually exclude the types that contain function pointers. But, is this the reasonable fix for this situation, or should the behavior of Builder.derive_partialord(true) change to exclude function pointers?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions