From: "Gary Guo" <gary@garyguo.net>
To: "Alvin Sun" <alvin.sun@linux.dev>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
"Luis Chamberlain" <mcgrof@kernel.org>,
"Petr Pavlu" <petr.pavlu@suse.com>,
"Daniel Gomez" <da.gomez@kernel.org>,
"Sami Tolvanen" <samitolvanen@google.com>,
"Aaron Tomlin" <atomlin@atomlin.com>,
"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
"Rafael J. Wysocki" <rafael@kernel.org>,
"David Airlie" <airlied@gmail.com>,
"Simona Vetter" <simona@ffwll.ch>,
"Daniel Almeida" <daniel.almeida@collabora.com>,
"Arnd Bergmann" <arnd@arndb.de>,
"Brendan Higgins" <brendan.higgins@linux.dev>,
"David Gow" <david@davidgow.net>,
"Rae Moar" <raemoar63@gmail.com>,
"Breno Leitao" <leitao@debian.org>,
"Jens Axboe" <axboe@kernel.dk>
Cc: <rust-for-linux@vger.kernel.org>, <linux-modules@vger.kernel.org>,
<driver-core@lists.linux.dev>, <dri-devel@lists.freedesktop.org>,
<nova-gpu@lists.linux.dev>, <linux-kselftest@vger.kernel.org>,
<kunit-dev@googlegroups.com>, <linux-block@vger.kernel.org>
Subject: Re: [PATCH v2 2/7] rust: macros: auto-insert ThisModule in #[vtable]
Date: Thu, 18 Jun 2026 15:13:20 +0100 [thread overview]
Message-ID: <DJC8JSYDERVX.2QI76YCBHDA93@garyguo.net> (raw)
In-Reply-To: <20260521-fix-fops-owner-v2-2-fd99079c5a04@linux.dev>
On Thu May 21, 2026 at 8:52 AM BST, Alvin Sun wrote:
> Auto-add `type ThisModule: ::kernel::ModuleMetadata;` as a required
> associated type on the trait side if not already defined, and
> auto-insert `type ThisModule = crate::LocalModule;` on the impl side
> if not explicitly provided, eliminating the need to manually declare
> and implement `ThisModule` in every vtable trait and impl.
>
> Signed-off-by: Alvin Sun <alvin.sun@linux.dev>
Suggested-by: Gary Guo <gary@garyguo.net>
Link: https://lore.kernel.org/all/DIMMWHUOLPSH.13JFRHDKDQJGO@garyguo.net
> ---
> rust/macros/lib.rs | 6 ++++++
> rust/macros/vtable.rs | 38 +++++++++++++++++++++++++++++++++++++-
> 2 files changed, 43 insertions(+), 1 deletion(-)
>
> diff --git a/rust/macros/lib.rs b/rust/macros/lib.rs
> index 2cfd59e0f9e7c..d35e45ea745c0 100644
> --- a/rust/macros/lib.rs
> +++ b/rust/macros/lib.rs
> @@ -176,6 +176,12 @@ pub fn module(input: TokenStream) -> TokenStream {
> ///
> /// This macro should not be used when all functions are required.
> ///
> +/// Additionally, this macro automatically handles the `ThisModule`
> +/// associated type: on the trait side, `type ThisModule: ModuleMetadata;`
> +/// is added as a required associated type if not already defined; on the
> +/// impl side, `type ThisModule = LocalModule;` is automatically inserted
> +/// if not explicitly defined.
> +///
> /// # Examples
> ///
> /// ```
> diff --git a/rust/macros/vtable.rs b/rust/macros/vtable.rs
> index c6510b0c4ea1d..d3d0e9cbd7172 100644
> --- a/rust/macros/vtable.rs
> +++ b/rust/macros/vtable.rs
> @@ -23,6 +23,7 @@
>
> fn handle_trait(mut item: ItemTrait) -> Result<ItemTrait> {
> let mut gen_items = Vec::new();
> + let mut has_this_module = false;
>
> gen_items.push(parse_quote! {
> /// A marker to prevent implementors from forgetting to use [`#[vtable]`](vtable)
> @@ -30,6 +31,28 @@ fn handle_trait(mut item: ItemTrait) -> Result<ItemTrait> {
> const USE_VTABLE_ATTR: ();
> });
>
> + // Detect existing type ThisModule so we don't add a duplicate.
> + for i in &item.items {
> + if let TraitItem::Type(type_item) = i {
> + if type_item.ident == "ThisModule" {
> + has_this_module = true;
> + }
> + }
> + }
> +
> + // Add `type ThisModule: ModuleMetadata` as a required associated type if
> + // the trait does not already define it. No default is used because
> + // `associated_type_defaults` is unstable (issue #29661).
I don't think this is relevant. What's the sensible default anyway?
> + if !has_this_module {
Perhaps just make this an one liner :
if !item.items.iter().any(|i| matches!(item, TraitItem::Type(t) if t.ident == "ThisModule")) {
> + gen_items.push(parse_quote! {
> + /// The module implementing this vtable trait.
> + ///
> + /// Automatically set to `crate::LocalModule` by the `#[vtable]`
> + /// impl macro.
> + type ThisModule: ::kernel::ModuleMetadata;
> + });
> + }
> +
> for item in &item.items {
> if let TraitItem::Fn(fn_item) = item {
> let name = &fn_item.sig.ident;
> @@ -58,18 +81,31 @@ fn handle_trait(mut item: ItemTrait) -> Result<ItemTrait> {
> fn handle_impl(mut item: ItemImpl) -> Result<ItemImpl> {
> let mut gen_items = Vec::new();
> let mut defined_consts = HashSet::new();
> + let mut defined_types = HashSet::new();
I'd just rename `defined_consts` to `defined_items` to reuse the same set as
there cannot be assoc items with same name anyway.
Best,
Gary
>
> - // Iterate over all user-defined constants to gather any possible explicit overrides.
> + // Iterate over all user-defined constants and types to gather any possible explicit overrides.
> for item in &item.items {
> if let ImplItem::Const(const_item) = item {
> defined_consts.insert(const_item.ident.clone());
> }
> + if let ImplItem::Type(type_item) = item {
> + defined_types.insert(type_item.ident.clone());
> + }
> }
>
> gen_items.push(parse_quote! {
> const USE_VTABLE_ATTR: () = ();
> });
>
> + // Auto-insert `type ThisModule = crate::LocalModule` if not explicitly defined.
> + // `crate::LocalModule` resolves to the real module type (via `module!`) or a
> + // dummy fallback in non-module contexts (e.g., doctests).
> + if !defined_types.contains(&parse_quote!(ThisModule)) {
> + gen_items.push(parse_quote! {
> + type ThisModule = crate::LocalModule;
> + });
> + }
> +
> for item in &item.items {
> if let ImplItem::Fn(fn_item) = item {
> let name = &fn_item.sig.ident;
next prev parent reply other threads:[~2026-06-18 14:13 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-21 7:52 [PATCH v2 0/7] Fix missing fops.owner in Rust DRM/misc abstractions Alvin Sun
2026-05-21 7:52 ` [PATCH v2 1/7] rust: module: add `THIS_MODULE` const to `ModuleMetadata` trait Alvin Sun
2026-06-18 12:04 ` Andreas Hindborg
2026-06-18 14:16 ` Gary Guo
2026-05-21 7:52 ` [PATCH v2 2/7] rust: macros: auto-insert ThisModule in #[vtable] Alvin Sun
2026-06-18 12:11 ` Andreas Hindborg
2026-06-18 14:13 ` Gary Guo [this message]
2026-05-21 7:52 ` [PATCH v2 3/7] rust: doctest: add LocalModule fallback for #[vtable] ThisModule Alvin Sun
2026-06-18 12:13 ` Andreas Hindborg
2026-05-21 7:52 ` [PATCH v2 4/7] rust: drm: set fops.owner from driver module pointer Alvin Sun
2026-06-18 14:15 ` Gary Guo
2026-05-21 7:52 ` [PATCH v2 5/7] rust: miscdevice: " Alvin Sun
2026-05-21 7:52 ` [PATCH v2 6/7] rust: configfs: use `LocalModule` for `THIS_MODULE` Alvin Sun
2026-05-21 7:52 ` [PATCH v2 7/7] block: rnull: " Alvin Sun
2026-06-18 12:17 ` Andreas Hindborg
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=DJC8JSYDERVX.2QI76YCBHDA93@garyguo.net \
--to=gary@garyguo.net \
--cc=a.hindborg@kernel.org \
--cc=airlied@gmail.com \
--cc=aliceryhl@google.com \
--cc=alvin.sun@linux.dev \
--cc=arnd@arndb.de \
--cc=atomlin@atomlin.com \
--cc=axboe@kernel.dk \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=brendan.higgins@linux.dev \
--cc=da.gomez@kernel.org \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=david@davidgow.net \
--cc=dri-devel@lists.freedesktop.org \
--cc=driver-core@lists.linux.dev \
--cc=gregkh@linuxfoundation.org \
--cc=kunit-dev@googlegroups.com \
--cc=leitao@debian.org \
--cc=linux-block@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-modules@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=mcgrof@kernel.org \
--cc=nova-gpu@lists.linux.dev \
--cc=ojeda@kernel.org \
--cc=petr.pavlu@suse.com \
--cc=raemoar63@gmail.com \
--cc=rafael@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=samitolvanen@google.com \
--cc=simona@ffwll.ch \
--cc=tmgross@umich.edu \
/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