Skip to contentSkip to conversation

`syn` rewrite of pin-init

PATCH New Benno Lossin 3 participants 40 messages Jan 8 – Jan 11

Subsystems (3)
lore
Share & tools
lore

Benno Lossin

author maintainer
#0 lore raw

cover `syn` rewrite of pin-init

Rewrite the proc-macros of pin-init by using the syn crate for Rust syntax parsing. This series has been a long way coming. At the very start of pin-init, I initially implemented everything using syn, since parsing is so much easier with it. Now after several years it is finally time to remove the dreaded 1600 lines of declarative macros required to parse and expand the initializer syntax.

The move to syn is not only a blessing for the maintenance work, but also improves the implementation of new features. This series includes many such improvements.

* Patch 1, 2 and 3 prepare for the rewrite. The first removes the superfluous try_ variants of the initializer macros from pin-init. Note that the kernel defines its own, so no code changes in the kernel are required. The second patch allows using ::pin_init in the pin-init crate. The third adds the syn dependency and cleans up some old workarounds and new clippy warnings.
* Patch 4, 5, 6, and 8 rewrite the derive macros for Zeroable, #[pinned_drop] attribute macro, #[pin_data] attribute macro, and the initializer macros respectively using syn.
* Patch 7 ensures soundness in the future by fixing generic bounds in generated code by #[pin_data].
* Patch 9 adds the #[default_error(type)] attribute to initializer macros allowing them to specify a default error that is used when no error is manually specified.
* Patch 10 uses #[default_error(type)] in the definition of the kernel's try_ variants of the initializer macros (which defaults to the kernel's Error type).
* Patch 11 allows putting attributes on fields in initializer macros (for example cfg attributes).
* Patch 12 adds #[disable_initialized_field_access] to support packed structs.
* Patch 13 adds Gary as a maintainer.

In addition to the new features, using syn results in much cleaner error messages and more accurate span information. The code is much easier to read as well and hopefully easier to understand as well.

As always, tests that ensure the correctness of the macro output are included and updated in the upstream pin-init repository. Take a look at the pull request on GitHub for the diff in the test output:

    https://github.com/Rust-for-Linux/pin-init/pull/89

I would greatly appreciate Tested-by's from people actively using pin-init weather in the kernel or outside.

Cheers,
Benno

Benno Lossin (13):
rust: pin-init: remove try_ versions of the initializer macros
rust: pin-init: allow the crate to refer to itself as pin-init in
doc tests
rust: pin-init: add syn dependency and remove proc-macro[2] and
quote workarounds
rust: pin-init: rewrite derive(Zeroable) and derive(MaybeZeroable)
using syn
rust: pin-init: rewrite the #[pinned_drop] attribute macro using

    `syn`

rust: pin-init: rewrite #[pin_data] using syn
rust: pin-init: add ?Sized bounds to traits in #[pin_data] macro
rust: pin-init: rewrite the initializer macros using syn
rust: pin-init: add #[default_error(<type>)] attribute to
initializer macros
rust: init: use #[default_error(err)] for the initializer macros
rust: pin-init: internal: init: add support for attributes on
initializer fields
rust: pin-init: internal: init: add escape hatch for referencing
initialized fields
MAINTAINERS: add Gary Guo to pin-init

 MAINTAINERS                               |    1 +
 rust/Makefile                             |   16 +-
 rust/kernel/init.rs                       |   40 +-
 rust/pin-init/README.md                   |    2 +-
 rust/pin-init/examples/linked_list.rs     |   19 +-
 rust/pin-init/examples/pthread_mutex.rs   |   10 +-
 rust/pin-init/internal/src/helpers.rs     |  152 --
 rust/pin-init/internal/src/init.rs        |  547 +++++++
 rust/pin-init/internal/src/lib.rs         |   60 +-
 rust/pin-init/internal/src/pin_data.rs    |  649 ++++++--
 rust/pin-init/internal/src/pinned_drop.rs |   89 +-
 rust/pin-init/internal/src/zeroable.rs    |  151 +-
 rust/pin-init/src/lib.rs                  |  173 +--
 rust/pin-init/src/macros.rs               | 1677 ---------------------
 scripts/generate_rust_analyzer.py         |    2 +-
 15 files changed, 1286 insertions(+), 2302 deletions(-)
 delete mode 100644 rust/pin-init/internal/src/helpers.rs
 create mode 100644 rust/pin-init/internal/src/init.rs
 delete mode 100644 rust/pin-init/src/macros.rs

base-commit: 9448598b22c50c8a5bb77a9103e2d49f134c9578

Signature · 1 line
2.51.2
author maintainer
#1 lore raw

PATCH 1/12 rust: pin-init: remove `try_` versions of the initializer macros

The try_[pin_]init! versions of the initializer macros are superfluous. Instead of forcing the user to always write an error in try_[pin_]init! and not allowing one in [pin_]init!, combine them into [pin_]init! that defaults the error to core::convert::Infallible, but also allows to specify a custom one.

Projects using pin-init still can provide their own defaulting initializers using the try_ prefix by using the #[default_error] attribute added in a future patch.

[ Adjust the definition of the kernel's version of the try_
initializer macros - Benno]

---
 rust/kernel/init.rs                     |   8 +-
 rust/pin-init/README.md                 |   2 +-
 rust/pin-init/examples/linked_list.rs   |  19 ++--
 rust/pin-init/examples/pthread_mutex.rs |  10 +-
 rust/pin-init/src/lib.rs                | 118 ++++--------------------
 5 files changed, 35 insertions(+), 122 deletions(-)
