Skip to contentSkip to conversation

rust: add support for trailing comma in fmt! macros with named format arguments

PATCH New Jack Whitehorn 1 participant 1 message Mar 6 Rust

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rust: add support for trailing comma in fmt! macros with named format arguments

Fixes an issue with fmt! and similar macros where the combination of a trailing comma and one or more named format arguments would cause the macro to fail.

Tested using a modified rust_minimal module, and checked that the rust samples and some rust drivers still compile.

---
 rust/macros/fmt.rs | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
rust/macros/fmt.rs +4 -5
index ce6c7249305a..a1d64c0d85fe 100644
@@ -50,6 +50,7 @@ pub(crate) fn fmt(input: TokenStream) -> TokenStream {
     let adapter = quote_spanned!(first_span => ::kernel::fmt::Adapter);
 
     let mut args = TokenStream::from_iter(first_opt);
+
     {
         let mut flush = |args: &mut TokenStream, current: &mut TokenStream| {
             let current = std::mem::take(current);
@@ -69,7 +70,7 @@ pub(crate) fn fmt(input: TokenStream) -> TokenStream {
                     }
                     (None, acc)
                 })();
-                args.extend(quote_spanned!(first_span => #lhs #adapter(&(#rhs))));
+                args.extend(quote_spanned!(first_span => , #lhs #adapter(&(#rhs))));
             }
         };
 
@@ -78,11 +79,9 @@ pub(crate) fn fmt(input: TokenStream) -> TokenStream {
             match &tt {
                 TokenTree::Punct(p) if p.as_char() == ',' => {
                     flush(&mut args, &mut current);
-                    &mut args
                 }
-                _ => &mut current,
-            }
-            .extend([tt]);
+                _ => current.extend([tt]),
+            };
         }
         flush(&mut args, &mut current);
     }
Signature · 1 line
2.53.0