mirror of
https://github.com/torvalds/linux.git
synced 2024-12-01 16:41:39 +00:00
9762dca54a
The generic parameters on a type definition can specify default values. Currently `parse_generics()` cannot handle this though. For example when parsing the following generics: <T: Clone, const N: usize = 0> The `impl_generics` will be set to `T: Clone, const N: usize = 0` and `ty_generics` will be set to `T, N`. Now using the `impl_generics` on an impl block: impl<$($impl_generics)*> Foo {} will result in invalid Rust code, because default values are only available on type definitions. Therefore add parsing support for generic parameter default values using a new kind of generics called `decl_generics` and change the old behavior of `impl_generics` to not contain the generic parameter default values. Now `Generics` has three fields: - `impl_generics`: the generics with bounds (e.g. `T: Clone, const N: usize`) - `decl_generics`: the generics with bounds and default values (e.g. `T: Clone, const N: usize = 0`) - `ty_generics`: contains the generics without bounds and without default values (e.g. `T, N`) `impl_generics` is designed to be used on `impl<$impl_generics>`, `decl_generics` for the type definition, so `struct Foo<$decl_generics>` and `ty_generics` whenever you use the type, so `Foo<$ty_generics>`. Here is an example that uses all three different types of generics: let (Generics { decl_generics, impl_generics, ty_generics }, rest) = parse_generics(input); quote! { struct Foo<$($decl_generics)*> { // ... } impl<$impl_generics> Foo<$ty_generics> { fn foo() { // ... } } } The next commit contains a fix to the `#[pin_data]` macro making it compatible with generic parameter default values by relying on this new behavior. Signed-off-by: Benno Lossin <benno.lossin@proton.me> Reviewed-by: Alice Ryhl <aliceryhl@google.com> Link: https://lore.kernel.org/r/20240309155243.482334-1-benno.lossin@proton.me Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
74 lines
2.6 KiB
Rust
74 lines
2.6 KiB
Rust
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
use crate::helpers::{parse_generics, Generics};
|
|
use proc_macro::{TokenStream, TokenTree};
|
|
|
|
pub(crate) fn derive(input: TokenStream) -> TokenStream {
|
|
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! { : ::kernel::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! { ::kernel::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),
|
|
}
|
|
}
|
|
assert_eq!(nested, 0);
|
|
if in_generic && !inserted {
|
|
new_impl_generics.extend(quote! { : ::kernel::init::Zeroable });
|
|
}
|
|
quote! {
|
|
::kernel::__derive_zeroable!(
|
|
parse_input:
|
|
@sig(#(#rest)*),
|
|
@impl_generics(#(#new_impl_generics)*),
|
|
@ty_generics(#(#ty_generics)*),
|
|
@body(#last),
|
|
);
|
|
}
|
|
}
|