rust: macros: Allow omitting trailing commas in module!
Subsystems (2)
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
@@ -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
@@ -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 keyin REQUIRED_KEYS {
---
base-commit: dc01dfb37b34beeefcfe1c3055364d41a4070c7e
change-id: 20260806-module-optional-trailing-comma-44a336ddf649
Best regards,
Signature · 1 line
Ethan Plant <[email protected]>
Gary Guo
reviewerQuoted 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
Suggested-by Benno Lossin <[email protected]>Signed-off-by Ethan Plant <[email protected]>--- rust/macros/lib.rs | 4 +++- rust/macros/module.rs | 7 ++++++- 2 files changed, 9 insertions(+), 2 deletions(-)rust/macros/lib.rs +3
-1@@ -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@@ -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 keyin REQUIRED_KEYS { ---base-commit:
dc01dfb37b34beeefcfe1c3055364d41a4070c7e
change-id: 20260806-module-optional-trailing-comma-44a336ddf649Best regards,
Signature · 1 line
Ethan Plant <[email protected]>
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