Skip to content

Macro syn rewrite #1073

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 27 commits into
base: rust-next
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
6b2262f
kbuild: rust: apply `CONFIG_WERROR` to hostprogs as well
ojeda Apr 7, 2024
a5f77c5
kbuild: rust: use shared host Rust flags for `macros`
ojeda Apr 7, 2024
e4527ff
kbuild: rust-analyzer: support key-value `cfg`s
ojeda Apr 10, 2024
c3b22c6
rust: proc-macro2: import crate
ojeda Oct 9, 2022
617c120
rust: proc-macro2: add SPDX License Identifiers
ojeda Oct 9, 2022
24954e2
rust: proc-macro2: remove `unicode_ident` dependency
ojeda Oct 9, 2022
925b30e
rust: quote: import crate
ojeda Oct 9, 2022
cdad907
rust: quote: add SPDX License Identifiers
ojeda Oct 9, 2022
328f151
rust: syn: import crate
ojeda Oct 9, 2022
cef2d41
rust: syn: add SPDX License Identifiers
ojeda Oct 9, 2022
82e9a4f
rust: syn: remove `unicode-ident` dependency
ojeda Oct 9, 2022
a59391f
rust: Kbuild: enable `proc-macro2`, `quote` and `syn`
ojeda Oct 9, 2022
1b729e1
rust: macros: fix soundness issue in `module!` macro
BennoLossin Apr 1, 2024
ed6e1a8
rust: macros: replace `quote!` with `quote::quote` and use `proc-macro2`
BennoLossin Apr 6, 2024
8938c4f
rust: macros: rewrite `#[vtable]` using `syn`
BennoLossin Apr 6, 2024
638dc79
rust: macros: rewrite `module!` using `syn`
BennoLossin Apr 6, 2024
bdb4cff
rust: macros: rewrite `Zeroable` derive macro using `syn`
BennoLossin Apr 6, 2024
2a88e8a
rust: macros: rewrite `#[pin_data]` using `syn`
BennoLossin Apr 6, 2024
b8459ad
rust: macros: rewrite `#[pinned_drop]` using `syn`
BennoLossin Apr 5, 2024
5d4eb2d
rust: macros: rewrite `__internal_init!` using `syn`
BennoLossin Apr 6, 2024
a8dae43
rust: macros: remove helpers
BennoLossin Apr 6, 2024
45d057b
rust: init: remove macros.rs
BennoLossin Apr 8, 2024
c7790b6
fixup! rust: macros: rewrite `#[pin_data]` using `syn`
BennoLossin Apr 16, 2024
66b9b59
fixup! rust: macros: rewrite `#[pinned_drop]` using `syn`
BennoLossin Apr 16, 2024
8d21a65
fixup! rust: macros: rewrite `__internal_init!` using `syn`
BennoLossin Apr 16, 2024
edd1ce0
fixup! rust: macros: rewrite `Zeroable` derive macro using `syn`
BennoLossin Apr 16, 2024
6ad858d
fixup! rust: macros: rewrite `#[vtable]` using `syn`
BennoLossin Apr 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fixup! rust: macros: rewrite #[pinned_drop] using syn
  • Loading branch information
