Skip to contentSkip to conversation

rust: macros: Allow omitting trailing commas in module!

PATCH In review Ethan Plant 3 participants 3 messages Aug 6

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rust: macros: Allow omitting trailing commas in module!

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.

---
 rust/macros/lib.rs    | 4 +++-
 rust/macros/module.rs | 7 ++++++-
 2 files changed, 9 insertions(+), 2 deletions(-)
rust/macros/lib.rs +3 -1
index 4a48fabbc2682..f6474b194687f 100644
@@ -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;
rust/macros/module.rs +6 -1
index 06c18e2075083..57ee79b49a7ec 100644
@@ -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,

Signature · 1 line
Ethan Plant <[email protected]>
reviewer
#1 lore raw
Quoted 10 lines · Ethan Plant

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

Quoted 58 lines
---
 rust/macros/lib.rs    | 4 +++-
 rust/macros/module.rs | 7 ++++++-
 2 files changed, 9 insertions(+), 2 deletions(-)
rust/macros/lib.rs +3 -1
index 4a48fabbc2682..f6474b194687f 100644
@@ -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;
rust/macros/module.rs +6 -2
index 06c18e2075083..57ee79b49a7ec 100644
@@ -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,

Signature · 1 line
Ethan Plant <[email protected]>
#2 lore raw
Quoted 7 lines · Gary Guo

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.

Yeah, it also means inconsistency can be introduced for little gain.

Cheers,
Miguel