rust/kernel/init.rs +4 -4
index 899b9a962762..917f7ef001fd 100644
@@ -222,14 +222,14 @@ macro_rules! try_init {
     ($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
         $($fields:tt)*
     }) => {
-        ::pin_init::try_init!($(&$this in)? $t $(::<$($generics),*>)? {
+        ::pin_init::init!($(&$this in)? $t $(::<$($generics),*>)? {
             $($fields)*
         }? $crate::error::Error)
     };
     ($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
         $($fields:tt)*
     }? $err:ty) => {
-        ::pin_init::try_init!($(&$this in)? $t $(::<$($generics),*>)? {
+        ::pin_init::init!($(&$this in)? $t $(::<$($generics),*>)? {
             $($fields)*
         }? $err)
     };
@@ -282,14 +282,14 @@ macro_rules! try_pin_init {
     ($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
         $($fields:tt)*
     }) => {
-        ::pin_init::try_pin_init!($(&$this in)? $t $(::<$($generics),*>)? {
+        ::pin_init::pin_init!($(&$this in)? $t $(::<$($generics),*>)? {
             $($fields)*
         }? $crate::error::Error)
     };
     ($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
         $($fields:tt)*
     }? $err:ty) => {
-        ::pin_init::try_pin_init!($(&$this in)? $t $(::<$($generics),*>)? {
+        ::pin_init::pin_init!($(&$this in)? $t $(::<$($generics),*>)? {
             $($fields)*
         }? $err)
     };
rust/pin-init/README.md +1 -1
index 74bbb4e0a2f7..6cee6ab1eb57 100644
@@ -135,7 +135,7 @@ struct DriverData {
 
 impl DriverData {
     fn new() -> impl PinInit<Self, Error> {
-        try_pin_init!(Self {
+        pin_init!(Self {
             status <- CMutex::new(0),
             buffer: Box::init(pin_init::init_zeroed())?,
         }? Error)
rust/pin-init/examples/linked_list.rs +9 -10
index f9e117c7dfe0..8445a5890cb7 100644
@@ -6,7 +6,6 @@
 
 use core::{
     cell::Cell,
-    convert::Infallible,
     marker::PhantomPinned,
     pin::Pin,
     ptr::{self, NonNull},
@@ -31,31 +30,31 @@ pub struct ListHead {
 
 impl ListHead {
     #[inline]
-    pub fn new() -> impl PinInit<Self, Infallible> {
-        try_pin_init!(&this in Self {
+    pub fn new() -> impl PinInit<Self> {
+        pin_init!(&this in Self {
             next: unsafe { Link::new_unchecked(this) },
             prev: unsafe { Link::new_unchecked(this) },
             pin: PhantomPinned,
-        }? Infallible)
+        })
     }
 
     #[inline]
     #[allow(dead_code)]
-    pub fn insert_next(list: &ListHead) -> impl PinInit<Self, Infallible> + '_ {
-        try_pin_init!(&this in Self {
+    pub fn insert_next(list: &ListHead) -> impl PinInit<Self> + '_ {
+        pin_init!(&this in Self {
             prev: list.next.prev().replace(unsafe { Link::new_unchecked(this)}),
             next: list.next.replace(unsafe { Link::new_unchecked(this)}),
             pin: PhantomPinned,
-        }? Infallible)
+        })
     }
 
     #[inline]
-    pub fn insert_prev(list: &ListHead) -> impl PinInit<Self, Infallible> + '_ {
-        try_pin_init!(&this in Self {
+    pub fn insert_prev(list: &ListHead) -> impl PinInit<Self> + '_ {
+        pin_init!(&this in Self {
             next: list.prev.next().replace(unsafe { Link::new_unchecked(this)}),
             prev: list.prev.replace(unsafe { Link::new_unchecked(this)}),
             pin: PhantomPinned,
-        }? Infallible)
+        })
     }
 
     #[inline]
rust/pin-init/examples/pthread_mutex.rs +5 -5
index 49b004c8c137..4e082ec7d5de 100644
@@ -98,11 +98,11 @@ fn init_raw() -> impl PinInit<UnsafeCell<libc::pthread_mutex_t>, Error> {
                 // SAFETY: mutex has been initialized
                 unsafe { pin_init_from_closure(init) }
             }
-            try_pin_init!(Self {
-            data: UnsafeCell::new(data),
-            raw <- init_raw(),
-            pin: PhantomPinned,
-        }? Error)
+            pin_init!(Self {
+                data: UnsafeCell::new(data),
+                raw <- init_raw(),
+                pin: PhantomPinned,
+            }? Error)
         }
 
         #[allow(dead_code)]
rust/pin-init/src/lib.rs +16 -102

191 lines not on this page · raw message

Signature · 1 line
2.51.2
author maintainer
#2 lore raw

PATCH 2/12 rust: pin-init: allow the crate to refer to itself as `pin-init` in doc tests

In reply to Benno Lossin

The syn approach requires use of ::pin_init::... instead of the $crate::... construct available to declarative macros. To be able to use the pin_init crate from itself (which includes doc tests), we have to declare it as such.

---
 rust/pin-init/src/lib.rs | 5 +++++
 1 file changed, 5 insertions(+)
rust/pin-init/src/lib.rs +5 -0
index 8673008f45d2..0e707f00061f 100644
@@ -290,6 +290,11 @@
     ptr::{self, NonNull},
 };
 
+// This is used by doc-tests -- the proc-macros expand to `::pin_init::...` and without this the
+// doc-tests wouldn't have an extern crate named `pin_init`.
+#[allow(unused_extern_crates)]
+extern crate self as pin_init;
+
 #[doc(hidden)]
 pub mod __internal;
 #[doc(hidden)]
Signature · 1 line
2.51.2
author maintainer
#3 lore raw

PATCH 3/12 rust: pin-init: add `syn` dependency and remove `proc-macro[2]` and `quote` workarounds

In reply to Benno Lossin

syn makes parsing Rust from proc-macros a lot simpler. pin-init has not used syn up until now, because the we did not support it. That changed in commit 54e3eae85562 ("Merge patch series "syn support""), so we can finally utilize the added ergonomics of parsing proc-macro input with syn.

Previously we only had the proc-macro library available, whereas the user-space version also used proc-macro2 and quote. Now both are available, so remove the workarounds.

Due to these changes, clippy emits warnings about unnecessary .to_string() as proc-macro2 provides an additional PartialEq impl on Ident, so the warnings are fixed.

[ Adjusted wording from upstream version and added build system changes
for the kernel - Benno ]

---
 rust/Makefile                             | 16 ++++++++++------
 rust/pin-init/internal/src/helpers.rs     |  7 ++-----
 rust/pin-init/internal/src/lib.rs         | 16 ----------------
 rust/pin-init/internal/src/pin_data.rs    | 18 ++++++------------
 rust/pin-init/internal/src/pinned_drop.rs | 10 ++++------
 rust/pin-init/internal/src/zeroable.rs    |  6 ++----
 scripts/generate_rust_analyzer.py         |  2 +-
 7 files changed, 25 insertions(+), 50 deletions(-)
rust/Makefile +10 -6
index 5d357dce1704..96f0ac53ec0e 100644
@@ -212,9 +212,10 @@ rustdoc-ffi: $(src)/ffi.rs rustdoc-core FORCE
 
 rustdoc-pin_init_internal: private rustdoc_host = yes
 rustdoc-pin_init_internal: private rustc_target_flags = --cfg kernel \
-    --extern proc_macro --crate-type proc-macro
+    --extern proc_macro --extern proc_macro2 --extern quote --extern syn \
+    --crate-type proc-macro
 rustdoc-pin_init_internal: $(src)/pin-init/internal/src/lib.rs \
-    rustdoc-clean FORCE
+    rustdoc-clean rustdoc-proc_macro2 rustdoc-quote rustdoc-syn FORCE
 	+$(call if_changed,rustdoc)
 
 rustdoc-pin_init: private rustdoc_host = yes
@@ -273,9 +274,10 @@ rusttestlib-macros: $(src)/macros/lib.rs \
 	+$(call if_changed,rustc_test_library)
 
 rusttestlib-pin_init_internal: private rustc_target_flags = --cfg kernel \
-    --extern proc_macro
+    --extern proc_macro --extern proc_macro2 --extern quote --extern syn
 rusttestlib-pin_init_internal: private rustc_test_library_proc = yes
-rusttestlib-pin_init_internal: $(src)/pin-init/internal/src/lib.rs FORCE
+rusttestlib-pin_init_internal: $(src)/pin-init/internal/src/lib.rs \
+    rusttestlib-proc_macro2 rusttestlib-quote rusttestlib-syn FORCE
 	+$(call if_changed,rustc_test_library)
 
 rusttestlib-pin_init: private rustc_target_flags = --extern pin_init_internal \
@@ -547,8 +549,10 @@ $(obj)/$(libmacros_name): $(src)/macros/lib.rs $(obj)/libproc_macro2.rlib \
     $(obj)/libquote.rlib $(obj)/libsyn.rlib FORCE
 	+$(call if_changed_dep,rustc_procmacro)
 
-$(obj)/$(libpin_init_internal_name): private rustc_target_flags = --cfg kernel
-$(obj)/$(libpin_init_internal_name): $(src)/pin-init/internal/src/lib.rs FORCE
+$(obj)/$(libpin_init_internal_name): private rustc_target_flags = --cfg kernel \
+    --extern proc_macro2 --extern quote --extern syn
+$(obj)/$(libpin_init_internal_name): $(src)/pin-init/internal/src/lib.rs \
+	$(obj)/libproc_macro2.rlib $(obj)/libquote.rlib $(obj)/libsyn.rlib FORCE
 	+$(call if_changed_dep,rustc_procmacro)
 
 quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L $@
rust/pin-init/internal/src/helpers.rs +2 -5
index 236f989a50f2..90f85eaa4123 100644
@@ -1,9 +1,6 @@
 // SPDX-License-Identifier: Apache-2.0 OR MIT
 
-#[cfg(not(kernel))]
-use proc_macro2 as proc_macro;
-
-use proc_macro::{TokenStream, TokenTree};
+use proc_macro2::{TokenStream, TokenTree};
 
 /// Parsed generics.
 ///
@@ -101,7 +98,7 @@ pub(crate) fn parse_generics(input: TokenStream) -> (Generics, Vec<TokenTree>) {
                     1 => {
                         // Here depending on the token, it might be a generic variable name.
                         match tt.clone() {
-                            TokenTree::Ident(i) if at_start && i.to_string() == "const" => {
+                            TokenTree::Ident(i) if at_start && i == "const" => {
                                 let Some(name) = toks.next() else {
                                     // Parsing error.
                                     break;
rust/pin-init/internal/src/lib.rs +0 -16
index 297b0129a5bf..4c4dc639ce82 100644
@@ -7,27 +7,11 @@
 //! `pin-init` proc macros.
 
 #![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))]
-// Allow `.into()` to convert
-// - `proc_macro2::TokenStream` into `proc_macro::TokenStream` in the user-space version.
-// - `proc_macro::TokenStream` into `proc_macro::TokenStream` in the kernel version.
-//   Clippy warns on this conversion, but it's required by the user-space version.
-//
-// Remove once we have `proc_macro2` in the kernel.
-#![allow(clippy::useless_conversion)]
 // Documentation is done in the pin-init crate instead.
 #![allow(missing_docs)]
 
 use proc_macro::TokenStream;
 
-#[cfg(kernel)]
-#[path = "../../../macros/quote.rs"]
-#[macro_use]
-#[cfg_attr(not(kernel), rustfmt::skip)]
-mod quote;
-#[cfg(not(kernel))]
-#[macro_use]
-extern crate quote;
-
 mod helpers;
 mod pin_data;
 mod pinned_drop;
rust/pin-init/internal/src/pin_data.rs +6 -12
index 87d4a7eb1d35..86a53b37cc66 100644
@@ -1,10 +1,8 @@
 // SPDX-License-Identifier: Apache-2.0 OR MIT
 
-#[cfg(not(kernel))]
-use proc_macro2 as proc_macro;
-
 use crate::helpers::{parse_generics, Generics};
-use proc_macro::{Group, Punct, Spacing, TokenStream, TokenTree};
+use proc_macro2::{Group, Punct, Spacing, TokenStream, TokenTree};
+use quote::quote;
 
 pub(crate) fn pin_data(args: TokenStream, input: TokenStream) -> TokenStream {
     // This proc-macro only does some pre-parsing and then delegates the actual parsing to
@@ -28,7 +26,7 @@ pub(crate) fn pin_data(args: TokenStream, input: TokenStream) -> TokenStream {
     // The name of the struct with ty_generics.
     let struct_name = rest
         .iter()
-        .skip_while(|tt| !matches!(tt, TokenTree::Ident(i) if i.to_string() == "struct"))
+        .skip_while(|tt| !matches!(tt, TokenTree::Ident(i) if i == "struct"))
         .nth(1)
         .and_then(|tt| match tt {
             TokenTree::Ident(_) => {
@@ -65,7 +63,7 @@ pub(crate) fn pin_data(args: TokenStream, input: TokenStream) -> TokenStream {
         .into_iter()
         .flat_map(|tt| {
             // We ignore top level `struct` tokens, since they would emit a compile error.
-            if matches!(&tt, TokenTree::Ident(i) if i.to_string() == "struct") {
+            if matches!(&tt, TokenTree::Ident(i) if i == "struct") {
                 vec![tt]
             } else {
                 replace_self_and_deny_type_defs(&struct_name, tt, &mut errs)
@@ -98,11 +96,7 @@ fn replace_self_and_deny_type_defs(
 ) -> Vec<TokenTree> {
     match tt {
         TokenTree::Ident(ref i)
-            if i.to_string() == "enum"
-                || i.to_string() == "trait"
-                || i.to_string() == "struct"
-                || i.to_string() == "union"
-                || i.to_string() == "impl" =>
+            if i == "enum" || i == "trait" || i == "struct" || i == "union" || i == "impl" =>
         {
             errs.extend(
                 format!(
@@ -119,7 +113,7 @@ fn replace_self_and_deny_type_defs(
             );
             vec![tt]
         }
-        TokenTree::Ident(i) if i.to_string() == "Self" => struct_name.clone(),
+        TokenTree::Ident(i) if i == "Self" => struct_name.clone(),
         TokenTree::Literal(_) | TokenTree::Punct(_) | TokenTree::Ident(_) => vec![tt],
         TokenTree::Group(g) => vec![TokenTree::Group(Group::new(
             g.delimiter(),
rust/pin-init/internal/src/pinned_drop.rs +4 -6
index c4ca7a70b726..cf8cd1c42984 100644
@@ -1,15 +1,13 @@
 // SPDX-License-Identifier: Apache-2.0 OR MIT
 
-#[cfg(not(kernel))]
-use proc_macro2 as proc_macro;
-
-use proc_macro::{TokenStream, TokenTree};
+use proc_macro2::{TokenStream, TokenTree};
+use quote::quote;
 
 pub(crate) fn pinned_drop(_args: TokenStream, input: TokenStream) -> TokenStream {
     let mut toks = input.into_iter().collect::<Vec<_>>();
     assert!(!toks.is_empty());
     // Ensure that we have an `impl` item.
-    assert!(matches!(&toks[0], TokenTree::Ident(i) if i.to_string() == "impl"));
+    assert!(matches!(&toks[0], TokenTree::Ident(i) if i == "impl"));
     // Ensure that we are implementing `PinnedDrop`.
     let mut nesting: usize = 0;
     let mut pinned_drop_idx = None;
@@ -27,7 +25,7 @@ pub(crate) fn pinned_drop(_args: TokenStream, input: TokenStream) -> TokenStream
         if i >= 1 && nesting == 0 {
             // Found the end of the generics, this should be `PinnedDrop`.
             assert!(
-                matches!(tt, TokenTree::Ident(i) if i.to_string() == "PinnedDrop"),
+                matches!(tt, TokenTree::Ident(i) if i == "PinnedDrop"),
                 "expected 'PinnedDrop', found: '{tt:?}'"
             );
             pinned_drop_idx = Some(i);
rust/pin-init/internal/src/zeroable.rs +2 -4
index e0ed3998445c..d8a5ef3883f4 100644
@@ -1,10 +1,8 @@
 // SPDX-License-Identifier: GPL-2.0
 
-#[cfg(not(kernel))]
-use proc_macro2 as proc_macro;
-
 use crate::helpers::{parse_generics, Generics};
-use proc_macro::{TokenStream, TokenTree};
+use proc_macro2::{TokenStream, TokenTree};
+use quote::quote;
 
 pub(crate) fn parse_zeroable_derive_input(
     input: TokenStream,
scripts/generate_rust_analyzer.py +1 -1
index 147d0cc94068..d31d93888658 100755
@@ -123,7 +123,7 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, core_edit
     append_crate(
         "pin_init_internal",
         srctree / "rust" / "pin-init" / "internal" / "src" / "lib.rs",
-        [],
+        ["std", "proc_macro", "proc_macro2", "quote", "syn"],
         cfg=["kernel"],
         is_proc_macro=True,
     )
Signature · 1 line
2.51.2
author maintainer
#4 lore raw

PATCH 4/12 rust: pin-init: rewrite `derive(Zeroable)` and `derive(MaybeZeroable)` using `syn`

In reply to Benno Lossin

Rewrite the two derive macros for Zeroable using syn. One positive side effect of this change is that tuple structs are now supported by them. Additionally, syntax errors and the error emitted when trying to use one of the derive macros on an enum are improved. Otherwise no functional changes intended.

For example:

    #[derive(Zeroable)]
    enum Num {
        A(u32),
        B(i32),
    }

Produced this error before this commit:

    error: no rules expected keyword `enum`
     --> tests/ui/compile-fail/zeroable/enum.rs:5:1
      |
    5 | enum Num {
      | ^^^^ no rules expected this token in macro call
      |
    note: while trying to match keyword `struct`
     --> src/macros.rs
      |
      |             $vis:vis struct $name:ident
      |                      ^^^^^^

Now the error is:

    error: cannot derive `Zeroable` for an enum
     --> tests/ui/compile-fail/zeroable/enum.rs:5:1
      |
    5 | enum Num {
      | ^^^^

    error: cannot derive `Zeroable` for an enum
---
 rust/pin-init/internal/src/lib.rs      |   5 +-
 rust/pin-init/internal/src/zeroable.rs | 149 ++++++++++---------------
 rust/pin-init/src/macros.rs            | 124 --------------------
 3 files changed, 65 insertions(+), 213 deletions(-)
rust/pin-init/internal/src/lib.rs +3 -2
index 4c4dc639ce82..ec593362c5ac 100644
@@ -11,6 +11,7 @@
 #![allow(missing_docs)]
 
 use proc_macro::TokenStream;
+use syn::parse_macro_input;
 
 mod helpers;
 mod pin_data;
@@ -29,10 +30,10 @@ pub fn pinned_drop(args: TokenStream, input: TokenStream) -> TokenStream {
 
 #[proc_macro_derive(Zeroable)]
 pub fn derive_zeroable(input: TokenStream) -> TokenStream {
-    zeroable::derive(input.into()).into()
+    zeroable::derive(parse_macro_input!(input as _)).into()
 }
 
 #[proc_macro_derive(MaybeZeroable)]
 pub fn maybe_derive_zeroable(input: TokenStream) -> TokenStream {
-    zeroable::maybe_derive(input.into()).into()
+    zeroable::maybe_derive(parse_macro_input!(input as _)).into()
 }
rust/pin-init/internal/src/zeroable.rs +62 -87

161 lines not on this page · raw message

rust/pin-init/src/macros.rs +0 -124

127 lines not on this page · raw message

Signature · 1 line
2.51.2
maintainer
#5 lore raw

Re: [PATCH 04/12] rust: pin-init: rewrite `derive(Zeroable)` and `derive(MaybeZeroable)` using `syn`

Quoted 38 lines · Benno Lossin

Rewrite the two derive macros for Zeroable using syn. One positive side effect of this change is that tuple structs are now supported by them. Additionally, syntax errors and the error emitted when trying to use one of the derive macros on an enum are improved. Otherwise no functional changes intended.

For example:

    #[derive(Zeroable)]
    enum Num {
        A(u32),
        B(i32),
    }

Produced this error before this commit:

    error: no rules expected keyword `enum`
     --> tests/ui/compile-fail/zeroable/enum.rs:5:1
      |
    5 | enum Num {
      | ^^^^ no rules expected this token in macro call
      |
    note: while trying to match keyword `struct`
     --> src/macros.rs
      |
      |             $vis:vis struct $name:ident
      |                      ^^^^^^

Now the error is:

    error: cannot derive `Zeroable` for an enum
     --> tests/ui/compile-fail/zeroable/enum.rs:5:1
      |
    5 | enum Num {
      | ^^^^

    error: cannot derive `Zeroable` for an enum
Quoted 6 lines
---
 rust/pin-init/internal/src/lib.rs      |   5 +-
 rust/pin-init/internal/src/zeroable.rs | 149 ++++++++++---------------
 rust/pin-init/src/macros.rs            | 124 --------------------
 3 files changed, 65 insertions(+), 213 deletions(-)

Yay! the updated code is much more readable.

Quoted 14 lines
rust/pin-init/internal/src/lib.rs +1 -0
index 4c4dc639ce82..ec593362c5ac 100644
@@ -11,6 +11,7 @@
 #![allow(missing_docs)]
 
 use proc_macro::TokenStream;
+use syn::parse_macro_input;
 
 mod helpers;
 mod pin_data;

rust/pin-init/internal/src/lib.rs pub fn pinned_drop(args: TokenStream, input: TokenStream) -> TokenStream { · line 33

Quoted 5 lines
rust/pin-init/internal/src/lib.rs +1 -1
@@ -29,10 +30,10 @@ pub fn pinned_drop(args: TokenStream, input: TokenStream) -> TokenStream {
 
 #[proc_macro_derive(Zeroable)]
 pub fn derive_zeroable(input: TokenStream) -> TokenStream {
-    zeroable::derive(input.into()).into()
+    zeroable::derive(parse_macro_input!(input as _)).into()

This can just be

zeroable::derive(parse_macro_input!(input)).into()

same for the below.

Quoted 78 lines
rust/pin-init/internal/src/lib.rs +1 -1
 }
 
 #[proc_macro_derive(MaybeZeroable)]
 pub fn maybe_derive_zeroable(input: TokenStream) -> TokenStream {
-    zeroable::maybe_derive(input.into()).into()
+    zeroable::maybe_derive(parse_macro_input!(input as _)).into()
 }
rust/pin-init/internal/src/zeroable.rs +3 -60
index d8a5ef3883f4..0328c3bdfceb 100644
@@ -1,99 +1,74 @@
 // SPDX-License-Identifier: GPL-2.0
 
-use crate::helpers::{parse_generics, Generics};
-use proc_macro2::{TokenStream, TokenTree};
-use quote::quote;
+use proc_macro2::TokenStream;
+use quote::{quote, quote_spanned};
+use syn::{parse_quote, Data, DeriveInput, Field, Fields};
 
-pub(crate) fn parse_zeroable_derive_input(
-    input: TokenStream,
-) -> (
-    Vec<TokenTree>,
-    Vec<TokenTree>,
-    Vec<TokenTree>,
-    Option<TokenTree>,
-) {
-    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! { : ::pin_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! { ::pin_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);

rust/pin-init/internal/src/zeroable.rs line 14

Quoted 10 lines
rust/pin-init/internal/src/zeroable.rs +8 -2
-            }
-            _ => new_impl_generics.push(tt),
+pub(crate) fn derive(input: DeriveInput) -> TokenStream {
+    let fields = match input.data {
+        Data::Struct(data_struct) => data_struct.fields,
+        Data::Union(data_union) => Fields::Named(data_union.fields),
+        Data::Enum(data_enum) => {
+            return quote_spanned! {data_enum.enum_token.span=>
+                ::core::compile_error!("cannot derive `Zeroable` for an enum");
+            };

Even if it's currently just one place that could error, I would still probably use syn's Error type as it's easier to extend in the future.

Best,
Gary

Quoted 14 lines
rust/pin-init/internal/src/zeroable.rs +5 -7
         }
+    };
+    let name = input.ident;
+    let mut generics = input.generics;
+    for param in generics.type_params_mut() {
+        param.bounds.insert(0, parse_quote!(::pin_init::Zeroable));
     }
-    assert_eq!(nested, 0);
-    if in_generic && !inserted {
-        new_impl_generics.extend(quote! { : ::pin_init::Zeroable });
-    }
-    (rest, new_impl_generics, ty_generics, last)
-}
-
author maintainer
#6 lore raw

PATCH 5/12 rust: pin-init: rewrite the `#[pinned_drop]` attribute macro using `syn`

In reply to Benno Lossin

Rewrite the attribute macro for implementing PinnedDrop using syn. Otherwise no functional changes intended aside from improved error messages on syntactic and semantical errors. For example:

When missing the drop function in the implementation, the old error was:

    error: no rules expected `)`
     --> tests/ui/compile-fail/pinned_drop/no_fn.rs:6:1
      |
    6 | #[pinned_drop]
      | ^^^^^^^^^^^^^^ no rules expected this token in macro call
      |
    note: while trying to match keyword `fn`
     --> src/macros.rs
      |
      |             fn drop($($sig:tt)*) {
      |             ^^
      = note: this error originates in the attribute macro `pinned_drop` (in Nightly builds, run with -Z macro-backtrace for more info)

And the new one is:

    error[E0046]: not all trait items implemented, missing: `drop`
     --> tests/ui/compile-fail/pinned_drop/no_fn.rs:7:1
      |
    7 | impl PinnedDrop for Foo {}
      | ^^^^^^^^^^^^^^^^^^^^^^^ missing `drop` in implementation
      |
      = help: implement the missing item: `fn drop(self: Pin<&mut Self>, _: OnlyCallFromDrop) { todo!() }`
---
 rust/pin-init/internal/src/lib.rs         |  6 +-
 rust/pin-init/internal/src/pinned_drop.rs | 87 ++++++++++++-----------
 rust/pin-init/src/macros.rs               | 28 --------
 3 files changed, 52 insertions(+), 69 deletions(-)
rust/pin-init/internal/src/lib.rs +5 -1
index ec593362c5ac..d0156b82b5d3 100644
@@ -25,7 +25,11 @@ pub fn pin_data(inner: TokenStream, item: TokenStream) -> TokenStream {
 
 #[proc_macro_attribute]
 pub fn pinned_drop(args: TokenStream, input: TokenStream) -> TokenStream {
-    pinned_drop::pinned_drop(args.into(), input.into()).into()
+    pinned_drop::pinned_drop(
+        parse_macro_input!(args as _),
+        parse_macro_input!(input as _),
+    )
+    .into()
 }
 
 #[proc_macro_derive(Zeroable)]
rust/pin-init/internal/src/pinned_drop.rs +47 -40

96 lines not on this page · raw message

rust/pin-init/src/macros.rs +0 -28
index 53ed5ce860fc..b80c95612fd6 100644
@@ -503,34 +503,6 @@
 #[cfg(not(kernel))]
 pub use ::paste::paste;
 
-/// Creates a `unsafe impl<...> PinnedDrop for $type` block.
-///
-/// See [`PinnedDrop`] for more information.
-///
-/// [`PinnedDrop`]: crate::PinnedDrop
-#[doc(hidden)]
-#[macro_export]
-macro_rules! __pinned_drop {
-    (
-        @impl_sig($($impl_sig:tt)*),
-        @impl_body(
-            $(#[$($attr:tt)*])*
-            fn drop($($sig:tt)*) {
-                $($inner:tt)*
-            }
-        ),
-    ) => {
-        // SAFETY: TODO.
-        unsafe $($impl_sig)* {
-            // Inherit all attributes and the type/ident tokens for the signature.
-            $(#[$($attr)*])*
-            fn drop($($sig)*, _: $crate::__internal::OnlyCallFromDrop) {
-                $($inner)*
-            }
-        }
-    }
-}
-
 /// This macro first parses the struct definition such that it separates pinned and not pinned
 /// fields. Afterwards it declares the struct and implement the `PinData` trait safely.
 #[doc(hidden)]
Signature · 1 line
2.51.2
maintainer
#7 lore raw

Re: [PATCH 05/12] rust: pin-init: rewrite the `#[pinned_drop]` attribute macro using `syn`

Quoted 42 lines · Benno Lossin

Rewrite the attribute macro for implementing PinnedDrop using syn. Otherwise no functional changes intended aside from improved error messages on syntactic and semantical errors. For example:

When missing the drop function in the implementation, the old error was:

    error: no rules expected `)`
     --> tests/ui/compile-fail/pinned_drop/no_fn.rs:6:1
      |
    6 | #[pinned_drop]
      | ^^^^^^^^^^^^^^ no rules expected this token in macro call
      |
    note: while trying to match keyword `fn`
     --> src/macros.rs
      |
      |             fn drop($($sig:tt)*) {
      |             ^^
      = note: this error originates in the attribute macro `pinned_drop` (in Nightly builds, run with -Z macro-backtrace for more info)

And the new one is:

    error[E0046]: not all trait items implemented, missing: `drop`
     --> tests/ui/compile-fail/pinned_drop/no_fn.rs:7:1
      |
    7 | impl PinnedDrop for Foo {}
      | ^^^^^^^^^^^^^^^^^^^^^^^ missing `drop` in implementation
      |
      = help: implement the missing item: `fn drop(self: Pin<&mut Self>, _: OnlyCallFromDrop) { todo!() }`
---
 rust/pin-init/internal/src/lib.rs         |  6 +-
 rust/pin-init/internal/src/pinned_drop.rs | 87 ++++++++++++-----------
 rust/pin-init/src/macros.rs               | 28 --------
 3 files changed, 52 insertions(+), 69 deletions(-)
rust/pin-init/internal/src/lib.rs +0 -0
index ec593362c5ac..d0156b82b5d3 100644

rust/pin-init/internal/src/lib.rs pub fn pin_data(inner: TokenStream, item: TokenStream) -> TokenStream { · line 31

Quoted 8 lines
rust/pin-init/internal/src/lib.rs +4 -1
@@ -25,7 +25,11 @@ pub fn pin_data(inner: TokenStream, item: TokenStream) -> TokenStream {
 
 #[proc_macro_attribute]
 pub fn pinned_drop(args: TokenStream, input: TokenStream) -> TokenStream {
-    pinned_drop::pinned_drop(args.into(), input.into()).into()
+    pinned_drop::pinned_drop(
+        parse_macro_input!(args as _),
+        parse_macro_input!(input as _),
+    )

as _ can be removed.

Quoted 21 lines
rust/pin-init/internal/src/lib.rs +1 -0
+    .into()
 }
 
 #[proc_macro_derive(Zeroable)]
rust/pin-init/internal/src/pinned_drop.rs +3 -6
index cf8cd1c42984..4df2cb9959fb 100644
@@ -1,49 +1,56 @@
 // SPDX-License-Identifier: Apache-2.0 OR MIT
 
-use proc_macro2::{TokenStream, TokenTree};
-use quote::quote;
+use proc_macro2::TokenStream;
+use quote::{quote, quote_spanned};
+use syn::{parse::Nothing, parse_quote, spanned::Spanned, ImplItem, ItemImpl, Token};
 
-pub(crate) fn pinned_drop(_args: TokenStream, input: TokenStream) -> TokenStream {
-    let mut toks = input.into_iter().collect::<Vec<_>>();
-    assert!(!toks.is_empty());
-    // Ensure that we have an `impl` item.

rust/pin-init/internal/src/pinned_drop.rs line 8

Quoted 10 lines
rust/pin-init/internal/src/pinned_drop.rs +2 -8
-    assert!(matches!(&toks[0], TokenTree::Ident(i) if i == "impl"));
-    // Ensure that we are implementing `PinnedDrop`.
-    let mut nesting: usize = 0;
-    let mut pinned_drop_idx = None;
-    for (i, tt) in toks.iter().enumerate() {
-        match tt {
-            TokenTree::Punct(p) if p.as_char() == '<' => {
-                nesting += 1;
+pub(crate) fn pinned_drop(_args: Nothing, mut input: ItemImpl) -> TokenStream {
+    let mut errors = vec![];

Any reason to not make this Vec<Error> and use syn::Error::combine?

Quoted 36 lines
rust/pin-init/internal/src/pinned_drop.rs +28 -6
+    if let Some(unsafety) = input.unsafety {
+        errors.push(quote_spanned! {unsafety.span=>
+            ::core::compile_error!("implementing `PinnedDrop` is safe");
+        });
+    }
+    input.unsafety = Some(Token![unsafe](input.impl_token.span));
+    match &mut input.trait_ {
+        Some((not, path, _for)) => {
+            if let Some(not) = not {
+                errors.push(quote_spanned! {not.span=>
+                    ::core::compile_error!("cannot implement `!PinnedDrop`");
+                });
             }
-            TokenTree::Punct(p) if p.as_char() == '>' => {
-                nesting = nesting.checked_sub(1).unwrap();
-                continue;
+            for (seg, expected) in path
+                .segments
+                .iter()
+                .rev()
+                .zip(["PinnedDrop", "pin_init", ""])
+            {
+                if expected.is_empty() || seg.ident != expected {
+                    errors.push(quote_spanned! {seg.span()=>
+                        ::core::compile_error!("bad import path for `PinnedDrop`");
+                    });
+                }
+                if !seg.arguments.is_none() {
+                    errors.push(quote_spanned! {seg.arguments.span()=>
+                        ::core::compile_error!("unexpected arguments for `PinnedDrop` path");
+                    });
+                }
             }
-            _ => {}
-        }
-        if i >= 1 && nesting == 0 {

rust/pin-init/internal/src/pinned_drop.rs line 41

Quoted 10 lines
rust/pin-init/internal/src/pinned_drop.rs +2 -7
-            // Found the end of the generics, this should be `PinnedDrop`.
-            assert!(
-                matches!(tt, TokenTree::Ident(i) if i == "PinnedDrop"),
-                "expected 'PinnedDrop', found: '{tt:?}'"
-            );
-            pinned_drop_idx = Some(i);
-            break;
+            *path = parse_quote!(::pin_init::PinnedDrop);
         }
+        None => errors.push(quote_spanned! {input.impl_token.span=>

nit: this can be input.impl_token.span.join(input.self_ty.span()).

Best,
Gary

Quoted 28 lines
rust/pin-init/internal/src/pinned_drop.rs +12 -13
+            ::core::compile_error!("expected `impl ... PinnedDrop for ...`, got inherent impl");
+        }),
     }
-    let idx = pinned_drop_idx
-        .unwrap_or_else(|| panic!("Expected an `impl` block implementing `PinnedDrop`."));
-    // Fully qualify the `PinnedDrop`, as to avoid any tampering.
-    toks.splice(idx..idx, quote!(::pin_init::));
-    // Take the `{}` body and call the declarative macro.
-    if let Some(TokenTree::Group(last)) = toks.pop() {
-        let last = last.stream();
-        quote!(::pin_init::__pinned_drop! {
-            @impl_sig(#(#toks)*),
-            @impl_body(#last),
-        })
-    } else {
-        TokenStream::from_iter(toks)
+    for item in &mut input.items {
+        if let ImplItem::Fn(fn_item) = item {
+            if fn_item.sig.ident == "drop" {
+                fn_item
+                    .sig
+                    .inputs
+                    .push(parse_quote!(_: ::pin_init::__internal::OnlyCallFromDrop));
+            }
+        }
     }
+    quote!(#(#errors)* #input)
 }
author maintainer
#8 lore raw

Re: [PATCH 05/12] rust: pin-init: rewrite the `#[pinned_drop]` attribute macro using `syn`

Quoted 14 lines · Gary Guo
Quoted 13 lines · Benno Lossin
rust/pin-init/internal/src/pinned_drop.rs +3 -2
index cf8cd1c42984..4df2cb9959fb 100644
@@ -1,49 +1,56 @@
 // SPDX-License-Identifier: Apache-2.0 OR MIT
 
-use proc_macro2::{TokenStream, TokenTree};
-use quote::quote;
+use proc_macro2::TokenStream;
+use quote::{quote, quote_spanned};
+use syn::{parse::Nothing, parse_quote, spanned::Spanned, ImplItem, ItemImpl, Token};
 
Quoted 16 lines
Quoted 4 lines
rust/pin-init/internal/src/pinned_drop.rs +0 -4
-pub(crate) fn pinned_drop(_args: TokenStream, input: TokenStream) -> TokenStream {
-    let mut toks = input.into_iter().collect::<Vec<_>>();
-    assert!(!toks.is_empty());
-    // Ensure that we have an `impl` item.

rust/pin-init/internal/src/pinned_drop.rs line 8

Quoted 10 lines
rust/pin-init/internal/src/pinned_drop.rs +2 -8
-    assert!(matches!(&toks[0], TokenTree::Ident(i) if i == "impl"));
-    // Ensure that we are implementing `PinnedDrop`.
-    let mut nesting: usize = 0;
-    let mut pinned_drop_idx = None;
-    for (i, tt) in toks.iter().enumerate() {
-        match tt {
-            TokenTree::Punct(p) if p.as_char() == '<' => {
-                nesting += 1;
+pub(crate) fn pinned_drop(_args: Nothing, mut input: ItemImpl) -> TokenStream {
+    let mut errors = vec![];

Any reason to not make this Vec<Error> and use syn::Error::combine?

Is there a way to have an "empty" error? I dislike using Option<Error> here... If not I'll just create my own helper type instead.

Cheers,
Benno

Quoted 12 lines
Quoted 12 lines
rust/pin-init/internal/src/pinned_drop.rs +12 -0
+    if let Some(unsafety) = input.unsafety {
+        errors.push(quote_spanned! {unsafety.span=>
+            ::core::compile_error!("implementing `PinnedDrop` is safe");
+        });
+    }
+    input.unsafety = Some(Token![unsafe](input.impl_token.span));
+    match &mut input.trait_ {
+        Some((not, path, _for)) => {
+            if let Some(not) = not {
+                errors.push(quote_spanned! {not.span=>
+                    ::core::compile_error!("cannot implement `!PinnedDrop`");
+                });
maintainer
#9 lore raw

Re: [PATCH 05/12] rust: pin-init: rewrite the `#[pinned_drop]` attribute macro using `syn`

Quoted 32 lines · Benno Lossin
Quoted 14 lines · Gary Guo
Quoted 13 lines · Benno Lossin
rust/pin-init/internal/src/pinned_drop.rs +3 -2
index cf8cd1c42984..4df2cb9959fb 100644
@@ -1,49 +1,56 @@
 // SPDX-License-Identifier: Apache-2.0 OR MIT
 
-use proc_macro2::{TokenStream, TokenTree};
-use quote::quote;
+use proc_macro2::TokenStream;
+use quote::{quote, quote_spanned};
+use syn::{parse::Nothing, parse_quote, spanned::Spanned, ImplItem, ItemImpl, Token};
 
Quoted 16 lines
Quoted 4 lines
rust/pin-init/internal/src/pinned_drop.rs +0 -4
-pub(crate) fn pinned_drop(_args: TokenStream, input: TokenStream) -> TokenStream {
-    let mut toks = input.into_iter().collect::<Vec<_>>();
-    assert!(!toks.is_empty());
-    // Ensure that we have an `impl` item.

rust/pin-init/internal/src/pinned_drop.rs line 8

Quoted 10 lines
rust/pin-init/internal/src/pinned_drop.rs +2 -8
-    assert!(matches!(&toks[0], TokenTree::Ident(i) if i == "impl"));
-    // Ensure that we are implementing `PinnedDrop`.
-    let mut nesting: usize = 0;
-    let mut pinned_drop_idx = None;
-    for (i, tt) in toks.iter().enumerate() {
-        match tt {
-            TokenTree::Punct(p) if p.as_char() == '<' => {
-                nesting += 1;
+pub(crate) fn pinned_drop(_args: Nothing, mut input: ItemImpl) -> TokenStream {
+    let mut errors = vec![];

Any reason to not make this Vec<Error> and use syn::Error::combine?

Quoted 2 lines

Is there a way to have an "empty" error? I dislike using Option<Error> here... If not I'll just create my own helper type instead.

Why do you need empty error?

You can turn a Vec<Error> into errors by

    errors.into_iter().reduce(|mut acc, e| { acc.combine(e); acc })

If you want helpers, this would be my preferred approach (similar to how rustc handles things):

    struct DiagCtxt {
        errors: Vec<Error>,
    }

    struct ErrorGuaranteed;

    impl DiagCtxt {
        fn emit(err: Error) -> ErrorGuaranteed {
            self.errors.push(err);
            ErrorGuaranteed
        }

        fn with<R: ToTokens>(f: impl FnOnce(&mut DiagCtxt) -> Result<T, ErrorGuaranteed>) -> TokenStream {
            let mut dcx = DiagCtxt { errors: Vec::new() };
            let mut result = match f(&mut dcx) {
                Ok(r) => r.into_token_stream(),
                Err(_) => quote!(),
            };
            for err in dcx.errors {
                result.extend(err.into_token_stream());
            }
            result
        }
    }

(untested)

and now you can

    DiagCtxt::with(|dcx| generate_xxx(dcx));

    // Non-fatal error
    dcx.emit(...)

    // Fatal-error
    Err(dcx.emit(...))?

Best,
Gary

Quoted 16 lines

Cheers,
Benno

Quoted 12 lines
Quoted 12 lines
rust/pin-init/internal/src/pinned_drop.rs +12 -0
+    if let Some(unsafety) = input.unsafety {
+        errors.push(quote_spanned! {unsafety.span=>
+            ::core::compile_error!("implementing `PinnedDrop` is safe");
+        });
+    }
+    input.unsafety = Some(Token![unsafe](input.impl_token.span));
+    match &mut input.trait_ {
+        Some((not, path, _for)) => {
+            if let Some(not) = not {
+                errors.push(quote_spanned! {not.span=>
+                    ::core::compile_error!("cannot implement `!PinnedDrop`");
+                });
author maintainer
#10 lore raw

PATCH 6/12 rust: pin-init: rewrite `#[pin_data]` using `syn`

In reply to Benno Lossin

Rewrite the attribute macro #[pin_data] using syn. No functional changes intended aside from improved error messages on syntactic and semantical errors. For example if one forgets a comma at the end of a field:

    #[pin_data]
    struct Foo {
        a: Box<Foo>
        b: Box<Foo>
    }

The declarative macro reports the following errors:

    error: expected `,`, or `}`, found `b`
     --> tests/ui/compile-fail/pin_data/missing_comma.rs:5:16
      |
    5 |     a: Box<Foo>
      |                ^ help: try adding a comma: `,`

    error: recursion limit reached while expanding `$crate::__pin_data!`
     --> tests/ui/compile-fail/pin_data/missing_comma.rs:3:1
      |
    3 | #[pin_data]
      | ^^^^^^^^^^^
      |
      = help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`$CRATE`)
      = note: this error originates in the macro `$crate::__pin_data` which comes from the expansion of the attribute macro `pin_data` (in Nightly builds, run with -Z macro-backtrace for more info)

The new syn version reports:

    error: expected `,`, or `}`, found `b`
     --> tests/ui/compile-fail/pin_data/missing_comma.rs:5:16
      |
    5 |     a: Box<Foo>
      |                ^ help: try adding a comma: `,`

    error: expected `,`
     --> tests/ui/compile-fail/pin_data/missing_comma.rs:6:5
      |
    6 |     b: Box<Foo>
      |     ^
---
 rust/pin-init/internal/src/helpers.rs  | 149 ------
 rust/pin-init/internal/src/lib.rs      |  12 +-
 rust/pin-init/internal/src/pin_data.rs | 645 ++++++++++++++++++++-----
 rust/pin-init/src/macros.rs            | 574 ----------------------
 4 files changed, 543 insertions(+), 837 deletions(-)
 delete mode 100644 rust/pin-init/internal/src/helpers.rs
rust/pin-init/internal/src/helpers.rs deleted +0 -149
deleted file mode 100644
index 90f85eaa4123..000000000000
@@ -1,149 +0,0 @@
-// SPDX-License-Identifier: Apache-2.0 OR MIT
-
-use proc_macro2::{TokenStream, TokenTree};
-
-/// Parsed generics.
-///
-/// See the field documentation for an explanation what each of the fields represents.
-///
-/// # Examples
-///
-/// ```rust,ignore
-/// # let input = todo!();
-/// 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() {
-///             // ...
-///         }
-///     }
-/// }
-/// ```
-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`).
-    pub(crate) decl_generics: Vec<TokenTree>,
-    /// The generics with bounds (e.g. `T: Clone, const N: usize`).
-    ///
-    /// Use this on `impl` blocks e.g. `impl<$impl_generics> Trait for ...`.
-    pub(crate) impl_generics: Vec<TokenTree>,
-    /// The generics without bounds and without default values (e.g. `T, N`).
-    ///
-    /// Use this when you use the type that is declared with these generics e.g.
-    /// `Foo<$ty_generics>`.
-    pub(crate) ty_generics: Vec<TokenTree>,
-}
-
-/// Parses the given `TokenStream` into `Generics` and the rest.
-///
-/// The generics are not present in the rest, but a where clause might remain.
-pub(crate) fn parse_generics(input: TokenStream) -> (Generics, Vec<TokenTree>) {
-    // The generics with bounds and default values.
-    let mut decl_generics = vec![];
-    // `impl_generics`, the declared generics with their bounds.
-    let mut impl_generics = vec![];
-    // Only the names of the generics, without any bounds.
-    let mut ty_generics = vec![];
-    // Tokens not related to the generics e.g. the `where` token and definition.
-    let mut rest = vec![];
-    // The current level of `<`.
-    let mut nesting = 0;
-    let mut toks = input.into_iter();
-    // If we are at the beginning of a generic parameter.
-    let mut at_start = true;
-    let mut skip_until_comma = false;
-    while let Some(tt) = toks.next() {
-        if nesting == 1 && matches!(&tt, TokenTree::Punct(p) if p.as_char() == '>') {
-            // Found the end of the generics.
-            break;
-        } else if nesting >= 1 {
-            decl_generics.push(tt.clone());
-        }
-        match tt.clone() {
-            TokenTree::Punct(p) if p.as_char() == '<' => {
-                if nesting >= 1 && !skip_until_comma {
-                    // This is inside of the generics and part of some bound.
-                    impl_generics.push(tt);
-                }
-                nesting += 1;
-            }
-            TokenTree::Punct(p) if p.as_char() == '>' => {
-                // This is a parsing error, so we just end it here.
-                if nesting == 0 {
-                    break;
-                } else {
-                    nesting -= 1;
-                    if nesting >= 1 && !skip_until_comma {
-                        // We are still inside of the generics and part of some bound.
-                        impl_generics.push(tt);
-                    }
-                }
-            }
-            TokenTree::Punct(p) if skip_until_comma && p.as_char() == ',' => {
-                if nesting == 1 {
-                    impl_generics.push(tt.clone());
-                    impl_generics.push(tt);
-                    skip_until_comma = false;
-                }
-            }
-            _ if !skip_until_comma => {
-                match nesting {
-                    // If we haven't entered the generics yet, we still want to keep these tokens.
-                    0 => rest.push(tt),
-                    1 => {
-                        // Here depending on the token, it might be a generic variable name.
-                        match tt.clone() {
-                            TokenTree::Ident(i) if at_start && i == "const" => {
-                                let Some(name) = toks.next() else {
-                                    // Parsing error.
-                                    break;
-                                };
-                                impl_generics.push(tt);
-                                impl_generics.push(name.clone());
-                                ty_generics.push(name.clone());
-                                decl_generics.push(name);
-                                at_start = false;
-                            }
-                            TokenTree::Ident(_) if at_start => {
-                                impl_generics.push(tt.clone());
-                                ty_generics.push(tt);
-                                at_start = false;
-                            }
-                            TokenTree::Punct(p) if p.as_char() == ',' => {
-                                impl_generics.push(tt.clone());
-                                ty_generics.push(tt);
-                                at_start = true;
-                            }
-                            // Lifetimes begin with `'`.
-                            TokenTree::Punct(p) if p.as_char() == '\'' && at_start => {
-                                impl_generics.push(tt.clone());
-                                ty_generics.push(tt);
-                            }
-                            // Generics can have default values, we skip these.
-                            TokenTree::Punct(p) if p.as_char() == '=' => {
-                                skip_until_comma = true;
-                            }
-                            _ => impl_generics.push(tt),
-                        }
-                    }
-                    _ => impl_generics.push(tt),
-                }
-            }
-            _ => {}
-        }
-    }
-    rest.extend(toks);
-    (
-        Generics {
-            impl_generics,
-            decl_generics,
-            ty_generics,
-        },
-        rest,
-    )
-}
rust/pin-init/internal/src/lib.rs +9 -3
index d0156b82b5d3..243684a1eedc 100644
@@ -13,14 +13,20 @@
 use proc_macro::TokenStream;
 use syn::parse_macro_input;
 
-mod helpers;
 mod pin_data;
 mod pinned_drop;
 mod zeroable;
 
 #[proc_macro_attribute]
-pub fn pin_data(inner: TokenStream, item: TokenStream) -> TokenStream {
-    pin_data::pin_data(inner.into(), item.into()).into()
+pub fn pin_data(args: TokenStream, input: TokenStream) -> TokenStream {
+    match pin_data::pin_data(
+        parse_macro_input!(args as _),
+        parse_macro_input!(input as _),
+    ) {
+        Ok(stream) => stream,
+        Err(err) => err.into_compile_error(),
+    }
+    .into()
 }
 
 #[proc_macro_attribute]
rust/pin-init/internal/src/pin_data.rs +534 -111

660 lines not on this page · raw message

rust/pin-init/src/macros.rs +0 -574

580 lines not on this page · raw message

Signature · 1 line
2.51.2
#11 lore raw

Re: [PATCH 06/12] rust: pin-init: rewrite `#[pin_data]` using `syn`

kernel test robot noticed the following build warnings:

Hi Benno,

kernel test robot noticed the following build warnings:

[auto build test WARNING on rust/rust-next]
[also build test WARNING on linus/master v6.19-rc4 next-20260109] [cannot apply to rust/pin-init-next]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Benno-Lossin/rust-pin-init-allow-the-crate-to-refer-to-itself-as-pin-init-in-doc-tests/20260108-234753
base:   https://github.com/Rust-for-Linux/linux rust-next
patch link:    https://lore.kernel.org/r/20260108135127.3153925-7-lossin%40kernel.org
patch subject: [PATCH 06/12] rust: pin-init: rewrite `#[pin_data]` using `syn`
config: x86_64-rhel-9.4-rust (https://download.01.org/0day-ci/archive/20260109/[email protected]/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
rustc: rustc 1.88.0 (6b00bc388 2025-06-23)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260109/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> warning: hidden lifetime parameters in types are deprecated
   --> rust/pin-init/internal/src/pin_data.rs:24:33
   |
   24 |     fn parse(input: syn::parse::ParseStream) -> Result<Self> {
   |                     ------------^^^^^^^^^^^
   |                     |
   |                     expected lifetime parameter
   |

= note: -W elided-lifetimes-in-paths implied by -W rust-2018-idioms = help: to override -W rust-2018-idioms add #[allow(elided_lifetimes_in_paths)]
help: indicate the anonymous lifetime
|
24 | fn parse(input: syn::parse::ParseStream<'_>) -> Result<Self> {
| ++++

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
maintainer
#12 lore raw

Re: [PATCH 06/12] rust: pin-init: rewrite `#[pin_data]` using `syn`

In reply to Benno Lossin
Quoted 79 lines · Benno Lossin

Rewrite the attribute macro #[pin_data] using syn. No functional changes intended aside from improved error messages on syntactic and semantical errors. For example if one forgets a comma at the end of a field:

    #[pin_data]
    struct Foo {
        a: Box<Foo>
        b: Box<Foo>
    }

The declarative macro reports the following errors:

    error: expected `,`, or `}`, found `b`
     --> tests/ui/compile-fail/pin_data/missing_comma.rs:5:16
      |
    5 |     a: Box<Foo>
      |                ^ help: try adding a comma: `,`

    error: recursion limit reached while expanding `$crate::__pin_data!`
     --> tests/ui/compile-fail/pin_data/missing_comma.rs:3:1
      |
    3 | #[pin_data]
      | ^^^^^^^^^^^
      |
      = help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`$CRATE`)
      = note: this error originates in the macro `$crate::__pin_data` which comes from the expansion of the attribute macro `pin_data` (in Nightly builds, run with -Z macro-backtrace for more info)

The new syn version reports:

    error: expected `,`, or `}`, found `b`
     --> tests/ui/compile-fail/pin_data/missing_comma.rs:5:16
      |
    5 |     a: Box<Foo>
      |                ^ help: try adding a comma: `,`

    error: expected `,`
     --> tests/ui/compile-fail/pin_data/missing_comma.rs:6:5
      |
    6 |     b: Box<Foo>
      |     ^
---
 rust/pin-init/internal/src/helpers.rs  | 149 ------
 rust/pin-init/internal/src/lib.rs      |  12 +-
 rust/pin-init/internal/src/pin_data.rs | 645 ++++++++++++++++++++-----
 rust/pin-init/src/macros.rs            | 574 ----------------------
 4 files changed, 543 insertions(+), 837 deletions(-)
 delete mode 100644 rust/pin-init/internal/src/helpers.rs
rust/pin-init/internal/src/pin_data.rs +14 -6
index 86a53b37cc66..d1e7ed121860 100644
@@ -1,126 +1,549 @@
 // SPDX-License-Identifier: Apache-2.0 OR MIT
 
-use crate::helpers::{parse_generics, Generics};
-use proc_macro2::{Group, Punct, Spacing, TokenStream, TokenTree};
-use quote::quote;
+use proc_macro2::TokenStream;
+use quote::{format_ident, quote};
+use syn::{
+    parse::{End, Nothing, Parse},
+    parse_quote, parse_quote_spanned,
+    spanned::Spanned,
+    visit_mut::VisitMut,
+    Error, Field, Ident, Item, ItemStruct, PathSegment, Result, Type, TypePath, WhereClause,
+};
 
-pub(crate) fn pin_data(args: TokenStream, input: TokenStream) -> TokenStream {
-    // This proc-macro only does some pre-parsing and then delegates the actual parsing to
-    // `pin_init::__pin_data!`.
+pub(crate) mod kw {
+    syn::custom_keyword!(PinnedDrop);
+}
+
+pub(crate) enum Args {

rust/pin-init/internal/src/pin_data.rs line 27

Quoted 10 lines
rust/pin-init/internal/src/pin_data.rs +10 -0
+    Nothing(Nothing),
+    #[allow(dead_code)]
+    PinnedDrop(kw::PinnedDrop),
+}
+
+impl Parse for Args {
+    fn parse(input: syn::parse::ParseStream) -> Result<Self> {
+        let lh = input.lookahead1();
+        if lh.peek(End) {
+            input.parse().map(Self::Nothing)

How about make this impl Parse for Option<Args> and remove the nothing variant? It looks a bit weird.

Quoted 39 lines
rust/pin-init/internal/src/pin_data.rs +39 -0
+        } else if lh.peek(kw::PinnedDrop) {
+            let res = input.parse().map(Self::PinnedDrop)?;
+            let lh = input.lookahead1();
+            if lh.peek(End) {
+                Ok(res)
+            } else {
+                Err(lh.error())
+            }
+        } else {
+            Err(lh.error())
+        }
+    }
+}
+
+pub(crate) fn pin_data(args: Args, input: Item) -> Result<TokenStream> {
+    let mut struct_ = match input {
+        Item::Struct(struct_) => struct_,
+        Item::Enum(enum_) => {
+            return Err(Error::new_spanned(
+                enum_.enum_token,
+                "`#[pin_data]` only supports structs for now",
+            ));
+        }
+        Item::Union(union) => {
+            return Err(Error::new_spanned(
+                union.union_token,
+                "`#[pin_data]` only supports structs for now",
+            ));
+        }
+        rest => {
+            return Err(Error::new_spanned(
+                rest,
+                "`#[pin_data]` can only be applied to struct, enum and union defintions",
+            ));
+        }
+    };
+
+    // The generics might contain the `Self` type. Since this macro will define a new type with the
+    // same generics and bounds, this poses a problem: `Self` will refer to the new type as opposed

rust/pin-init/internal/src/pin_data.rs line 76

Quoted 10 lines
rust/pin-init/internal/src/pin_data.rs +10 -0
+    // to this struct definition. Therefore we have to replace `Self` with the concrete name.
+    let mut replacer = {
+        let name = &struct_.ident;
+        let (_, ty_generics, _) = struct_.generics.split_for_impl();
+        SelfReplacer(parse_quote!(#name #ty_generics))
+    };
+    replacer.visit_generics_mut(&mut struct_.generics);
+    replacer.visit_fields_mut(&mut struct_.fields);
+
+    let mut error: Option<Error> = None;

I would probably just collect into a Vec and merge at the end.

rust/pin-init/internal/src/pin_data.rs line 82

Quoted 6 lines
rust/pin-init/internal/src/pin_data.rs +6 -0
+    for field in &struct_.fields {
+        if !is_field_structurally_pinned(field) && is_phantom_pinned(&field.ty) {
+            let mut err = Error::new_spanned(
+                field,
+                format!(
+                    "The field `{}` of type `PhantomData` only has an effect \

You mean PhantomPinned?

Quoted 62 lines
rust/pin-init/internal/src/pin_data.rs +62 -0
+                if it has the `#[pin]` attribute",
+                    field.ident.as_ref().expect(""),
+                ),
+            );
+            if let Some(mut error) = error.take() {
+                error.combine(err);
+                err = error;
+            }
+            error = Some(err);
+        }
+    }
+
+    let unpin_impl = generate_unpin_impl(&struct_);
+    let drop_impl = generate_drop_impl(&struct_, args);
+    let projections = generate_projections(&struct_);
+    let the_pin_data = generate_the_pin_data(&struct_);
+
+    strip_pin_annotations(&mut struct_);
+
+    let error = error.map(|e| e.into_compile_error());
+
+    Ok(quote! {
+        #struct_
+        #projections
+        // We put the rest into this const item, because it then will not be accessible to anything
+        // outside.
+        const _: () = {
+            #the_pin_data
+            #unpin_impl
+            #drop_impl
+        };
+        #error
+    })
+}
+
+fn is_phantom_pinned(ty: &Type) -> bool {
+    match ty {
+        Type::Path(TypePath { qself: None, path }) => {
+            // Cannot possibly refer to `PhantomPinned` (except alias, but that's on the user).
+            if path.segments.len() > 3 {
+                return false;
+            }
+            // If there is a `::`, then the path needs to be `::core::marker::PhantomPinned` or
+            // `::std::marker::PhantomPinned`.
+            if path.leading_colon.is_some() && path.segments.len() != 3 {
+                return false;
+            }
+            let expected: Vec<&[&str]> = vec![&["PhantomPinned"], &["marker"], &["core", "std"]];
+            for (actual, expected) in path.segments.iter().rev().zip(expected) {
+                if !actual.arguments.is_empty() || expected.iter().all(|e| actual.ident != e) {
+                    return false;
+                }
+            }
+            true
+        }
+        _ => false,
+    }
+}
+
+fn is_field_structurally_pinned(field: &Field) -> bool {
+    field.attrs.iter().any(|a| a.path().is_ident("pin"))
+}

rust/pin-init/internal/src/pin_data.rs line 154

Quoted 10 lines
rust/pin-init/internal/src/pin_data.rs +9 -0
 
+fn generate_unpin_impl(
+    ItemStruct {
+        ident,
+        generics,
+        fields,
+        ..
+    }: &ItemStruct,
+) -> TokenStream {
+    let generics_with_pinlt = {

Can you name this generics_with_pin_lt? Otherwise I read it as a misspelled pinit :)

Quoted 126 lines
rust/pin-init/internal/src/pin_data.rs +99 -23
+        let mut g = generics.clone();
+        g.params.insert(0, parse_quote!('__pin));
+        let _ = g.make_where_clause();
+        g
+    };
     let (
+        impl_generics_with_pinlt,
+        ty_generics_with_pinlt,
+        Some(WhereClause {
+            where_token,
+            predicates,
+        }),
+    ) = generics_with_pinlt.split_for_impl()
+    else {
+        unreachable!()
+    };
+    let (_, ty_generics, _) = generics.split_for_impl();
+    let mut pinned_fields = fields
         .iter()
+        .filter(|f| is_field_structurally_pinned(f))
+        .cloned()
+        .collect::<Vec<_>>();
+    for field in &mut pinned_fields {
+        field.attrs.retain(|a| !a.path().is_ident("pin"));
+    }
+    quote! {
+        // 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
+        #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
+            __Unpin #ty_generics_with_pinlt: ::core::marker::Unpin,
+            #predicates
+        {}
+    }
+}
+
+fn generate_drop_impl(
+    ItemStruct {
+        ident, generics, ..
+    }: &syn::ItemStruct,
+    args: Args,
+) -> TokenStream {
+    let (impl_generics, ty_generics, whr) = generics.split_for_impl();
+    let has_pinned_drop = matches!(args, Args::PinnedDrop(_));
+    // We need to disallow normal `Drop` implementation, the exact behavior depends on whether
+    // `PinnedDrop` was specified in `args`.
+    if has_pinned_drop {
+        // When `PinnedDrop` was specified we just implement `Drop` and delegate.
+        quote! {
+            impl #impl_generics ::core::ops::Drop for #ident #ty_generics
+                #whr
+            {
+                fn drop(&mut self) {
+                    // SAFETY: Since this is a destructor, `self` will not move after this function
+                    // terminates, since it is inaccessible.
+                    let pinned = unsafe { ::core::pin::Pin::new_unchecked(self) };
+                    // SAFETY: Since this is a drop function, we can create this token to call the
+                    // pinned destructor of this type.
+                    let token = unsafe { ::pin_init::__internal::OnlyCallFromDrop::new() };
+                    ::pin_init::PinnedDrop::drop(pinned, token);
                 }
-                Some(res)
             }
-            _ => None,
-        })
-        .unwrap_or_else(|| {
-            // If we did not find the name of the struct then we will use `Self` as the replacement
-            // and add a compile error to ensure it does not compile.
-            errs.extend(
-                "::core::compile_error!(\"Could not locate type name.\");"
-                    .parse::<TokenStream>()
-                    .unwrap(),
-            );
-            "Self".parse::<TokenStream>().unwrap().into_iter().collect()
-        });
-    let impl_generics = impl_generics
-        .into_iter()
-        .flat_map(|tt| replace_self_and_deny_type_defs(&struct_name, tt, &mut errs))
-        .collect::<Vec<_>>();
-    let mut rest = rest
-        .into_iter()
-        .flat_map(|tt| {
-            // We ignore top level `struct` tokens, since they would emit a compile error.
-            if matches!(&tt, TokenTree::Ident(i) if i == "struct") {
-                vec![tt]
+        }
+    } else {
+        // When no `PinnedDrop` was specified, then we have to prevent implementing drop.
+        quote! {
+            // We prevent this by creating a trait that will be implemented for all types implementing
+            // `Drop`. Additionally we will implement this trait for the struct leading to a conflict,
+            // if it also implements `Drop`
+            trait MustNotImplDrop {}
+            #[expect(drop_bounds)]
+            impl<T: ::core::ops::Drop> MustNotImplDrop for T {}
+            impl #impl_generics MustNotImplDrop for #ident #ty_generics
+                #whr
+            {}
+            // We also take care to prevent users from writing a useless `PinnedDrop` implementation.
+            // They might implement `PinnedDrop` correctly for the struct, but forget to give
+            // `PinnedDrop` as the parameter to `#[pin_data]`.
+            #[expect(non_camel_case_types)]
+            trait UselessPinnedDropImpl_you_need_to_specify_PinnedDrop {}
+            impl<T: ::pin_init::PinnedDrop>
+                UselessPinnedDropImpl_you_need_to_specify_PinnedDrop for T {}
+            impl #impl_generics
+                UselessPinnedDropImpl_you_need_to_specify_PinnedDrop for #ident #ty_generics
+                #whr
+            {}
+        }
+    }
+}
+

rust/pin-init/internal/src/pin_data.rs line 267

Quoted 10 lines
rust/pin-init/internal/src/pin_data.rs +10 -0
+fn generate_projections(
+    ItemStruct {
+        vis,
+        ident,
+        generics,
+        fields,
+        ..
+    }: &ItemStruct,
+) -> TokenStream {
+    let (og_impl_gen, og_ty_gen, _) = generics.split_for_impl();

Please make sure the names match the early generate_ function, so impl_generics, ty_generics for the original ones, and _with_pin_lt for the ones with lifetime added.

Quoted 58 lines
rust/pin-init/internal/src/pin_data.rs +43 -13
+    let mut generics = generics.clone();
+    generics.params.insert(0, parse_quote!('__pin));
+    let (_, ty_gen, whr) = generics.split_for_impl();
+    let projection = format_ident!("{ident}Projection");
+    let this = format_ident!("this");
+
+    let (fields_decl, fields_proj) = collect_tuple(fields.iter().map(
+        |f @ Field {
+             vis,
+             ident,
+             ty,
+             attrs,
+             ..
+         }| {
+            let mut attrs = attrs.clone();
+            attrs.retain(|a| !a.path().is_ident("pin"));
+            let mut no_doc_attrs = attrs.clone();
+            no_doc_attrs.retain(|a| !a.path().is_ident("doc"));
+            let ident = ident
+                .as_ref()
+                .expect("only structs with named fields are supported");
+            if is_field_structurally_pinned(f) {
+                (
+                    quote!(
+                        #(#attrs)*
+                        #vis #ident: ::core::pin::Pin<&'__pin mut #ty>,
+                    ),
+                    quote!(
+                        #(#no_doc_attrs)*
+                        // SAFETY: this field is structurally pinned.
+                        #ident: unsafe { ::core::pin::Pin::new_unchecked(&mut #this.#ident) },
+                    ),
+                )
             } else {
-                replace_self_and_deny_type_defs(&struct_name, tt, &mut errs)
+                (
+                    quote!(
+                        #(#attrs)*
+                        #vis #ident: &'__pin mut #ty,
+                    ),
+                    quote!(
+                        #(#no_doc_attrs)*
+                        #ident: &mut #this.#ident,
+                    ),
+                )
             }
-        })
-        .collect::<Vec<_>>();
-    // This should be the body of the struct `{...}`.
-    let last = rest.pop();
-    let mut quoted = quote!(::pin_init::__pin_data! {
-        parse_input:
-        @args(#args),
-        @sig(#(#rest)*),
-        @impl_generics(#(#impl_generics)*),
-        @ty_generics(#(#ty_generics)*),
-        @decl_generics(#(#decl_generics)*),
-        @body(#last),

rust/pin-init/internal/src/pin_data.rs line 319

Quoted 10 lines
rust/pin-init/internal/src/pin_data.rs +7 -3
-    });
-    quoted.extend(errs);
-    quoted
+        },
+    ));
+    let structurally_pinned_fields_docs = fields
+        .iter()
+        .filter(|f| is_field_structurally_pinned(f))
+        .map(|Field { ident, .. }| {
+            let doc = format!(" - `{}`", ident.as_ref().expect(""));

I'd just use unwrap over expect("").

rust/pin-init/internal/src/pin_data.rs line 328

Quoted 9 lines
rust/pin-init/internal/src/pin_data.rs +9 -0
+            quote!(#[doc = #doc])
+        });
+    let not_structurally_pinned_fields_docs = fields
+        .iter()
+        .filter(|f| !is_field_structurally_pinned(f))
+        .map(|Field { ident, .. }| {
+            let doc = format!(" - `{}`", ident.as_ref().expect(""));
+            quote!(#[doc = #doc])
+        });

Instead of building the #[doc = ...] streams for each field, I think we should just build the docs entirely and insert in one go.

Quoted 67 lines
rust/pin-init/internal/src/pin_data.rs +65 -0
+    let docs = format!(" Pin-projections of [`{ident}`]");
+    quote! {
+        #[doc = #docs]
+        #[allow(dead_code)]
+        #[doc(hidden)]
+        #vis struct #projection #generics {
+            #(#fields_decl)*
+            ___pin_phantom_data: ::core::marker::PhantomData<&'__pin mut ()>,
+        }
+
+        impl #og_impl_gen #ident #og_ty_gen
+            #whr
+        {
+            /// Pin-projects all fields of `Self`.
+            ///
+            /// These fields are structurally pinned:
+            #(#structurally_pinned_fields_docs)*
+            ///
+            /// These fields are **not** structurally pinned:
+            #(#not_structurally_pinned_fields_docs)*
+            #[inline]
+            #vis fn project<'__pin>(
+                self: ::core::pin::Pin<&'__pin mut Self>,
+            ) -> #projection #ty_gen {
+                // SAFETY: we only give access to `&mut` for fields not structurally pinned.
+                let #this = unsafe { ::core::pin::Pin::get_unchecked_mut(self) };
+                #projection {
+                    #(#fields_proj)*
+                    ___pin_phantom_data: ::core::marker::PhantomData,
+                }
+            }
+        }
+    }
 }
 
+fn generate_the_pin_data(
+    ItemStruct {
+        generics,
+        fields,
+        ident,
+        vis,
+        ..
+    }: &syn::ItemStruct,
+) -> TokenStream {
+    let (impl_generics, ty_generics, whr) = generics.split_for_impl();
+
+    // For every field, we create an initializing projection function according to its projection
+    // type. If a field is structurally pinned, then it must be initialized via `PinInit`, if it is
+    // not structurally pinned, then it can be initialized via `Init`.
+    //
+    // The functions are `unsafe` to prevent accidentally calling them.
+    fn handle_field(
+        Field {
+            vis,
+            ident,
+            ty,
+            attrs,
+            ..
+        }: &Field,
+        struct_ident: &Ident,
+        pinned: bool,
+    ) -> TokenStream {
+        let mut attrs = attrs.clone();
+        attrs.retain(|a| !a.path().is_ident("pin"));
+        let ident = ident
+            .as_ref()
+            .expect("only structs with named fields are supported");

rust/pin-init/internal/src/pin_data.rs line 405

Quoted 10 lines
rust/pin-init/internal/src/pin_data.rs +10 -0
+        let project_ident = format_ident!("__project_{ident}");
+        let (init_ty, init_fn, project_ty, project_body, pin_safety) = if pinned {
+            (
+                quote!(PinInit),
+                quote!(__pinned_init),
+                quote!(::core::pin::Pin<&'__slot mut #ty>),
+                // SAFETY: this field is structurally pinned.
+                quote!(unsafe { ::core::pin::Pin::new_unchecked(slot) }),
+                quote!(
+                    #[doc = " - `slot` will not move until it is dropped, i.e. it will be pinned."]

This can just be /// ...

Quoted 119 lines
rust/pin-init/internal/src/pin_data.rs +94 -24
+                ),
+            )
+        } else {
+            (
+                quote!(Init),
+                quote!(__init),
+                quote!(&'__slot mut #ty),
+                quote!(slot),
+                quote!(),
+            )
+        };
+        let slot_safety = format!(
+            " `slot` points at the field `{ident}` inside of `{struct_ident}`, which is pinned.",
+        );
+        quote! {
+            /// # Safety
+            ///
+            /// - `slot` is a valid pointer to uninitialized memory.
+            /// - the caller does not touch `slot` when `Err` is returned, they are only permitted
+            ///   to deallocate.
+            #pin_safety
+            #(#attrs)*
+            #vis unsafe fn #ident<E>(
+                self,
+                slot: *mut #ty,
+                init: impl ::pin_init::#init_ty<#ty, E>,
+            ) -> ::core::result::Result<(), E> {
+                // SAFETY: this function has the same safety requirements as the __init function
+                // called below.
+                unsafe { ::pin_init::#init_ty::#init_fn(init, slot) }
+            }
+
+            /// # Safety
+            ///
+            #[doc = #slot_safety]
+            #(#attrs)*
+            #vis unsafe fn #project_ident<'__slot>(
+                self,
+                slot: &'__slot mut #ty,
+            ) -> #project_ty {
+                #project_body
+            }
+        }
+    }
+
+    let field_accessors = fields
+        .iter()
+        .map(|f| handle_field(f, ident, is_field_structurally_pinned(f)))
+        .collect::<TokenStream>();
+    quote! {
+        // 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.
+        #[doc(hidden)]
+        #vis struct __ThePinData #generics
+            #whr
         {
-            errs.extend(
-                format!(
-                    "::core::compile_error!(\"Cannot use `{i}` inside of struct definition with \
-                        `#[pin_data]`.\");"
-                )
-                .parse::<TokenStream>()
-                .unwrap()
-                .into_iter()
-                .map(|mut tok| {
-                    tok.set_span(tt.span());
-                    tok
-                }),
-            );
-            vec![tt]
-        }
-        TokenTree::Ident(i) if i == "Self" => struct_name.clone(),
-        TokenTree::Literal(_) | TokenTree::Punct(_) | TokenTree::Ident(_) => vec![tt],
-        TokenTree::Group(g) => vec![TokenTree::Group(Group::new(
-            g.delimiter(),
-            g.stream()
-                .into_iter()
-                .flat_map(|tt| replace_self_and_deny_type_defs(struct_name, tt, errs))
-                .collect(),
-        ))],
+            __phantom: ::core::marker::PhantomData<
+                fn(#ident #ty_generics) -> #ident #ty_generics
+            >,
+        }
+
+        impl #impl_generics ::core::clone::Clone for __ThePinData #ty_generics
+            #whr
+        {
+            fn clone(&self) -> Self { *self }
+        }
+
+        impl #impl_generics ::core::marker::Copy for __ThePinData #ty_generics
+            #whr
+        {}
+
+        #[allow(dead_code)] // Some functions might never be used and private.
+        #[expect(clippy::missing_safety_doc)]
+        impl #impl_generics __ThePinData #ty_generics
+            #whr
+        {
+            #field_accessors
+        }
+
+        // SAFETY: We have added the correct projection functions above to `__ThePinData` and
+        // we also use the least restrictive generics possible.
+        unsafe impl #impl_generics ::pin_init::__internal::HasPinData for #ident #ty_generics
+            #whr
+        {
+            type PinData = __ThePinData #ty_generics;
+
+            unsafe fn __pin_data() -> Self::PinData {
+                __ThePinData { __phantom: ::core::marker::PhantomData }
+            }
+        }
+
+        // SAFETY: TODO
+        unsafe impl #impl_generics ::pin_init::__internal::PinData for __ThePinData #ty_generics
+            #whr
+        {

rust/pin-init/internal/src/pin_data.rs line 510

Quoted 10 lines
rust/pin-init/internal/src/pin_data.rs +10 -0
+            type Datee = #ident #ty_generics;
+        }
+    }
+}
+
+fn strip_pin_annotations(struct_: &mut syn::ItemStruct) {
+    for field in &mut struct_.fields {
+        field.attrs.retain(|a| !a.path().is_ident("pin"));
+    }
+}

Multiple places have similar things for stripping annotations and checking if structurally pinned. Would it make sense to do this at the very beginning, and build a HashSet of structurally pinned fields, and use that as canonical source for all generate_ functions?

Best,
Gary

Quoted 39 lines
rust/pin-init/internal/src/pin_data.rs +37 -0
+
+struct SelfReplacer(PathSegment);
+
+impl VisitMut for SelfReplacer {
+    fn visit_path_mut(&mut self, i: &mut syn::Path) {
+        if i.is_ident("Self") {
+            let span = i.span();
+            let seg = &self.0;
+            *i = parse_quote_spanned!(span=> #seg);
+        } else {
+            syn::visit_mut::visit_path_mut(self, i);
+        }
+    }
+
+    fn visit_path_segment_mut(&mut self, seg: &mut PathSegment) {
+        if seg.ident == "Self" {
+            let span = seg.span();
+            let this = &self.0;
+            *seg = parse_quote_spanned!(span=> #this);
+        } else {
+            syn::visit_mut::visit_path_segment_mut(self, seg);
+        }
+    }
+
+    fn visit_item_mut(&mut self, _: &mut Item) {
+        // Do not descend into items, since items reset/change what `Self` refers to.
+    }
+}
+
+// replace with `.collect()` once MSRV is above 1.79
+fn collect_tuple<A, B>(iter: impl Iterator<Item = (A, B)>) -> (Vec<A>, Vec<B>) {
+    let mut res_a = vec![];
+    let mut res_b = vec![];
+    for (a, b) in iter {
+        res_a.push(a);
+        res_b.push(b);
     }
+    (res_a, res_b)
 }
author maintainer
#13 lore raw

Re: [PATCH 06/12] rust: pin-init: rewrite `#[pin_data]` using `syn`

Quoted 9 lines · Gary Guo

rust/pin-init/internal/src/pin_data.rs line 27

Quoted 5 lines · Benno Lossin
rust/pin-init/internal/src/pin_data.rs +5 -0
+impl Parse for Args {
+    fn parse(input: syn::parse::ParseStream) -> Result<Self> {
+        let lh = input.lookahead1();
+        if lh.peek(End) {
+            input.parse().map(Self::Nothing)

How about make this impl Parse for Option<Args> and remove the nothing variant? It looks a bit weird.

Option is not fundamental, so I can't change the impl. Parse doesn't have a blanket impl on Option. Maybe I'm not fully understanding the change you're proposing.

Quoted 10 lines

rust/pin-init/internal/src/pin_data.rs line 510

Quoted 5 lines
rust/pin-init/internal/src/pin_data.rs +5 -0
+fn strip_pin_annotations(struct_: &mut syn::ItemStruct) {
+    for field in &mut struct_.fields {
+        field.attrs.retain(|a| !a.path().is_ident("pin"));
+    }
+}

Multiple places have similar things for stripping annotations and checking if structurally pinned. Would it make sense to do this at the very beginning, and build a HashSet of structurally pinned fields, and use that as canonical source for all generate_ functions?

There is https://github.com/Rust-for-Linux/pin-init/pull/94, and I don't want to take away their work. I'll contact them and see if we can fold it into this commit/series.

Cheers,
Benno

maintainer
#14 lore raw

Re: [PATCH 06/12] rust: pin-init: rewrite `#[pin_data]` using `syn`

Quoted 8 lines · Benno Lossin
Quoted 7 lines · Gary Guo
Quoted 5 lines · Benno Lossin
rust/pin-init/internal/src/pin_data.rs +5 -0
+impl Parse for Args {
+    fn parse(input: syn::parse::ParseStream) -> Result<Self> {
+        let lh = input.lookahead1();
+        if lh.peek(End) {
+            input.parse().map(Self::Nothing)
Quoted 6 lines
Quoted 2 lines

How about make this impl Parse for Option<Args> and remove the nothing variant? It looks a bit weird.

Option is not fundamental, so I can't change the impl. Parse doesn't have a blanket impl on Option. Maybe I'm not fully understanding the change you're proposing.

nvm. I forgot about orphan rules from time to time.

Quoted 12 lines
Quoted 11 lines

rust/pin-init/internal/src/pin_data.rs line 510

Quoted 6 lines
rust/pin-init/internal/src/pin_data.rs +5 -0
+fn strip_pin_annotations(struct_: &mut syn::ItemStruct) {
+    for field in &mut struct_.fields {
+        field.attrs.retain(|a| !a.path().is_ident("pin"));
+    }
+}

Multiple places have similar things for stripping annotations and checking if structurally pinned. Would it make sense to do this at the very beginning, and build a HashSet of structurally pinned fields, and use that as canonical source for all generate_ functions?

Quoted 3 lines

There is https://github.com/Rust-for-Linux/pin-init/pull/94, and I don't want to take away their work. I'll contact them and see if we can fold it into this commit/series.

I don't think that's the same? I am proposing remove #[pin] and build a set at the very beginning of fn pin_data and just use that set. Which is not what the reference PR does.

Best,
Gary

author maintainer
#15 lore raw

Re: [PATCH 06/12] rust: pin-init: rewrite `#[pin_data]` using `syn`

Quoted 14 lines · Gary Guo
Quoted 13 lines · Benno Lossin
Quoted 11 lines · Gary Guo

rust/pin-init/internal/src/pin_data.rs line 510

Quoted 5 lines · Benno Lossin
rust/pin-init/internal/src/pin_data.rs +5 -0
+fn strip_pin_annotations(struct_: &mut syn::ItemStruct) {
+    for field in &mut struct_.fields {
+        field.attrs.retain(|a| !a.path().is_ident("pin"));
+    }
+}

Multiple places have similar things for stripping annotations and checking if structurally pinned. Would it make sense to do this at the very beginning, and build a HashSet of structurally pinned fields, and use that as canonical source for all generate_ functions?

Quoted 7 lines
Quoted 3 lines

There is https://github.com/Rust-for-Linux/pin-init/pull/94, and I don't want to take away their work. I'll contact them and see if we can fold it into this commit/series.

I don't think that's the same? I am proposing remove #[pin] and build a set at the very beginning of fn pin_data and just use that set. Which is not what the reference PR does.

Oh yeah you're right. But Field doesn't implement Hash, so we sadly can't create a set of pinned fields. I'll just create a Vec with a bool and pass that around.

Cheers,
Benno

maintainer
#16 lore raw

Re: [PATCH 06/12] rust: pin-init: rewrite `#[pin_data]` using `syn`

Quoted 19 lines · Benno Lossin
Quoted 18 lines · Gary Guo
Quoted 16 lines · Benno Lossin
Quoted 11 lines · Gary Guo

rust/pin-init/internal/src/pin_data.rs line 510

Quoted 5 lines · Benno Lossin
rust/pin-init/internal/src/pin_data.rs +5 -0
+fn strip_pin_annotations(struct_: &mut syn::ItemStruct) {
+    for field in &mut struct_.fields {
+        field.attrs.retain(|a| !a.path().is_ident("pin"));
+    }
+}

Multiple places have similar things for stripping annotations and checking if structurally pinned. Would it make sense to do this at the very beginning, and build a HashSet of structurally pinned fields, and use that as canonical source for all generate_ functions?

There is https://github.com/Rust-for-Linux/pin-init/pull/94, and I don't want to take away their work. I'll contact them and see if we can fold it into this commit/series.

Quoted 7 lines
Quoted 3 lines

I don't think that's the same? I am proposing remove #[pin] and build a set at the very beginning of fn pin_data and just use that set. Which is not what the reference PR does.

Oh yeah you're right. But Field doesn't implement Hash, so we sadly can't create a set of pinned fields. I'll just create a Vec with a bool and pass that around.

I was imagining just using Ident but I guess cfg is part of that too... Or maybe just use numeric index inside struct_.fields.

Best,
Gary

author maintainer
#17 lore raw

PATCH 7/12 rust: pin-init: add `?Sized` bounds to traits in `#[pin_data]` macro

In reply to Benno Lossin

The #[pin_data] macro uses some auxiliary traits to ensure that a user does not implement Drop for the annotated struct, as that is unsound and can lead to UB. However, if the struct that is annotated is !Sized, the current bounds do not work, because Sized is an implicit bound for generics.

This is *not* a soundness hole of pin-init, as it currently is impossible to construct an unsized struct using pin-init.

---
 rust/pin-init/internal/src/pin_data.rs | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
rust/pin-init/internal/src/pin_data.rs +2 -2
index d1e7ed121860..0fc0ce5fd4e4 100644
@@ -236,7 +236,7 @@ fn drop(&mut self) {
             // if it also implements `Drop`
             trait MustNotImplDrop {}
             #[expect(drop_bounds)]
-            impl<T: ::core::ops::Drop> MustNotImplDrop for T {}
+            impl<T: ::core::ops::Drop + ?::core::marker::Sized> MustNotImplDrop for T {}
             impl #impl_generics MustNotImplDrop for #ident #ty_generics
                 #whr
             {}
@@ -245,7 +245,7 @@ impl #impl_generics MustNotImplDrop for #ident #ty_generics
             // `PinnedDrop` as the parameter to `#[pin_data]`.
             #[expect(non_camel_case_types)]
             trait UselessPinnedDropImpl_you_need_to_specify_PinnedDrop {}
-            impl<T: ::pin_init::PinnedDrop>
+            impl<T: ::pin_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
Signature · 1 line
2.51.2
author maintainer
#18 lore raw

PATCH 8/12 rust: pin-init: rewrite the initializer macros using `syn`

In reply to Benno Lossin

Rewrite the initializer macros [pin_]init! using syn. No functional changes intended aside from improved error messages on syntactic and semantical errors. For example if one forgets to use <- with an initializer (and instead uses :):

    impl Bar {
        fn new() -> impl PinInit<Self> { ... }
    }

    impl Foo {
        fn new() -> impl PinInit<Self> {
            pin_init!(Self { bar: Bar::new() })
        }
    }

Then the declarative macro would report:

    error[E0308]: mismatched types
      --> tests/ui/compile-fail/init/colon_instead_of_arrow.rs:21:9
       |
    14 |     fn new() -> impl PinInit<Self> {
       |                 ------------------ the found opaque type
    ...
    21 |         pin_init!(Self { bar: Bar::new() })
       |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
       |         |
       |         expected `Bar`, found opaque type
       |         arguments to this function are incorrect
       |
       = note:   expected struct `Bar`
               found opaque type `impl pin_init::PinInit<Bar>`
    note: function defined here
      --> $RUST/core/src/ptr/mod.rs
       |
       | pub const unsafe fn write<T>(dst: *mut T, src: T) {
       |                     ^^^^^
       = 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)

And the new error is:

    error[E0308]: mismatched types
      --> tests/ui/compile-fail/init/colon_instead_of_arrow.rs:21:31
       |
    14 |     fn new() -> impl PinInit<Self> {
       |                 ------------------ the found opaque type
    ...
    21 |         pin_init!(Self { bar: Bar::new() })
       |                          ---  ^^^^^^^^^^ expected `Bar`, found opaque type
       |                          |
       |                          arguments to this function are incorrect
       |
       = note:   expected struct `Bar`
               found opaque type `impl pin_init::PinInit<Bar>`
    note: function defined here
      --> $RUST/core/src/ptr/mod.rs
       |
       | pub const unsafe fn write<T>(dst: *mut T, src: T) {
       |                     ^^^^^

Importantly, this error gives much more accurate span locations, pointing to the offending field, rather than the entire macro invocation.

---
 rust/pin-init/internal/src/init.rs | 437 +++++++++++++
 rust/pin-init/internal/src/lib.rs  |  21 +
 rust/pin-init/src/lib.rs           |  56 +-
 rust/pin-init/src/macros.rs        | 951 -----------------------------
 4 files changed, 460 insertions(+), 1005 deletions(-)
 create mode 100644 rust/pin-init/internal/src/init.rs
 delete mode 100644 rust/pin-init/src/macros.rs
rust/pin-init/internal/src/init.rs new file +437 -0
new file mode 100644
index 000000000000..c02a99692980
@@ -0,0 +1,437 @@
+use proc_macro2::{Span, TokenStream};
+use quote::{format_ident, quote, quote_spanned};
+use syn::{
+    braced,
+    parse::{End, Parse},
+    parse_quote,
+    punctuated::Punctuated,
+    spanned::Spanned,
+    token, Block, Expr, ExprCall, ExprPath, Ident, Path, Token, Type,
+};
+
+pub struct Initializer {
+    this: Option<This>,
+    path: Path,
+    brace_token: token::Brace,
+    fields: Punctuated<InitializerField, Token![,]>,
+    rest: Option<(Token![..], Expr)>,
+    error: Option<(Token![?], Type)>,
+}
+
+struct This {
+    _and_token: Token![&],
+    ident: Ident,
+    _in_token: Token![in],
+}
+
+enum InitializerField {
+    Value {
+        ident: Ident,
+        value: Option<(Token![:], Expr)>,
+    },
+    Init {
+        ident: Ident,
+        _left_arrow_token: Token![<-],
+        value: Expr,
+    },
+    Code {
+        _underscore_token: Token![_],
+        _colon_token: Token![:],
+        block: Block,
+    },
+}
+
+impl InitializerField {
+    fn ident(&self) -> Option<&Ident> {
+        match self {
+            Self::Value { ident, .. } | Self::Init { ident, .. } => Some(ident),
+            Self::Code { .. } => None,
+        }
+    }
+}
+
+pub(crate) fn expand(
+    Initializer {
+        this,
+        path,
+        brace_token,
+        fields,
+        rest,
+        mut error,
+    }: Initializer,
+    default_error: Option<&'static str>,
+    pinned: bool,
+) -> TokenStream {
+    let mut errors = TokenStream::new();
+    if let Some(default_error) = default_error {
+        error.get_or_insert((Default::default(), syn::parse_str(default_error).unwrap()));
+    }
+    let error = error.map(|(_, err)| err).unwrap_or_else(|| {
+        errors.extend(quote_spanned!(brace_token.span.close()=>
+            ::core::compile_error!("expected `? <type>` after `}`");
+        ));
+        parse_quote!(::core::convert::Infallible)
+    });
+    let slot = format_ident!("slot");
+    let (has_data_trait, data_trait, get_data, init_from_closure) = if pinned {
+        (
+            format_ident!("HasPinData"),
+            format_ident!("PinData"),
+            format_ident!("__pin_data"),
+            format_ident!("pin_init_from_closure"),
+        )
+    } else {
+        (
+            format_ident!("HasInitData"),
+            format_ident!("InitData"),
+            format_ident!("__init_data"),
+            format_ident!("init_from_closure"),
+        )
+    };
+    let init_kind = get_init_kind(rest, &mut errors);
+    let zeroable_check = match init_kind {
+        InitKind::Normal => quote!(),
+        InitKind::Zeroing => quote! {
+            // The user specified `..Zeroable::zeroed()` at the end of the list of fields.
+            // Therefore we check if the struct implements `Zeroable` and then zero the memory.
+            // This allows us to also remove the check that all fields are present (since we
+            // already set the memory to zero and that is a valid bit pattern).
+            fn assert_zeroable<T: ?::core::marker::Sized>(_: *mut T)
+            where T: ::pin_init::Zeroable
+            {}
+            // Ensure that the struct is indeed `Zeroable`.
+            assert_zeroable(#slot);
+            // SAFETY: The type implements `Zeroable` by the check above.
+            unsafe { ::core::ptr::write_bytes(#slot, 0, 1) };
+        },
+    };
+    let this = match this {
+        None => quote!(),
+        Some(This { ident, .. }) => quote! {
+            // Create the `this` so it can be referenced by the user inside of the
+            // expressions creating the individual fields.
+            let #ident = unsafe { ::core::ptr::NonNull::new_unchecked(slot) };
+        },
+    };
+    // `mixed_site` ensures that the data is not accessible to the user-controlled code.
+    let data = format_ident!("__data", span = Span::mixed_site());
+    let init_fields = init_fields(&fields, pinned, &data, &slot);
+    let field_check = make_field_check(&fields, init_kind, &path);
+    quote! {{
+        // We do not want to allow arbitrary returns, so we declare this type as the `Ok` return
+        // type and shadow it later when we insert the arbitrary user code. That way there will be
+        // no possibility of returning without `unsafe`.
+        struct __InitOk;
+
+        // Get the data about fields from the supplied type.
+        // SAFETY: TODO
+        let #data = unsafe {
+            use ::pin_init::__internal::#has_data_trait;
+            // Can't use `<#path as #has_data_trait>::#get_data`, since the user is able to omit
+            // generics (which need to be present with that syntax).
+            #path::#get_data()
+        };
+        // Ensure that `#data` really is of type `#data` and help with type inference:
+        let init = ::pin_init::__internal::#data_trait::make_closure::<_, __InitOk, #error>(
+            #data,
+            move |slot| {
+                {
+                    // Shadow the structure so it cannot be used to return early.
+                    struct __InitOk;
+                    #zeroable_check
+                    #this
+                    #init_fields
+                    #field_check
+                }
+                Ok(__InitOk)
+            }
+        );
+        let init = move |slot| -> ::core::result::Result<(), #error> {
+            init(slot).map(|__InitOk| ())
+        };
+        // SAFETY: TODO
+        let init = unsafe { ::pin_init::#init_from_closure::<_, #error>(init) };
+        init
+    }}
+}
+
+enum InitKind {
+    Normal,
+    Zeroing,
+}
+
+fn get_init_kind(rest: Option<(Token![..], Expr)>, errors: &mut TokenStream) -> InitKind {
+    let Some((dotdot, expr)) = rest else {
+        return InitKind::Normal;
+    };
+    match &expr {
+        Expr::Call(ExprCall { func, args, .. }) if args.is_empty() => match &**func {
+            Expr::Path(ExprPath {
+                attrs,
+                qself: None,
+                path:
+                    Path {
+                        leading_colon: None,
+                        segments,
+                    },
+            }) if attrs.is_empty()
+                && segments.len() == 2
+                && segments[0].ident == "Zeroable"
+                && segments[0].arguments.is_none()
+                && segments[1].ident == "init_zeroed"
+                && segments[1].arguments.is_none() =>
+            {
+                return InitKind::Zeroing;
+            }
+            _ => {}
+        },
+        _ => {}
+    }
+    let span = quote!(#dotdot #expr).span();
+    errors.extend(quote_spanned!(span=>
+        ::core::compile_error!("expected nothing or `..Zeroable::init_zeroed()`.");
+    ));
+    InitKind::Normal
+}
+
+/// Generate the code that initializes the fields of the struct using the initializers in `field`.
+fn init_fields(
+    fields: &Punctuated<InitializerField, Token![,]>,
+    pinned: bool,
+    data: &Ident,
+    slot: &Ident,
+) -> TokenStream {
+    let mut guards = vec![];
+    let mut res = TokenStream::new();
+    for field in fields {
+        let init = match field {
+            InitializerField::Value { ident, value } => {
+                let mut value_ident = ident.clone();
+                let value_prep = value.as_ref().map(|value| &value.1).map(|value| {
+                    // Setting the span of `value_ident` to `value`'s span improves error messages
+                    // when the type of `value` is wrong.
+                    value_ident.set_span(value.span());
+                    quote!(let #value_ident = #value;)
+                });
+                // Again span for better diagnostics
+                let write = quote_spanned!(ident.span()=> ::core::ptr::write);
+                let accessor = if pinned {
+                    let project_ident = format_ident!("__project_{ident}");
+                    quote! {
+                        // SAFETY: TODO
+                        unsafe { #data.#project_ident(&mut (*#slot).#ident) }
+                    }
+                } else {
+                    quote! {
+                        // SAFETY: TODO
+                        unsafe { &mut (*#slot).#ident }
+                    }
+                };
+                quote! {
+                    {
+                        #value_prep
+                        // SAFETY: TODO
+                        unsafe { #write(::core::ptr::addr_of_mut!((*#slot).#ident), #value_ident) };
+                    }
+                    #[allow(unused_variables)]
+                    let #ident = #accessor;
+                }
+            }
+            InitializerField::Init { ident, value, .. } => {
+                // Again span for better diagnostics
+                let init = format_ident!("init", span = value.span());
+                if pinned {
+                    let project_ident = format_ident!("__project_{ident}");
+                    quote! {
+                        {
+                            let #init = #value;
+                            // SAFETY:
+                            // - `slot` is valid, because we are inside of an initializer closure, we
+                            //   return when an error/panic occurs.
+                            // - We also use `#data` to require the correct trait (`Init` or `PinInit`)
+                            //   for `#ident`.
+                            unsafe { #data.#ident(::core::ptr::addr_of_mut!((*#slot).#ident), #init)? };
+                        }
+                        // SAFETY: TODO
+                        #[allow(unused_variables)]
+                        let #ident = unsafe { #data.#project_ident(&mut (*#slot).#ident) };
+                    }
+                } else {
+                    quote! {
+                        {
+                            let #init = #value;
+                            // SAFETY: `slot` is valid, because we are inside of an initializer
+                            // closure, we return when an error/panic occurs.
+                            unsafe {
+                                ::pin_init::Init::__init(
+                                    #init,
+                                    ::core::ptr::addr_of_mut!((*#slot).#ident),
+                                )?
+                            };
+                        }
+                        // SAFETY: TODO
+                        #[allow(unused_variables)]
+                        let #ident = unsafe { &mut (*#slot).#ident };
+                    }
+                }
+            }
+            InitializerField::Code { block: value, .. } => quote!(#[allow(unused_braces)] #value),
+        };
+        res.extend(init);
+        if let Some(ident) = field.ident() {
+            // `mixed_site` ensures that the guard is not accessible to the user-controlled code.
+            let guard = format_ident!("__{ident}_guard", span = Span::mixed_site());
+            guards.push(guard.clone());
+            res.extend(quote! {
+                // Create the drop guard:
+                //
+                // We rely on macro hygiene to make it impossible for users to access this local
+                // variable.
+                // SAFETY: We forget the guard later when initialization has succeeded.
+                let #guard = unsafe {
+                    ::pin_init::__internal::DropGuard::new(
+                        ::core::ptr::addr_of_mut!((*slot).#ident)
+                    )
+                };
+            });
+        }
+    }
+    quote! {
+        #res
+        // If execution reaches this point, all fields have been initialized. Therefore we can now
+        // dismiss the guards by forgetting them.
+        #(::core::mem::forget(#guards);)*
+    }
+}
+
+/// Generate the check for ensuring that every field has been initialized.
+fn make_field_check(
+    fields: &Punctuated<InitializerField, Token![,]>,
+    init_kind: InitKind,
+    path: &Path,
+) -> TokenStream {
+    let fields = fields.iter().filter_map(|f| f.ident());
+    match init_kind {
+        InitKind::Normal => quote! {
+            // We use unreachable code to ensure that all fields have been mentioned exactly once,
+            // this struct initializer will still be type-checked and complain with a very natural
+            // error message if a field is forgotten/mentioned more than once.
+            #[allow(unreachable_code, clippy::diverging_sub_expression)]
+            // SAFETY: this code is never executed.
+            let _ = || unsafe {
+                ::core::ptr::write(slot, #path {
+                    #(
+                        #fields: ::core::panic!(),
+                    )*
+                })
+            };
+        },
+        InitKind::Zeroing => quote! {
+            // We use unreachable code to ensure that all fields have been mentioned at most once.
+            // Since the user specified `..Zeroable::zeroed()` at the end, all missing fields will
+            // be zeroed. This struct initializer will still be type-checked and complain with a
+            // very natural error message if a field is mentioned more than once, or doesn't exist.
+            #[allow(unreachable_code, clippy::diverging_sub_expression, unused_assignments)]
+            // SAFETY: this code is never executed.
+            let _ = || unsafe {
+                let mut zeroed = ::core::mem::zeroed();
+                ::core::ptr::write(slot, zeroed);
+                zeroed = ::core::mem::zeroed();
+                ::core::ptr::write(slot, #path {
+                    #(
+                        #fields: ::core::panic!(),
+                    )*
+                    ..zeroed
+                })
+            };
+        },
+    }
+}
+
+impl Parse for Initializer {
+    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
+        let this = input.peek(Token![&]).then(|| input.parse()).transpose()?;
+        let path = input.parse()?;
+        let content;
+        let brace_token = braced!(content in input);
+        let mut fields = Punctuated::new();
+        loop {
+            let lh = content.lookahead1();
+            if lh.peek(End) || lh.peek(Token![..]) {
+                break;
+            } else if lh.peek(Ident) || lh.peek(Token![_]) {
+                fields.push_value(content.parse()?);
+                let lh = content.lookahead1();
+                if lh.peek(End) {
+                    break;
+                } else if lh.peek(Token![,]) {
+                    fields.push_punct(content.parse()?);
+                } else {
+                    return Err(lh.error());
+                }
+            } else {
+                return Err(lh.error());
+            }
+        }
+        let rest = content
+            .peek(Token![..])
+            .then(|| Ok::<_, syn::Error>((content.parse()?, content.parse()?)))
+            .transpose()?;
+        let error = input
+            .peek(Token![?])
+            .then(|| Ok::<_, syn::Error>((input.parse()?, input.parse()?)))
+            .transpose()?;
+        Ok(Self {
+            this,
+            path,
+            brace_token,
+            fields,
+            rest,
+            error,
+        })
+    }
+}
+
+impl Parse for This {
+    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
+        Ok(Self {
+            _and_token: input.parse()?,
+            ident: input.parse()?,
+            _in_token: input.parse()?,
+        })
+    }
+}
+
+impl Parse for InitializerField {
+    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
+        let lh = input.lookahead1();
+        if lh.peek(Token![_]) {
+            Ok(Self::Code {
+                _underscore_token: input.parse()?,
+                _colon_token: input.parse()?,
+                block: input.parse()?,
+            })
+        } else if lh.peek(Ident) {
+            let ident = input.parse()?;
+            let lh = input.lookahead1();
+            if lh.peek(Token![<-]) {
+                Ok(Self::Init {
+                    ident,
+                    _left_arrow_token: input.parse()?,
+                    value: input.parse()?,
+                })
+            } else if lh.peek(Token![:]) {
+                Ok(Self::Value {
+                    ident,
+                    value: Some((input.parse()?, input.parse()?)),
+                })
+            } else if lh.peek(Token![,]) || lh.peek(End) {
+                Ok(Self::Value { ident, value: None })
+            } else {
+                Err(lh.error())
+            }
+        } else {
+            Err(lh.error())
+        }
+    }
+}
rust/pin-init/internal/src/lib.rs +21 -0
index 243684a1eedc..c7c59ae77eda 100644
@@ -13,6 +13,7 @@
 use proc_macro::TokenStream;
 use syn::parse_macro_input;
 
+mod init;
 mod pin_data;
 mod pinned_drop;
 mod zeroable;
@@ -47,3 +48,23 @@ pub fn derive_zeroable(input: TokenStream) -> TokenStream {
 pub fn maybe_derive_zeroable(input: TokenStream) -> TokenStream {
     zeroable::maybe_derive(parse_macro_input!(input as _)).into()
 }
+
+#[proc_macro]
+pub fn init(input: TokenStream) -> TokenStream {
+    init::expand(
+        parse_macro_input!(input as _),
+        Some("::core::convert::Infallible"),
+        false,
+    )
+    .into()
+}
+
+#[proc_macro]
+pub fn pin_init(input: TokenStream) -> TokenStream {
+    init::expand(
+        parse_macro_input!(input as _),
+        Some("::core::convert::Infallible"),
+        true,
+    )
+    .into()
+}
rust/pin-init/src/lib.rs +2 -54

74 lines not on this page · raw message

rust/pin-init/src/macros.rs deleted +0 -951

951 lines not on this page · raw message

Signature · 1 line
2.51.2
#19 lore raw

Re: [PATCH 08/12] rust: pin-init: rewrite the initializer macros using `syn`

kernel test robot noticed the following build warnings:

Hi Benno,

kernel test robot noticed the following build warnings:

[auto build test WARNING on rust/rust-next]
[also build test WARNING on linus/master v6.19-rc4 next-20260109] [cannot apply to rust/pin-init-next]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Benno-Lossin/rust-pin-init-allow-the-crate-to-refer-to-itself-as-pin-init-in-doc-tests/20260108-234753
base:   https://github.com/Rust-for-Linux/linux rust-next
patch link:    https://lore.kernel.org/r/20260108135127.3153925-9-lossin%40kernel.org
patch subject: [PATCH 08/12] rust: pin-init: rewrite the initializer macros using `syn`
config: x86_64-rhel-9.4-rust (https://download.01.org/0day-ci/archive/20260109/[email protected]/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
rustc: rustc 1.88.0 (6b00bc388 2025-06-23)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260109/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> warning: unreachable `pub` item
   --> rust/pin-init/internal/src/init.rs:12:1
   |
   12 | pub struct Initializer {
   | ---^^^^^^^^^^^^^^^^^^^
   | |
   | help: consider restricting its visibility: `pub(crate)`
   |

= help: or consider exporting it for use by other crates = note: requested on the command line with -W unreachable-pub

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
maintainer
#20 lore raw

Re: [PATCH 08/12] rust: pin-init: rewrite the initializer macros using `syn`

In reply to Benno Lossin
Quoted 134 lines · Benno Lossin

Rewrite the initializer macros [pin_]init! using syn. No functional changes intended aside from improved error messages on syntactic and semantical errors. For example if one forgets to use <- with an initializer (and instead uses :):

    impl Bar {
        fn new() -> impl PinInit<Self> { ... }
    }

    impl Foo {
        fn new() -> impl PinInit<Self> {
            pin_init!(Self { bar: Bar::new() })
        }
    }

Then the declarative macro would report:

    error[E0308]: mismatched types
      --> tests/ui/compile-fail/init/colon_instead_of_arrow.rs:21:9
       |
    14 |     fn new() -> impl PinInit<Self> {
       |                 ------------------ the found opaque type
    ...
    21 |         pin_init!(Self { bar: Bar::new() })
       |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
       |         |
       |         expected `Bar`, found opaque type
       |         arguments to this function are incorrect
       |
       = note:   expected struct `Bar`
               found opaque type `impl pin_init::PinInit<Bar>`
    note: function defined here
      --> $RUST/core/src/ptr/mod.rs
       |
       | pub const unsafe fn write<T>(dst: *mut T, src: T) {
       |                     ^^^^^
       = 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)

And the new error is:

    error[E0308]: mismatched types
      --> tests/ui/compile-fail/init/colon_instead_of_arrow.rs:21:31
       |
    14 |     fn new() -> impl PinInit<Self> {
       |                 ------------------ the found opaque type
    ...
    21 |         pin_init!(Self { bar: Bar::new() })
       |                          ---  ^^^^^^^^^^ expected `Bar`, found opaque type
       |                          |
       |                          arguments to this function are incorrect
       |
       = note:   expected struct `Bar`
               found opaque type `impl pin_init::PinInit<Bar>`
    note: function defined here
      --> $RUST/core/src/ptr/mod.rs
       |
       | pub const unsafe fn write<T>(dst: *mut T, src: T) {
       |                     ^^^^^

Importantly, this error gives much more accurate span locations, pointing to the offending field, rather than the entire macro invocation.

---
 rust/pin-init/internal/src/init.rs | 437 +++++++++++++
 rust/pin-init/internal/src/lib.rs  |  21 +
 rust/pin-init/src/lib.rs           |  56 +-
 rust/pin-init/src/macros.rs        | 951 -----------------------------
 4 files changed, 460 insertions(+), 1005 deletions(-)
 create mode 100644 rust/pin-init/internal/src/init.rs
 delete mode 100644 rust/pin-init/src/macros.rs
rust/pin-init/internal/src/init.rs new file +55 -0
new file mode 100644
index 000000000000..c02a99692980
@@ -0,0 +1,437 @@
+use proc_macro2::{Span, TokenStream};
+use quote::{format_ident, quote, quote_spanned};
+use syn::{
+    braced,
+    parse::{End, Parse},
+    parse_quote,
+    punctuated::Punctuated,
+    spanned::Spanned,
+    token, Block, Expr, ExprCall, ExprPath, Ident, Path, Token, Type,
+};
+
+pub struct Initializer {
+    this: Option<This>,
+    path: Path,
+    brace_token: token::Brace,
+    fields: Punctuated<InitializerField, Token![,]>,
+    rest: Option<(Token![..], Expr)>,
+    error: Option<(Token![?], Type)>,
+}
+
+struct This {
+    _and_token: Token![&],
+    ident: Ident,
+    _in_token: Token![in],
+}
+
+enum InitializerField {
+    Value {
+        ident: Ident,
+        value: Option<(Token![:], Expr)>,
+    },
+    Init {
+        ident: Ident,
+        _left_arrow_token: Token![<-],
+        value: Expr,
+    },
+    Code {
+        _underscore_token: Token![_],
+        _colon_token: Token![:],
+        block: Block,
+    },
+}
+
+impl InitializerField {
+    fn ident(&self) -> Option<&Ident> {
+        match self {
+            Self::Value { ident, .. } | Self::Init { ident, .. } => Some(ident),
+            Self::Code { .. } => None,
+        }
+    }
+}
+
+pub(crate) fn expand(
+    Initializer {
+        this,

rust/pin-init/internal/src/init.rs line 65

Quoted 10 lines
rust/pin-init/internal/src/init.rs new file +10 -0
+        path,
+        brace_token,
+        fields,
+        rest,
+        mut error,
+    }: Initializer,
+    default_error: Option<&'static str>,
+    pinned: bool,
+) -> TokenStream {
+    let mut errors = TokenStream::new();

Use Vec<syn::Error> perhaps?

rust/pin-init/internal/src/init.rs line 74

Quoted 9 lines
rust/pin-init/internal/src/init.rs new file +9 -0
+    if let Some(default_error) = default_error {
+        error.get_or_insert((Default::default(), syn::parse_str(default_error).unwrap()));
+    }
+    let error = error.map(|(_, err)| err).unwrap_or_else(|| {
+        errors.extend(quote_spanned!(brace_token.span.close()=>
+            ::core::compile_error!("expected `? <type>` after `}`");
+        ));
+        parse_quote!(::core::convert::Infallible)
+    });

How about

    let error = error.map_or_else(|(_, err)| err, || {
        if let Some(default_error) = default_error {
            syn::parse_str(default_err).unwrap()
        } else {
            errors.push(Error::new_spanned(brace_token.span.close(),
                "error type must be explicitly specified with `? <type>` after `}`"
            );
            parse_quote!(::core::convert::Infallible)
        }
    }));
Quoted 21 lines
rust/pin-init/internal/src/init.rs new file +21 -0
+    let slot = format_ident!("slot");
+    let (has_data_trait, data_trait, get_data, init_from_closure) = if pinned {
+        (
+            format_ident!("HasPinData"),
+            format_ident!("PinData"),
+            format_ident!("__pin_data"),
+            format_ident!("pin_init_from_closure"),
+        )
+    } else {
+        (
+            format_ident!("HasInitData"),
+            format_ident!("InitData"),
+            format_ident!("__init_data"),
+            format_ident!("init_from_closure"),
+        )
+    };
+    let init_kind = get_init_kind(rest, &mut errors);
+    let zeroable_check = match init_kind {
+        InitKind::Normal => quote!(),
+        InitKind::Zeroing => quote! {
+            // The user specified `..Zeroable::zeroed()` at the end of the list of fields.

rust/pin-init/internal/src/init.rs line 105

Quoted 10 lines
rust/pin-init/internal/src/init.rs new file +10 -0
+            // Therefore we check if the struct implements `Zeroable` and then zero the memory.
+            // This allows us to also remove the check that all fields are present (since we
+            // already set the memory to zero and that is a valid bit pattern).
+            fn assert_zeroable<T: ?::core::marker::Sized>(_: *mut T)
+            where T: ::pin_init::Zeroable
+            {}
+            // Ensure that the struct is indeed `Zeroable`.
+            assert_zeroable(#slot);
+            // SAFETY: The type implements `Zeroable` by the check above.
+            unsafe { ::core::ptr::write_bytes(#slot, 0, 1) };

Can this be #slot.write(::pin_init::zeroed())?

rust/pin-init/internal/src/init.rs line 117

Quoted 12 lines
rust/pin-init/internal/src/init.rs new file +12 -0
+        },
+    };
+    let this = match this {
+        None => quote!(),
+        Some(This { ident, .. }) => quote! {
+            // Create the `this` so it can be referenced by the user inside of the
+            // expressions creating the individual fields.
+            let #ident = unsafe { ::core::ptr::NonNull::new_unchecked(slot) };
+        },
+    };
+    // `mixed_site` ensures that the data is not accessible to the user-controlled code.
+    let data = format_ident!("__data", span = Span::mixed_site());

Looks like this can just be using Ident constructor.

Quoted 107 lines
rust/pin-init/internal/src/init.rs new file +107 -0
+    let init_fields = init_fields(&fields, pinned, &data, &slot);
+    let field_check = make_field_check(&fields, init_kind, &path);
+    quote! {{
+        // We do not want to allow arbitrary returns, so we declare this type as the `Ok` return
+        // type and shadow it later when we insert the arbitrary user code. That way there will be
+        // no possibility of returning without `unsafe`.
+        struct __InitOk;
+
+        // Get the data about fields from the supplied type.
+        // SAFETY: TODO
+        let #data = unsafe {
+            use ::pin_init::__internal::#has_data_trait;
+            // Can't use `<#path as #has_data_trait>::#get_data`, since the user is able to omit
+            // generics (which need to be present with that syntax).
+            #path::#get_data()
+        };
+        // Ensure that `#data` really is of type `#data` and help with type inference:
+        let init = ::pin_init::__internal::#data_trait::make_closure::<_, __InitOk, #error>(
+            #data,
+            move |slot| {
+                {
+                    // Shadow the structure so it cannot be used to return early.
+                    struct __InitOk;
+                    #zeroable_check
+                    #this
+                    #init_fields
+                    #field_check
+                }
+                Ok(__InitOk)
+            }
+        );
+        let init = move |slot| -> ::core::result::Result<(), #error> {
+            init(slot).map(|__InitOk| ())
+        };
+        // SAFETY: TODO
+        let init = unsafe { ::pin_init::#init_from_closure::<_, #error>(init) };
+        init
+    }}
+}
+
+enum InitKind {
+    Normal,
+    Zeroing,
+}
+
+fn get_init_kind(rest: Option<(Token![..], Expr)>, errors: &mut TokenStream) -> InitKind {
+    let Some((dotdot, expr)) = rest else {
+        return InitKind::Normal;
+    };
+    match &expr {
+        Expr::Call(ExprCall { func, args, .. }) if args.is_empty() => match &**func {
+            Expr::Path(ExprPath {
+                attrs,
+                qself: None,
+                path:
+                    Path {
+                        leading_colon: None,
+                        segments,
+                    },
+            }) if attrs.is_empty()
+                && segments.len() == 2
+                && segments[0].ident == "Zeroable"
+                && segments[0].arguments.is_none()
+                && segments[1].ident == "init_zeroed"
+                && segments[1].arguments.is_none() =>
+            {
+                return InitKind::Zeroing;
+            }
+            _ => {}
+        },
+        _ => {}
+    }
+    let span = quote!(#dotdot #expr).span();
+    errors.extend(quote_spanned!(span=>
+        ::core::compile_error!("expected nothing or `..Zeroable::init_zeroed()`.");
+    ));
+    InitKind::Normal
+}
+
+/// Generate the code that initializes the fields of the struct using the initializers in `field`.
+fn init_fields(
+    fields: &Punctuated<InitializerField, Token![,]>,
+    pinned: bool,
+    data: &Ident,
+    slot: &Ident,
+) -> TokenStream {
+    let mut guards = vec![];
+    let mut res = TokenStream::new();
+    for field in fields {
+        let init = match field {
+            InitializerField::Value { ident, value } => {
+                let mut value_ident = ident.clone();
+                let value_prep = value.as_ref().map(|value| &value.1).map(|value| {
+                    // Setting the span of `value_ident` to `value`'s span improves error messages
+                    // when the type of `value` is wrong.
+                    value_ident.set_span(value.span());
+                    quote!(let #value_ident = #value;)
+                });
+                // Again span for better diagnostics
+                let write = quote_spanned!(ident.span()=> ::core::ptr::write);
+                let accessor = if pinned {
+                    let project_ident = format_ident!("__project_{ident}");
+                    quote! {
+                        // SAFETY: TODO
+                        unsafe { #data.#project_ident(&mut (*#slot).#ident) }
+                    }
+                } else {

rust/pin-init/internal/src/init.rs line 234

Quoted 10 lines
rust/pin-init/internal/src/init.rs new file +10 -0
+                    quote! {
+                        // SAFETY: TODO
+                        unsafe { &mut (*#slot).#ident }
+                    }
+                };
+                quote! {
+                    {
+                        #value_prep
+                        // SAFETY: TODO
+                        unsafe { #write(::core::ptr::addr_of_mut!((*#slot).#ident), #value_ident) };

This should be &raw mut now?

Quoted 94 lines
rust/pin-init/internal/src/init.rs new file +94 -0
+                    }
+                    #[allow(unused_variables)]
+                    let #ident = #accessor;
+                }
+            }
+            InitializerField::Init { ident, value, .. } => {
+                // Again span for better diagnostics
+                let init = format_ident!("init", span = value.span());
+                if pinned {
+                    let project_ident = format_ident!("__project_{ident}");
+                    quote! {
+                        {
+                            let #init = #value;
+                            // SAFETY:
+                            // - `slot` is valid, because we are inside of an initializer closure, we
+                            //   return when an error/panic occurs.
+                            // - We also use `#data` to require the correct trait (`Init` or `PinInit`)
+                            //   for `#ident`.
+                            unsafe { #data.#ident(::core::ptr::addr_of_mut!((*#slot).#ident), #init)? };
+                        }
+                        // SAFETY: TODO
+                        #[allow(unused_variables)]
+                        let #ident = unsafe { #data.#project_ident(&mut (*#slot).#ident) };
+                    }
+                } else {
+                    quote! {
+                        {
+                            let #init = #value;
+                            // SAFETY: `slot` is valid, because we are inside of an initializer
+                            // closure, we return when an error/panic occurs.
+                            unsafe {
+                                ::pin_init::Init::__init(
+                                    #init,
+                                    ::core::ptr::addr_of_mut!((*#slot).#ident),
+                                )?
+                            };
+                        }
+                        // SAFETY: TODO
+                        #[allow(unused_variables)]
+                        let #ident = unsafe { &mut (*#slot).#ident };
+                    }
+                }
+            }
+            InitializerField::Code { block: value, .. } => quote!(#[allow(unused_braces)] #value),
+        };
+        res.extend(init);
+        if let Some(ident) = field.ident() {
+            // `mixed_site` ensures that the guard is not accessible to the user-controlled code.
+            let guard = format_ident!("__{ident}_guard", span = Span::mixed_site());
+            guards.push(guard.clone());
+            res.extend(quote! {
+                // Create the drop guard:
+                //
+                // We rely on macro hygiene to make it impossible for users to access this local
+                // variable.
+                // SAFETY: We forget the guard later when initialization has succeeded.
+                let #guard = unsafe {
+                    ::pin_init::__internal::DropGuard::new(
+                        ::core::ptr::addr_of_mut!((*slot).#ident)
+                    )
+                };
+            });
+        }
+    }
+    quote! {
+        #res
+        // If execution reaches this point, all fields have been initialized. Therefore we can now
+        // dismiss the guards by forgetting them.
+        #(::core::mem::forget(#guards);)*
+    }
+}
+
+/// Generate the check for ensuring that every field has been initialized.
+fn make_field_check(
+    fields: &Punctuated<InitializerField, Token![,]>,
+    init_kind: InitKind,
+    path: &Path,
+) -> TokenStream {
+    let fields = fields.iter().filter_map(|f| f.ident());
+    match init_kind {
+        InitKind::Normal => quote! {
+            // We use unreachable code to ensure that all fields have been mentioned exactly once,
+            // this struct initializer will still be type-checked and complain with a very natural
+            // error message if a field is forgotten/mentioned more than once.
+            #[allow(unreachable_code, clippy::diverging_sub_expression)]
+            // SAFETY: this code is never executed.
+            let _ = || unsafe {
+                ::core::ptr::write(slot, #path {
+                    #(
+                        #fields: ::core::panic!(),
+                    )*
+                })
+            };
+        },

rust/pin-init/internal/src/init.rs line 338

Quoted 10 lines
rust/pin-init/internal/src/init.rs new file +10 -0
+        InitKind::Zeroing => quote! {
+            // We use unreachable code to ensure that all fields have been mentioned at most once.
+            // Since the user specified `..Zeroable::zeroed()` at the end, all missing fields will
+            // be zeroed. This struct initializer will still be type-checked and complain with a
+            // very natural error message if a field is mentioned more than once, or doesn't exist.
+            #[allow(unreachable_code, clippy::diverging_sub_expression, unused_assignments)]
+            // SAFETY: this code is never executed.
+            let _ = || unsafe {
+                let mut zeroed = ::core::mem::zeroed();
+                ::core::ptr::write(slot, zeroed);

Looks like the comment explaining why this is done gets missed.

rust/pin-init/internal/src/init.rs line 344

Quoted 6 lines
rust/pin-init/internal/src/init.rs new file +6 -0
+                zeroed = ::core::mem::zeroed();
+                ::core::ptr::write(slot, #path {
+                    #(
+                        #fields: ::core::panic!(),
+                    )*
+                    ..zeroed

Would just ::core::mem::zeroed() here work or does it have same inference issue? IIUC the type inference should work here as ..Default::default() works.

Best,
Gary

Quoted 94 lines
rust/pin-init/internal/src/init.rs new file +93 -0
+                })
+            };
+        },
+    }
+}
+
+impl Parse for Initializer {
+    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
+        let this = input.peek(Token![&]).then(|| input.parse()).transpose()?;
+        let path = input.parse()?;
+        let content;
+        let brace_token = braced!(content in input);
+        let mut fields = Punctuated::new();
+        loop {
+            let lh = content.lookahead1();
+            if lh.peek(End) || lh.peek(Token![..]) {
+                break;
+            } else if lh.peek(Ident) || lh.peek(Token![_]) {
+                fields.push_value(content.parse()?);
+                let lh = content.lookahead1();
+                if lh.peek(End) {
+                    break;
+                } else if lh.peek(Token![,]) {
+                    fields.push_punct(content.parse()?);
+                } else {
+                    return Err(lh.error());
+                }
+            } else {
+                return Err(lh.error());
+            }
+        }
+        let rest = content
+            .peek(Token![..])
+            .then(|| Ok::<_, syn::Error>((content.parse()?, content.parse()?)))
+            .transpose()?;
+        let error = input
+            .peek(Token![?])
+            .then(|| Ok::<_, syn::Error>((input.parse()?, input.parse()?)))
+            .transpose()?;
+        Ok(Self {
+            this,
+            path,
+            brace_token,
+            fields,
+            rest,
+            error,
+        })
+    }
+}
+
+impl Parse for This {
+    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
+        Ok(Self {
+            _and_token: input.parse()?,
+            ident: input.parse()?,
+            _in_token: input.parse()?,
+        })
+    }
+}
+
+impl Parse for InitializerField {
+    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
+        let lh = input.lookahead1();
+        if lh.peek(Token![_]) {
+            Ok(Self::Code {
+                _underscore_token: input.parse()?,
+                _colon_token: input.parse()?,
+                block: input.parse()?,
 
+            })
+        } else if lh.peek(Ident) {
+            let ident = input.parse()?;
+            let lh = input.lookahead1();
+            if lh.peek(Token![<-]) {
+                Ok(Self::Init {
+                    ident,
+                    _left_arrow_token: input.parse()?,
+                    value: input.parse()?,
+                })
+            } else if lh.peek(Token![:]) {
+                Ok(Self::Value {
+                    ident,
+                    value: Some((input.parse()?, input.parse()?)),
+                })
+            } else if lh.peek(Token![,]) || lh.peek(End) {
+                Ok(Self::Value { ident, value: None })
+            } else {
+                Err(lh.error())
+            }
+        } else {
+            Err(lh.error())
+        }
+    }
+}
author maintainer
#21 lore raw

Re: [PATCH 08/12] rust: pin-init: rewrite the initializer macros using `syn`

Quoted 17 lines · Gary Guo
Quoted 5 lines · Benno Lossin
rust/pin-init/internal/src/init.rs new file +5 -0
+    let init_kind = get_init_kind(rest, &mut errors);
+    let zeroable_check = match init_kind {
+        InitKind::Normal => quote!(),
+        InitKind::Zeroing => quote! {
+            // The user specified `..Zeroable::zeroed()` at the end of the list of fields.

rust/pin-init/internal/src/init.rs line 105

Quoted 10 lines
rust/pin-init/internal/src/init.rs new file +10 -0
+            // Therefore we check if the struct implements `Zeroable` and then zero the memory.
+            // This allows us to also remove the check that all fields are present (since we
+            // already set the memory to zero and that is a valid bit pattern).
+            fn assert_zeroable<T: ?::core::marker::Sized>(_: *mut T)
+            where T: ::pin_init::Zeroable
+            {}
+            // Ensure that the struct is indeed `Zeroable`.
+            assert_zeroable(#slot);
+            // SAFETY: The type implements `Zeroable` by the check above.
+            unsafe { ::core::ptr::write_bytes(#slot, 0, 1) };
Quoted 1 line

Can this be #slot.write(::pin_init::zeroed())?

That could overflow the stack?

Quoted 14 lines

rust/pin-init/internal/src/init.rs line 338

Quoted 13 lines
rust/pin-init/internal/src/init.rs new file +12 -0
+        },
+    };
 
+        InitKind::Zeroing => quote! {
+            // We use unreachable code to ensure that all fields have been mentioned at most once.
+            // Since the user specified `..Zeroable::zeroed()` at the end, all missing fields will
+            // be zeroed. This struct initializer will still be type-checked and complain with a
+            // very natural error message if a field is mentioned more than once, or doesn't exist.
+            #[allow(unreachable_code, clippy::diverging_sub_expression, unused_assignments)]
+            // SAFETY: this code is never executed.
+            let _ = || unsafe {
+                let mut zeroed = ::core::mem::zeroed();
+                ::core::ptr::write(slot, zeroed);
Quoted 1 line

Looks like the comment explaining why this is done gets missed.

Good catch!

Quoted 9 lines

rust/pin-init/internal/src/init.rs line 344

Quoted 6 lines
rust/pin-init/internal/src/init.rs new file +6 -0
+                zeroed = ::core::mem::zeroed();
+                ::core::ptr::write(slot, #path {
+                    #(
+                        #fields: ::core::panic!(),
+                    )*
+                    ..zeroed

Would just ::core::mem::zeroed() here work or does it have same inference issue? IIUC the type inference should work here as ..Default::default() works.

I haven't checked this, will do so.

Cheers,
Benno

author maintainer
#22 lore raw

Re: [PATCH 08/12] rust: pin-init: rewrite the initializer macros using `syn`

Quoted 9 lines · Benno Lossin
Quoted 8 lines · Gary Guo
Quoted 6 lines · Benno Lossin
rust/pin-init/internal/src/init.rs new file +6 -0
+                zeroed = ::core::mem::zeroed();
+                ::core::ptr::write(slot, #path {
+                    #(
+                        #fields: ::core::panic!(),
+                    )*
+                    ..zeroed
Quoted 4 lines
Quoted 2 lines

Would just ::core::mem::zeroed() here work or does it have same inference issue? IIUC the type inference should work here as ..Default::default() works.

I haven't checked this, will do so.

I checked it (also in the kernel) and it does seem to work, so I'll change it to your suggestion.

Cheers,
Benno

author maintainer
#23 lore raw

Re: [PATCH 08/12] rust: pin-init: rewrite the initializer macros using `syn`

In reply to Gary Guo
Quoted 8 lines · Gary Guo

rust/pin-init/internal/src/init.rs line 234

Quoted 5 lines · Benno Lossin
rust/pin-init/internal/src/init.rs new file +5 -0
+                quote! {
+                    {
+                        #value_prep
+                        // SAFETY: TODO
+                        unsafe { #write(::core::ptr::addr_of_mut!((*#slot).#ident), #value_ident) };

This should be &raw mut now?

Yes, but that involves adding workarounds for 1.81 and earlier. I'll leave it for a future series.

Cheers,
Benno

maintainer
#24 lore raw

Re: [PATCH 08/12] rust: pin-init: rewrite the initializer macros using `syn`

Quoted 8 lines · Benno Lossin
Quoted 7 lines · Gary Guo
Quoted 5 lines · Benno Lossin
rust/pin-init/internal/src/init.rs new file +5 -0
+                quote! {
+                    {
+                        #value_prep
+                        // SAFETY: TODO
+                        unsafe { #write(::core::ptr::addr_of_mut!((*#slot).#ident), #value_ident) };
Quoted 4 lines
Quoted 1 line

This should be &raw mut now?

Yes, but that involves adding workarounds for 1.81 and earlier. I'll leave it for a future series.

You can just enable raw_ref_op feature globally. We've already enabled it for the kernel crate.

Best,
Gary

author maintainer
#25 lore raw

Re: [PATCH 08/12] rust: pin-init: rewrite the initializer macros using `syn`

Quoted 9 lines · Gary Guo
Quoted 8 lines · Benno Lossin
Quoted 7 lines · Gary Guo
Quoted 5 lines · Benno Lossin
rust/pin-init/internal/src/init.rs new file +5 -0
+                quote! {
+                    {
+                        #value_prep
+                        // SAFETY: TODO
+                        unsafe { #write(::core::ptr::addr_of_mut!((*#slot).#ident), #value_ident) };
Quoted 7 lines
Quoted 4 lines
Quoted 1 line

This should be &raw mut now?

Yes, but that involves adding workarounds for 1.81 and earlier. I'll leave it for a future series.

You can just enable raw_ref_op feature globally. We've already enabled it for the kernel crate.

It must be possible to compile the pin-init crate using a stable compiler. Enabling an already stable feature still causes a compiler error (last time I checked). So we unfortunately can't enable it without workarounds.

Cheers,
Benno

maintainer
#26 lore raw

Re: [PATCH 08/12] rust: pin-init: rewrite the initializer macros using `syn`

Quoted 12 lines · Benno Lossin
Quoted 11 lines · Gary Guo
Quoted 10 lines · Benno Lossin
Quoted 8 lines · Gary Guo

rust/pin-init/internal/src/init.rs line 234

Quoted 5 lines · Benno Lossin
rust/pin-init/internal/src/init.rs new file +5 -0
+                quote! {
+                    {
+                        #value_prep
+                        // SAFETY: TODO
+                        unsafe { #write(::core::ptr::addr_of_mut!((*#slot).#ident), #value_ident) };

This should be &raw mut now?

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Yes, but that involves adding workarounds for 1.81 and earlier. I'll leave it for a future series.

You can just enable raw_ref_op feature globally. We've already enabled it for the kernel crate.

It must be possible to compile the pin-init crate using a stable compiler.

pin-init crate just need to use the feature without any source code change. Enabling can be done from Makefile.

In fact, in e1dfaa33fd2d (rust: enable raw_ref_op feature) it has already been globally enabled for driver crates. You just might need to explicitly enable it for pin-init in rust/Makefile.

Quoted 3 lines

Enabling an already stable feature still causes a compiler error (last time I checked). So we unfortunately can't enable it without workarounds.

stable_features is a lint that is globally allowed in our Makefile.

Best,
Gary

author maintainer
#27 lore raw

Re: [PATCH 08/12] rust: pin-init: rewrite the initializer macros using `syn`

Quoted 16 lines · Gary Guo
Quoted 15 lines · Benno Lossin
Quoted 14 lines · Gary Guo
Quoted 12 lines · Benno Lossin
Quoted 8 lines · Gary Guo

rust/pin-init/internal/src/init.rs line 234

Quoted 5 lines · Benno Lossin
rust/pin-init/internal/src/init.rs new file +5 -0
+                quote! {
+                    {
+                        #value_prep
+                        // SAFETY: TODO
+                        unsafe { #write(::core::ptr::addr_of_mut!((*#slot).#ident), #value_ident) };

This should be &raw mut now?

Yes, but that involves adding workarounds for 1.81 and earlier. I'll leave it for a future series.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

You can just enable raw_ref_op feature globally. We've already enabled it for the kernel crate.

It must be possible to compile the pin-init crate using a stable compiler.

pin-init crate just need to use the feature without any source code change. Enabling can be done from Makefile.

The tests as well as the examples need the workaround. Since we use #[test] in src/__internal.rs we also need it in src/lib.rs.

Quoted 3 lines

In fact, in e1dfaa33fd2d (rust: enable raw_ref_op feature) it has already been globally enabled for driver crates. You just might need to explicitly enable it for pin-init in rust/Makefile.

It's not about the kernel, but the user-land crate.

Quoted 5 lines
Quoted 3 lines

Enabling an already stable feature still causes a compiler error (last time I checked). So we unfortunately can't enable it without workarounds.

stable_features is a lint that is globally allowed in our Makefile.

That's not enough, you still get an error on stable:

    https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=854ab9c0ffb47d78c9f9e6012deaf821

Cheers,
Benno

author maintainer
#28 lore raw

PATCH 9/12 rust: pin-init: add `#[default_error(<type>)]` attribute to initializer macros

In reply to Benno Lossin

The #[default_error(<type>)] attribute can be used to supply a default type as the error used for the [pin_]init! macros. This way one can easily define custom try_[pin_]init! variants that default to your project specific error type. Just write the following declarative macro:

    macro_rules! try_init {
        ($($args:tt)*) => {
            ::pin_init::init!(
                #[default_error(YourCustomErrorType)]
                $($args)*
            )
        }
    }
---
 rust/pin-init/internal/src/init.rs | 53 +++++++++++++++++++++++++++---
 1 file changed, 49 insertions(+), 4 deletions(-)
rust/pin-init/internal/src/init.rs +49 -4
index c02a99692980..e14bacc88f41 100644
@@ -6,10 +6,11 @@
     parse_quote,
     punctuated::Punctuated,
     spanned::Spanned,
-    token, Block, Expr, ExprCall, ExprPath, Ident, Path, Token, Type,
+    token, Attribute, Block, Expr, ExprCall, ExprPath, Ident, Path, Token, Type,
 };
 
 pub struct Initializer {
+    attrs: Vec<InitializerAttribute>,
     this: Option<This>,
     path: Path,
     brace_token: token::Brace,
@@ -50,23 +51,44 @@ fn ident(&self) -> Option<&Ident> {
     }
 }
 
+enum InitializerAttribute {
+    DefaultError(DefaultErrorAttribute),
+}
+
+struct DefaultErrorAttribute {
+    ty: Type,
+}
+
 pub(crate) fn expand(
     Initializer {
+        attrs,
         this,
         path,
         brace_token,
         fields,
         rest,
-        mut error,
+        error,
     }: Initializer,
     default_error: Option<&'static str>,
     pinned: bool,
 ) -> TokenStream {
     let mut errors = TokenStream::new();
+    let mut error = error.map(|(_, err)| err);
+    if let Some(default_error) = attrs.iter().fold(None, |acc, attr| {
+        #[expect(irrefutable_let_patterns)]
+        if let InitializerAttribute::DefaultError(DefaultErrorAttribute { ty }) = attr {
+            Some(ty.clone())
+        } else {
+            acc
+        }
+    }) {
+        error.get_or_insert(default_error);
+    }
     if let Some(default_error) = default_error {
-        error.get_or_insert((Default::default(), syn::parse_str(default_error).unwrap()));
+        error.get_or_insert(syn::parse_str(default_error).unwrap());
     }
-    let error = error.map(|(_, err)| err).unwrap_or_else(|| {
+
+    let error = error.unwrap_or_else(|| {
         errors.extend(quote_spanned!(brace_token.span.close()=>
             ::core::compile_error!("expected `? <type>` after `}`");
         ));
@@ -350,6 +372,7 @@ fn make_field_check(
 
 impl Parse for Initializer {
     fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
+        let attrs = input.call(Attribute::parse_outer)?;
         let this = input.peek(Token![&]).then(|| input.parse()).transpose()?;
         let path = input.parse()?;
         let content;
@@ -381,7 +404,19 @@ fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
             .peek(Token![?])
             .then(|| Ok::<_, syn::Error>((input.parse()?, input.parse()?)))
             .transpose()?;
+        let attrs = attrs
+            .into_iter()
+            .map(|a| {
+                if a.path().is_ident("default_error") {
+                    a.parse_args::<DefaultErrorAttribute>()
+                        .map(InitializerAttribute::DefaultError)
+                } else {
+                    Err(syn::Error::new_spanned(a, "unknown initializer attribute"))
+                }
+            })
+            .collect::<Result<Vec<_>, _>>()?;
         Ok(Self {
+            attrs,
             this,
             path,
             brace_token,
@@ -392,6 +427,16 @@ fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
     }
 }
 
+impl Parse for DefaultErrorAttribute {
+    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
+        let ty = input.parse()?;
+        if !input.peek(End) {
+            return Err(input.error("expected end of input"));
+        }
+        Ok(Self { ty })
+    }
+}
+
 impl Parse for This {
     fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
         Ok(Self {
Signature · 1 line
2.51.2
maintainer
#29 lore raw

Re: [PATCH 09/12] rust: pin-init: add `#[default_error(<type>)]` attribute to initializer macros

Quoted 113 lines · Benno Lossin

The #[default_error(<type>)] attribute can be used to supply a default type as the error used for the [pin_]init! macros. This way one can easily define custom try_[pin_]init! variants that default to your project specific error type. Just write the following declarative macro:

    macro_rules! try_init {
        ($($args:tt)*) => {
            ::pin_init::init!(
                #[default_error(YourCustomErrorType)]
                $($args)*
            )
        }
    }
---
 rust/pin-init/internal/src/init.rs | 53 +++++++++++++++++++++++++++---
 1 file changed, 49 insertions(+), 4 deletions(-)
rust/pin-init/internal/src/init.rs +39 -4
index c02a99692980..e14bacc88f41 100644
@@ -6,10 +6,11 @@
     parse_quote,
     punctuated::Punctuated,
     spanned::Spanned,
-    token, Block, Expr, ExprCall, ExprPath, Ident, Path, Token, Type,
+    token, Attribute, Block, Expr, ExprCall, ExprPath, Ident, Path, Token, Type,
 };
 
 pub struct Initializer {
+    attrs: Vec<InitializerAttribute>,
     this: Option<This>,
     path: Path,
     brace_token: token::Brace,
@@ -50,23 +51,44 @@ fn ident(&self) -> Option<&Ident> {
     }
 }
 
+enum InitializerAttribute {
+    DefaultError(DefaultErrorAttribute),
+}
+
+struct DefaultErrorAttribute {
+    ty: Type,
+}
+
 pub(crate) fn expand(
     Initializer {
+        attrs,
         this,
         path,
         brace_token,
         fields,
         rest,
-        mut error,
+        error,
     }: Initializer,
     default_error: Option<&'static str>,
     pinned: bool,
 ) -> TokenStream {
     let mut errors = TokenStream::new();
+    let mut error = error.map(|(_, err)| err);
+    if let Some(default_error) = attrs.iter().fold(None, |acc, attr| {
+        #[expect(irrefutable_let_patterns)]
+        if let InitializerAttribute::DefaultError(DefaultErrorAttribute { ty }) = attr {
+            Some(ty.clone())
+        } else {
+            acc
+        }
+    }) {
+        error.get_or_insert(default_error);
+    }
     if let Some(default_error) = default_error {
-        error.get_or_insert((Default::default(), syn::parse_str(default_error).unwrap()));
+        error.get_or_insert(syn::parse_str(default_error).unwrap());
     }
-    let error = error.map(|(_, err)| err).unwrap_or_else(|| {
+
+    let error = error.unwrap_or_else(|| {
         errors.extend(quote_spanned!(brace_token.span.close()=>
             ::core::compile_error!("expected `? <type>` after `}`");
         ));
@@ -350,6 +372,7 @@ fn make_field_check(
 
 impl Parse for Initializer {
     fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
+        let attrs = input.call(Attribute::parse_outer)?;
         let this = input.peek(Token![&]).then(|| input.parse()).transpose()?;
         let path = input.parse()?;
         let content;
@@ -381,7 +404,19 @@ fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
             .peek(Token![?])
             .then(|| Ok::<_, syn::Error>((input.parse()?, input.parse()?)))
             .transpose()?;
+        let attrs = attrs
+            .into_iter()
+            .map(|a| {
+                if a.path().is_ident("default_error") {
+                    a.parse_args::<DefaultErrorAttribute>()
+                        .map(InitializerAttribute::DefaultError)
+                } else {
+                    Err(syn::Error::new_spanned(a, "unknown initializer attribute"))
+                }
+            })
+            .collect::<Result<Vec<_>, _>>()?;
         Ok(Self {
+            attrs,
             this,
             path,
             brace_token,

rust/pin-init/internal/src/init.rs fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> { · line 435

Quoted 9 lines
rust/pin-init/internal/src/init.rs +6 -0
@@ -392,6 +427,16 @@ fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
     }
 }
 
+impl Parse for DefaultErrorAttribute {
+    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
+        let ty = input.parse()?;
+        if !input.peek(End) {
+            return Err(input.error("expected end of input"));
+        }

I don't think you need to handle end of input explicitly. Most of syn will complain if a stream has token left unparsed.

Best,
Gary

Quoted 7 lines
rust/pin-init/internal/src/init.rs +4 -0
+        Ok(Self { ty })
+    }
+}
+
 impl Parse for This {
     fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
         Ok(Self {
author maintainer
#30 lore raw

PATCH 10/12 rust: init: use `#[default_error(err)]` for the initializer macros

In reply to Benno Lossin

Initializer macros should use this attribute instead of manually parsing the macro's input. This is because the syntax is now parsed using syn, which permits more complex constructs to be parsed. In addition, this ensures that the kernel's initializer marcos will have the exact same syntax as the ones from pin-init.

---
 rust/kernel/init.rs | 40 ++++++++++++----------------------------
 1 file changed, 12 insertions(+), 28 deletions(-)
rust/kernel/init.rs +12 -28
index 917f7ef001fd..7a0d4559d7b5 100644
@@ -219,20 +219,12 @@ fn init<E>(init: impl Init<T, E>, flags: Flags) -> error::Result<Self>
 /// [`Error`]: crate::error::Error
 #[macro_export]
 macro_rules! try_init {
-    ($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
-        $($fields:tt)*
-    }) => {
-        ::pin_init::init!($(&$this in)? $t $(::<$($generics),*>)? {
-            $($fields)*
-        }? $crate::error::Error)
-    };
-    ($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
-        $($fields:tt)*
-    }? $err:ty) => {
-        ::pin_init::init!($(&$this in)? $t $(::<$($generics),*>)? {
-            $($fields)*
-        }? $err)
-    };
+    ($($args:tt)*) => {
+        ::pin_init::init!(
+            #[default_error($crate::error::Error)]
+            $($args)*
+        )
+    }
 }
 
 /// Construct an in-place, fallible pinned initializer for `struct`s.
@@ -279,18 +271,10 @@ macro_rules! try_init {
 /// [`Error`]: crate::error::Error
 #[macro_export]
 macro_rules! try_pin_init {
-    ($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
-        $($fields:tt)*
-    }) => {
-        ::pin_init::pin_init!($(&$this in)? $t $(::<$($generics),*>)? {
-            $($fields)*
-        }? $crate::error::Error)
-    };
-    ($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
-        $($fields:tt)*
-    }? $err:ty) => {
-        ::pin_init::pin_init!($(&$this in)? $t $(::<$($generics),*>)? {
-            $($fields)*
-        }? $err)
-    };
+    ($($args:tt)*) => {
+        ::pin_init::pin_init!(
+            #[default_error($crate::error::Error)]
+            $($args)*
+        )
+    }
 }
Signature · 1 line
2.51.2
author maintainer
#31 lore raw

PATCH 11/12 rust: pin-init: internal: init: add support for attributes on initializer fields

In reply to Benno Lossin

Initializer fields ought to support the same attributes that are allowed in struct initializers on fields. For example, cfg or lint levels such as expect, allow etc. Add parsing support for these attributes using syn to initializer fields and adjust the macro expansion accordingly.

---
 rust/pin-init/internal/src/init.rs | 64 +++++++++++++++++++++++-------
 1 file changed, 50 insertions(+), 14 deletions(-)
rust/pin-init/internal/src/init.rs +50 -14
index e14bacc88f41..c20be37e7fef 100644
@@ -25,7 +25,12 @@ struct This {
     _in_token: Token![in],
 }
 
-enum InitializerField {
+struct InitializerField {
+    attrs: Vec<Attribute>,
+    kind: InitializerKind,
+}
+
+enum InitializerKind {
     Value {
         ident: Ident,
         value: Option<(Token![:], Expr)>,
@@ -42,7 +47,7 @@ enum InitializerField {
     },
 }
 
-impl InitializerField {
+impl InitializerKind {
     fn ident(&self) -> Option<&Ident> {
         match self {
             Self::Value { ident, .. } | Self::Init { ident, .. } => Some(ident),
@@ -224,10 +229,11 @@ fn init_fields(
     slot: &Ident,
 ) -> TokenStream {
     let mut guards = vec![];
+    let mut guard_attrs = vec![];
     let mut res = TokenStream::new();
-    for field in fields {
-        let init = match field {
-            InitializerField::Value { ident, value } => {
+    for InitializerField { attrs, kind } in fields {
+        let init = match kind {
+            InitializerKind::Value { ident, value } => {
                 let mut value_ident = ident.clone();
                 let value_prep = value.as_ref().map(|value| &value.1).map(|value| {
                     // Setting the span of `value_ident` to `value`'s span improves error messages
@@ -250,21 +256,24 @@ fn init_fields(
                     }
                 };
                 quote! {
+                    #(#attrs)*
                     {
                         #value_prep
                         // SAFETY: TODO
                         unsafe { #write(::core::ptr::addr_of_mut!((*#slot).#ident), #value_ident) };
                     }
+                    #(#attrs)*
                     #[allow(unused_variables)]
                     let #ident = #accessor;
                 }
             }
-            InitializerField::Init { ident, value, .. } => {
+            InitializerKind::Init { ident, value, .. } => {
                 // Again span for better diagnostics
                 let init = format_ident!("init", span = value.span());
                 if pinned {
                     let project_ident = format_ident!("__project_{ident}");
                     quote! {
+                        #(#attrs)*
                         {
                             let #init = #value;
                             // SAFETY:
@@ -274,12 +283,14 @@ fn init_fields(
                             //   for `#ident`.
                             unsafe { #data.#ident(::core::ptr::addr_of_mut!((*#slot).#ident), #init)? };
                         }
+                        #(#attrs)*
                         // SAFETY: TODO
                         #[allow(unused_variables)]
                         let #ident = unsafe { #data.#project_ident(&mut (*#slot).#ident) };
                     }
                 } else {
                     quote! {
+                        #(#attrs)*
                         {
                             let #init = #value;
                             // SAFETY: `slot` is valid, because we are inside of an initializer
@@ -291,20 +302,25 @@ fn init_fields(
                                 )?
                             };
                         }
+                        #(#attrs)*
                         // SAFETY: TODO
                         #[allow(unused_variables)]
                         let #ident = unsafe { &mut (*#slot).#ident };
                     }
                 }
             }
-            InitializerField::Code { block: value, .. } => quote!(#[allow(unused_braces)] #value),
+            InitializerKind::Code { block: value, .. } => quote! {
+                #(#attrs)*
+                #[allow(unused_braces)]
+                #value
+            },
         };
         res.extend(init);
-        if let Some(ident) = field.ident() {
+        if let Some(ident) = kind.ident() {
             // `mixed_site` ensures that the guard is not accessible to the user-controlled code.
             let guard = format_ident!("__{ident}_guard", span = Span::mixed_site());
-            guards.push(guard.clone());
             res.extend(quote! {
+                #(#attrs)*
                 // Create the drop guard:
                 //
                 // We rely on macro hygiene to make it impossible for users to access this local
@@ -316,13 +332,18 @@ fn init_fields(
                     )
                 };
             });
+            guards.push(guard);
+            guard_attrs.push(attrs);
         }
     }
     quote! {
         #res
         // If execution reaches this point, all fields have been initialized. Therefore we can now
         // dismiss the guards by forgetting them.
-        #(::core::mem::forget(#guards);)*
+        #(
+            #(#guard_attrs)*
+            ::core::mem::forget(#guards);
+        )*
     }
 }
 
@@ -332,7 +353,10 @@ fn make_field_check(
     init_kind: InitKind,
     path: &Path,
 ) -> TokenStream {
-    let fields = fields.iter().filter_map(|f| f.ident());
+    let field_attrs = fields
+        .iter()
+        .filter_map(|f| f.kind.ident().map(|_| &f.attrs));
+    let field_name = fields.iter().filter_map(|f| f.kind.ident());
     match init_kind {
         InitKind::Normal => quote! {
             // We use unreachable code to ensure that all fields have been mentioned exactly once,
@@ -343,7 +367,8 @@ fn make_field_check(
             let _ = || unsafe {
                 ::core::ptr::write(slot, #path {
                     #(
-                        #fields: ::core::panic!(),
+                        #(#field_attrs)*
+                        #field_name: ::core::panic!(),
                     )*
                 })
             };
@@ -361,7 +386,8 @@ fn make_field_check(
                 zeroed = ::core::mem::zeroed();
                 ::core::ptr::write(slot, #path {
                     #(
-                        #fields: ::core::panic!(),
+                        #(#field_attrs)*
+                        #field_name: ::core::panic!(),
                     )*
                     ..zeroed
                 })
@@ -382,7 +408,7 @@ fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
             let lh = content.lookahead1();
             if lh.peek(End) || lh.peek(Token![..]) {
                 break;
-            } else if lh.peek(Ident) || lh.peek(Token![_]) {
+            } else if lh.peek(Ident) || lh.peek(Token![_]) || lh.peek(Token![#]) {
                 fields.push_value(content.parse()?);
                 let lh = content.lookahead1();
                 if lh.peek(End) {
@@ -448,6 +474,16 @@ fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
 }
 
 impl Parse for InitializerField {
+    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
+        let attrs = input.call(Attribute::parse_outer)?;
+        Ok(Self {
+            attrs,
+            kind: input.parse()?,
+        })
+    }
+}
+
+impl Parse for InitializerKind {
     fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
         let lh = input.lookahead1();
         if lh.peek(Token![_]) {
Signature · 1 line
2.51.2
maintainer
#32 lore raw

Re: [PATCH 11/12] rust: pin-init: internal: init: add support for attributes on initializer fields

Quoted 123 lines · Benno Lossin

Initializer fields ought to support the same attributes that are allowed in struct initializers on fields. For example, cfg or lint levels such as expect, allow etc. Add parsing support for these attributes using syn to initializer fields and adjust the macro expansion accordingly.

---
 rust/pin-init/internal/src/init.rs | 64 +++++++++++++++++++++++-------
 1 file changed, 50 insertions(+), 14 deletions(-)
rust/pin-init/internal/src/init.rs +25 -9
index e14bacc88f41..c20be37e7fef 100644
@@ -25,7 +25,12 @@ struct This {
     _in_token: Token![in],
 }
 
-enum InitializerField {
+struct InitializerField {
+    attrs: Vec<Attribute>,
+    kind: InitializerKind,
+}
+
+enum InitializerKind {
     Value {
         ident: Ident,
         value: Option<(Token![:], Expr)>,
@@ -42,7 +47,7 @@ enum InitializerField {
     },
 }
 
-impl InitializerField {
+impl InitializerKind {
     fn ident(&self) -> Option<&Ident> {
         match self {
             Self::Value { ident, .. } | Self::Init { ident, .. } => Some(ident),
@@ -224,10 +229,11 @@ fn init_fields(
     slot: &Ident,
 ) -> TokenStream {
     let mut guards = vec![];
+    let mut guard_attrs = vec![];
     let mut res = TokenStream::new();
-    for field in fields {
-        let init = match field {
-            InitializerField::Value { ident, value } => {
+    for InitializerField { attrs, kind } in fields {
+        let init = match kind {
+            InitializerKind::Value { ident, value } => {
                 let mut value_ident = ident.clone();
                 let value_prep = value.as_ref().map(|value| &value.1).map(|value| {
                     // Setting the span of `value_ident` to `value`'s span improves error messages
@@ -250,21 +256,24 @@ fn init_fields(
                     }
                 };
                 quote! {
+                    #(#attrs)*
                     {
                         #value_prep
                         // SAFETY: TODO
                         unsafe { #write(::core::ptr::addr_of_mut!((*#slot).#ident), #value_ident) };
                     }
+                    #(#attrs)*
                     #[allow(unused_variables)]
                     let #ident = #accessor;
                 }
             }
-            InitializerField::Init { ident, value, .. } => {
+            InitializerKind::Init { ident, value, .. } => {
                 // Again span for better diagnostics
                 let init = format_ident!("init", span = value.span());
                 if pinned {
                     let project_ident = format_ident!("__project_{ident}");
                     quote! {
+                        #(#attrs)*
                         {
                             let #init = #value;
                             // SAFETY:
@@ -274,12 +283,14 @@ fn init_fields(
                             //   for `#ident`.
                             unsafe { #data.#ident(::core::ptr::addr_of_mut!((*#slot).#ident), #init)? };
                         }
+                        #(#attrs)*
                         // SAFETY: TODO
                         #[allow(unused_variables)]
                         let #ident = unsafe { #data.#project_ident(&mut (*#slot).#ident) };
                     }
                 } else {
                     quote! {
+                        #(#attrs)*
                         {
                             let #init = #value;
                             // SAFETY: `slot` is valid, because we are inside of an initializer
@@ -291,20 +302,25 @@ fn init_fields(
                                 )?
                             };
                         }
+                        #(#attrs)*
                         // SAFETY: TODO
                         #[allow(unused_variables)]
                         let #ident = unsafe { &mut (*#slot).#ident };
                     }
                 }
             }
-            InitializerField::Code { block: value, .. } => quote!(#[allow(unused_braces)] #value),
+            InitializerKind::Code { block: value, .. } => quote! {
+                #(#attrs)*
+                #[allow(unused_braces)]
+                #value
+            },
         };
         res.extend(init);
-        if let Some(ident) = field.ident() {
+        if let Some(ident) = kind.ident() {
             // `mixed_site` ensures that the guard is not accessible to the user-controlled code.
             let guard = format_ident!("__{ident}_guard", span = Span::mixed_site());
-            guards.push(guard.clone());
             res.extend(quote! {
+                #(#attrs)*
                 // Create the drop guard:
                 //
                 // We rely on macro hygiene to make it impossible for users to access this local

rust/pin-init/internal/src/init.rs fn init_fields( · line 336

Quoted 5 lines
rust/pin-init/internal/src/init.rs +2 -0
@@ -316,13 +332,18 @@ fn init_fields(
                     )
                 };
             });
+            guards.push(guard);
+            guard_attrs.push(attrs);

I think guard_attrs should just get the cfg ones, not including lint levels. Otherwise, #[expect] would be broken?

Best,
Gary

Quoted 72 lines
rust/pin-init/internal/src/init.rs +23 -5
         }
     }
     quote! {
         #res
         // If execution reaches this point, all fields have been initialized. Therefore we can now
         // dismiss the guards by forgetting them.
-        #(::core::mem::forget(#guards);)*
+        #(
+            #(#guard_attrs)*
+            ::core::mem::forget(#guards);
+        )*
     }
 }
 
@@ -332,7 +353,10 @@ fn make_field_check(
     init_kind: InitKind,
     path: &Path,
 ) -> TokenStream {
-    let fields = fields.iter().filter_map(|f| f.ident());
+    let field_attrs = fields
+        .iter()
+        .filter_map(|f| f.kind.ident().map(|_| &f.attrs));
+    let field_name = fields.iter().filter_map(|f| f.kind.ident());
     match init_kind {
         InitKind::Normal => quote! {
             // We use unreachable code to ensure that all fields have been mentioned exactly once,
@@ -343,7 +367,8 @@ fn make_field_check(
             let _ = || unsafe {
                 ::core::ptr::write(slot, #path {
                     #(
-                        #fields: ::core::panic!(),
+                        #(#field_attrs)*
+                        #field_name: ::core::panic!(),
                     )*
                 })
             };
@@ -361,7 +386,8 @@ fn make_field_check(
                 zeroed = ::core::mem::zeroed();
                 ::core::ptr::write(slot, #path {
                     #(
-                        #fields: ::core::panic!(),
+                        #(#field_attrs)*
+                        #field_name: ::core::panic!(),
                     )*
                     ..zeroed
                 })
@@ -382,7 +408,7 @@ fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
             let lh = content.lookahead1();
             if lh.peek(End) || lh.peek(Token![..]) {
                 break;
-            } else if lh.peek(Ident) || lh.peek(Token![_]) {
+            } else if lh.peek(Ident) || lh.peek(Token![_]) || lh.peek(Token![#]) {
                 fields.push_value(content.parse()?);
                 let lh = content.lookahead1();
                 if lh.peek(End) {
@@ -448,6 +474,16 @@ fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
 }
 
 impl Parse for InitializerField {
+    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
+        let attrs = input.call(Attribute::parse_outer)?;
+        Ok(Self {
+            attrs,
+            kind: input.parse()?,
+        })
+    }
+}
+
+impl Parse for InitializerKind {
     fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
         let lh = input.lookahead1();
         if lh.peek(Token![_]) {
author maintainer
#33 lore raw

Re: [PATCH 11/12] rust: pin-init: internal: init: add support for attributes on initializer fields

Quoted 12 lines · Gary Guo
Quoted 11 lines · Benno Lossin
rust/pin-init/internal/src/init.rs +2 -2
-        if let Some(ident) = field.ident() {
+        if let Some(ident) = kind.ident() {
             // `mixed_site` ensures that the guard is not accessible to the user-controlled code.
             let guard = format_ident!("__{ident}_guard", span = Span::mixed_site());
-            guards.push(guard.clone());
             res.extend(quote! {
+                #(#attrs)*
                 // Create the drop guard:
                 //
                 // We rely on macro hygiene to make it impossible for users to access this local
@@ -316,13 +332,18 @@ fn init_fields(
Quoted 8 lines

rust/pin-init/internal/src/init.rs fn init_fields( · line 336

Quoted 5 lines
rust/pin-init/internal/src/init.rs +2 -0
                     )
                 };
             });
+            guards.push(guard);
+            guard_attrs.push(attrs);

I think guard_attrs should just get the cfg ones, not including lint levels. Otherwise, #[expect] would be broken?

Oh yeah that is probably right. I do not yet have extensive attribute tests due to lack of time.

Do you also think that only cfg ones should be included in the #(#attrs)* in the quote! above?

Cheers,
Benno

maintainer
#34 lore raw

Re: [PATCH 11/12] rust: pin-init: internal: init: add support for attributes on initializer fields

Quoted 22 lines · Benno Lossin
Quoted 12 lines · Gary Guo
Quoted 11 lines · Benno Lossin
rust/pin-init/internal/src/init.rs +2 -2
-        if let Some(ident) = field.ident() {
+        if let Some(ident) = kind.ident() {
             // `mixed_site` ensures that the guard is not accessible to the user-controlled code.
             let guard = format_ident!("__{ident}_guard", span = Span::mixed_site());
-            guards.push(guard.clone());
             res.extend(quote! {
+                #(#attrs)*
                 // Create the drop guard:
                 //
                 // We rely on macro hygiene to make it impossible for users to access this local
@@ -316,13 +332,18 @@ fn init_fields(
Quoted 8 lines

rust/pin-init/internal/src/init.rs fn init_fields( · line 336

Quoted 5 lines
rust/pin-init/internal/src/init.rs +2 -0
                     )
                 };
             });
+            guards.push(guard);
+            guard_attrs.push(attrs);

I think guard_attrs should just get the cfg ones, not including lint levels. Otherwise, #[expect] would be broken?

Quoted 5 lines

Oh yeah that is probably right. I do not yet have extensive attribute tests due to lack of time.

Do you also think that only cfg ones should be included in the #(#attrs)* in the quote! above?

I think the full attribute list should get applied when surrounding the user-provided expression. Everything else should just get the cfg.

Best,
Gary

author maintainer
#35 lore raw

PATCH 12/12 rust: pin-init: internal: init: add escape hatch for referencing initialized fields

In reply to Benno Lossin

The initializer macro emits mutable references for already initialized fields, which allows modifying or accessing them later in code blocks or when initializing other fields. This behavior results in compiler errors when combining with packed structs, since those do not permit creating references to misaligned fields. For example:

    #[repr(C, packed)]
    struct Foo {
        a: i8,
        b: i32,
    }

    fn main() {
        let _ = init!(Foo { a: -42, b: 42 });
    }

This will lead to an error like this:

error[E0793]: reference to field of packed struct is unaligned
--> tests/ui/compile-fail/init/packed_struct.rs:10:13
|
10 | let _ = init!(Foo { a: -42, b: 42 });
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this struct is 1-byte aligned, but the type of this field may require higher alignment
= note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
= help: copy the field contents to a local variable, or replace the reference with a raw pointer and use read_unaligned/write_unaligned (loads and stores via *p must be properly aligned even when using raw pointers) = note: this error originates in the macro init (in Nightly builds, run with -Z macro-backtrace for more info)

This was requested by Janne Grunau [1] and will most certainly be used by the kernel when we eventually end up with trying to initialize packed structs.

Thus add an initializer attribute #[disable_initialized_field_access] that does what the name suggests: do not generate references to already initialized fields.

There is space for future work: add yet another attribute which can be applied on fields of initializers that ask for said field to be made accessible. We can add that when the need arises.

---
 rust/pin-init/internal/src/init.rs | 75 +++++++++++++++++++++---------
 1 file changed, 52 insertions(+), 23 deletions(-)
rust/pin-init/internal/src/init.rs +52 -23
index c20be37e7fef..148f43bf3c7b 100644
@@ -58,6 +58,7 @@ fn ident(&self) -> Option<&Ident> {
 
 enum InitializerAttribute {
     DefaultError(DefaultErrorAttribute),
+    DisableInitializedFieldAccess,
 }
 
 struct DefaultErrorAttribute {
@@ -80,7 +81,6 @@ pub(crate) fn expand(
     let mut errors = TokenStream::new();
     let mut error = error.map(|(_, err)| err);
     if let Some(default_error) = attrs.iter().fold(None, |acc, attr| {
-        #[expect(irrefutable_let_patterns)]
         if let InitializerAttribute::DefaultError(DefaultErrorAttribute { ty }) = attr {
             Some(ty.clone())
         } else {
@@ -142,7 +142,15 @@ fn assert_zeroable<T: ?::core::marker::Sized>(_: *mut T)
     };
     // `mixed_site` ensures that the data is not accessible to the user-controlled code.
     let data = format_ident!("__data", span = Span::mixed_site());
-    let init_fields = init_fields(&fields, pinned, &data, &slot);
+    let init_fields = init_fields(
+        &fields,
+        pinned,
+        !attrs
+            .iter()
+            .any(|attr| matches!(attr, InitializerAttribute::DisableInitializedFieldAccess)),
+        &data,
+        &slot,
+    );
     let field_check = make_field_check(&fields, init_kind, &path);
     quote! {{
         // We do not want to allow arbitrary returns, so we declare this type as the `Ok` return
@@ -225,6 +233,7 @@ fn get_init_kind(rest: Option<(Token![..], Expr)>, errors: &mut TokenStream) ->
 fn init_fields(
     fields: &Punctuated<InitializerField, Token![,]>,
     pinned: bool,
+    generate_initialized_accessors: bool,
     data: &Ident,
     slot: &Ident,
 ) -> TokenStream {
@@ -255,6 +264,13 @@ fn init_fields(
                         unsafe { &mut (*#slot).#ident }
                     }
                 };
+                let accessor = generate_initialized_accessors.then(|| {
+                    quote! {
+                        #(#attrs)*
+                        #[allow(unused_variables)]
+                        let #ident = #accessor;
+                    }
+                });
                 quote! {
                     #(#attrs)*
                     {
@@ -262,37 +278,31 @@ fn init_fields(
                         // SAFETY: TODO
                         unsafe { #write(::core::ptr::addr_of_mut!((*#slot).#ident), #value_ident) };
                     }
-                    #(#attrs)*
-                    #[allow(unused_variables)]
-                    let #ident = #accessor;
+                    #accessor
                 }
             }
             InitializerKind::Init { ident, value, .. } => {
                 // Again span for better diagnostics
                 let init = format_ident!("init", span = value.span());
-                if pinned {
+                let (value_init, accessor) = if pinned {
                     let project_ident = format_ident!("__project_{ident}");
-                    quote! {
-                        #(#attrs)*
-                        {
-                            let #init = #value;
+                    (
+                        quote! {
                             // SAFETY:
                             // - `slot` is valid, because we are inside of an initializer closure, we
                             //   return when an error/panic occurs.
                             // - We also use `#data` to require the correct trait (`Init` or `PinInit`)
                             //   for `#ident`.
                             unsafe { #data.#ident(::core::ptr::addr_of_mut!((*#slot).#ident), #init)? };
-                        }
-                        #(#attrs)*
-                        // SAFETY: TODO
-                        #[allow(unused_variables)]
-                        let #ident = unsafe { #data.#project_ident(&mut (*#slot).#ident) };
-                    }
+                        },
+                        quote! {
+                            // SAFETY: TODO
+                            unsafe { #data.#project_ident(&mut (*#slot).#ident) }
+                        },
+                    )
                 } else {
-                    quote! {
-                        #(#attrs)*
-                        {
-                            let #init = #value;
+                    (
+                        quote! {
                             // SAFETY: `slot` is valid, because we are inside of an initializer
                             // closure, we return when an error/panic occurs.
                             unsafe {
@@ -301,12 +311,27 @@ fn init_fields(
                                     ::core::ptr::addr_of_mut!((*#slot).#ident),
                                 )?
                             };
-                        }
+                        },
+                        quote! {
+                            // SAFETY: TODO
+                            unsafe { &mut (*#slot).#ident }
+                        },
+                    )
+                };
+                let accessor = generate_initialized_accessors.then(|| {
+                    quote! {
                         #(#attrs)*
-                        // SAFETY: TODO
                         #[allow(unused_variables)]
-                        let #ident = unsafe { &mut (*#slot).#ident };
+                        let #ident = #accessor;
+                    }
+                });
+                quote! {
+                    #(#attrs)*
+                    {
+                        let #init = #value;
+                        #value_init
                     }
+                    #accessor
                 }
             }
             InitializerKind::Code { block: value, .. } => quote! {
@@ -436,6 +461,10 @@ fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
                 if a.path().is_ident("default_error") {
                     a.parse_args::<DefaultErrorAttribute>()
                         .map(InitializerAttribute::DefaultError)
+                } else if a.path().is_ident("disable_initialized_field_access") {
+                    a.meta
+                        .require_path_only()
+                        .map(|_| InitializerAttribute::DisableInitializedFieldAccess)
                 } else {
                     Err(syn::Error::new_spanned(a, "unknown initializer attribute"))
                 }
Signature · 1 line
2.51.2
#36 lore raw

Re: [PATCH 12/12] rust: pin-init: internal: init: add escape hatch for referencing initialized fields

kernel test robot noticed the following build warnings:

Hi Benno,

kernel test robot noticed the following build warnings:

[auto build test WARNING on rust/rust-next]
[also build test WARNING on linus/master v6.19-rc4 next-20260109] [cannot apply to rust/pin-init-next]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Benno-Lossin/rust-pin-init-allow-the-crate-to-refer-to-itself-as-pin-init-in-doc-tests/20260108-234753
base:   https://github.com/Rust-for-Linux/linux rust-next
patch link:    https://lore.kernel.org/r/20260108135127.3153925-13-lossin%40kernel.org
patch subject: [PATCH 12/12] rust: pin-init: internal: init: add escape hatch for referencing initialized fields
config: x86_64-rhel-9.4-rust (https://download.01.org/0day-ci/archive/20260109/[email protected]/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
rustc: rustc 1.88.0 (6b00bc388 2025-06-23)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260109/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> warning: large size difference between variants
   --> rust/pin-init/internal/src/init.rs:59:1
   |
   59 | / enum InitializerAttribute {
   60 | |     DefaultError(DefaultErrorAttribute),
   | |     ----------------------------------- the largest variant contains at least 224 bytes
   61 | |     DisableInitializedFieldAccess,
   | |     ----------------------------- the second-largest variant carries no data at all
   62 | | }
   | |_^ the entire enum is at least 224 bytes
   |

= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#large_enum_variant
= note: -W clippy::large-enum-variant implied by -W clippy::all
= help: to override -W clippy::all add #[allow(clippy::large_enum_variant)]
help: consider boxing the large fields to reduce the total size of the enum
|
60 - DefaultError(DefaultErrorAttribute),
60 + DefaultError(Box<DefaultErrorAttribute>),
|

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
maintainer
#37 lore raw

Re: [PATCH 12/12] rust: pin-init: internal: init: add escape hatch for referencing initialized fields

In reply to Benno Lossin
Quoted 33 lines · Benno Lossin

The initializer macro emits mutable references for already initialized fields, which allows modifying or accessing them later in code blocks or when initializing other fields. This behavior results in compiler errors when combining with packed structs, since those do not permit creating references to misaligned fields. For example:

    #[repr(C, packed)]
    struct Foo {
        a: i8,
        b: i32,
    }

    fn main() {
        let _ = init!(Foo { a: -42, b: 42 });
    }

This will lead to an error like this:

error[E0793]: reference to field of packed struct is unaligned
--> tests/ui/compile-fail/init/packed_struct.rs:10:13
|
10 | let _ = init!(Foo { a: -42, b: 42 });
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this struct is 1-byte aligned, but the type of this field may require higher alignment
= note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
= help: copy the field contents to a local variable, or replace the reference with a raw pointer and use read_unaligned/write_unaligned (loads and stores via *p must be properly aligned even when using raw pointers) = note: this error originates in the macro init (in Nightly builds, run with -Z macro-backtrace for more info)

This was requested by Janne Grunau [1] and will most certainly be used by the kernel when we eventually end up with trying to initialize packed structs.

Quoted 7 lines

Thus add an initializer attribute #[disable_initialized_field_access] that does what the name suggests: do not generate references to already initialized fields.

There is space for future work: add yet another attribute which can be applied on fields of initializers that ask for said field to be made accessible. We can add that when the need arises.

An alternative might be checking if the specific field is actually used later and only generate references if so. Although, that might be "too much magic"?

Best,
Gary

Quoted 4 lines
author maintainer
#38 lore raw

Re: [PATCH 12/12] rust: pin-init: internal: init: add escape hatch for referencing initialized fields

Quoted 38 lines · Gary Guo
Quoted 37 lines · Benno Lossin

The initializer macro emits mutable references for already initialized fields, which allows modifying or accessing them later in code blocks or when initializing other fields. This behavior results in compiler errors when combining with packed structs, since those do not permit creating references to misaligned fields. For example:

    #[repr(C, packed)]
    struct Foo {
        a: i8,
        b: i32,
    }

    fn main() {
        let _ = init!(Foo { a: -42, b: 42 });
    }

This will lead to an error like this:

error[E0793]: reference to field of packed struct is unaligned
--> tests/ui/compile-fail/init/packed_struct.rs:10:13
|
10 | let _ = init!(Foo { a: -42, b: 42 });
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this struct is 1-byte aligned, but the type of this field may require higher alignment
= note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
= help: copy the field contents to a local variable, or replace the reference with a raw pointer and use read_unaligned/write_unaligned (loads and stores via *p must be properly aligned even when using raw pointers) = note: this error originates in the macro init (in Nightly builds, run with -Z macro-backtrace for more info)

This was requested by Janne Grunau [1] and will most certainly be used by the kernel when we eventually end up with trying to initialize packed structs.

Thus add an initializer attribute #[disable_initialized_field_access] that does what the name suggests: do not generate references to already initialized fields.

Quoted 6 lines
Quoted 3 lines

There is space for future work: add yet another attribute which can be applied on fields of initializers that ask for said field to be made accessible. We can add that when the need arises.

An alternative might be checking if the specific field is actually used later and only generate references if so. Although, that might be "too much magic"?

Sounds like a good idea, I'll consider for a future series.

Cheers,
Benno

author maintainer
#39 lore raw

PATCH 13/13 MAINTAINERS: add Gary Guo to pin-init

In reply to Benno Lossin

Gary has been involved in pin-init since the very beginning. He created his own version before I even joined Rust-for-Linux, contributed ideas to my solution, and reviewed it. With the removal of the declarative macro mess, he is also going to be a maintainer of pin-init.

---
 MAINTAINERS | 1 +
 1 file changed, 1 insertion(+)
MAINTAINERS +1 -0
index dc731d37c8fe..fb6534339b75 100644
@@ -22916,6 +22916,7 @@ F:	rust/kernel/num/
 
 RUST [PIN-INIT]
 M:	Benno Lossin <[email protected]>
+M:	Gary Guo <[email protected]>
 L:	[email protected]
 S:	Maintained
 W:	https://rust-for-linux.com/pin-init
Signature · 1 line
2.51.2