rust: macros: allow generic parameter default values in `#[pin_data]`
authorBenno Lossin <benno.lossin@proton.me>
Sat, 9 Mar 2024 15:54:04 +0000 (15:54 +0000)
committerMiguel Ojeda <ojeda@kernel.org>
Sun, 7 Apr 2024 20:03:42 +0000 (22:03 +0200)
commit22eed6068d76d1d9672f33334740657208a91483
treec1af740527ce07ab52d9ada7f6bdd0ae4e1c67ac
parent9762dca54a4fec433b50eb83fdd8ff0a876cccf2
rust: macros: allow generic parameter default values in `#[pin_data]`

Add support for generic parameters defaults in `#[pin_data]` by using
the newly introduced `decl_generics` instead of the `impl_generics`.

Before this would not compile:

    #[pin_data]
    struct Foo<const N: usize = 0> {
        // ...
    }

because it would be expanded to this:

    struct Foo<const N: usize = 0> {
        // ...
    }

    const _: () = {
        struct __ThePinData<const N: usize = 0> {
            __phantom: ::core::marker::PhantomData<fn(Foo<N>) -> Foo<N>>,
        }
        impl<const N: usize = 0> ::core::clone::Clone for __ThePinData<N> {
            fn clone(&self) -> Self {
                *self
            }
        }

        // [...] rest of expansion omitted
    };

The problem is with the `impl<const N: usize = 0>`, since that is
invalid Rust syntax. It should not mention the default value at all,
since default values only make sense on type definitions.

The new `impl_generics` do not contain the default values, thus
generating correct Rust code.

This is used by the next commit that puts `#[pin_data]` on
`kernel::workqueue::Work`.

Signed-off-by: Benno Lossin <benno.lossin@proton.me>
Reviewed-by: Martin Rodriguez Reboredo <yakoyoku@gmail.com>
Reviewed-by: Gary Guo <gary@garyguo.net>
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Tested-by: Alice Ryhl <aliceryhl@google.com>
Link: https://lore.kernel.org/r/20240309155243.482334-2-benno.lossin@proton.me
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
rust/kernel/init/macros.rs
rust/macros/helpers.rs
rust/macros/pin_data.rs