Alice Ryhl
2025-Aug-28 06:39 UTC
[PATCH v2 2/5] rust: macros: extend custom `quote!()` macro
On Fri, Aug 15, 2025 at 7:32?AM Jesung Yang <y.j3ms.n at gmail.com> 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> > --- > rust/macros/quote.rs | 30 ++++++++++++++++++++++++++++++ > 1 file changed, 30 insertions(+) > > diff --git a/rust/macros/quote.rs b/rust/macros/quote.rs > index 8a89f0b1e785..24764b04a07d 100644 > --- a/rust/macros/quote.rs > +++ b/rust/macros/quote.rs > @@ -150,6 +150,36 @@ macro_rules! quote_spanned { > )); > quote_spanned!(@proc $v $span $($tt)*); > }; > + (@proc $v:ident $span:ident -> $($tt:tt)*) => { > + $v.push(::proc_macro::TokenTree::Punct( > + ::proc_macro::Punct::new('-', ::proc_macro::Spacing::Joint) > + )); > + $v.push(::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( > + ::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( > + ::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( > + ::proc_macro::Punct::new('=', ::proc_macro::Spacing::Joint) > + )); > + $v.push(::proc_macro::TokenTree::Punct( > + ::proc_macro::Punct::new('=', ::proc_macro::Spacing::Alone) > + )); > + quote_spanned!(@proc $v $span $($tt)*);Not a blocker, but if the way to implement this one is to push twice, then I think the pattern should just be a single = and then you push a = once. The pattern can match twice to handle ==. Alice
Benno Lossin
2025-Aug-28 07:14 UTC
[PATCH v2 2/5] rust: macros: extend custom `quote!()` macro
On Thu Aug 28, 2025 at 8:39 AM CEST, Alice Ryhl wrote:> On Fri, Aug 15, 2025 at 7:32?AM Jesung Yang <y.j3ms.n at gmail.com> wrote: >> + (@proc $v:ident $span:ident == $($tt:tt)*) => { >> + $v.push(::proc_macro::TokenTree::Punct( >> + ::proc_macro::Punct::new('=', ::proc_macro::Spacing::Joint) >> + )); >> + $v.push(::proc_macro::TokenTree::Punct( >> + ::proc_macro::Punct::new('=', ::proc_macro::Spacing::Alone) >> + )); >> + quote_spanned!(@proc $v $span $($tt)*); > > Not a blocker, but if the way to implement this one is to push > twice, then I think the pattern should just be a single = and then you > push a = once. The pattern can match twice to handle ==.You can't do that, since the first one needs the `Joint` spacing and the second one the `Alone` one. `==` also is a single token in macro input, so matching only on `=` doesn't work. --- Cheers, Benno