Alexandre Courbot
2025-Sep-30 04:58 UTC
[PATCH v2 2/5] rust: macros: extend custom `quote!()` macro
Hi Jesung, On Fri Aug 15, 2025 at 2:32 PM JST, Jesung Yang wrote:> Extend the `quote_spanned!()` macro to support additional punctuation > tokens: `->`, `<`, `>`, and `==`. This symbols are commonly needed when > dealing with functions, generic bounds, and equality comparisons. > > Tested-by: Alexandre Courbot <acourbot at nvidia.com> > Signed-off-by: Jesung Yang <y.j3ms.n at gmail.com> > ---Note that this patch doesn't apply cleanly in `rust-next`, I've had to add the following on top of it. I suggest waiting for -rc1 to be released and using it as a base for a new version - hopefully this will also give time for more feedback to come. diff --git a/rust/macros/quote.rs b/rust/macros/quote.rs index 76a99f7e01c4..bb6970fd2a26 100644 --- a/rust/macros/quote.rs +++ b/rust/macros/quote.rs @@ -147,33 +147,33 @@ macro_rules! quote_spanned { quote_spanned!(@proc $v $span $($tt)*); }; (@proc $v:ident $span:ident -> $($tt:tt)*) => { - $v.push(::proc_macro::TokenTree::Punct( + $v.extend([::proc_macro::TokenTree::Punct( ::proc_macro::Punct::new('-', ::proc_macro::Spacing::Joint) - )); - $v.push(::proc_macro::TokenTree::Punct( + )]); + $v.extend([::proc_macro::TokenTree::Punct( ::proc_macro::Punct::new('>', ::proc_macro::Spacing::Alone) - )); + )]); quote_spanned!(@proc $v $span $($tt)*); }; (@proc $v:ident $span:ident < $($tt:tt)*) => { - $v.push(::proc_macro::TokenTree::Punct( + $v.extend([::proc_macro::TokenTree::Punct( ::proc_macro::Punct::new('<', ::proc_macro::Spacing::Alone) - )); + )]); quote_spanned!(@proc $v $span $($tt)*); }; (@proc $v:ident $span:ident > $($tt:tt)*) => { - $v.push(::proc_macro::TokenTree::Punct( + $v.extend([::proc_macro::TokenTree::Punct( ::proc_macro::Punct::new('>', ::proc_macro::Spacing::Alone) - )); + )]); quote_spanned!(@proc $v $span $($tt)*); }; (@proc $v:ident $span:ident == $($tt:tt)*) => { - $v.push(::proc_macro::TokenTree::Punct( + $v.extend([::proc_macro::TokenTree::Punct( ::proc_macro::Punct::new('=', ::proc_macro::Spacing::Joint) - )); - $v.push(::proc_macro::TokenTree::Punct( + )]); + $v.extend([::proc_macro::TokenTree::Punct( ::proc_macro::Punct::new('=', ::proc_macro::Spacing::Alone) - )); + )]); quote_spanned!(@proc $v $span $($tt)*); }; (@proc $v:ident $span:ident # $($tt:tt)*) => {