xref: /linux/rust/kernel/init/macros.rs (revision 071cedc84e907f6984b3de3285ec2b077d3c3cdb)
1 // SPDX-License-Identifier: Apache-2.0 OR MIT
2 
3 //! This module provides the macros that actually implement the proc-macros `pin_data` and
4 //! `pinned_drop`. It also contains `__init_internal` the implementation of the `{try_}{pin_}init!`
5 //! macros.
6 //!
7 //! These macros should never be called directly, since they expect their input to be
8 //! in a certain format which is internal. If used incorrectly, these macros can lead to UB even in
9 //! safe code! Use the public facing macros instead.
10 //!
11 //! This architecture has been chosen because the kernel does not yet have access to `syn` which
12 //! would make matters a lot easier for implementing these as proc-macros.
13 //!
14 //! # Macro expansion example
15 //!
16 //! This section is intended for readers trying to understand the macros in this module and the
17 //! `pin_init!` macros from `init.rs`.
18 //!
19 //! We will look at the following example:
20 //!
21 //! ```rust,ignore
22 //! # use kernel::init::*;
23 //! # use core::pin::Pin;
24 //! #[pin_data]
25 //! #[repr(C)]
26 //! struct Bar<T> {
27 //!     #[pin]
28 //!     t: T,
29 //!     pub x: usize,
30 //! }
31 //!
32 //! impl<T> Bar<T> {
33 //!     fn new(t: T) -> impl PinInit<Self> {
34 //!         pin_init!(Self { t, x: 0 })
35 //!     }
36 //! }
37 //!
38 //! #[pin_data(PinnedDrop)]
39 //! struct Foo {
40 //!     a: usize,
41 //!     #[pin]
42 //!     b: Bar<u32>,
43 //! }
44 //!
45 //! #[pinned_drop]
46 //! impl PinnedDrop for Foo {
47 //!     fn drop(self: Pin<&mut Self>) {
48 //!         println!("{self:p} is getting dropped.");
49 //!     }
50 //! }
51 //!
52 //! let a = 42;
53 //! let initializer = pin_init!(Foo {
54 //!     a,
55 //!     b <- Bar::new(36),
56 //! });
57 //! ```
58 //!
59 //! This example includes the most common and important features of the pin-init API.
60 //!
61 //! Below you can find individual section about the different macro invocations. Here are some
62 //! general things we need to take into account when designing macros:
63 //! - use global paths, similarly to file paths, these start with the separator: `::core::panic!()`
64 //!   this ensures that the correct item is used, since users could define their own `mod core {}`
65 //!   and then their own `panic!` inside to execute arbitrary code inside of our macro.
66 //! - macro `unsafe` hygiene: we need to ensure that we do not expand arbitrary, user-supplied
67 //!   expressions inside of an `unsafe` block in the macro, because this would allow users to do
68 //!   `unsafe` operations without an associated `unsafe` block.
69 //!
70 //! ## `#[pin_data]` on `Bar`
71 //!
72 //! This macro is used to specify which fields are structurally pinned and which fields are not. It
73 //! is placed on the struct definition and allows `#[pin]` to be placed on the fields.
74 //!
75 //! Here is the definition of `Bar` from our example:
76 //!
77 //! ```rust,ignore
78 //! # use kernel::init::*;
79 //! #[pin_data]
80 //! #[repr(C)]
81 //! struct Bar<T> {
82 //!     #[pin]
83 //!     t: T,
84 //!     pub x: usize,
85 //! }
86 //! ```
87 //!
88 //! This expands to the following code:
89 //!
90 //! ```rust,ignore
91 //! // Firstly the normal definition of the struct, attributes are preserved:
92 //! #[repr(C)]
93 //! struct Bar<T> {
94 //!     t: T,
95 //!     pub x: usize,
96 //! }
97 //! // Then an anonymous constant is defined, this is because we do not want any code to access the
98 //! // types that we define inside:
99 //! const _: () = {
100 //!     // We define the pin-data carrying struct, it is a ZST and needs to have the same generics,
101 //!     // since we need to implement access functions for each field and thus need to know its
102 //!     // type.
103 //!     struct __ThePinData<T> {
104 //!         __phantom: ::core::marker::PhantomData<fn(Bar<T>) -> Bar<T>>,
105 //!     }
106 //!     // We implement `Copy` for the pin-data struct, since all functions it defines will take
107 //!     // `self` by value.
108 //!     impl<T> ::core::clone::Clone for __ThePinData<T> {
109 //!         fn clone(&self) -> Self {
110 //!             *self
111 //!         }
112 //!     }
113 //!     impl<T> ::core::marker::Copy for __ThePinData<T> {}
114 //!     // For every field of `Bar`, the pin-data struct will define a function with the same name
115 //!     // and accessor (`pub` or `pub(crate)` etc.). This function will take a pointer to the
116 //!     // field (`slot`) and a `PinInit` or `Init` depending on the projection kind of the field
117 //!     // (if pinning is structural for the field, then `PinInit` otherwise `Init`).
118 //!     #[allow(dead_code)]
119 //!     impl<T> __ThePinData<T> {
120 //!         unsafe fn t<E>(
121 //!             self,
122 //!             slot: *mut T,
123 //!             // Since `t` is `#[pin]`, this is `PinInit`.
124 //!             init: impl ::kernel::init::PinInit<T, E>,
125 //!         ) -> ::core::result::Result<(), E> {
126 //!             unsafe { ::kernel::init::PinInit::__pinned_init(init, slot) }
127 //!         }
128 //!         pub unsafe fn x<E>(
129 //!             self,
130 //!             slot: *mut usize,
131 //!             // Since `x` is not `#[pin]`, this is `Init`.
132 //!             init: impl ::kernel::init::Init<usize, E>,
133 //!         ) -> ::core::result::Result<(), E> {
134 //!             unsafe { ::kernel::init::Init::__init(init, slot) }
135 //!         }
136 //!     }
137 //!     // Implement the internal `HasPinData` trait that associates `Bar` with the pin-data struct
138 //!     // that we constructed above.
139 //!     unsafe impl<T> ::kernel::init::__internal::HasPinData for Bar<T> {
140 //!         type PinData = __ThePinData<T>;
141 //!         unsafe fn __pin_data() -> Self::PinData {
142 //!             __ThePinData {
143 //!                 __phantom: ::core::marker::PhantomData,
144 //!             }
145 //!         }
146 //!     }
147 //!     // Implement the internal `PinData` trait that marks the pin-data struct as a pin-data
148 //!     // struct. This is important to ensure that no user can implement a rouge `__pin_data`
149 //!     // function without using `unsafe`.
150 //!     unsafe impl<T> ::kernel::init::__internal::PinData for __ThePinData<T> {
151 //!         type Datee = Bar<T>;
152 //!     }
153 //!     // Now we only want to implement `Unpin` for `Bar` when every structurally pinned field is
154 //!     // `Unpin`. In other words, whether `Bar` is `Unpin` only depends on structurally pinned
155 //!     // fields (those marked with `#[pin]`). These fields will be listed in this struct, in our
156 //!     // case no such fields exist, hence this is almost empty. The two phantomdata fields exist
157 //!     // for two reasons:
158 //!     // - `__phantom`: every generic must be used, since we cannot really know which generics
159 //!     //   are used, we declere all and then use everything here once.
160 //!     // - `__phantom_pin`: uses the `'__pin` lifetime and ensures that this struct is invariant
161 //!     //   over it. The lifetime is needed to work around the limitation that trait bounds must
162 //!     //   not be trivial, e.g. the user has a `#[pin] PhantomPinned` field -- this is
163 //!     //   unconditionally `!Unpin` and results in an error. The lifetime tricks the compiler
164 //!     //   into accepting these bounds regardless.
165 //!     #[allow(dead_code)]
166 //!     struct __Unpin<'__pin, T> {
167 //!         __phantom_pin: ::core::marker::PhantomData<fn(&'__pin ()) -> &'__pin ()>,
168 //!         __phantom: ::core::marker::PhantomData<fn(Bar<T>) -> Bar<T>>,
169 //!         // Our only `#[pin]` field is `t`.
170 //!         t: T,
171 //!     }
172 //!     #[doc(hidden)]
173 //!     impl<'__pin, T>
174 //!         ::core::marker::Unpin for Bar<T> where __Unpin<'__pin, T>: ::core::marker::Unpin {}
175 //!     // Now we need to ensure that `Bar` does not implement `Drop`, since that would give users
176 //!     // access to `&mut self` inside of `drop` even if the struct was pinned. This could lead to
177 //!     // UB with only safe code, so we disallow this by giving a trait implementation error using
178 //!     // a direct impl and a blanket implementation.
179 //!     trait MustNotImplDrop {}
180 //!     // Normally `Drop` bounds do not have the correct semantics, but for this purpose they do
181 //!     // (normally people want to know if a type has any kind of drop glue at all, here we want
182 //!     // to know if it has any kind of custom drop glue, which is exactly what this bound does).
183 //!     #[allow(drop_bounds)]
184 //!     impl<T: ::core::ops::Drop> MustNotImplDrop for T {}
185 //!     impl<T> MustNotImplDrop for Bar<T> {}
186 //!     // Here comes a convenience check, if one implemented `PinnedDrop`, but forgot to add it to
187 //!     // `#[pin_data]`, then this will error with the same mechanic as above, this is not needed
188 //!     // for safety, but a good sanity check, since no normal code calls `PinnedDrop::drop`.
189 //!     #[allow(non_camel_case_types)]
190 //!     trait UselessPinnedDropImpl_you_need_to_specify_PinnedDrop {}
191 //!     impl<T: ::kernel::init::PinnedDrop>
192 //!         UselessPinnedDropImpl_you_need_to_specify_PinnedDrop for T {}
193 //!     impl<T> UselessPinnedDropImpl_you_need_to_specify_PinnedDrop for Bar<T> {}
194 //! };
195 //! ```
196 //!
197 //! ## `pin_init!` in `impl Bar`
198 //!
199 //! This macro creates an pin-initializer for the given struct. It requires that the struct is
200 //! annotated by `#[pin_data]`.
201 //!
202 //! Here is the impl on `Bar` defining the new function:
203 //!
204 //! ```rust,ignore
205 //! impl<T> Bar<T> {
206 //!     fn new(t: T) -> impl PinInit<Self> {
207 //!         pin_init!(Self { t, x: 0 })
208 //!     }
209 //! }
210 //! ```
211 //!
212 //! This expands to the following code:
213 //!
214 //! ```rust,ignore
215 //! impl<T> Bar<T> {
216 //!     fn new(t: T) -> impl PinInit<Self> {
217 //!         {
218 //!             // We do not want to allow arbitrary returns, so we declare this type as the `Ok`
219 //!             // return type and shadow it later when we insert the arbitrary user code. That way
220 //!             // there will be no possibility of returning without `unsafe`.
221 //!             struct __InitOk;
222 //!             // Get the pin-data type from the initialized type.
223 //!             // - the function is unsafe, hence the unsafe block
224 //!             // - we `use` the `HasPinData` trait in the block, it is only available in that
225 //!             //   scope.
226 //!             let data = unsafe {
227 //!                 use ::kernel::init::__internal::HasPinData;
228 //!                 Self::__pin_data()
229 //!             };
230 //!             // Use `data` to help with type inference, the closure supplied will have the type
231 //!             // `FnOnce(*mut Self) -> Result<__InitOk, Infallible>`.
232 //!             let init = ::kernel::init::__internal::PinData::make_closure::<
233 //!                 _,
234 //!                 __InitOk,
235 //!                 ::core::convert::Infallible,
236 //!             >(data, move |slot| {
237 //!                 {
238 //!                     // Shadow the structure so it cannot be used to return early. If a user
239 //!                     // tries to write `return Ok(__InitOk)`, then they get a type error, since
240 //!                     // that will refer to this struct instead of the one defined above.
241 //!                     struct __InitOk;
242 //!                     // This is the expansion of `t,`, which is syntactic sugar for `t: t,`.
243 //!                     unsafe { ::core::ptr::write(::core::addr_of_mut!((*slot).t), t) };
244 //!                     // Since initialization could fail later (not in this case, since the error
245 //!                     // type is `Infallible`) we will need to drop this field if there is an
246 //!                     // error later. This `DropGuard` will drop the field when it gets dropped
247 //!                     // and has not yet been forgotten. We make a reference to it, so users
248 //!                     // cannot `mem::forget` it from the initializer, since the name is the same
249 //!                     // as the field (including hygiene).
250 //!                     let t = &unsafe {
251 //!                         ::kernel::init::__internal::DropGuard::new(
252 //!                             ::core::addr_of_mut!((*slot).t),
253 //!                         )
254 //!                     };
255 //!                     // Expansion of `x: 0,`:
256 //!                     // Since this can be an arbitrary expression we cannot place it inside of
257 //!                     // the `unsafe` block, so we bind it here.
258 //!                     let x = 0;
259 //!                     unsafe { ::core::ptr::write(::core::addr_of_mut!((*slot).x), x) };
260 //!                     // We again create a `DropGuard`.
261 //!                     let x = &unsafe {
262 //!                         ::kernel::init::__internal::DropGuard::new(
263 //!                             ::core::addr_of_mut!((*slot).x),
264 //!                         )
265 //!                     };
266 //!
267 //!                     // Here we use the type checker to ensure that every field has been
268 //!                     // initialized exactly once, since this is `if false` it will never get
269 //!                     // executed, but still type-checked.
270 //!                     // Additionally we abuse `slot` to automatically infer the correct type for
271 //!                     // the struct. This is also another check that every field is accessible
272 //!                     // from this scope.
273 //!                     #[allow(unreachable_code, clippy::diverging_sub_expression)]
274 //!                     if false {
275 //!                         unsafe {
276 //!                             ::core::ptr::write(
277 //!                                 slot,
278 //!                                 Self {
279 //!                                     // We only care about typecheck finding every field here,
280 //!                                     // the expression does not matter, just conjure one using
281 //!                                     // `panic!()`:
282 //!                                     t: ::core::panic!(),
283 //!                                     x: ::core::panic!(),
284 //!                                 },
285 //!                             );
286 //!                         };
287 //!                     }
288 //!                     // Since initialization has successfully completed, we can now forget the
289 //!                     // guards. This is not `mem::forget`, since we only have `&DropGuard`.
290 //!                     unsafe { ::kernel::init::__internal::DropGuard::forget(t) };
291 //!                     unsafe { ::kernel::init::__internal::DropGuard::forget(x) };
292 //!                 }
293 //!                 // We leave the scope above and gain access to the previously shadowed
294 //!                 // `__InitOk` that we need to return.
295 //!                 Ok(__InitOk)
296 //!             });
297 //!             // Change the return type from `__InitOk` to `()`.
298 //!             let init = move |slot| -> ::core::result::Result<(), ::core::convert::Infallible> {
299 //!                 init(slot).map(|__InitOk| ())
300 //!             };
301 //!             // Construct the initializer.
302 //!             let init = unsafe {
303 //!                 ::kernel::init::pin_init_from_closure::<_, ::core::convert::Infallible>(init)
304 //!             };
305 //!             init
306 //!         }
307 //!     }
308 //! }
309 //! ```
310 //!
311 //! ## `#[pin_data]` on `Foo`
312 //!
313 //! Since we already took a look at `#[pin_data]` on `Bar`, this section will only explain the
314 //! differences/new things in the expansion of the `Foo` definition:
315 //!
316 //! ```rust,ignore
317 //! #[pin_data(PinnedDrop)]
318 //! struct Foo {
319 //!     a: usize,
320 //!     #[pin]
321 //!     b: Bar<u32>,
322 //! }
323 //! ```
324 //!
325 //! This expands to the following code:
326 //!
327 //! ```rust,ignore
328 //! struct Foo {
329 //!     a: usize,
330 //!     b: Bar<u32>,
331 //! }
332 //! const _: () = {
333 //!     struct __ThePinData {
334 //!         __phantom: ::core::marker::PhantomData<fn(Foo) -> Foo>,
335 //!     }
336 //!     impl ::core::clone::Clone for __ThePinData {
337 //!         fn clone(&self) -> Self {
338 //!             *self
339 //!         }
340 //!     }
341 //!     impl ::core::marker::Copy for __ThePinData {}
342 //!     #[allow(dead_code)]
343 //!     impl __ThePinData {
344 //!         unsafe fn b<E>(
345 //!             self,
346 //!             slot: *mut Bar<u32>,
347 //!             init: impl ::kernel::init::PinInit<Bar<u32>, E>,
348 //!         ) -> ::core::result::Result<(), E> {
349 //!             unsafe { ::kernel::init::PinInit::__pinned_init(init, slot) }
350 //!         }
351 //!         unsafe fn a<E>(
352 //!             self,
353 //!             slot: *mut usize,
354 //!             init: impl ::kernel::init::Init<usize, E>,
355 //!         ) -> ::core::result::Result<(), E> {
356 //!             unsafe { ::kernel::init::Init::__init(init, slot) }
357 //!         }
358 //!     }
359 //!     unsafe impl ::kernel::init::__internal::HasPinData for Foo {
360 //!         type PinData = __ThePinData;
361 //!         unsafe fn __pin_data() -> Self::PinData {
362 //!             __ThePinData {
363 //!                 __phantom: ::core::marker::PhantomData,
364 //!             }
365 //!         }
366 //!     }
367 //!     unsafe impl ::kernel::init::__internal::PinData for __ThePinData {
368 //!         type Datee = Foo;
369 //!     }
370 //!     #[allow(dead_code)]
371 //!     struct __Unpin<'__pin> {
372 //!         __phantom_pin: ::core::marker::PhantomData<fn(&'__pin ()) -> &'__pin ()>,
373 //!         __phantom: ::core::marker::PhantomData<fn(Foo) -> Foo>,
374 //!         b: Bar<u32>,
375 //!     }
376 //!     #[doc(hidden)]
377 //!     impl<'__pin> ::core::marker::Unpin for Foo where __Unpin<'__pin>: ::core::marker::Unpin {}
378 //!     // Since we specified `PinnedDrop` as the argument to `#[pin_data]`, we expect `Foo` to
379 //!     // implement `PinnedDrop`. Thus we do not need to prevent `Drop` implementations like
380 //!     // before, instead we implement `Drop` here and delegate to `PinnedDrop`.
381 //!     impl ::core::ops::Drop for Foo {
382 //!         fn drop(&mut self) {
383 //!             // Since we are getting dropped, no one else has a reference to `self` and thus we
384 //!             // can assume that we never move.
385 //!             let pinned = unsafe { ::core::pin::Pin::new_unchecked(self) };
386 //!             // Create the unsafe token that proves that we are inside of a destructor, this
387 //!             // type is only allowed to be created in a destructor.
388 //!             let token = unsafe { ::kernel::init::__internal::OnlyCallFromDrop::new() };
389 //!             ::kernel::init::PinnedDrop::drop(pinned, token);
390 //!         }
391 //!     }
392 //! };
393 //! ```
394 //!
395 //! ## `#[pinned_drop]` on `impl PinnedDrop for Foo`
396 //!
397 //! This macro is used to implement the `PinnedDrop` trait, since that trait is `unsafe` and has an
398 //! extra parameter that should not be used at all. The macro hides that parameter.
399 //!
400 //! Here is the `PinnedDrop` impl for `Foo`:
401 //!
402 //! ```rust,ignore
403 //! #[pinned_drop]
404 //! impl PinnedDrop for Foo {
405 //!     fn drop(self: Pin<&mut Self>) {
406 //!         println!("{self:p} is getting dropped.");
407 //!     }
408 //! }
409 //! ```
410 //!
411 //! This expands to the following code:
412 //!
413 //! ```rust,ignore
414 //! // `unsafe`, full path and the token parameter are added, everything else stays the same.
415 //! unsafe impl ::kernel::init::PinnedDrop for Foo {
416 //!     fn drop(self: Pin<&mut Self>, _: ::kernel::init::__internal::OnlyCallFromDrop) {
417 //!         println!("{self:p} is getting dropped.");
418 //!     }
419 //! }
420 //! ```
421 //!
422 //! ## `pin_init!` on `Foo`
423 //!
424 //! Since we already took a look at `pin_init!` on `Bar`, this section will only show the expansion
425 //! of `pin_init!` on `Foo`:
426 //!
427 //! ```rust,ignore
428 //! let a = 42;
429 //! let initializer = pin_init!(Foo {
430 //!     a,
431 //!     b <- Bar::new(36),
432 //! });
433 //! ```
434 //!
435 //! This expands to the following code:
436 //!
437 //! ```rust,ignore
438 //! let a = 42;
439 //! let initializer = {
440 //!     struct __InitOk;
441 //!     let data = unsafe {
442 //!         use ::kernel::init::__internal::HasPinData;
443 //!         Foo::__pin_data()
444 //!     };
445 //!     let init = ::kernel::init::__internal::PinData::make_closure::<
446 //!         _,
447 //!         __InitOk,
448 //!         ::core::convert::Infallible,
449 //!     >(data, move |slot| {
450 //!         {
451 //!             struct __InitOk;
452 //!             unsafe { ::core::ptr::write(::core::addr_of_mut!((*slot).a), a) };
453 //!             let a = &unsafe {
454 //!                 ::kernel::init::__internal::DropGuard::new(::core::addr_of_mut!((*slot).a))
455 //!             };
456 //!             let b = Bar::new(36);
457 //!             unsafe { data.b(::core::addr_of_mut!((*slot).b), b)? };
458 //!             let b = &unsafe {
459 //!                 ::kernel::init::__internal::DropGuard::new(::core::addr_of_mut!((*slot).b))
460 //!             };
461 //!
462 //!             #[allow(unreachable_code, clippy::diverging_sub_expression)]
463 //!             if false {
464 //!                 unsafe {
465 //!                     ::core::ptr::write(
466 //!                         slot,
467 //!                         Foo {
468 //!                             a: ::core::panic!(),
469 //!                             b: ::core::panic!(),
470 //!                         },
471 //!                     );
472 //!                 };
473 //!             }
474 //!             unsafe { ::kernel::init::__internal::DropGuard::forget(a) };
475 //!             unsafe { ::kernel::init::__internal::DropGuard::forget(b) };
476 //!         }
477 //!         Ok(__InitOk)
478 //!     });
479 //!     let init = move |slot| -> ::core::result::Result<(), ::core::convert::Infallible> {
480 //!         init(slot).map(|__InitOk| ())
481 //!     };
482 //!     let init = unsafe {
483 //!         ::kernel::init::pin_init_from_closure::<_, ::core::convert::Infallible>(init)
484 //!     };
485 //!     init
486 //! };
487 //! ```
488 
489 /// Creates a `unsafe impl<...> PinnedDrop for $type` block.
490 ///
491 /// See [`PinnedDrop`] for more information.
492 #[doc(hidden)]
493 #[macro_export]
494 macro_rules! __pinned_drop {
495     (
496         @impl_sig($($impl_sig:tt)*),
497         @impl_body(
498             $(#[$($attr:tt)*])*
499             fn drop($($sig:tt)*) {
500                 $($inner:tt)*
501             }
502         ),
503     ) => {
504         unsafe $($impl_sig)* {
505             // Inherit all attributes and the type/ident tokens for the signature.
506             $(#[$($attr)*])*
507             fn drop($($sig)*, _: $crate::init::__internal::OnlyCallFromDrop) {
508                 $($inner)*
509             }
510         }
511     }
512 }
513 
514 /// This macro first parses the struct definition such that it separates pinned and not pinned
515 /// fields. Afterwards it declares the struct and implement the `PinData` trait safely.
516 #[doc(hidden)]
517 #[macro_export]
518 macro_rules! __pin_data {
519     // Proc-macro entry point, this is supplied by the proc-macro pre-parsing.
520     (parse_input:
521         @args($($pinned_drop:ident)?),
522         @sig(
523             $(#[$($struct_attr:tt)*])*
524             $vis:vis struct $name:ident
525             $(where $($whr:tt)*)?
526         ),
527         @impl_generics($($impl_generics:tt)*),
528         @ty_generics($($ty_generics:tt)*),
529         @body({ $($fields:tt)* }),
530     ) => {
531         // We now use token munching to iterate through all of the fields. While doing this we
532         // identify fields marked with `#[pin]`, these fields are the 'pinned fields'. The user
533         // wants these to be structurally pinned. The rest of the fields are the
534         // 'not pinned fields'. Additionally we collect all fields, since we need them in the right
535         // order to declare the struct.
536         //
537         // In this call we also put some explaining comments for the parameters.
538         $crate::__pin_data!(find_pinned_fields:
539             // Attributes on the struct itself, these will just be propagated to be put onto the
540             // struct definition.
541             @struct_attrs($(#[$($struct_attr)*])*),
542             // The visibility of the struct.
543             @vis($vis),
544             // The name of the struct.
545             @name($name),
546             // The 'impl generics', the generics that will need to be specified on the struct inside
547             // of an `impl<$ty_generics>` block.
548             @impl_generics($($impl_generics)*),
549             // The 'ty generics', the generics that will need to be specified on the impl blocks.
550             @ty_generics($($ty_generics)*),
551             // The where clause of any impl block and the declaration.
552             @where($($($whr)*)?),
553             // The remaining fields tokens that need to be processed.
554             // We add a `,` at the end to ensure correct parsing.
555             @fields_munch($($fields)* ,),
556             // The pinned fields.
557             @pinned(),
558             // The not pinned fields.
559             @not_pinned(),
560             // All fields.
561             @fields(),
562             // The accumulator containing all attributes already parsed.
563             @accum(),
564             // Contains `yes` or `` to indicate if `#[pin]` was found on the current field.
565             @is_pinned(),
566             // The proc-macro argument, this should be `PinnedDrop` or ``.
567             @pinned_drop($($pinned_drop)?),
568         );
569     };
570     (find_pinned_fields:
571         @struct_attrs($($struct_attrs:tt)*),
572         @vis($vis:vis),
573         @name($name:ident),
574         @impl_generics($($impl_generics:tt)*),
575         @ty_generics($($ty_generics:tt)*),
576         @where($($whr:tt)*),
577         // We found a PhantomPinned field, this should generally be pinned!
578         @fields_munch($field:ident : $($($(::)?core::)?marker::)?PhantomPinned, $($rest:tt)*),
579         @pinned($($pinned:tt)*),
580         @not_pinned($($not_pinned:tt)*),
581         @fields($($fields:tt)*),
582         @accum($($accum:tt)*),
583         // This field is not pinned.
584         @is_pinned(),
585         @pinned_drop($($pinned_drop:ident)?),
586     ) => {
587         ::core::compile_error!(concat!(
588             "The field `",
589             stringify!($field),
590             "` of type `PhantomPinned` only has an effect, if it has the `#[pin]` attribute.",
591         ));
592         $crate::__pin_data!(find_pinned_fields:
593             @struct_attrs($($struct_attrs)*),
594             @vis($vis),
595             @name($name),
596             @impl_generics($($impl_generics)*),
597             @ty_generics($($ty_generics)*),
598             @where($($whr)*),
599             @fields_munch($($rest)*),
600             @pinned($($pinned)* $($accum)* $field: ::core::marker::PhantomPinned,),
601             @not_pinned($($not_pinned)*),
602             @fields($($fields)* $($accum)* $field: ::core::marker::PhantomPinned,),
603             @accum(),
604             @is_pinned(),
605             @pinned_drop($($pinned_drop)?),
606         );
607     };
608     (find_pinned_fields:
609         @struct_attrs($($struct_attrs:tt)*),
610         @vis($vis:vis),
611         @name($name:ident),
612         @impl_generics($($impl_generics:tt)*),
613         @ty_generics($($ty_generics:tt)*),
614         @where($($whr:tt)*),
615         // We reached the field declaration.
616         @fields_munch($field:ident : $type:ty, $($rest:tt)*),
617         @pinned($($pinned:tt)*),
618         @not_pinned($($not_pinned:tt)*),
619         @fields($($fields:tt)*),
620         @accum($($accum:tt)*),
621         // This field is pinned.
622         @is_pinned(yes),
623         @pinned_drop($($pinned_drop:ident)?),
624     ) => {
625         $crate::__pin_data!(find_pinned_fields:
626             @struct_attrs($($struct_attrs)*),
627             @vis($vis),
628             @name($name),
629             @impl_generics($($impl_generics)*),
630             @ty_generics($($ty_generics)*),
631             @where($($whr)*),
632             @fields_munch($($rest)*),
633             @pinned($($pinned)* $($accum)* $field: $type,),
634             @not_pinned($($not_pinned)*),
635             @fields($($fields)* $($accum)* $field: $type,),
636             @accum(),
637             @is_pinned(),
638             @pinned_drop($($pinned_drop)?),
639         );
640     };
641     (find_pinned_fields:
642         @struct_attrs($($struct_attrs:tt)*),
643         @vis($vis:vis),
644         @name($name:ident),
645         @impl_generics($($impl_generics:tt)*),
646         @ty_generics($($ty_generics:tt)*),
647         @where($($whr:tt)*),
648         // We reached the field declaration.
649         @fields_munch($field:ident : $type:ty, $($rest:tt)*),
650         @pinned($($pinned:tt)*),
651         @not_pinned($($not_pinned:tt)*),
652         @fields($($fields:tt)*),
653         @accum($($accum:tt)*),
654         // This field is not pinned.
655         @is_pinned(),
656         @pinned_drop($($pinned_drop:ident)?),
657     ) => {
658         $crate::__pin_data!(find_pinned_fields:
659             @struct_attrs($($struct_attrs)*),
660             @vis($vis),
661             @name($name),
662             @impl_generics($($impl_generics)*),
663             @ty_generics($($ty_generics)*),
664             @where($($whr)*),
665             @fields_munch($($rest)*),
666             @pinned($($pinned)*),
667             @not_pinned($($not_pinned)* $($accum)* $field: $type,),
668             @fields($($fields)* $($accum)* $field: $type,),
669             @accum(),
670             @is_pinned(),
671             @pinned_drop($($pinned_drop)?),
672         );
673     };
674     (find_pinned_fields:
675         @struct_attrs($($struct_attrs:tt)*),
676         @vis($vis:vis),
677         @name($name:ident),
678         @impl_generics($($impl_generics:tt)*),
679         @ty_generics($($ty_generics:tt)*),
680         @where($($whr:tt)*),
681         // We found the `#[pin]` attr.
682         @fields_munch(#[pin] $($rest:tt)*),
683         @pinned($($pinned:tt)*),
684         @not_pinned($($not_pinned:tt)*),
685         @fields($($fields:tt)*),
686         @accum($($accum:tt)*),
687         @is_pinned($($is_pinned:ident)?),
688         @pinned_drop($($pinned_drop:ident)?),
689     ) => {
690         $crate::__pin_data!(find_pinned_fields:
691             @struct_attrs($($struct_attrs)*),
692             @vis($vis),
693             @name($name),
694             @impl_generics($($impl_generics)*),
695             @ty_generics($($ty_generics)*),
696             @where($($whr)*),
697             @fields_munch($($rest)*),
698             // We do not include `#[pin]` in the list of attributes, since it is not actually an
699             // attribute that is defined somewhere.
700             @pinned($($pinned)*),
701             @not_pinned($($not_pinned)*),
702             @fields($($fields)*),
703             @accum($($accum)*),
704             // Set this to `yes`.
705             @is_pinned(yes),
706             @pinned_drop($($pinned_drop)?),
707         );
708     };
709     (find_pinned_fields:
710         @struct_attrs($($struct_attrs:tt)*),
711         @vis($vis:vis),
712         @name($name:ident),
713         @impl_generics($($impl_generics:tt)*),
714         @ty_generics($($ty_generics:tt)*),
715         @where($($whr:tt)*),
716         // We reached the field declaration with visibility, for simplicity we only munch the
717         // visibility and put it into `$accum`.
718         @fields_munch($fvis:vis $field:ident $($rest:tt)*),
719         @pinned($($pinned:tt)*),
720         @not_pinned($($not_pinned:tt)*),
721         @fields($($fields:tt)*),
722         @accum($($accum:tt)*),
723         @is_pinned($($is_pinned:ident)?),
724         @pinned_drop($($pinned_drop:ident)?),
725     ) => {
726         $crate::__pin_data!(find_pinned_fields:
727             @struct_attrs($($struct_attrs)*),
728             @vis($vis),
729             @name($name),
730             @impl_generics($($impl_generics)*),
731             @ty_generics($($ty_generics)*),
732             @where($($whr)*),
733             @fields_munch($field $($rest)*),
734             @pinned($($pinned)*),
735             @not_pinned($($not_pinned)*),
736             @fields($($fields)*),
737             @accum($($accum)* $fvis),
738             @is_pinned($($is_pinned)?),
739             @pinned_drop($($pinned_drop)?),
740         );
741     };
742     (find_pinned_fields:
743         @struct_attrs($($struct_attrs:tt)*),
744         @vis($vis:vis),
745         @name($name:ident),
746         @impl_generics($($impl_generics:tt)*),
747         @ty_generics($($ty_generics:tt)*),
748         @where($($whr:tt)*),
749         // Some other attribute, just put it into `$accum`.
750         @fields_munch(#[$($attr:tt)*] $($rest:tt)*),
751         @pinned($($pinned:tt)*),
752         @not_pinned($($not_pinned:tt)*),
753         @fields($($fields:tt)*),
754         @accum($($accum:tt)*),
755         @is_pinned($($is_pinned:ident)?),
756         @pinned_drop($($pinned_drop:ident)?),
757     ) => {
758         $crate::__pin_data!(find_pinned_fields:
759             @struct_attrs($($struct_attrs)*),
760             @vis($vis),
761             @name($name),
762             @impl_generics($($impl_generics)*),
763             @ty_generics($($ty_generics)*),
764             @where($($whr)*),
765             @fields_munch($($rest)*),
766             @pinned($($pinned)*),
767             @not_pinned($($not_pinned)*),
768             @fields($($fields)*),
769             @accum($($accum)* #[$($attr)*]),
770             @is_pinned($($is_pinned)?),
771             @pinned_drop($($pinned_drop)?),
772         );
773     };
774     (find_pinned_fields:
775         @struct_attrs($($struct_attrs:tt)*),
776         @vis($vis:vis),
777         @name($name:ident),
778         @impl_generics($($impl_generics:tt)*),
779         @ty_generics($($ty_generics:tt)*),
780         @where($($whr:tt)*),
781         // We reached the end of the fields, plus an optional additional comma, since we added one
782         // before and the user is also allowed to put a trailing comma.
783         @fields_munch($(,)?),
784         @pinned($($pinned:tt)*),
785         @not_pinned($($not_pinned:tt)*),
786         @fields($($fields:tt)*),
787         @accum(),
788         @is_pinned(),
789         @pinned_drop($($pinned_drop:ident)?),
790     ) => {
791         // Declare the struct with all fields in the correct order.
792         $($struct_attrs)*
793         $vis struct $name <$($impl_generics)*>
794         where $($whr)*
795         {
796             $($fields)*
797         }
798 
799         // We put the rest into this const item, because it then will not be accessible to anything
800         // outside.
801         const _: () = {
802             // We declare this struct which will host all of the projection function for our type.
803             // it will be invariant over all generic parameters which are inherited from the
804             // struct.
805             $vis struct __ThePinData<$($impl_generics)*>
806             where $($whr)*
807             {
808                 __phantom: ::core::marker::PhantomData<
809                     fn($name<$($ty_generics)*>) -> $name<$($ty_generics)*>
810                 >,
811             }
812 
813             impl<$($impl_generics)*> ::core::clone::Clone for __ThePinData<$($ty_generics)*>
814             where $($whr)*
815             {
816                 fn clone(&self) -> Self { *self }
817             }
818 
819             impl<$($impl_generics)*> ::core::marker::Copy for __ThePinData<$($ty_generics)*>
820             where $($whr)*
821             {}
822 
823             // Make all projection functions.
824             $crate::__pin_data!(make_pin_data:
825                 @pin_data(__ThePinData),
826                 @impl_generics($($impl_generics)*),
827                 @ty_generics($($ty_generics)*),
828                 @where($($whr)*),
829                 @pinned($($pinned)*),
830                 @not_pinned($($not_pinned)*),
831             );
832 
833             // SAFETY: We have added the correct projection functions above to `__ThePinData` and
834             // we also use the least restrictive generics possible.
835             unsafe impl<$($impl_generics)*>
836                 $crate::init::__internal::HasPinData for $name<$($ty_generics)*>
837             where $($whr)*
838             {
839                 type PinData = __ThePinData<$($ty_generics)*>;
840 
841                 unsafe fn __pin_data() -> Self::PinData {
842                     __ThePinData { __phantom: ::core::marker::PhantomData }
843                 }
844             }
845 
846             unsafe impl<$($impl_generics)*>
847                 $crate::init::__internal::PinData for __ThePinData<$($ty_generics)*>
848             where $($whr)*
849             {
850                 type Datee = $name<$($ty_generics)*>;
851             }
852 
853             // This struct will be used for the unpin analysis. Since only structurally pinned
854             // fields are relevant whether the struct should implement `Unpin`.
855             #[allow(dead_code)]
856             struct __Unpin <'__pin, $($impl_generics)*>
857             where $($whr)*
858             {
859                 __phantom_pin: ::core::marker::PhantomData<fn(&'__pin ()) -> &'__pin ()>,
860                 __phantom: ::core::marker::PhantomData<
861                     fn($name<$($ty_generics)*>) -> $name<$($ty_generics)*>
862                 >,
863                 // Only the pinned fields.
864                 $($pinned)*
865             }
866 
867             #[doc(hidden)]
868             impl<'__pin, $($impl_generics)*> ::core::marker::Unpin for $name<$($ty_generics)*>
869             where
870                 __Unpin<'__pin, $($ty_generics)*>: ::core::marker::Unpin,
871                 $($whr)*
872             {}
873 
874             // We need to disallow normal `Drop` implementation, the exact behavior depends on
875             // whether `PinnedDrop` was specified as the parameter.
876             $crate::__pin_data!(drop_prevention:
877                 @name($name),
878                 @impl_generics($($impl_generics)*),
879                 @ty_generics($($ty_generics)*),
880                 @where($($whr)*),
881                 @pinned_drop($($pinned_drop)?),
882             );
883         };
884     };
885     // When no `PinnedDrop` was specified, then we have to prevent implementing drop.
886     (drop_prevention:
887         @name($name:ident),
888         @impl_generics($($impl_generics:tt)*),
889         @ty_generics($($ty_generics:tt)*),
890         @where($($whr:tt)*),
891         @pinned_drop(),
892     ) => {
893         // We prevent this by creating a trait that will be implemented for all types implementing
894         // `Drop`. Additionally we will implement this trait for the struct leading to a conflict,
895         // if it also implements `Drop`
896         trait MustNotImplDrop {}
897         #[allow(drop_bounds)]
898         impl<T: ::core::ops::Drop> MustNotImplDrop for T {}
899         impl<$($impl_generics)*> MustNotImplDrop for $name<$($ty_generics)*>
900         where $($whr)* {}
901         // We also take care to prevent users from writing a useless `PinnedDrop` implementation.
902         // They might implement `PinnedDrop` correctly for the struct, but forget to give
903         // `PinnedDrop` as the parameter to `#[pin_data]`.
904         #[allow(non_camel_case_types)]
905         trait UselessPinnedDropImpl_you_need_to_specify_PinnedDrop {}
906         impl<T: $crate::init::PinnedDrop>
907             UselessPinnedDropImpl_you_need_to_specify_PinnedDrop for T {}
908         impl<$($impl_generics)*>
909             UselessPinnedDropImpl_you_need_to_specify_PinnedDrop for $name<$($ty_generics)*>
910         where $($whr)* {}
911     };
912     // When `PinnedDrop` was specified we just implement `Drop` and delegate.
913     (drop_prevention:
914         @name($name:ident),
915         @impl_generics($($impl_generics:tt)*),
916         @ty_generics($($ty_generics:tt)*),
917         @where($($whr:tt)*),
918         @pinned_drop(PinnedDrop),
919     ) => {
920         impl<$($impl_generics)*> ::core::ops::Drop for $name<$($ty_generics)*>
921         where $($whr)*
922         {
923             fn drop(&mut self) {
924                 // SAFETY: Since this is a destructor, `self` will not move after this function
925                 // terminates, since it is inaccessible.
926                 let pinned = unsafe { ::core::pin::Pin::new_unchecked(self) };
927                 // SAFETY: Since this is a drop function, we can create this token to call the
928                 // pinned destructor of this type.
929                 let token = unsafe { $crate::init::__internal::OnlyCallFromDrop::new() };
930                 $crate::init::PinnedDrop::drop(pinned, token);
931             }
932         }
933     };
934     // If some other parameter was specified, we emit a readable error.
935     (drop_prevention:
936         @name($name:ident),
937         @impl_generics($($impl_generics:tt)*),
938         @ty_generics($($ty_generics:tt)*),
939         @where($($whr:tt)*),
940         @pinned_drop($($rest:tt)*),
941     ) => {
942         compile_error!(
943             "Wrong parameters to `#[pin_data]`, expected nothing or `PinnedDrop`, got '{}'.",
944             stringify!($($rest)*),
945         );
946     };
947     (make_pin_data:
948         @pin_data($pin_data:ident),
949         @impl_generics($($impl_generics:tt)*),
950         @ty_generics($($ty_generics:tt)*),
951         @where($($whr:tt)*),
952         @pinned($($(#[$($p_attr:tt)*])* $pvis:vis $p_field:ident : $p_type:ty),* $(,)?),
953         @not_pinned($($(#[$($attr:tt)*])* $fvis:vis $field:ident : $type:ty),* $(,)?),
954     ) => {
955         // For every field, we create a projection function according to its projection type. If a
956         // field is structurally pinned, then it must be initialized via `PinInit`, if it is not
957         // structurally pinned, then it can be initialized via `Init`.
958         //
959         // The functions are `unsafe` to prevent accidentally calling them.
960         #[allow(dead_code)]
961         impl<$($impl_generics)*> $pin_data<$($ty_generics)*>
962         where $($whr)*
963         {
964             $(
965                 $(#[$($p_attr)*])*
966                 $pvis unsafe fn $p_field<E>(
967                     self,
968                     slot: *mut $p_type,
969                     init: impl $crate::init::PinInit<$p_type, E>,
970                 ) -> ::core::result::Result<(), E> {
971                     unsafe { $crate::init::PinInit::__pinned_init(init, slot) }
972                 }
973             )*
974             $(
975                 $(#[$($attr)*])*
976                 $fvis unsafe fn $field<E>(
977                     self,
978                     slot: *mut $type,
979                     init: impl $crate::init::Init<$type, E>,
980                 ) -> ::core::result::Result<(), E> {
981                     unsafe { $crate::init::Init::__init(init, slot) }
982                 }
983             )*
984         }
985     };
986 }
987 
988 /// The internal init macro. Do not call manually!
989 ///
990 /// This is called by the `{try_}{pin_}init!` macros with various inputs.
991 ///
992 /// This macro has multiple internal call configurations, these are always the very first ident:
993 /// - nothing: this is the base case and called by the `{try_}{pin_}init!` macros.
994 /// - `init_slot`: recursively creates the code that initializes all fields in `slot`.
995 /// - `make_initializer`: recursively create the struct initializer that guarantees that every
996 ///   field has been initialized exactly once.
997 /// - `forget_guards`: recursively forget the drop guards for every field.
998 #[doc(hidden)]
999 #[macro_export]
1000 macro_rules! __init_internal {
1001     (
1002         @this($($this:ident)?),
1003         @typ($t:ident $(::<$($generics:ty),*>)?),
1004         @fields($($fields:tt)*),
1005         @error($err:ty),
1006         // Either `PinData` or `InitData`, `$use_data` should only be present in the `PinData`
1007         // case.
1008         @data($data:ident, $($use_data:ident)?),
1009         // `HasPinData` or `HasInitData`.
1010         @has_data($has_data:ident, $get_data:ident),
1011         // `pin_init_from_closure` or `init_from_closure`.
1012         @construct_closure($construct_closure:ident),
1013     ) => {{
1014         // We do not want to allow arbitrary returns, so we declare this type as the `Ok` return
1015         // type and shadow it later when we insert the arbitrary user code. That way there will be
1016         // no possibility of returning without `unsafe`.
1017         struct __InitOk;
1018         // Get the data about fields from the supplied type.
1019         let data = unsafe {
1020             use $crate::init::__internal::$has_data;
1021             $t$(::<$($generics),*>)?::$get_data()
1022         };
1023         // Ensure that `data` really is of type `$data` and help with type inference:
1024         let init = $crate::init::__internal::$data::make_closure::<_, __InitOk, $err>(
1025             data,
1026             move |slot| {
1027                 {
1028                     // Shadow the structure so it cannot be used to return early.
1029                     struct __InitOk;
1030                     // Create the `this` so it can be referenced by the user inside of the
1031                     // expressions creating the individual fields.
1032                     $(let $this = unsafe { ::core::ptr::NonNull::new_unchecked(slot) };)?
1033                     // Initialize every field.
1034                     $crate::__init_internal!(init_slot($($use_data)?):
1035                         @data(data),
1036                         @slot(slot),
1037                         @munch_fields($($fields)*,),
1038                     );
1039                     // We use unreachable code to ensure that all fields have been mentioned exactly
1040                     // once, this struct initializer will still be type-checked and complain with a
1041                     // very natural error message if a field is forgotten/mentioned more than once.
1042                     #[allow(unreachable_code, clippy::diverging_sub_expression)]
1043                     if false {
1044                         $crate::__init_internal!(make_initializer:
1045                             @slot(slot),
1046                             @type_name($t),
1047                             @munch_fields($($fields)*,),
1048                             @acc(),
1049                         );
1050                     }
1051                     // Forget all guards, since initialization was a success.
1052                     $crate::__init_internal!(forget_guards:
1053                         @munch_fields($($fields)*,),
1054                     );
1055                 }
1056                 Ok(__InitOk)
1057             }
1058         );
1059         let init = move |slot| -> ::core::result::Result<(), $err> {
1060             init(slot).map(|__InitOk| ())
1061         };
1062         let init = unsafe { $crate::init::$construct_closure::<_, $err>(init) };
1063         init
1064     }};
1065     (init_slot($($use_data:ident)?):
1066         @data($data:ident),
1067         @slot($slot:ident),
1068         @munch_fields($(,)?),
1069     ) => {
1070         // Endpoint of munching, no fields are left.
1071     };
1072     (init_slot($use_data:ident): // `use_data` is present, so we use the `data` to init fields.
1073         @data($data:ident),
1074         @slot($slot:ident),
1075         // In-place initialization syntax.
1076         @munch_fields($field:ident <- $val:expr, $($rest:tt)*),
1077     ) => {
1078         let $field = $val;
1079         // Call the initializer.
1080         //
1081         // SAFETY: `slot` is valid, because we are inside of an initializer closure, we
1082         // return when an error/panic occurs.
1083         // We also use the `data` to require the correct trait (`Init` or `PinInit`) for `$field`.
1084         unsafe { $data.$field(::core::ptr::addr_of_mut!((*$slot).$field), $field)? };
1085         // Create the drop guard.
1086         //
1087         // We only give access to `&DropGuard`, so it cannot be forgotten via safe code.
1088         //
1089         // SAFETY: We forget the guard later when initialization has succeeded.
1090         let $field = &unsafe {
1091             $crate::init::__internal::DropGuard::new(::core::ptr::addr_of_mut!((*$slot).$field))
1092         };
1093 
1094         $crate::__init_internal!(init_slot($use_data):
1095             @data($data),
1096             @slot($slot),
1097             @munch_fields($($rest)*),
1098         );
1099     };
1100     (init_slot(): // No `use_data`, so we use `Init::__init` directly.
1101         @data($data:ident),
1102         @slot($slot:ident),
1103         // In-place initialization syntax.
1104         @munch_fields($field:ident <- $val:expr, $($rest:tt)*),
1105     ) => {
1106         let $field = $val;
1107         // Call the initializer.
1108         //
1109         // SAFETY: `slot` is valid, because we are inside of an initializer closure, we
1110         // return when an error/panic occurs.
1111         unsafe { $crate::init::Init::__init($field, ::core::ptr::addr_of_mut!((*$slot).$field))? };
1112         // Create the drop guard.
1113         //
1114         // We only give access to `&DropGuard`, so it cannot be forgotten via safe code.
1115         //
1116         // SAFETY: We forget the guard later when initialization has succeeded.
1117         let $field = &unsafe {
1118             $crate::init::__internal::DropGuard::new(::core::ptr::addr_of_mut!((*$slot).$field))
1119         };
1120 
1121         $crate::__init_internal!(init_slot():
1122             @data($data),
1123             @slot($slot),
1124             @munch_fields($($rest)*),
1125         );
1126     };
1127     (init_slot($($use_data:ident)?):
1128         @data($data:ident),
1129         @slot($slot:ident),
1130         // Init by-value.
1131         @munch_fields($field:ident $(: $val:expr)?, $($rest:tt)*),
1132     ) => {
1133         $(let $field = $val;)?
1134         // Initialize the field.
1135         //
1136         // SAFETY: The memory at `slot` is uninitialized.
1137         unsafe { ::core::ptr::write(::core::ptr::addr_of_mut!((*$slot).$field), $field) };
1138         // Create the drop guard:
1139         //
1140         // We only give access to `&DropGuard`, so it cannot be accidentally forgotten.
1141         //
1142         // SAFETY: We forget the guard later when initialization has succeeded.
1143         let $field = &unsafe {
1144             $crate::init::__internal::DropGuard::new(::core::ptr::addr_of_mut!((*$slot).$field))
1145         };
1146 
1147         $crate::__init_internal!(init_slot($($use_data)?):
1148             @data($data),
1149             @slot($slot),
1150             @munch_fields($($rest)*),
1151         );
1152     };
1153     (make_initializer:
1154         @slot($slot:ident),
1155         @type_name($t:ident),
1156         @munch_fields($(,)?),
1157         @acc($($acc:tt)*),
1158     ) => {
1159         // Endpoint, nothing more to munch, create the initializer.
1160         // Since we are in the `if false` branch, this will never get executed. We abuse `slot` to
1161         // get the correct type inference here:
1162         unsafe {
1163             ::core::ptr::write($slot, $t {
1164                 $($acc)*
1165             });
1166         }
1167     };
1168     (make_initializer:
1169         @slot($slot:ident),
1170         @type_name($t:ident),
1171         @munch_fields($field:ident <- $val:expr, $($rest:tt)*),
1172         @acc($($acc:tt)*),
1173     ) => {
1174         $crate::__init_internal!(make_initializer:
1175             @slot($slot),
1176             @type_name($t),
1177             @munch_fields($($rest)*),
1178             @acc($($acc)* $field: ::core::panic!(),),
1179         );
1180     };
1181     (make_initializer:
1182         @slot($slot:ident),
1183         @type_name($t:ident),
1184         @munch_fields($field:ident $(: $val:expr)?, $($rest:tt)*),
1185         @acc($($acc:tt)*),
1186     ) => {
1187         $crate::__init_internal!(make_initializer:
1188             @slot($slot),
1189             @type_name($t),
1190             @munch_fields($($rest)*),
1191             @acc($($acc)* $field: ::core::panic!(),),
1192         );
1193     };
1194     (forget_guards:
1195         @munch_fields($(,)?),
1196     ) => {
1197         // Munching finished.
1198     };
1199     (forget_guards:
1200         @munch_fields($field:ident <- $val:expr, $($rest:tt)*),
1201     ) => {
1202         unsafe { $crate::init::__internal::DropGuard::forget($field) };
1203 
1204         $crate::__init_internal!(forget_guards:
1205             @munch_fields($($rest)*),
1206         );
1207     };
1208     (forget_guards:
1209         @munch_fields($field:ident $(: $val:expr)?, $($rest:tt)*),
1210     ) => {
1211         unsafe { $crate::init::__internal::DropGuard::forget($field) };
1212 
1213         $crate::__init_internal!(forget_guards:
1214             @munch_fields($($rest)*),
1215         );
1216     };
1217 }
1218 
1219 #[doc(hidden)]
1220 #[macro_export]
1221 macro_rules! __derive_zeroable {
1222     (parse_input:
1223         @sig(
1224             $(#[$($struct_attr:tt)*])*
1225             $vis:vis struct $name:ident
1226             $(where $($whr:tt)*)?
1227         ),
1228         @impl_generics($($impl_generics:tt)*),
1229         @ty_generics($($ty_generics:tt)*),
1230         @body({
1231             $(
1232                 $(#[$($field_attr:tt)*])*
1233                 $field:ident : $field_ty:ty
1234             ),* $(,)?
1235         }),
1236     ) => {
1237         // SAFETY: Every field type implements `Zeroable` and padding bytes may be zero.
1238         #[automatically_derived]
1239         unsafe impl<$($impl_generics)*> $crate::init::Zeroable for $name<$($ty_generics)*>
1240         where
1241             $($($whr)*)?
1242         {}
1243         const _: () = {
1244             fn assert_zeroable<T: ?::core::marker::Sized + $crate::init::Zeroable>() {}
1245             fn ensure_zeroable<$($impl_generics)*>()
1246                 where $($($whr)*)?
1247             {
1248                 $(assert_zeroable::<$field_ty>();)*
1249             }
1250         };
1251     };
1252 }
1253