Re: [PATCH 07/11] rust: macros: convert `concat_idents!` to use `syn`

Gary Guo
2025-12-22T12:00:06Z

On Mon, 22 Dec 2025 07:34:24 +0100
"Benno Lossin" <lossin@kernel.org> wrote:

> On Thu Dec 11, 2025 at 7:56 PM CET, Gary Guo wrote:
> > From: Gary Guo <gary@garyguo.net>
> >
> > This eliminates the need for `expect_punct` helper.
>
> This commit message could use some more details :)
>
> > Signed-off-by: Gary Guo <gary@garyguo.net>
>
> Reviewed-by: Benno Lossin <lossin@kernel.org>
>
> > ---
> > rust/macros/concat_idents.rs | 39 ++++++++++++++++++++++++------------
> > rust/macros/helpers.rs | 14 +------------
> > rust/macros/lib.rs | 4 ++--
> > 3 files changed, 29 insertions(+), 28 deletions(-)
>
> > -pub(crate) fn concat_idents(ts: TokenStream) -> TokenStream {
> > - let mut it = ts.into_iter();
> > - let a = expect_ident(&mut it);
> > - assert_eq!(expect_punct(&mut it), ',');
> > - let b = expect_ident(&mut it);
> > - assert!(it.next().is_none(), "only two idents can be concatenated");
> > +pub(crate) fn concat_idents(Input { a, b, .. }: Input) -> TokenStream {
> > let res = Ident::new(&format!("{a}{b}"), b.span());
>
> There is `quote::format_ident!`, which directly returns an identifier,
> but with the span of the first of the two. Not sure if that is better.

All our use cases have a prefix concatenated to a changing identifier,
hence the existing implementation use b's span.

Best,
Gary

>
> Cheers,
> Benno
>
> > TokenStream::from_iter([TokenTree::Ident(res)])
> > }