mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Gary Guo" <gary@garyguo.net>
To: "Benno Lossin" <lossin@kernel.org>, "Gary Guo" <gary@garyguo.net>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Boqun Feng" <boqun.feng@gmail.com>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Fiona Behrens" <me@kloenk.dev>,
	"Christian Schrefl" <chrisi.schrefl@gmail.com>,
	"Alban Kurti" <kurti@invicto.ai>
Cc: <rust-for-linux@vger.kernel.org>, <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH 04/12] rust: pin-init: rewrite `derive(Zeroable)` and `derive(MaybeZeroable)` using `syn`
Date: Fri, 09 Jan 2026 12:02:57 +0000	[thread overview]
Message-ID: <DFK1KT28KH9S.QV7VCG9O9VGV@garyguo.net> (raw)
In-Reply-To: <20260108135127.3153925-5-lossin@kernel.org>

On Thu Jan 8, 2026 at 1:50 PM GMT, Benno Lossin wrote:
> Rewrite the two derive macros for `Zeroable` using `syn`. One positive
> side effect of this change is that tuple structs are now supported by
> them. Additionally, syntax errors and the error emitted when trying to
> use one of the derive macros on an `enum` are improved. Otherwise no
> functional changes intended.
>
> For example:
>
>     #[derive(Zeroable)]
>     enum Num {
>         A(u32),
>         B(i32),
>     }
>
> Produced this error before this commit:
>
>     error: no rules expected keyword `enum`
>      --> tests/ui/compile-fail/zeroable/enum.rs:5:1
>       |
>     5 | enum Num {
>       | ^^^^ no rules expected this token in macro call
>       |
>     note: while trying to match keyword `struct`
>      --> src/macros.rs
>       |
>       |             $vis:vis struct $name:ident
>       |                      ^^^^^^
>
> Now the error is:
>
>     error: cannot derive `Zeroable` for an enum
>      --> tests/ui/compile-fail/zeroable/enum.rs:5:1
>       |
>     5 | enum Num {
>       | ^^^^
>
>     error: cannot derive `Zeroable` for an enum
>
> Signed-off-by: Benno Lossin <lossin@kernel.org>
> ---
>  rust/pin-init/internal/src/lib.rs      |   5 +-
>  rust/pin-init/internal/src/zeroable.rs | 149 ++++++++++---------------
>  rust/pin-init/src/macros.rs            | 124 --------------------
>  3 files changed, 65 insertions(+), 213 deletions(-)

Yay! the updated code is much more readable.

>
> diff --git a/rust/pin-init/internal/src/lib.rs b/rust/pin-init/internal/src/lib.rs
> index 4c4dc639ce82..ec593362c5ac 100644
> --- a/rust/pin-init/internal/src/lib.rs
> +++ b/rust/pin-init/internal/src/lib.rs
> @@ -11,6 +11,7 @@
>  #![allow(missing_docs)]
>  
>  use proc_macro::TokenStream;
> +use syn::parse_macro_input;
>  
>  mod helpers;
>  mod pin_data;
> @@ -29,10 +30,10 @@ pub fn pinned_drop(args: TokenStream, input: TokenStream) -> TokenStream {
>  
>  #[proc_macro_derive(Zeroable)]
>  pub fn derive_zeroable(input: TokenStream) -> TokenStream {
> -    zeroable::derive(input.into()).into()
> +    zeroable::derive(parse_macro_input!(input as _)).into()

This can just be

    zeroable::derive(parse_macro_input!(input)).into()

same for the below.

>  }
>  
>  #[proc_macro_derive(MaybeZeroable)]
>  pub fn maybe_derive_zeroable(input: TokenStream) -> TokenStream {
> -    zeroable::maybe_derive(input.into()).into()
> +    zeroable::maybe_derive(parse_macro_input!(input as _)).into()
>  }
> diff --git a/rust/pin-init/internal/src/zeroable.rs b/rust/pin-init/internal/src/zeroable.rs
> index d8a5ef3883f4..0328c3bdfceb 100644
> --- a/rust/pin-init/internal/src/zeroable.rs
> +++ b/rust/pin-init/internal/src/zeroable.rs
> @@ -1,99 +1,74 @@
>  // SPDX-License-Identifier: GPL-2.0
>  
> -use crate::helpers::{parse_generics, Generics};
> -use proc_macro2::{TokenStream, TokenTree};
> -use quote::quote;
> +use proc_macro2::TokenStream;
> +use quote::{quote, quote_spanned};
> +use syn::{parse_quote, Data, DeriveInput, Field, Fields};
>  
> -pub(crate) fn parse_zeroable_derive_input(
> -    input: TokenStream,
> -) -> (
> -    Vec<TokenTree>,
> -    Vec<TokenTree>,
> -    Vec<TokenTree>,
> -    Option<TokenTree>,
> -) {
> -    let (
> -        Generics {
> -            impl_generics,
> -            decl_generics: _,
> -            ty_generics,
> -        },
> -        mut rest,
> -    ) = parse_generics(input);
> -    // This should be the body of the struct `{...}`.
> -    let last = rest.pop();
> -    // Now we insert `Zeroable` as a bound for every generic parameter in `impl_generics`.
> -    let mut new_impl_generics = Vec::with_capacity(impl_generics.len());
> -    // Are we inside of a generic where we want to add `Zeroable`?
> -    let mut in_generic = !impl_generics.is_empty();
> -    // Have we already inserted `Zeroable`?
> -    let mut inserted = false;
> -    // Level of `<>` nestings.
> -    let mut nested = 0;
> -    for tt in impl_generics {
> -        match &tt {
> -            // If we find a `,`, then we have finished a generic/constant/lifetime parameter.
> -            TokenTree::Punct(p) if nested == 0 && p.as_char() == ',' => {
> -                if in_generic && !inserted {
> -                    new_impl_generics.extend(quote! { : ::pin_init::Zeroable });
> -                }
> -                in_generic = true;
> -                inserted = false;
> -                new_impl_generics.push(tt);
> -            }
> -            // If we find `'`, then we are entering a lifetime.
> -            TokenTree::Punct(p) if nested == 0 && p.as_char() == '\'' => {
> -                in_generic = false;
> -                new_impl_generics.push(tt);
> -            }
> -            TokenTree::Punct(p) if nested == 0 && p.as_char() == ':' => {
> -                new_impl_generics.push(tt);
> -                if in_generic {
> -                    new_impl_generics.extend(quote! { ::pin_init::Zeroable + });
> -                    inserted = true;
> -                }
> -            }
> -            TokenTree::Punct(p) if p.as_char() == '<' => {
> -                nested += 1;
> -                new_impl_generics.push(tt);
> -            }
> -            TokenTree::Punct(p) if p.as_char() == '>' => {
> -                assert!(nested > 0);
> -                nested -= 1;
> -                new_impl_generics.push(tt);
> -            }
> -            _ => new_impl_generics.push(tt),
> +pub(crate) fn derive(input: DeriveInput) -> TokenStream {
> +    let fields = match input.data {
> +        Data::Struct(data_struct) => data_struct.fields,
> +        Data::Union(data_union) => Fields::Named(data_union.fields),
> +        Data::Enum(data_enum) => {
> +            return quote_spanned! {data_enum.enum_token.span=>
> +                ::core::compile_error!("cannot derive `Zeroable` for an enum");
> +            };

Even if it's currently just one place that could error, I would still probably
use `syn`'s Error type as it's easier to extend in the future.

Best,
Gary

>          }
> +    };
> +    let name = input.ident;
> +    let mut generics = input.generics;
> +    for param in generics.type_params_mut() {
> +        param.bounds.insert(0, parse_quote!(::pin_init::Zeroable));
>      }
> -    assert_eq!(nested, 0);
> -    if in_generic && !inserted {
> -        new_impl_generics.extend(quote! { : ::pin_init::Zeroable });
> -    }
> -    (rest, new_impl_generics, ty_generics, last)
> -}
> -


  reply	other threads:[~2026-01-09 12:03 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-08 13:50 [PATCH 00/13] `syn` rewrite of pin-init Benno Lossin
2026-01-08 13:50 ` [PATCH 01/12] rust: pin-init: remove `try_` versions of the initializer macros Benno Lossin
2026-01-08 13:50 ` [PATCH 02/12] rust: pin-init: allow the crate to refer to itself as `pin-init` in doc tests Benno Lossin
2026-01-08 13:50 ` [PATCH 03/12] rust: pin-init: add `syn` dependency and remove `proc-macro[2]` and `quote` workarounds Benno Lossin
2026-01-08 13:50 ` [PATCH 04/12] rust: pin-init: rewrite `derive(Zeroable)` and `derive(MaybeZeroable)` using `syn` Benno Lossin
2026-01-09 12:02   ` Gary Guo [this message]
2026-01-08 13:50 ` [PATCH 05/12] rust: pin-init: rewrite the `#[pinned_drop]` attribute macro " Benno Lossin
2026-01-09 12:12   ` Gary Guo
2026-01-09 15:34     ` Benno Lossin
2026-01-09 16:42       ` Gary Guo
2026-01-08 13:50 ` [PATCH 06/12] rust: pin-init: rewrite `#[pin_data]` " Benno Lossin
2026-01-09  7:45   ` kernel test robot
2026-01-09 12:47   ` Gary Guo
2026-01-09 16:39     ` Benno Lossin
2026-01-09 16:46       ` Gary Guo
2026-01-10 16:41         ` Benno Lossin
2026-01-10 19:18           ` Gary Guo
2026-01-08 13:50 ` [PATCH 07/12] rust: pin-init: add `?Sized` bounds to traits in `#[pin_data]` macro Benno Lossin
2026-01-08 13:50 ` [PATCH 08/12] rust: pin-init: rewrite the initializer macros using `syn` Benno Lossin
2026-01-09  8:44   ` kernel test robot
2026-01-09 13:45   ` Gary Guo
2026-01-09 17:24     ` Benno Lossin
2026-01-10 16:21       ` Benno Lossin
2026-01-10 18:14     ` Benno Lossin
2026-01-10 19:20       ` Gary Guo
2026-01-10 23:18         ` Benno Lossin
2026-01-11  1:10           ` Gary Guo
2026-01-11 10:04             ` Benno Lossin
2026-01-08 13:50 ` [PATCH 09/12] rust: pin-init: add `#[default_error(<type>)]` attribute to initializer macros Benno Lossin
2026-01-09 13:52   ` Gary Guo
2026-01-08 13:50 ` [PATCH 10/12] rust: init: use `#[default_error(err)]` for the " Benno Lossin
2026-01-08 13:50 ` [PATCH 11/12] rust: pin-init: internal: init: add support for attributes on initializer fields Benno Lossin
2026-01-09 13:55   ` Gary Guo
2026-01-09 18:02     ` Benno Lossin
2026-01-09 21:16       ` Gary Guo
2026-01-08 13:50 ` [PATCH 12/12] rust: pin-init: internal: init: add escape hatch for referencing initialized fields Benno Lossin
2026-01-09  9:44   ` kernel test robot
2026-01-09 13:58   ` Gary Guo
2026-01-09 18:04     ` Benno Lossin
2026-01-08 13:50 ` [PATCH 13/13] MAINTAINERS: add Gary Guo to pin-init Benno Lossin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=DFK1KT28KH9S.QV7VCG9O9VGV@garyguo.net \
    --to=gary@garyguo.net \
    --cc=a.hindborg@kernel.org \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=chrisi.schrefl@gmail.com \
    --cc=dakr@kernel.org \
    --cc=kurti@invicto.ai \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=me@kloenk.dev \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tmgross@umich.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome