From: FUJITA Tomonori <fujita.tomonori@gmail.com>
To: netdev@vger.kernel.org
Cc: rust-for-linux@vger.kernel.org, andrew@lunn.ch,
tmgross@umich.edu, miguel.ojeda.sandonis@gmail.com,
benno.lossin@proton.me, wedsonaf@gmail.com,
Miguel Ojeda <ojeda@kernel.org>
Subject: [PATCH net-next v7 3/5] rust: add second `bindgen` pass for enum exhaustiveness checking
Date: Thu, 26 Oct 2023 09:10:48 +0900 [thread overview]
Message-ID: <20231026001050.1720612-4-fujita.tomonori@gmail.com> (raw)
In-Reply-To: <20231026001050.1720612-1-fujita.tomonori@gmail.com>
From: Miguel Ojeda <ojeda@kernel.org>
This patch makes sure that the C's enum is sync with Rust sides. If
the enum is out of sync, compiling fails with an error like the
following.
Note that this is a temporary solution. It will be replaced with
bindgen when it supports generating the enum conversion code.
error[E0005]: refutable pattern in function argument
--> rust/bindings/bindings_enum_check.rs:29:6
|
29 | (phy_state::PHY_DOWN
| ______^
30 | | | phy_state::PHY_READY
31 | | | phy_state::PHY_HALTED
32 | | | phy_state::PHY_ERROR
... |
35 | | | phy_state::PHY_NOLINK
36 | | | phy_state::PHY_CABLETEST): phy_state,
| |______________________________^ pattern `phy_state::PHY_NEW` not covered
|
note: `phy_state` defined here
--> rust/bindings/bindings_generated_enum_check.rs:60739:10
|
60739 | pub enum phy_state {
| ^^^^^^^^^
...
60745 | PHY_NEW = 5,
| ------- not covered
= note: the matched value is of type `phy_state`
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
---
rust/.gitignore | 1 +
rust/Makefile | 14 +++++++++++
rust/bindings/bindings_enum_check.rs | 36 ++++++++++++++++++++++++++++
3 files changed, 51 insertions(+)
create mode 100644 rust/bindings/bindings_enum_check.rs
diff --git a/rust/.gitignore b/rust/.gitignore
index d3829ffab80b..1a76ad0d6603 100644
--- a/rust/.gitignore
+++ b/rust/.gitignore
@@ -1,6 +1,7 @@
# SPDX-License-Identifier: GPL-2.0
bindings_generated.rs
+bindings_generated_enum_check.rs
bindings_helpers_generated.rs
doctests_kernel_generated.rs
doctests_kernel_generated_kunit.c
diff --git a/rust/Makefile b/rust/Makefile
index 87958e864be0..a622111c8c50 100644
--- a/rust/Makefile
+++ b/rust/Makefile
@@ -15,6 +15,7 @@ always-$(CONFIG_RUST) += libmacros.so
no-clean-files += libmacros.so
always-$(CONFIG_RUST) += bindings/bindings_generated.rs bindings/bindings_helpers_generated.rs
+always-$(CONFIG_RUST) += bindings/bindings_generated_enum_check.rs
obj-$(CONFIG_RUST) += alloc.o bindings.o kernel.o
always-$(CONFIG_RUST) += exports_alloc_generated.h exports_bindings_generated.h \
exports_kernel_generated.h
@@ -341,6 +342,19 @@ $(obj)/bindings/bindings_generated.rs: $(src)/bindings/bindings_helper.h \
$(src)/bindgen_parameters FORCE
$(call if_changed_dep,bindgen)
+$(obj)/bindings/bindings_generated_enum_check.rs: private bindgen_target_flags = \
+ $(shell grep -v '^#\|^$$' $(srctree)/$(src)/bindgen_parameters) \
+ --default-enum-style rust
+$(obj)/bindings/bindings_generated_enum_check.rs: private bindgen_target_extra = ; \
+ OBJTREE=$(abspath $(objtree)) $(RUSTC_OR_CLIPPY) $(rust_flags) $(rustc_target_flags) \
+ --crate-type rlib -L$(objtree)/$(obj) \
+ --emit=dep-info=$(obj)/bindings/.bindings_enum_check.rs.d \
+ --emit=metadata=$(obj)/bindings/libbindings_enum_check.rmeta \
+ --crate-name enum_check $(srctree)/$(src)/bindings/bindings_enum_check.rs
+$(obj)/bindings/bindings_generated_enum_check.rs: $(src)/bindings/bindings_helper.h \
+ $(src)/bindings/bindings_enum_check.rs $(src)/bindgen_parameters FORCE
+ $(call if_changed_dep,bindgen)
+
$(obj)/uapi/uapi_generated.rs: private bindgen_target_flags = \
$(shell grep -v '^#\|^$$' $(srctree)/$(src)/bindgen_parameters)
$(obj)/uapi/uapi_generated.rs: $(src)/uapi/uapi_helper.h \
diff --git a/rust/bindings/bindings_enum_check.rs b/rust/bindings/bindings_enum_check.rs
new file mode 100644
index 000000000000..eef7e9ca3c54
--- /dev/null
+++ b/rust/bindings/bindings_enum_check.rs
@@ -0,0 +1,36 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Bindings' enum exhaustiveness check.
+//!
+//! Eventually, this should be replaced by a safe version of `--rustified-enum`, see
+//! https://github.com/rust-lang/rust-bindgen/issues/2646.
+
+#![no_std]
+#![allow(
+ clippy::all,
+ dead_code,
+ missing_docs,
+ non_camel_case_types,
+ non_upper_case_globals,
+ non_snake_case,
+ improper_ctypes,
+ unreachable_pub,
+ unsafe_op_in_unsafe_fn
+)]
+
+include!(concat!(
+ env!("OBJTREE"),
+ "/rust/bindings/bindings_generated_enum_check.rs"
+));
+
+fn check_phy_state(
+ (phy_state::PHY_DOWN
+ | phy_state::PHY_READY
+ | phy_state::PHY_HALTED
+ | phy_state::PHY_ERROR
+ | phy_state::PHY_UP
+ | phy_state::PHY_RUNNING
+ | phy_state::PHY_NOLINK
+ | phy_state::PHY_CABLETEST): phy_state,
+) {
+}
--
2.34.1
next prev parent reply other threads:[~2023-10-26 0:16 UTC|newest]
Thread overview: 108+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-10-26 0:10 [PATCH net-next v7 0/5] Rust abstractions for network PHY drivers FUJITA Tomonori
2023-10-26 0:10 ` [PATCH net-next v7 1/5] rust: core " FUJITA Tomonori
2023-10-27 19:09 ` Boqun Feng
2023-10-28 10:00 ` FUJITA Tomonori
2023-10-27 19:59 ` Boqun Feng
2023-10-27 21:19 ` Benno Lossin
2023-10-27 22:21 ` Boqun Feng
2023-10-27 22:36 ` Andrew Lunn
2023-10-27 22:50 ` Benno Lossin
2023-10-27 23:26 ` Boqun Feng
2023-10-27 23:52 ` Boqun Feng
2023-10-28 8:35 ` Benno Lossin
2023-10-27 22:40 ` Andrew Lunn
2023-10-28 15:16 ` Miguel Ojeda
2023-10-28 18:18 ` Andrew Lunn
2023-10-28 9:27 ` FUJITA Tomonori
2023-10-28 14:53 ` Andrew Lunn
2023-10-28 16:09 ` FUJITA Tomonori
2023-10-28 16:39 ` Benno Lossin
2023-10-28 19:06 ` Boqun Feng
2023-10-28 19:23 ` Andrew Lunn
2023-10-28 23:26 ` Boqun Feng
2023-10-28 16:37 ` Benno Lossin
2023-10-28 18:23 ` Andrew Lunn
2023-10-28 18:45 ` Benno Lossin
2023-10-29 4:21 ` FUJITA Tomonori
2023-10-29 16:48 ` Boqun Feng
2023-10-29 18:09 ` Boqun Feng
2023-10-29 18:26 ` Boqun Feng
2023-10-29 19:39 ` Andrew Lunn
2023-10-30 12:07 ` Miguel Ojeda
2023-10-30 12:32 ` Andrew Lunn
2023-10-29 22:58 ` FUJITA Tomonori
2023-10-30 0:19 ` Boqun Feng
2023-10-30 8:34 ` Benno Lossin
2023-10-30 12:49 ` FUJITA Tomonori
2023-10-30 16:45 ` Benno Lossin
2023-11-08 10:46 ` FUJITA Tomonori
2023-11-10 13:26 ` Andrew Lunn
2023-10-29 17:32 ` Andrew Lunn
2023-10-30 8:37 ` Benno Lossin
2023-10-30 11:22 ` Miguel Ojeda
2023-11-17 9:39 ` Alice Ryhl
2023-11-17 13:34 ` Andrew Lunn
2023-11-17 15:42 ` Alice Ryhl
2023-11-17 16:28 ` Andrew Lunn
2023-11-17 18:27 ` Alice Ryhl
2023-11-21 12:47 ` FUJITA Tomonori
2023-11-17 9:39 ` Alice Ryhl
2023-11-17 13:53 ` Andrew Lunn
2023-11-17 19:50 ` Greg KH
2023-11-17 23:28 ` Boqun Feng
2023-11-18 15:32 ` Andrew Lunn
2023-11-18 15:54 ` Boqun Feng
2023-11-19 11:06 ` Trevor Gross
2023-11-21 2:13 ` FUJITA Tomonori
2023-11-22 18:16 ` Boqun Feng
2023-11-19 13:51 ` FUJITA Tomonori
2023-11-19 16:08 ` Andrew Lunn
2023-10-26 0:10 ` [PATCH net-next v7 2/5] rust: net::phy add module_phy_driver macro FUJITA Tomonori
2023-11-17 9:39 ` Alice Ryhl
2023-11-19 10:50 ` FUJITA Tomonori
2023-11-19 10:54 ` Benno Lossin
2023-11-17 22:21 ` Boqun Feng
2023-11-17 22:54 ` Andrew Lunn
2023-11-17 23:01 ` Benno Lossin
2023-11-17 23:18 ` Andrew Lunn
2023-11-19 9:41 ` FUJITA Tomonori
2023-11-19 9:25 ` FUJITA Tomonori
2023-11-19 15:50 ` Andrew Lunn
2023-11-20 13:54 ` FUJITA Tomonori
2023-11-20 14:13 ` Andrew Lunn
2023-11-21 0:49 ` FUJITA Tomonori
2023-11-19 9:44 ` FUJITA Tomonori
2023-10-26 0:10 ` FUJITA Tomonori [this message]
2023-10-26 11:02 ` [PATCH net-next v7 3/5] rust: add second `bindgen` pass for enum exhaustiveness checking Miguel Ojeda
2023-10-26 11:54 ` FUJITA Tomonori
2023-10-26 12:22 ` Miguel Ojeda
2023-10-27 0:07 ` Andrew Lunn
2023-10-27 10:50 ` Miguel Ojeda
2023-10-26 0:10 ` [PATCH net-next v7 4/5] MAINTAINERS: add Rust PHY abstractions for ETHERNET PHY LIBRARY FUJITA Tomonori
2023-10-26 23:53 ` Andrew Lunn
2023-10-26 0:10 ` [PATCH net-next v7 5/5] net: phy: add Rust Asix PHY driver FUJITA Tomonori
2023-11-17 9:39 ` Alice Ryhl
2023-11-19 9:57 ` FUJITA Tomonori
2023-11-19 16:03 ` Andrew Lunn
2023-11-21 6:19 ` FUJITA Tomonori
2023-11-21 7:12 ` Greg KH
2023-10-26 10:39 ` [PATCH net-next v7 0/5] Rust abstractions for network PHY drivers Miguel Ojeda
2023-10-26 23:48 ` Andrew Lunn
2023-10-27 2:06 ` Boqun Feng
2023-10-27 2:47 ` Andrew Lunn
2023-10-27 3:11 ` Boqun Feng
2023-10-27 4:26 ` Boqun Feng
2023-10-27 14:26 ` Andrew Lunn
2023-10-27 16:41 ` Miguel Ojeda
2023-10-27 13:00 ` Andrew Lunn
2023-10-27 10:22 ` Miguel Ojeda
2023-10-27 13:09 ` Andrew Lunn
2023-10-27 10:21 ` Miguel Ojeda
2023-10-27 14:26 ` Jakub Kicinski
2023-10-27 16:36 ` Miguel Ojeda
2023-10-27 22:55 ` Andrew Lunn
2023-10-28 11:07 ` Miguel Ojeda
2023-10-28 11:41 ` Benno Lossin
2023-10-28 15:11 ` Miguel Ojeda
2023-10-28 15:00 ` Andrew Lunn
2023-10-28 15:11 ` Miguel Ojeda
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20231026001050.1720612-4-fujita.tomonori@gmail.com \
--to=fujita.tomonori@gmail.com \
--cc=andrew@lunn.ch \
--cc=benno.lossin@proton.me \
--cc=miguel.ojeda.sandonis@gmail.com \
--cc=netdev@vger.kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
--cc=wedsonaf@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).