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

オープン
#3,360 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
35/100
issue の種類
機能追加
明瞭さ
説明が足りない
活発さ
静か
技術スタック
rust
領域
devtools

調査の方向性

Builder::derive_partialord オプションと、関数ポインタを含む structs に対して derives を出力するコードパスから始めます。示されている C 宣言と生成された Rust 出力を再現し、その後、既存の derive 関連テストを調査するか、回帰テストのカバレッジを追加します。報告された rustc 警告なしで意図した動作が文書化され、検証されていれば完了です。

索引モデルが issue の本文から書いたものです。

説明

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?

主要言語
Rust
スター
5.3k
フォーク
846
平均マージ
2日 6時間
マージ済み PR(30日)
11

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

rust-lang/rust-bindgen のほかの issue

rust-lang/rust-bindgen の issue をすべて見る

似ている issue

Rust の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。