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 #[pin_data] using syn
  • Loading branch information
BennoLossin committed Apr 16, 2024
commit c7790b67c0c76874dab60c81c40232a7eab6d493
10 changes: 3 additions & 7 deletions rust/macros/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,13 +262,9 @@ pub fn concat_idents(ts: TokenStream) -> TokenStream {
// ^ cannot use direct link, since `kernel` is not a dependency of `macros`.
#[proc_macro_attribute]
pub fn pin_data(args: TokenStream, input: TokenStream) -> TokenStream {
match syn::parse(input.clone()) {
Ok(input) => pin_data::pin_data(args.into(), input)
.unwrap_or_else(|e| e.into_compile_error())
.into(),
// Let the compiler handle the error.
Err(_) => input,
}
pin_data::pin_data(args.into(), parse_macro_input!(input))
.unwrap_or_else(|e| e.into_compile_error())
.into()
}

/// Used to implement `PinnedDrop` safely.
Expand Down
30 changes: 12 additions & 18 deletions rust/macros/pin_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ use proc_macro2::TokenStream;
use quote::quote;
use syn::{
parse_quote,
spanned::Spanned,
visit_mut::{visit_path_segment_mut, VisitMut},
Error, Field, Ident, Item, ItemStruct, PathSegment, Result, Token, Type, TypePath, WhereClause,
Error, Field, ItemStruct, PathSegment, Result, Type, TypePath, WhereClause,
};

pub(crate) fn pin_data(args: TokenStream, mut struct_: ItemStruct) -> Result<TokenStream> {
Expand Down Expand Up @@ -34,14 +33,10 @@ pub(crate) fn pin_data(args: TokenStream, mut struct_: ItemStruct) -> Result<Tok
}
Ok(quote! {
#struct_

#errors

const _: () = {
#the_pin_data

#unpin_impl

#drop_impl
};
})
Expand All @@ -57,8 +52,7 @@ impl VisitMut for SelfReplacer {
visit_path_segment_mut(self, seg);
}
}

fn visit_item_mut(&mut self, _: &mut Item) {
fn visit_item_mut(&mut self, _: &mut syn::Item) {
// Do not descend into items, since items reset/change what `Self` refers to.
}
}
Expand Down Expand Up @@ -139,6 +133,7 @@ fn generate_the_pin_data(
// We declare this struct which will host all of the projection function for our type. It
// will be invariant over all generic parameters which are inherited from the struct.
#vis struct __ThePinData #generics
#whr
{
__phantom: ::core::marker::PhantomData<
fn(#ident #ty_generics) -> #ident #ty_generics
Expand Down Expand Up @@ -194,14 +189,9 @@ fn unpin_impl(
}: &ItemStruct,
) -> TokenStream {
let generics_with_pinlt = {
let span = generics.span();
let mut g = generics.clone();
g.params.insert(0, parse_quote!('__pin));
let whr = g.make_where_clause();
whr.where_token = Token![where](span);
if !whr.predicates.empty_or_trailing() {
whr.predicates.push_punct(Default::default());
}
let _ = g.make_where_clause();
g
};
let (
Expand All @@ -228,18 +218,22 @@ fn unpin_impl(
// This struct will be used for the unpin analysis. It is needed, because only structurally
// pinned fields are relevant whether the struct should implement `Unpin`.
#[allow(dead_code)] // The fields below are never used.
struct __Unpin #generics_with_pinlt {
struct __Unpin #generics_with_pinlt
#where_token
#predicates
{
__phantom_pin: ::core::marker::PhantomData<fn(&'__pin ()) -> &'__pin ()>,
__phantom: ::core::marker::PhantomData<
fn(#ident #ty_generics) -> #ident #ty_generics
>,
#(#pinned_fields),*
}

#[doc(hidden)]
impl #impl_generics_with_pinlt ::core::marker::Unpin for #ident #ty_generics
#where_token
#predicates
__Unpin #ty_generics_with_pinlt: ::core::marker::Unpin,
#predicates
{}
}
}
Expand All @@ -251,7 +245,7 @@ fn drop_impl(
args: TokenStream,
) -> Result<TokenStream> {
let (impl_generics, ty_generics, whr) = generics.split_for_impl();
let has_pinned_drop = match syn::parse2::<Option<Ident>>(args.clone()) {
let has_pinned_drop = match syn::parse2::<Option<syn::Ident>>(args.clone()) {
Ok(None) => false,
Ok(Some(ident)) if ident == "PinnedDrop" => true,
_ => {
Expand Down Expand Up @@ -297,7 +291,7 @@ fn drop_impl(
// `PinnedDrop` as the parameter to `#[pin_data]`.
#[allow(non_camel_case_types)]
trait UselessPinnedDropImpl_you_need_to_specify_PinnedDrop {}
impl<T: ::kernel::init::PinnedDrop + ::core::marker::Sized>
impl<T: ::kernel::init::PinnedDrop + ?::core::marker::Sized>
UselessPinnedDropImpl_you_need_to_specify_PinnedDrop for T {}
impl #impl_generics
UselessPinnedDropImpl_you_need_to_specify_PinnedDrop for #ident #ty_generics
Expand Down