rust: macros: support additional tokens in quote!
authorAlice Ryhl <aliceryhl@google.com>
Mon, 3 Mar 2025 08:45:13 +0000 (08:45 +0000)
committerMiguel Ojeda <ojeda@kernel.org>
Sun, 9 Mar 2025 19:52:46 +0000 (20:52 +0100)
commit85525eda4f13c496defc46712348fe0711a59b2b
treecf3962a22b8fb10a084c24d29b434b740d559672
parent901b3290bd4dc35e613d13abd03c129e754dd3dd
rust: macros: support additional tokens in quote!

This gives the quote! macro support for the following additional tokens:

* The = token.
* The _ token.
* The # token. (when not followed by an identifier)
* Using #my_var with variables of type Ident.

Additionally, some type annotations are added to allow cases where
groups are empty. For example, quote! does support () in the input, but
only when it is *not* empty. When it is empty, there are zero `.push`
calls, so the compiler can't infer the item type and also emits a
warning about it not needing to be mutable.

These additional quote! features are used by a new proc macro that
generates code looking like this:

const _: () = {
    if true {
        ::kernel::bindings::#name
    } else {
        #name
    };
};

where #name has type Ident.

Reviewed-by: Andreas Hindborg <a.hindborg@kernel.org>
Reviewed-by: Tamir Duberstein <tamird@gmail.com>
Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Alice Ryhl <aliceryhl@google.com>
Link: https://lore.kernel.org/r/20250303-export-macro-v3-2-41fbad85a27f@google.com
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
rust/macros/quote.rs