BennoLossin committed Apr 16, 2024
commit 66b9b59688cfe2c21090d592400d6c6f5f829def
18 changes: 3 additions & 15 deletions rust/macros/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,21 +292,9 @@ pub fn pin_data(args: TokenStream, input: TokenStream) -> TokenStream {
#[proc_macro_attribute]
pub fn pinned_drop(args: TokenStream, input: TokenStream) -> TokenStream {
parse_macro_input!(args as syn::parse::Nothing);
match syn::parse(input.clone()) {
Ok(input) => match pinned_drop::pinned_drop(input) {
Ok(output) => output,
Err((err, impl_)) => {
let err = err.into_compile_error();
quote! {
#impl_
#err
}
}
}
.into(),
// Let the compiler handle the error.
Err(_) => input,
}
pinned_drop::pinned_drop(parse_macro_input!(input))
.unwrap_or_else(|e| e.into_compile_error())
.into()
}

/// Paste identifiers together.
Expand Down
159 changes: 58 additions & 101 deletions rust/macros/pinned_drop.rs
Original file line number Diff line number Diff line change
@@ -1,112 +1,69 @@
// SPDX-License-Identifier: Apache-2.0 OR MIT

use proc_macro2::TokenStream;
use quote::{quote, ToTokens};
use syn::{parse_quote, Error, ImplItem, ImplItemFn, ItemImpl, Token};
use quote::quote;
use syn::{parse_quote, spanned::Spanned, Error, ImplItem, ImplItemFn, ItemImpl, Result, Token};

struct PinnedDropImpl(ItemImpl, ImplItemFn);

impl TryFrom<ItemImpl> for PinnedDropImpl {
type Error = (Error, TokenStream);
fn try_from(mut impl_: ItemImpl) -> Result<Self, Self::Error> {
let aux_impl = match &impl_ {
ItemImpl {
attrs,
impl_token,
generics,
trait_: Some((polarity, path, for_)),
self_ty,
..
} if path.is_ident("PinnedDrop") => {
let (impl_generics, _, whr) = generics.split_for_impl();
quote! {
#(#attrs)*
unsafe #impl_token #impl_generics #polarity ::kernel::init::PinnedDrop #for_ #self_ty
#whr
{
fn drop(
self: ::core::pin::Pin<&mut Self>,
_: ::kernel::init::__internal::OnlyCallFromDrop,
) {}
}
}
}
_ => quote!(#impl_),
};
if impl_.unsafety.is_some() {
return Err((
Error::new_spanned(
impl_.unsafety,
"The `PinnedDrop` impl must not be `unsafe`.",
),
aux_impl,
));
}
let trait_tokens = match &impl_.trait_ {
None => quote!(),
Some((None, path, _)) => quote!(#path),
Some((Some(not), path, _)) => quote!(#not #path),
};
match &impl_.trait_ {
Some((None, path, _)) if path.is_ident("PinnedDrop") => {}
None | Some((None, ..)) => {
return Err((
Error::new_spanned(
trait_tokens,
"`#[pinned_drop]` can only be used on `PinnedDrop` impls.",
),
aux_impl,
));
}
Some((Some(_), ..)) => {
return Err((
Error::new_spanned(
trait_tokens,
"`#[pinned_drop]` can only be used on positive `PinnedDrop` impls.",
),
aux_impl,
));
pub(crate) fn pinned_drop(mut input: ItemImpl) -> Result<TokenStream> {
let Some((_, path, _)) = &mut input.trait_ else {
return Err(Error::new_spanned(
input,
"expected an `impl` block implementing `PinnedDrop`",
));
};
if !is_pinned_drop(&path) {
return Err(Error::new_spanned(
input,
"expected an `impl` block implementing `PinnedDrop`",
));
}
let mut error = None;
if let Some(unsafety) = input.unsafety.take() {
error = Some(
Error::new_spanned(
unsafety,
"implementing the trait `PinnedDrop` via `#[pinned_drop]` is not unsafe",
)
.into_compile_error(),
);
}
input.unsafety = Some(Token![unsafe](input.impl_token.span()));
if path.segments.len() != 2 {
path.segments.insert(0, parse_quote!(pinned_init));
}
path.leading_colon.get_or_insert(Token![::](path.span()));
for item in &mut input.items {
match item {
ImplItem::Fn(ImplItemFn { sig, .. }) if sig.ident == "drop" => {
sig.inputs
.push(parse_quote!(_: ::kernel::init::__internal::OnlyCallFromDrop));
}
_ => {}
}
if impl_.items.len() != 1 {
return Err((
Error::new(
impl_.brace_token.span.join(),
"Expected exactly one function in the `PinnedDrop` impl.",
),
aux_impl,
));
}
let ImplItem::Fn(drop) = impl_.items.pop().unwrap() else {
return Err((
Error::new(impl_.brace_token.span.join(), "Expected a function."),
aux_impl,
));
};

Ok(Self(impl_, drop))
}
Ok(quote! {
#error
#input
})
}

impl ToTokens for PinnedDropImpl {
fn to_tokens(&self, tokens: &mut TokenStream) {
self.0.to_tokens(tokens)
fn is_pinned_drop(path: &syn::Path) -> bool {
if path.segments.len() > 2 {
return false;
}
}

pub(crate) fn pinned_drop(drop_impl: ItemImpl) -> Result<TokenStream, (Error, TokenStream)> {
let PinnedDropImpl(mut drop_impl, mut drop) = drop_impl.try_into()?;
drop.sig
.inputs
.push(parse_quote!(_: ::kernel::init::__internal::OnlyCallFromDrop));
let span = drop_impl.impl_token.span;

drop_impl.items.push(ImplItem::Fn(drop));
drop_impl.unsafety = Some(Token![unsafe](span));
drop_impl.trait_ = Some((
None,
parse_quote!(::kernel::init::PinnedDrop),
Token![for](span),
));
Ok(quote! { #drop_impl })
// If there is a `::`, then the path needs to be `::kernel::init::PinnedDrop`.
if path.leading_colon.is_some() && path.segments.len() != 2 {
return false;
}
for (actual, expected) in path
.segments
.iter()
.rev()
.zip(["PinnedDrop", "pinned_init"])
{
if actual.ident != expected {
return false;
}
}
true
}