rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Gary Guo <gary@kernel.org>
To: "Miguel Ojeda" <ojeda@kernel.org>,
	"Boqun Feng" <boqun.feng@gmail.com>,
	"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>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	"Guilherme Giacomo Simoes" <trintaeoitogc@gmail.com>,
	"Tamir Duberstein" <tamird@gmail.com>
Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH 09/11] rust: macros: allow arbitrary types to be used in `module!` macro
Date: Thu, 11 Dec 2025 18:56:49 +0000	[thread overview]
Message-ID: <20251211185805.2835633-10-gary@kernel.org> (raw)
In-Reply-To: <20251211185805.2835633-1-gary@kernel.org>

From: Gary Guo <gary@garyguo.net>

Previously this only accepts an identifier, but now with `syn` it is
easy to make it accepts any type.

With this change, the span of types are preserved -- as a benefit, Rust
analyzer will be able to use the "navigate to definition" feature on
type name inside `module!` macro invocation.

Signed-off-by: Gary Guo <gary@garyguo.net>
---
 rust/macros/module.rs | 29 +++++++++++++++--------------
 1 file changed, 15 insertions(+), 14 deletions(-)

diff --git a/rust/macros/module.rs b/rust/macros/module.rs
index 4a02aadef25a7..d6298d04c86f4 100644
--- a/rust/macros/module.rs
+++ b/rust/macros/module.rs
@@ -4,7 +4,6 @@
 
 use proc_macro2::{
     Literal,
-    Span,
     TokenStream, //
 };
 use quote::{
@@ -23,7 +22,8 @@
     Ident,
     LitStr,
     Result,
-    Token, //
+    Token,
+    Type, //
 };
 
 use crate::helpers::*;
@@ -105,7 +105,7 @@ mod kw {
 
 #[allow(dead_code, reason = "some fields are only parsed into")]
 enum ModInfoField {
-    Type(Token![type], Token![:], Ident),
+    Type(Token![type], Token![:], Type),
     Name(kw::name, Token![:], AsciiLitStr),
     Authors(
         kw::authors,
@@ -193,9 +193,9 @@ fn parse(input: ParseStream<'_>) -> Result<Self> {
     }
 }
 
-#[derive(Debug, Default)]
+#[derive(Default)]
 pub(crate) struct ModuleInfo {
-    type_: String,
+    type_: Option<Type>,
     license: String,
     name: String,
     authors: Option<Vec<String>>,
@@ -237,7 +237,7 @@ fn parse(input: ParseStream<'_>) -> Result<Self> {
             seen_keys.push(key);
 
             match field {
-                ModInfoField::Type(_, _, ty) => info.type_ = ty.to_string(),
+                ModInfoField::Type(_, _, ty) => info.type_ = Some(ty),
                 ModInfoField::Name(_, _, name) => info.name = name.value(),
                 ModInfoField::Authors(_, _, _, list) => {
                     info.authors = Some(list.into_iter().map(|x| x.value()).collect())
@@ -286,16 +286,17 @@ fn parse(input: ParseStream<'_>) -> Result<Self> {
 
 pub(crate) fn module(info: ModuleInfo) -> Result<TokenStream> {
     let ModuleInfo {
-        type_,
+        type_: Some(type_),
         license,
         name,
         authors,
         description,
         alias,
         firmware,
-    } = info;
-
-    let type_ = Ident::new(&type_, Span::mixed_site());
+    } = info
+    else {
+        unreachable!();
+    };
 
     // Rust does not allow hyphens in identifiers, use underscore instead.
     let ident = name.replace('-', "_");
@@ -376,7 +377,6 @@ impl ::kernel::ModuleMetadata for #type_ {
         // Double nested modules, since then nobody can access the public items inside.
         mod __module_init {
             mod __module_init {
-                use super::super::#type_;
                 use pin_init::PinInit;
 
                 /// The "Rust loadable module" mark.
@@ -388,7 +388,7 @@ mod __module_init {
                 #[used(compiler)]
                 static __IS_RUST_MODULE: () = ();
 
-                static mut __MOD: ::core::mem::MaybeUninit<#type_> =
+                static mut __MOD: ::core::mem::MaybeUninit<super::super::LocalModule> =
                     ::core::mem::MaybeUninit::uninit();
 
                 // Loadable modules need to export the `{init,cleanup}_module` identifiers.
@@ -475,8 +475,9 @@ pub extern "C" fn #ident_exit() {
                 ///
                 /// This function must only be called once.
                 unsafe fn __init() -> ::kernel::ffi::c_int {
-                    let initer =
-                        <#type_ as ::kernel::InPlaceModule>::init(&super::super::THIS_MODULE);
+                    let initer = <super::super::LocalModule as ::kernel::InPlaceModule>::init(
+                        &super::super::THIS_MODULE
+                    );
                     // SAFETY: No data race, since `__MOD` can only be accessed by this module
                     // and there only `__init` and `__exit` access it. These functions are only
                     // called once and `__exit` cannot be called before or during `__init`.
-- 
2.51.2


  parent reply	other threads:[~2025-12-11 19:30 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-12-11 18:56 [PATCH 00/11] refactor Rust proc macros with `syn` Gary Guo
2025-12-11 18:56 ` [PATCH 01/11] rust: pin-init: internal: remove proc-macro[2] and quote workarounds Gary Guo
2025-12-11 21:50   ` Christian Schrefl
2025-12-15 13:01     ` Gary Guo
2025-12-16  9:43       ` Benno Lossin
2025-12-11 18:56 ` [PATCH 02/11] rust: macros: use `quote!` from vendored crate Gary Guo
2025-12-16  9:47   ` Benno Lossin
2025-12-11 18:56 ` [PATCH 03/11] rust: macros: convert `#[vtable]` macro to use `syn` Gary Guo
2025-12-11 18:56 ` [PATCH 04/11] rust: macros: use `syn` to parse `module!` macro Gary Guo
2025-12-11 18:56 ` [PATCH 05/11] rust: macros: use `quote!` for " Gary Guo
2025-12-11 18:56 ` [PATCH 06/11] rust: macros: convert `#[export]` to use `syn` Gary Guo
2025-12-11 18:56 ` [PATCH 07/11] rust: macros: convert `concat_idents!` " Gary Guo
2025-12-11 18:56 ` [PATCH 08/11] rust: macros: convert `#[kunit_tests]` macro " Gary Guo
2025-12-11 18:56 ` Gary Guo [this message]
2025-12-11 18:56 ` [PATCH 10/11] rust: macros: rearrange `#[doc(hidden)]` in `module!` macro Gary Guo
2025-12-11 18:56 ` [PATCH 11/11] rust: kunit: use `pin_init::zeroed` instead of custom null value Gary Guo
2025-12-16 12:48   ` Benno Lossin

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=20251211185805.2835633-10-gary@kernel.org \
    --to=gary@kernel.org \
    --cc=a.hindborg@kernel.org \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=dakr@kernel.org \
    --cc=gary@garyguo.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tamird@gmail.com \
    --cc=tmgross@umich.edu \
    --cc=trintaeoitogc@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).