Re: [PATCH] rust: macros: Allow omitting trailing commas in module!

From: Gary Guo

Date: Thu Aug 06 2026 - 07:55:25 EST


On Thu Aug 6, 2026 at 11:14 AM BST, Ethan Plant wrote:
> The module! macro currently requires a trailing comma after the final
> argument, despite using syntax modeled on Rust struct initializers,
> where trailing commas are optional.
>
> Allow parsing to finish after the final field when the input stream is
> empty. If more input remains, continue requiring a comma, so missing
> separators between fields are still rejected.
>
> Document that the final trailing comma is optional and update an existing
> module! doctest to exercise the syntax without one.

Hmm, I am not sure if we really want this.

`module!` invocation usually spans multiple lines so the formatting rules would
always prefer adding trailing comma for this case.

Allowing omitting sounds like encouraging bad practice.

Best,
Gary

>
> Suggested-by: Benno Lossin <lossin@xxxxxxxxxx>
> Link: https://github.com/Rust-for-Linux/linux/issues/1172
> Signed-off-by: Ethan Plant <plant.ethan@xxxxxxxxx>
> ---
> rust/macros/lib.rs | 4 +++-
> rust/macros/module.rs | 7 ++++++-
> 2 files changed, 9 insertions(+), 2 deletions(-)
>
> diff --git a/rust/macros/lib.rs b/rust/macros/lib.rs
> index 4a48fabbc2682..f6474b194687f 100644
> --- a/rust/macros/lib.rs
> +++ b/rust/macros/lib.rs
> @@ -61,6 +61,8 @@
> ///
> /// [`Module`]: ../kernel/trait.Module.html
> ///
> +/// The trailing comma after the final field is optional.
> +///
> /// # Examples
> ///
> /// ```ignore
> @@ -112,7 +114,7 @@
> /// authors: ["Rust for Linux Contributors"],
> /// description: "My device driver requires firmware",
> /// license: "GPL",
> -/// firmware: ["my_device_firmware1.bin", "my_device_firmware2.bin"],
> +/// firmware: ["my_device_firmware1.bin", "my_device_firmware2.bin"]
> /// }
> ///
> /// struct MyDeviceDriverModule;
> diff --git a/rust/macros/module.rs b/rust/macros/module.rs
> index 06c18e2075083..57ee79b49a7ec 100644
> --- a/rust/macros/module.rs
> +++ b/rust/macros/module.rs
> @@ -261,8 +261,13 @@ macro_rules! parse_ordered_fields {
> }
> }
>
> - $input.parse::<Token![,]>()?;
> seen_keys.push(key);
> +
> + if $input.is_empty() {
> + break;
> + }
> +
> + $input.parse::<Token![,]>()?;
> }
>
> for key in REQUIRED_KEYS {
>
> ---
> base-commit: dc01dfb37b34beeefcfe1c3055364d41a4070c7e
> change-id: 20260806-module-optional-trailing-comma-44a336ddf649
>
> Best regards,
> --
> Ethan Plant <plant.ethan@xxxxxxxxx>