From: Andreas Hindborg <a.hindborg@kernel.org>
To: "Alice Ryhl" <aliceryhl@google.com>
Cc: "Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Petr Mladek" <pmladek@suse.com>,
"Steven Rostedt" <rostedt@goodmis.org>,
"Andy Shevchenko" <andriy.shevchenko@linux.intel.com>,
"Rasmus Villemoes" <linux@rasmusvillemoes.dk>,
"Sergey Senozhatsky" <senozhatsky@chromium.org>,
"Andrew Morton" <akpm@linux-foundation.org>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <benno.lossin@proton.me>,
"Trevor Gross" <tmgross@umich.edu>,
"Maarten Lankhorst" <maarten.lankhorst@linux.intel.com>,
"Maxime Ripard" <mripard@kernel.org>,
"Thomas Zimmermann" <tzimmermann@suse.de>,
"David Airlie" <airlied@gmail.com>,
"Simona Vetter" <simona@ffwll.ch>,
linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
dri-devel@lists.freedesktop.org
Subject: Re: [PATCH v2 3/5] rust: add #[export] macro
Date: Fri, 28 Feb 2025 19:53:31 +0100 [thread overview]
Message-ID: <87y0xplx3o.fsf@kernel.org> (raw)
In-Reply-To: <20250228-export-macro-v2-3-569cc7e8926c@google.com> (Alice Ryhl's message of "Fri, 28 Feb 2025 12:39:32 +0000")
"Alice Ryhl" <aliceryhl@google.com> writes:
> Rust has two different tools for generating function declarations to
> call across the FFI boundary:
>
> * bindgen. Generates Rust declarations from a C header.
> * cbindgen. Generates C headers from Rust declarations.
>
> In the kernel, we only use bindgen. This is because cbindgen assumes a
> cargo-based buildsystem, so it is not compatible with the kernel's build
> system. This means that when C code calls a Rust function by name, its
> signature must be duplicated in both Rust code and a C header, and the
> signature needs to be kept in sync manually.
>
> To eliminate this manual checking, introduce a new macro that verifies
> at compile time that the two function declarations use the same
> signature. The idea is to run the C declaration through bindgen, and
> then have rustc verify that the function pointers have the same type.
>
> The signature must still be written twice, but at least you can no
> longer get it wrong. If the signatures don't match, you will get errors
> that look like this:
>
> error[E0308]: `if` and `else` have incompatible types
> --> <linux>/rust/kernel/print.rs:22:22
> |
> 21 | #[export]
> | --------- expected because of this
> 22 | unsafe extern "C" fn rust_fmt_argument(
> | ^^^^^^^^^^^^^^^^^ expected `u8`, found `i8`
> |
> = note: expected fn item `unsafe extern "C" fn(*mut u8, *mut u8, *mut c_void) -> *mut u8 {bindings::rust_fmt_argument}`
> found fn item `unsafe extern "C" fn(*mut i8, *mut i8, *const c_void) -> *mut i8 {print::rust_fmt_argument}`
>
> It is unfortunate that the error message starts out by saying "`if` and
> `else` have incompatible types", but I believe the rest of the error
> message is reasonably clear and not too confusing.
>
> Signed-off-by: Alice Ryhl <aliceryhl@google.com>
Reviewed-by: Andreas Hindborg <a.hindborg@kernel.org>
Best regards,
Andreas Hindborg
next prev parent reply other threads:[~2025-02-28 18:58 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-28 12:39 [PATCH v2 0/5] Check Rust signatures at compile time Alice Ryhl
2025-02-28 12:39 ` [PATCH v2 1/5] rust: fix signature of rust_fmt_argument Alice Ryhl
2025-02-28 15:13 ` Tamir Duberstein
2025-02-28 12:39 ` [PATCH v2 2/5] rust: macros: support additional tokens in quote! Alice Ryhl
2025-02-28 15:25 ` Tamir Duberstein
2025-03-03 8:17 ` Alice Ryhl
2025-02-28 18:51 ` Andreas Hindborg
2025-02-28 12:39 ` [PATCH v2 3/5] rust: add #[export] macro Alice Ryhl
2025-02-28 15:40 ` Tamir Duberstein
2025-02-28 15:49 ` Miguel Ojeda
2025-02-28 15:51 ` Tamir Duberstein
2025-03-03 8:28 ` Alice Ryhl
2025-02-28 18:53 ` Andreas Hindborg [this message]
2025-02-28 12:39 ` [PATCH v2 4/5] print: use new #[export] macro for rust_fmt_argument Alice Ryhl
2025-02-28 15:36 ` Andy Shevchenko
2025-02-28 17:11 ` Alice Ryhl
2025-02-28 15:48 ` Tamir Duberstein
2025-02-28 12:39 ` [PATCH v2 5/5] panic_qr: use new #[export] macro Alice Ryhl
2025-02-28 15:34 ` Andy Shevchenko
2025-02-28 17:06 ` Alice Ryhl
2025-02-28 15:54 ` Tamir Duberstein
2025-02-28 17:08 ` Alice Ryhl
2025-02-28 17:15 ` Tamir Duberstein
2025-03-03 8:52 ` Alice Ryhl
2025-02-28 17:06 ` Simona Vetter
2025-02-28 12:45 ` [PATCH v2 0/5] Check Rust signatures at compile time Andy Shevchenko
2025-02-28 13:12 ` Miguel Ojeda
2025-02-28 13:17 ` Alice Ryhl
2025-03-01 4:43 ` Greg Kroah-Hartman
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=87y0xplx3o.fsf@kernel.org \
--to=a.hindborg@kernel.org \
--cc=airlied@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=aliceryhl@google.com \
--cc=andriy.shevchenko@linux.intel.com \
--cc=benno.lossin@proton.me \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=gary@garyguo.net \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@rasmusvillemoes.dk \
--cc=maarten.lankhorst@linux.intel.com \
--cc=mripard@kernel.org \
--cc=ojeda@kernel.org \
--cc=pmladek@suse.com \
--cc=rostedt@goodmis.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=senozhatsky@chromium.org \
--cc=simona@ffwll.ch \
--cc=tmgross@umich.edu \
--cc=tzimmermann@suse.de \
/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).