Skip to content

Commit 5d4eb2d

Browse files
committed
rust: macros: rewrite __internal_init! using syn
Now that `syn` and `quote` are available in the Kernel use them for the `__internal_init!` macro instead of the current hybrid proc-macro/declarative approach. Note that the new macro is called `primitive_init!`. Proc-macros written using `syn` are a lot more readable and thus easier to maintain than convoluted declarative macros. An additional advantage of `syn` is the improved error reporting, here is an example: When adding an additional `,` at the end of `..Zeroable::zeroed()` pin_init!(Foo { a: Bar, b <- Bar, c: Bar, d: Bar, ..Zeroable::zeroed(), // ^ this is the culprit! }) Prior to this patch, the error reads: error: no rules expected the token `,` --> samples/rust/rust_minimal.rs:51:5 | 51 | / pin_init!(Foo { 52 | | a: Bar, 53 | | b <- Bar, 54 | | c: Bar, 55 | | d: Bar, 56 | | ..Zeroable::zeroed(), 57 | | }) | |______^ no rules expected this token in macro call | note: while trying to match `)` --> rust/kernel/init/macros.rs:1169:53 | 1169 | @munch_fields($(..Zeroable::zeroed())? $(,)?), | ^ = note: this error originates in the macro `$crate::__init_internal` which comes from the expansion of the macro `pin_init` (in Nightly builds, run with -Z macro-backtrace for more info) error: no rules expected the token `,` --> samples/rust/rust_minimal.rs:51:5 | 51 | / pin_init!(Foo { 52 | | a: Bar, 53 | | b <- Bar, 54 | | c: Bar, 55 | | d: Bar, 56 | | ..Zeroable::zeroed(), 57 | | }) | |______^ no rules expected this token in macro call | note: while trying to match `)` --> rust/kernel/init/macros.rs:1273:49 | 1273 | @munch_fields(..Zeroable::zeroed() $(,)?), | ^ = note: this error originates in the macro `$crate::__init_internal` which comes from the expansion of the macro `pin_init` (in Nightly builds, run with -Z macro-backtrace for more info) error: aborting due to 2 previous errors After this patch, the error reads: error: unexpected token --> samples/rust/rust_minimal.rs:56:29 | 56 | ..Zeroable::zeroed(), | ^ error: aborting due to 1 previous error Signed-off-by: Benno Lossin <[email protected]>
1 parent b8459ad commit 5d4eb2d

File tree

4 files changed

+418
-410
lines changed

4 files changed

+418
-410
lines changed

rust/kernel/init.rs

+18-60
Original file line numberDiff line numberDiff line change
@@ -557,16 +557,9 @@ macro_rules! pin_init {
557557
($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
558558
$($fields:tt)*
559559
}) => {
560-
$crate::__init_internal!(
561-
@this($($this)?),
562-
@typ($t $(::<$($generics),*>)?),
563-
@fields($($fields)*),
564-
@error(::core::convert::Infallible),
565-
@data(PinData, use_data),
566-
@has_data(HasPinData, __pin_data),
567-
@construct_closure(pin_init_from_closure),
568-
@munch_fields($($fields)*),
569-
)
560+
$crate::macros::primitive_init!($(&$this in)? pin $t $(::<$($generics),*>)? {
561+
$($fields)*
562+
}? ::core::convert::Infallible)
570563
};
571564
}
572565

@@ -614,30 +607,16 @@ macro_rules! try_pin_init {
614607
($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
615608
$($fields:tt)*
616609
}) => {
617-
$crate::__init_internal!(
618-
@this($($this)?),
619-
@typ($t $(::<$($generics),*>)? ),
620-
@fields($($fields)*),
621-
@error($crate::error::Error),
622-
@data(PinData, use_data),
623-
@has_data(HasPinData, __pin_data),
624-
@construct_closure(pin_init_from_closure),
625-
@munch_fields($($fields)*),
626-
)
610+
$crate::macros::primitive_init!($(&$this in)? pin $t $(::<$($generics),*>)? {
611+
$($fields)*
612+
}? $crate::error::Error)
627613
};
628614
($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
629615
$($fields:tt)*
630616
}? $err:ty) => {
631-
$crate::__init_internal!(
632-
@this($($this)?),
633-
@typ($t $(::<$($generics),*>)? ),
634-
@fields($($fields)*),
635-
@error($err),
636-
@data(PinData, use_data),
637-
@has_data(HasPinData, __pin_data),
638-
@construct_closure(pin_init_from_closure),
639-
@munch_fields($($fields)*),
640-
)
617+
$crate::macros::primitive_init!($(&$this in)? pin $t $(::<$($generics),*>)? {
618+
$($fields)*
619+
}? $err)
641620
};
642621
}
643622

@@ -663,16 +642,9 @@ macro_rules! init {
663642
($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
664643
$($fields:tt)*
665644
}) => {
666-
$crate::__init_internal!(
667-
@this($($this)?),
668-
@typ($t $(::<$($generics),*>)?),
669-
@fields($($fields)*),
670-
@error(::core::convert::Infallible),
671-
@data(InitData, /*no use_data*/),
672-
@has_data(HasInitData, __init_data),
673-
@construct_closure(init_from_closure),
674-
@munch_fields($($fields)*),
675-
)
645+
$crate::macros::primitive_init!($(&$this in)? $t $(::<$($generics),*>)? {
646+
$($fields)*
647+
}? ::core::convert::Infallible)
676648
}
677649
}
678650

@@ -714,30 +686,16 @@ macro_rules! try_init {
714686
($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
715687
$($fields:tt)*
716688
}) => {
717-
$crate::__init_internal!(
718-
@this($($this)?),
719-
@typ($t $(::<$($generics),*>)?),
720-
@fields($($fields)*),
721-
@error($crate::error::Error),
722-
@data(InitData, /*no use_data*/),
723-
@has_data(HasInitData, __init_data),
724-
@construct_closure(init_from_closure),
725-
@munch_fields($($fields)*),
726-
)
689+
$crate::macros::primitive_init!($(&$this in)? $t $(::<$($generics),*>)? {
690+
$($fields)*
691+
}? $crate::error::Error)
727692
};
728693
($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
729694
$($fields:tt)*
730695
}? $err:ty) => {
731-
$crate::__init_internal!(
732-
@this($($this)?),
733-
@typ($t $(::<$($generics),*>)?),
734-
@fields($($fields)*),
735-
@error($err),
736-
@data(InitData, /*no use_data*/),
737-
@has_data(HasInitData, __init_data),
738-
@construct_closure(init_from_closure),
739-
@munch_fields($($fields)*),
740-
)
696+
$crate::macros::primitive_init!($(&$this in)? $t $(::<$($generics),*>)? {
697+
$($fields)*
698+
}? $err)
741699
};
742700
}
743701

0 commit comments

Comments
 (0)