aboutsummaryrefslogtreecommitdiff
path: root/rust/macros
diff options
context:
space:
mode:
authorGravatar Benno Lossin <benno.lossin@proton.me> 2024-03-09 15:54:04 +0000
committerGravatar Miguel Ojeda <ojeda@kernel.org> 2024-04-07 22:03:42 +0200
commit22eed6068d76d1d9672f33334740657208a91483 (patch)
treec1af740527ce07ab52d9ada7f6bdd0ae4e1c67ac /rust/macros
parentrust: macros: add `decl_generics` to `parse_generics()` (diff)
downloadlinux-22eed6068d76d1d9672f33334740657208a91483.tar.gz
linux-22eed6068d76d1d9672f33334740657208a91483.tar.bz2
linux-22eed6068d76d1d9672f33334740657208a91483.zip
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>
Diffstat (limited to 'rust/macros')
-rw-r--r--rust/macros/helpers.rs1
-rw-r--r--rust/macros/pin_data.rs3
2 files changed, 2 insertions, 2 deletions
diff --git a/rust/macros/helpers.rs b/rust/macros/helpers.rs
index 2497d6519f2f..563dcd2b7ace 100644
--- a/rust/macros/helpers.rs
+++ b/rust/macros/helpers.rs
@@ -95,7 +95,6 @@ pub(crate) struct Generics {
/// The generics with bounds and default values (e.g. `T: Clone, const N: usize = 0`).
///
/// Use this on type definitions e.g. `struct Foo<$decl_generics> ...` (or `union`/`enum`).
- #[allow(dead_code)]
pub(crate) decl_generics: Vec<TokenTree>,
/// The generics with bounds (e.g. `T: Clone, const N: usize`).
///
diff --git a/rust/macros/pin_data.rs b/rust/macros/pin_data.rs
index 022e68e9720d..1d4a3547c684 100644
--- a/rust/macros/pin_data.rs
+++ b/rust/macros/pin_data.rs
@@ -10,7 +10,7 @@ pub(crate) fn pin_data(args: TokenStream, input: TokenStream) -> TokenStream {
let (
Generics {
impl_generics,
- decl_generics: _,
+ decl_generics,
ty_generics,
},
rest,
@@ -77,6 +77,7 @@ pub(crate) fn pin_data(args: TokenStream, input: TokenStream) -> TokenStream {
@sig(#(#rest)*),
@impl_generics(#(#impl_generics)*),
@ty_generics(#(#ty_generics)*),
+ @decl_generics(#(#decl_generics)*),
@body(#last),
});
quoted.extend(errs);