[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAH5fLgjgfgaoMP7TD46u9OsYuje6yA4ooM+6eCd5whAx+tiTqg@mail.gmail.com>
Date: Mon, 3 Mar 2025 09:17:03 +0100
From: Alice Ryhl <aliceryhl@...gle.com>
To: Tamir Duberstein <tamird@...il.com>
Cc: Greg Kroah-Hartman <gregkh@...uxfoundation.org>, Miguel Ojeda <ojeda@...nel.org>,
Petr Mladek <pmladek@...e.com>, Steven Rostedt <rostedt@...dmis.org>,
Andy Shevchenko <andriy.shevchenko@...ux.intel.com>,
Rasmus Villemoes <linux@...musvillemoes.dk>, Sergey Senozhatsky <senozhatsky@...omium.org>,
Andrew Morton <akpm@...ux-foundation.org>, Boqun Feng <boqun.feng@...il.com>,
Gary Guo <gary@...yguo.net>, Björn Roy Baron <bjorn3_gh@...tonmail.com>,
Benno Lossin <benno.lossin@...ton.me>, Andreas Hindborg <a.hindborg@...nel.org>,
Trevor Gross <tmgross@...ch.edu>, Maarten Lankhorst <maarten.lankhorst@...ux.intel.com>,
Maxime Ripard <mripard@...nel.org>, Thomas Zimmermann <tzimmermann@...e.de>,
David Airlie <airlied@...il.com>, Simona Vetter <simona@...ll.ch>, linux-kernel@...r.kernel.org,
rust-for-linux@...r.kernel.org, dri-devel@...ts.freedesktop.org
Subject: Re: [PATCH v2 2/5] rust: macros: support additional tokens in quote!
On Fri, Feb 28, 2025 at 4:26 PM Tamir Duberstein <tamird@...il.com> wrote:
>
> On Fri, Feb 28, 2025 at 7:42 AM Alice Ryhl <aliceryhl@...gle.com> wrote:
> >
> > This gives the quote! macro support for the following additional tokens:
> >
> > * The = token.
> > * The _ token.
> > * 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, the compiler cannot infer
> > the item type of `tokens`.
> >
> > 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.
> >
> > Signed-off-by: Alice Ryhl <aliceryhl@...gle.com>
> > ---
> > rust/macros/quote.rs | 21 +++++++++++++++++++--
> > 1 file changed, 19 insertions(+), 2 deletions(-)
> >
> > diff --git a/rust/macros/quote.rs b/rust/macros/quote.rs
> > index 33a199e4f176..c18960a91082 100644
> > --- a/rust/macros/quote.rs
> > +++ b/rust/macros/quote.rs
> > @@ -20,6 +20,12 @@ fn to_tokens(&self, tokens: &mut TokenStream) {
> > }
> > }
> >
> > +impl ToTokens for proc_macro::Ident {
> > + fn to_tokens(&self, tokens: &mut TokenStream) {
> > + tokens.extend([TokenTree::from(self.clone())]);
> > + }
> > +}
> > +
> > impl ToTokens for TokenTree {
> > fn to_tokens(&self, tokens: &mut TokenStream) {
> > tokens.extend([self.clone()]);
> > @@ -40,7 +46,7 @@ fn to_tokens(&self, tokens: &mut TokenStream) {
> > /// `quote` crate but provides only just enough functionality needed by the current `macros` crate.
> > macro_rules! quote_spanned {
> > ($span:expr => $($tt:tt)*) => {{
> > - let mut tokens;
> > + let mut tokens: ::std::vec::Vec<::proc_macro::TokenTree>;
> > #[allow(clippy::vec_init_then_push)]
> > {
> > tokens = ::std::vec::Vec::new();
> > @@ -65,7 +71,8 @@ macro_rules! quote_spanned {
> > quote_spanned!(@proc $v $span $($tt)*);
> > };
> > (@proc $v:ident $span:ident ( $($inner:tt)* ) $($tt:tt)*) => {
> > - let mut tokens = ::std::vec::Vec::new();
> > + #[allow(unused_mut)]
>
> It'd be nice to mention the need for this attribute in the commit
> message along with the added type annotations.
Adding a note to mention that when the () is empty, not only can't it
infer the item type, it's also never modified.
Alice
Powered by blists - more mailing lists