[PATCH v2 2/5] rust: macros: extend custom `quote!()` macro

Jesung Yang posted 5 patches 1 month, 2 weeks ago
[PATCH v2 2/5] rust: macros: extend custom `quote!()` macro
Posted by Jesung Yang 1 month, 2 weeks ago
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@nvidia.com>
Signed-off-by: Jesung Yang <y.j3ms.n@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)*);
+    };
     (@proc $v:ident $span:ident = $($tt:tt)*) => {
         $v.push(::proc_macro::TokenTree::Punct(
                 ::proc_macro::Punct::new('=', ::proc_macro::Spacing::Alone)
-- 
2.39.5
Re: [PATCH v2 2/5] rust: macros: extend custom `quote!()` macro
Posted by Alexandre Courbot 4 days, 9 hours ago
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@nvidia.com>
> Signed-off-by: Jesung Yang <y.j3ms.n@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)*) => {
Re: [PATCH v2 2/5] rust: macros: extend custom `quote!()` macro
Posted by Jesung Yang 4 days, 7 hours ago
Hi,

On Tue, Sep 30, 2025 at 1:58 PM Alexandre Courbot <acourbot@nvidia.com> wrote:
[...]
> 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@nvidia.com>
> > Signed-off-by: Jesung Yang <y.j3ms.n@gmail.com>
> > ---
>
> Note that this patch doesn't apply cleanly in `rust-next`, I've had to
> add the following on top of it.

Thanks for pointing out the conflict. I see that the commit
9578c3906c7d ("rust: macros: reduce collections in `quote!` macro") was
added after my patch series.

> 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.

Sure, happy to wait until -rc1 and perhaps we'll get more feedback by
then. Once it's out, I'll rebase on top of it and send v3.

Best Regards,
Jesung

>
> 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)*) => {
Re: [PATCH v2 2/5] rust: macros: extend custom `quote!()` macro
Posted by Alice Ryhl 1 month, 1 week ago
On Fri, Aug 15, 2025 at 7:32 AM Jesung Yang <y.j3ms.n@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@nvidia.com>
> Signed-off-by: Jesung Yang <y.j3ms.n@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
Re: [PATCH v2 2/5] rust: macros: extend custom `quote!()` macro
Posted by Benno Lossin 1 month, 1 week ago
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@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
Re: [PATCH v2 2/5] rust: macros: extend custom `quote!()` macro
Posted by Alice Ryhl 1 month, 1 week ago
On Thu, Aug 28, 2025 at 9:14 AM Benno Lossin <lossin@kernel.org> wrote:
>
> 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@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.

Ah, ok. LGTM then.