zerocopy_derive/
lib.rs

1// Copyright 2019 The Fuchsia Authors
2//
3// Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0
4// <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT
5// license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
6// This file may not be copied, modified, or distributed except according to
7// those terms.
8
9//! Derive macros for [zerocopy]'s traits.
10//!
11//! [zerocopy]: https://docs.rs/zerocopy
12
13// Sometimes we want to use lints which were added after our MSRV.
14// `unknown_lints` is `warn` by default and we deny warnings in CI, so without
15// this attribute, any unknown lint would cause a CI failure when testing with
16// our MSRV.
17#![allow(unknown_lints)]
18#![deny(renamed_and_removed_lints)]
19#![deny(clippy::all, clippy::missing_safety_doc, clippy::undocumented_unsafe_blocks)]
20#![deny(
21    rustdoc::bare_urls,
22    rustdoc::broken_intra_doc_links,
23    rustdoc::invalid_codeblock_attributes,
24    rustdoc::invalid_html_tags,
25    rustdoc::invalid_rust_codeblocks,
26    rustdoc::missing_crate_level_docs,
27    rustdoc::private_intra_doc_links
28)]
29#![recursion_limit = "128"]
30
31mod r#enum;
32mod ext;
33#[cfg(test)]
34mod output_tests;
35mod repr;
36
37use proc_macro2::{TokenStream, TokenTree};
38use quote::ToTokens;
39
40use {
41    proc_macro2::Span,
42    quote::quote,
43    syn::{
44        parse_quote, Attribute, Data, DataEnum, DataStruct, DataUnion, DeriveInput, Error, Expr,
45        ExprLit, ExprUnary, GenericParam, Ident, Lit, Meta, Path, Type, UnOp, WherePredicate,
46    },
47};
48
49use {crate::ext::*, crate::repr::*};
50
51// TODO(https://github.com/rust-lang/rust/issues/54140): Some errors could be
52// made better if we could add multiple lines of error output like this:
53//
54// error: unsupported representation
55//   --> enum.rs:28:8
56//    |
57// 28 | #[repr(transparent)]
58//    |
59// help: required by the derive of FromBytes
60//
61// Instead, we have more verbose error messages like "unsupported representation
62// for deriving FromZeros, FromBytes, IntoBytes, or Unaligned on an enum"
63//
64// This will probably require Span::error
65// (https://doc.rust-lang.org/nightly/proc_macro/struct.Span.html#method.error),
66// which is currently unstable. Revisit this once it's stable.
67
68/// Defines a derive function named `$outer` which parses its input
69/// `TokenStream` as a `DeriveInput` and then invokes the `$inner` function.
70///
71/// Note that the separate `$outer` parameter is required - proc macro functions
72/// are currently required to live at the crate root, and so the caller must
73/// specify the name in order to avoid name collisions.
74macro_rules! derive {
75    ($trait:ident => $outer:ident => $inner:ident) => {
76        #[proc_macro_derive($trait, attributes(zerocopy))]
77        pub fn $outer(ts: proc_macro::TokenStream) -> proc_macro::TokenStream {
78            let ast = syn::parse_macro_input!(ts as DeriveInput);
79            let zerocopy_crate = match extract_zerocopy_crate(&ast.attrs) {
80                Ok(zerocopy_crate) => zerocopy_crate,
81                Err(e) => return e.into_compile_error().into(),
82            };
83            $inner(&ast, Trait::$trait, &zerocopy_crate).into_ts().into()
84        }
85    };
86}
87
88trait IntoTokenStream {
89    fn into_ts(self) -> TokenStream;
90}
91
92impl IntoTokenStream for TokenStream {
93    fn into_ts(self) -> TokenStream {
94        self
95    }
96}
97
98impl IntoTokenStream for Result<TokenStream, Error> {
99    fn into_ts(self) -> TokenStream {
100        match self {
101            Ok(ts) => ts,
102            Err(err) => err.to_compile_error(),
103        }
104    }
105}
106
107/// Attempt to extract a crate path from the provided attributes. Defaults to `::zerocopy` if not
108/// found.
109fn extract_zerocopy_crate(attrs: &[Attribute]) -> Result<Path, Error> {
110    let mut path = parse_quote!(::zerocopy);
111
112    for attr in attrs {
113        if let Meta::List(ref meta_list) = attr.meta {
114            if meta_list.path.is_ident("zerocopy") {
115                attr.parse_nested_meta(|meta| {
116                    if meta.path.is_ident("crate") {
117                        let expr = meta.value().and_then(|value| value.parse());
118                        if let Ok(Expr::Lit(ExprLit { lit: Lit::Str(lit), .. })) = expr {
119                            if let Ok(path_lit) = lit.parse() {
120                                path = path_lit;
121                                return Ok(());
122                            }
123                        }
124
125                        return Err(Error::new(
126                            Span::call_site(),
127                            "`crate` attribute requires a path as the value",
128                        ));
129                    }
130
131                    Err(Error::new(
132                        Span::call_site(),
133                        format!("unknown attribute encountered: {}", meta.path.into_token_stream()),
134                    ))
135                })?;
136            }
137        }
138    }
139
140    Ok(path)
141}
142
143derive!(KnownLayout => derive_known_layout => derive_known_layout_inner);
144derive!(Immutable => derive_no_cell => derive_no_cell_inner);
145derive!(TryFromBytes => derive_try_from_bytes => derive_try_from_bytes_inner);
146derive!(FromZeros => derive_from_zeros => derive_from_zeros_inner);
147derive!(FromBytes => derive_from_bytes => derive_from_bytes_inner);
148derive!(IntoBytes => derive_into_bytes => derive_into_bytes_inner);
149derive!(Unaligned => derive_unaligned => derive_unaligned_inner);
150derive!(ByteHash => derive_hash => derive_hash_inner);
151derive!(ByteEq => derive_eq => derive_eq_inner);
152derive!(SplitAt => derive_split_at => derive_split_at_inner);
153
154/// Deprecated: prefer [`FromZeros`] instead.
155#[deprecated(since = "0.8.0", note = "`FromZeroes` was renamed to `FromZeros`")]
156#[doc(hidden)]
157#[proc_macro_derive(FromZeroes)]
158pub fn derive_from_zeroes(ts: proc_macro::TokenStream) -> proc_macro::TokenStream {
159    derive_from_zeros(ts)
160}
161
162/// Deprecated: prefer [`IntoBytes`] instead.
163#[deprecated(since = "0.8.0", note = "`AsBytes` was renamed to `IntoBytes`")]
164#[doc(hidden)]
165#[proc_macro_derive(AsBytes)]
166pub fn derive_as_bytes(ts: proc_macro::TokenStream) -> proc_macro::TokenStream {
167    derive_into_bytes(ts)
168}
169
170fn derive_known_layout_inner(
171    ast: &DeriveInput,
172    _top_level: Trait,
173    zerocopy_crate: &Path,
174) -> Result<TokenStream, Error> {
175    let is_repr_c_struct = match &ast.data {
176        Data::Struct(..) => {
177            let repr = StructUnionRepr::from_attrs(&ast.attrs)?;
178            if repr.is_c() {
179                Some(repr)
180            } else {
181                None
182            }
183        }
184        Data::Enum(..) | Data::Union(..) => None,
185    };
186
187    let fields = ast.data.fields();
188
189    let (self_bounds, inner_extras, outer_extras) = if let (
190        Some(repr),
191        Some((trailing_field, leading_fields)),
192    ) = (is_repr_c_struct, fields.split_last())
193    {
194        let (_vis, trailing_field_name, trailing_field_ty) = trailing_field;
195        let leading_fields_tys = leading_fields.iter().map(|(_vis, _name, ty)| ty);
196
197        let core_path = quote!(#zerocopy_crate::util::macro_util::core_reexport);
198        let repr_align = repr
199            .get_align()
200            .map(|align| {
201                let align = align.t.get();
202                quote!(#core_path::num::NonZeroUsize::new(#align as usize))
203            })
204            .unwrap_or_else(|| quote!(#core_path::option::Option::None));
205        let repr_packed = repr
206            .get_packed()
207            .map(|packed| {
208                let packed = packed.get();
209                quote!(#core_path::num::NonZeroUsize::new(#packed as usize))
210            })
211            .unwrap_or_else(|| quote!(#core_path::option::Option::None));
212
213        let make_methods = |trailing_field_ty| {
214            quote! {
215                // SAFETY:
216                // - The returned pointer has the same address and provenance as
217                //   `bytes`:
218                //   - The recursive call to `raw_from_ptr_len` preserves both
219                //     address and provenance.
220                //   - The `as` cast preserves both address and provenance.
221                //   - `NonNull::new_unchecked` preserves both address and
222                //     provenance.
223                // - If `Self` is a slice DST, the returned pointer encodes
224                //   `elems` elements in the trailing slice:
225                //   - This is true of the recursive call to `raw_from_ptr_len`.
226                //   - `trailing.as_ptr() as *mut Self` preserves trailing slice
227                //     element count [1].
228                //   - `NonNull::new_unchecked` preserves trailing slice element
229                //     count.
230                //
231                // [1] Per https://doc.rust-lang.org/reference/expressions/operator-expr.html#pointer-to-pointer-cast:
232                //
233                //   `*const T`` / `*mut T` can be cast to `*const U` / `*mut U`
234                //   with the following behavior:
235                //     ...
236                //     - If `T` and `U` are both unsized, the pointer is also
237                //       returned unchanged. In particular, the metadata is
238                //       preserved exactly.
239                //
240                //       For instance, a cast from `*const [T]` to `*const [U]`
241                //       preserves the number of elements. ... The same holds
242                //       for str and any compound type whose unsized tail is a
243                //       slice type, such as struct `Foo(i32, [u8])` or `(u64, Foo)`.
244                #[inline(always)]
245                fn raw_from_ptr_len(
246                    bytes: #zerocopy_crate::util::macro_util::core_reexport::ptr::NonNull<u8>,
247                    meta: Self::PointerMetadata,
248                ) -> #zerocopy_crate::util::macro_util::core_reexport::ptr::NonNull<Self> {
249                    use #zerocopy_crate::KnownLayout;
250                    let trailing = <#trailing_field_ty as KnownLayout>::raw_from_ptr_len(bytes, meta);
251                    let slf = trailing.as_ptr() as *mut Self;
252                    // SAFETY: Constructed from `trailing`, which is non-null.
253                    unsafe { #zerocopy_crate::util::macro_util::core_reexport::ptr::NonNull::new_unchecked(slf) }
254                }
255
256                #[inline(always)]
257                fn pointer_to_metadata(ptr: *mut Self) -> Self::PointerMetadata {
258                    <#trailing_field_ty>::pointer_to_metadata(ptr as *mut _)
259                }
260            }
261        };
262
263        let inner_extras = {
264            let leading_fields_tys = leading_fields_tys.clone();
265            let methods = make_methods(*trailing_field_ty);
266            let (_, ty_generics, _) = ast.generics.split_for_impl();
267
268            quote!(
269                type PointerMetadata = <#trailing_field_ty as #zerocopy_crate::KnownLayout>::PointerMetadata;
270
271                type MaybeUninit = __ZerocopyKnownLayoutMaybeUninit #ty_generics;
272
273                // SAFETY: `LAYOUT` accurately describes the layout of `Self`.
274                // The layout of `Self` is reflected using a sequence of
275                // invocations of `DstLayout::{new_zst,extend,pad_to_align}`.
276                // The documentation of these items vows that invocations in
277                // this manner will acurately describe a type, so long as:
278                //
279                //  - that type is `repr(C)`,
280                //  - its fields are enumerated in the order they appear,
281                //  - the presence of `repr_align` and `repr_packed` are correctly accounted for.
282                //
283                // We respect all three of these preconditions here. This
284                // expansion is only used if `is_repr_c_struct`, we enumerate
285                // the fields in order, and we extract the values of `align(N)`
286                // and `packed(N)`.
287                const LAYOUT: #zerocopy_crate::DstLayout = {
288                    use #zerocopy_crate::util::macro_util::core_reexport::num::NonZeroUsize;
289                    use #zerocopy_crate::{DstLayout, KnownLayout};
290
291                    let repr_align = #repr_align;
292                    let repr_packed = #repr_packed;
293
294                    DstLayout::new_zst(repr_align)
295                        #(.extend(DstLayout::for_type::<#leading_fields_tys>(), repr_packed))*
296                        .extend(<#trailing_field_ty as KnownLayout>::LAYOUT, repr_packed)
297                        .pad_to_align()
298                };
299
300                #methods
301            )
302        };
303
304        let outer_extras = {
305            let ident = &ast.ident;
306            let vis = &ast.vis;
307            let params = &ast.generics.params;
308            let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl();
309
310            let predicates = if let Some(where_clause) = where_clause {
311                where_clause.predicates.clone()
312            } else {
313                Default::default()
314            };
315
316            // Generate a valid ident for a type-level handle to a field of a
317            // given `name`.
318            let field_index =
319                |name| Ident::new(&format!("__Zerocopy_Field_{}", name), ident.span());
320
321            let field_indices: Vec<_> =
322                fields.iter().map(|(_vis, name, _ty)| field_index(name)).collect();
323
324            // Define the collection of type-level field handles.
325            let field_defs = field_indices.iter().zip(&fields).map(|(idx, (vis, _, _))| {
326                quote! {
327                    #[allow(non_camel_case_types)]
328                    #vis struct #idx;
329                }
330            });
331
332            let field_impls = field_indices.iter().zip(&fields).map(|(idx, (_, _, ty))| quote! {
333                // SAFETY: `#ty` is the type of `#ident`'s field at `#idx`.
334                unsafe impl #impl_generics #zerocopy_crate::util::macro_util::Field<#idx> for #ident #ty_generics
335                where
336                    #predicates
337                {
338                    type Type = #ty;
339                }
340            });
341
342            let trailing_field_index = field_index(trailing_field_name);
343            let leading_field_indices =
344                leading_fields.iter().map(|(_vis, name, _ty)| field_index(name));
345
346            let trailing_field_ty = quote! {
347                <#ident #ty_generics as
348                    #zerocopy_crate::util::macro_util::Field<#trailing_field_index>
349                >::Type
350            };
351
352            let methods = make_methods(&parse_quote! {
353                <#trailing_field_ty as #zerocopy_crate::KnownLayout>::MaybeUninit
354            });
355
356            quote! {
357                #(#field_defs)*
358
359                #(#field_impls)*
360
361                // SAFETY: This has the same layout as the derive target type,
362                // except that it admits uninit bytes. This is ensured by using
363                // the same repr as the target type, and by using field types
364                // which have the same layout as the target type's fields,
365                // except that they admit uninit bytes. We indirect through
366                // `Field` to ensure that occurrences of `Self` resolve to
367                // `#ty`, not `__ZerocopyKnownLayoutMaybeUninit` (see #2116).
368                #repr
369                #[doc(hidden)]
370                // Required on some rustc versions due to a lint that is only
371                // triggered when `derive(KnownLayout)` is applied to `repr(C)`
372                // structs that are generated by macros. See #2177 for details.
373                #[allow(private_bounds)]
374                #vis struct __ZerocopyKnownLayoutMaybeUninit<#params> (
375                    #(#zerocopy_crate::util::macro_util::core_reexport::mem::MaybeUninit<
376                        <#ident #ty_generics as
377                            #zerocopy_crate::util::macro_util::Field<#leading_field_indices>
378                        >::Type
379                    >,)*
380                    // NOTE(#2302): We wrap in `ManuallyDrop` here in case the
381                    // type we're operating on is both generic and
382                    // `repr(packed)`. In that case, Rust needs to know that the
383                    // type is *either* `Sized` or has a trivial `Drop`.
384                    // `ManuallyDrop` has a trivial `Drop`, and so satisfies
385                    // this requirement.
386                    #zerocopy_crate::util::macro_util::core_reexport::mem::ManuallyDrop<
387                        <#trailing_field_ty as #zerocopy_crate::KnownLayout>::MaybeUninit
388                    >
389                )
390                where
391                    #trailing_field_ty: #zerocopy_crate::KnownLayout,
392                    #predicates;
393
394                // SAFETY: We largely defer to the `KnownLayout` implementation on
395                // the derive target type (both by using the same tokens, and by
396                // deferring to impl via type-level indirection). This is sound,
397                // since  `__ZerocopyKnownLayoutMaybeUninit` is guaranteed to
398                // have the same layout as the derive target type, except that
399                // `__ZerocopyKnownLayoutMaybeUninit` admits uninit bytes.
400                unsafe impl #impl_generics #zerocopy_crate::KnownLayout for __ZerocopyKnownLayoutMaybeUninit #ty_generics
401                where
402                    #trailing_field_ty: #zerocopy_crate::KnownLayout,
403                    #predicates
404                {
405                    #[allow(clippy::missing_inline_in_public_items)]
406                    fn only_derive_is_allowed_to_implement_this_trait() {}
407
408                    type PointerMetadata = <#ident #ty_generics as #zerocopy_crate::KnownLayout>::PointerMetadata;
409
410                    type MaybeUninit = Self;
411
412                    const LAYOUT: #zerocopy_crate::DstLayout = <#ident #ty_generics as #zerocopy_crate::KnownLayout>::LAYOUT;
413
414                    #methods
415                }
416            }
417        };
418
419        (SelfBounds::None, inner_extras, Some(outer_extras))
420    } else {
421        // For enums, unions, and non-`repr(C)` structs, we require that
422        // `Self` is sized, and as a result don't need to reason about the
423        // internals of the type.
424        (
425            SelfBounds::SIZED,
426            quote!(
427                type PointerMetadata = ();
428                type MaybeUninit =
429                    #zerocopy_crate::util::macro_util::core_reexport::mem::MaybeUninit<Self>;
430
431                // SAFETY: `LAYOUT` is guaranteed to accurately describe the
432                // layout of `Self`, because that is the documented safety
433                // contract of `DstLayout::for_type`.
434                const LAYOUT: #zerocopy_crate::DstLayout = #zerocopy_crate::DstLayout::for_type::<Self>();
435
436                // SAFETY: `.cast` preserves address and provenance.
437                //
438                // TODO(#429): Add documentation to `.cast` that promises that
439                // it preserves provenance.
440                #[inline(always)]
441                fn raw_from_ptr_len(
442                    bytes: #zerocopy_crate::util::macro_util::core_reexport::ptr::NonNull<u8>,
443                    _meta: (),
444                ) -> #zerocopy_crate::util::macro_util::core_reexport::ptr::NonNull<Self>
445                {
446                    bytes.cast::<Self>()
447                }
448
449                #[inline(always)]
450                fn pointer_to_metadata(_ptr: *mut Self) -> () {}
451            ),
452            None,
453        )
454    };
455
456    Ok(match &ast.data {
457        Data::Struct(strct) => {
458            let require_trait_bound_on_field_types = if self_bounds == SelfBounds::SIZED {
459                FieldBounds::None
460            } else {
461                FieldBounds::TRAILING_SELF
462            };
463
464            // A bound on the trailing field is required, since structs are
465            // unsized if their trailing field is unsized. Reflecting the layout
466            // of an usized trailing field requires that the field is
467            // `KnownLayout`.
468            ImplBlockBuilder::new(
469                ast,
470                strct,
471                Trait::KnownLayout,
472                require_trait_bound_on_field_types,
473                zerocopy_crate,
474            )
475            .self_type_trait_bounds(self_bounds)
476            .inner_extras(inner_extras)
477            .outer_extras(outer_extras)
478            .build()
479        }
480        Data::Enum(enm) => {
481            // A bound on the trailing field is not required, since enums cannot
482            // currently be unsized.
483            ImplBlockBuilder::new(ast, enm, Trait::KnownLayout, FieldBounds::None, zerocopy_crate)
484                .self_type_trait_bounds(SelfBounds::SIZED)
485                .inner_extras(inner_extras)
486                .outer_extras(outer_extras)
487                .build()
488        }
489        Data::Union(unn) => {
490            // A bound on the trailing field is not required, since unions
491            // cannot currently be unsized.
492            ImplBlockBuilder::new(ast, unn, Trait::KnownLayout, FieldBounds::None, zerocopy_crate)
493                .self_type_trait_bounds(SelfBounds::SIZED)
494                .inner_extras(inner_extras)
495                .outer_extras(outer_extras)
496                .build()
497        }
498    })
499}
500
501fn derive_no_cell_inner(
502    ast: &DeriveInput,
503    _top_level: Trait,
504    zerocopy_crate: &Path,
505) -> TokenStream {
506    match &ast.data {
507        Data::Struct(strct) => ImplBlockBuilder::new(
508            ast,
509            strct,
510            Trait::Immutable,
511            FieldBounds::ALL_SELF,
512            zerocopy_crate,
513        )
514        .build(),
515        Data::Enum(enm) => {
516            ImplBlockBuilder::new(ast, enm, Trait::Immutable, FieldBounds::ALL_SELF, zerocopy_crate)
517                .build()
518        }
519        Data::Union(unn) => {
520            ImplBlockBuilder::new(ast, unn, Trait::Immutable, FieldBounds::ALL_SELF, zerocopy_crate)
521                .build()
522        }
523    }
524}
525
526fn derive_try_from_bytes_inner(
527    ast: &DeriveInput,
528    top_level: Trait,
529    zerocopy_crate: &Path,
530) -> Result<TokenStream, Error> {
531    match &ast.data {
532        Data::Struct(strct) => derive_try_from_bytes_struct(ast, strct, top_level, zerocopy_crate),
533        Data::Enum(enm) => derive_try_from_bytes_enum(ast, enm, top_level, zerocopy_crate),
534        Data::Union(unn) => Ok(derive_try_from_bytes_union(ast, unn, top_level, zerocopy_crate)),
535    }
536}
537
538fn derive_from_zeros_inner(
539    ast: &DeriveInput,
540    top_level: Trait,
541    zerocopy_crate: &Path,
542) -> Result<TokenStream, Error> {
543    let try_from_bytes = derive_try_from_bytes_inner(ast, top_level, zerocopy_crate)?;
544    let from_zeros = match &ast.data {
545        Data::Struct(strct) => derive_from_zeros_struct(ast, strct, zerocopy_crate),
546        Data::Enum(enm) => derive_from_zeros_enum(ast, enm, zerocopy_crate)?,
547        Data::Union(unn) => derive_from_zeros_union(ast, unn, zerocopy_crate),
548    };
549    Ok(IntoIterator::into_iter([try_from_bytes, from_zeros]).collect())
550}
551
552fn derive_from_bytes_inner(
553    ast: &DeriveInput,
554    top_level: Trait,
555    zerocopy_crate: &Path,
556) -> Result<TokenStream, Error> {
557    let from_zeros = derive_from_zeros_inner(ast, top_level, zerocopy_crate)?;
558    let from_bytes = match &ast.data {
559        Data::Struct(strct) => derive_from_bytes_struct(ast, strct, zerocopy_crate),
560        Data::Enum(enm) => derive_from_bytes_enum(ast, enm, zerocopy_crate)?,
561        Data::Union(unn) => derive_from_bytes_union(ast, unn, zerocopy_crate),
562    };
563
564    Ok(IntoIterator::into_iter([from_zeros, from_bytes]).collect())
565}
566
567fn derive_into_bytes_inner(
568    ast: &DeriveInput,
569    _top_level: Trait,
570    zerocopy_crate: &Path,
571) -> Result<TokenStream, Error> {
572    match &ast.data {
573        Data::Struct(strct) => derive_into_bytes_struct(ast, strct, zerocopy_crate),
574        Data::Enum(enm) => derive_into_bytes_enum(ast, enm, zerocopy_crate),
575        Data::Union(unn) => derive_into_bytes_union(ast, unn, zerocopy_crate),
576    }
577}
578
579fn derive_unaligned_inner(
580    ast: &DeriveInput,
581    _top_level: Trait,
582    zerocopy_crate: &Path,
583) -> Result<TokenStream, Error> {
584    match &ast.data {
585        Data::Struct(strct) => derive_unaligned_struct(ast, strct, zerocopy_crate),
586        Data::Enum(enm) => derive_unaligned_enum(ast, enm, zerocopy_crate),
587        Data::Union(unn) => derive_unaligned_union(ast, unn, zerocopy_crate),
588    }
589}
590
591fn derive_hash_inner(
592    ast: &DeriveInput,
593    _top_level: Trait,
594    zerocopy_crate: &Path,
595) -> Result<TokenStream, Error> {
596    // This doesn't delegate to `impl_block` because `impl_block` assumes it is deriving a
597    // `zerocopy`-defined trait, and these trait impls share a common shape that `Hash` does not.
598    // In particular, `zerocopy` traits contain a method that only `zerocopy_derive` macros
599    // are supposed to implement, and `impl_block` generating this trait method is incompatible
600    // with `Hash`.
601    let type_ident = &ast.ident;
602    let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl();
603    let where_predicates = where_clause.map(|clause| &clause.predicates);
604    Ok(quote! {
605        // TODO(#553): Add a test that generates a warning when
606        // `#[allow(deprecated)]` isn't present.
607        #[allow(deprecated)]
608        // While there are not currently any warnings that this suppresses (that
609        // we're aware of), it's good future-proofing hygiene.
610        #[automatically_derived]
611        impl #impl_generics #zerocopy_crate::util::macro_util::core_reexport::hash::Hash for #type_ident #ty_generics
612        where
613            Self: #zerocopy_crate::IntoBytes + #zerocopy_crate::Immutable,
614            #where_predicates
615        {
616            fn hash<H>(&self, state: &mut H)
617            where
618                H: #zerocopy_crate::util::macro_util::core_reexport::hash::Hasher,
619            {
620                #zerocopy_crate::util::macro_util::core_reexport::hash::Hasher::write(
621                    state,
622                    #zerocopy_crate::IntoBytes::as_bytes(self)
623                )
624            }
625
626            fn hash_slice<H>(data: &[Self], state: &mut H)
627            where
628                H: #zerocopy_crate::util::macro_util::core_reexport::hash::Hasher,
629            {
630                #zerocopy_crate::util::macro_util::core_reexport::hash::Hasher::write(
631                    state,
632                    #zerocopy_crate::IntoBytes::as_bytes(data)
633                )
634            }
635        }
636    })
637}
638
639fn derive_eq_inner(
640    ast: &DeriveInput,
641    _top_level: Trait,
642    zerocopy_crate: &Path,
643) -> Result<TokenStream, Error> {
644    // This doesn't delegate to `impl_block` because `impl_block` assumes it is deriving a
645    // `zerocopy`-defined trait, and these trait impls share a common shape that `Eq` does not.
646    // In particular, `zerocopy` traits contain a method that only `zerocopy_derive` macros
647    // are supposed to implement, and `impl_block` generating this trait method is incompatible
648    // with `Eq`.
649    let type_ident = &ast.ident;
650    let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl();
651    let where_predicates = where_clause.map(|clause| &clause.predicates);
652    Ok(quote! {
653        // TODO(#553): Add a test that generates a warning when
654        // `#[allow(deprecated)]` isn't present.
655        #[allow(deprecated)]
656        // While there are not currently any warnings that this suppresses (that
657        // we're aware of), it's good future-proofing hygiene.
658        #[automatically_derived]
659        impl #impl_generics #zerocopy_crate::util::macro_util::core_reexport::cmp::PartialEq for #type_ident #ty_generics
660        where
661            Self: #zerocopy_crate::IntoBytes + #zerocopy_crate::Immutable,
662            #where_predicates
663        {
664            fn eq(&self, other: &Self) -> bool {
665                #zerocopy_crate::util::macro_util::core_reexport::cmp::PartialEq::eq(
666                    #zerocopy_crate::IntoBytes::as_bytes(self),
667                    #zerocopy_crate::IntoBytes::as_bytes(other),
668                )
669            }
670        }
671
672        // TODO(#553): Add a test that generates a warning when
673        // `#[allow(deprecated)]` isn't present.
674        #[allow(deprecated)]
675        // While there are not currently any warnings that this suppresses (that
676        // we're aware of), it's good future-proofing hygiene.
677        #[automatically_derived]
678        impl #impl_generics #zerocopy_crate::util::macro_util::core_reexport::cmp::Eq for #type_ident #ty_generics
679        where
680            Self: #zerocopy_crate::IntoBytes + #zerocopy_crate::Immutable,
681            #where_predicates
682        {
683        }
684    })
685}
686
687fn derive_split_at_inner(
688    ast: &DeriveInput,
689    _top_level: Trait,
690    zerocopy_crate: &Path,
691) -> Result<TokenStream, Error> {
692    let repr = StructUnionRepr::from_attrs(&ast.attrs)?;
693
694    match &ast.data {
695        Data::Struct(_) => {}
696        Data::Enum(_) | Data::Union(_) => {
697            return Err(Error::new(Span::call_site(), "can only be applied to structs"));
698        }
699    };
700
701    if repr.get_packed().is_some() {
702        return Err(Error::new(Span::call_site(), "must not have #[repr(packed)] attribute"));
703    }
704
705    let fields = ast.data.fields();
706    let trailing_field = if let Some(((_, _, trailing_field), _)) = fields.split_last() {
707        trailing_field
708    } else {
709        return Err(Error::new(Span::call_site(), "must at least one field"));
710    };
711
712    // SAFETY: `#ty`, per the above check, is not packed; its trailing slice
713    // field is guaranteed to be well-aligned for its type.
714    Ok(ImplBlockBuilder::new(
715        ast,
716        &ast.data,
717        Trait::SplitAt,
718        FieldBounds::TRAILING_SELF,
719        zerocopy_crate,
720    )
721    .inner_extras(quote! {
722        type Elem = <#trailing_field as ::zerocopy::SplitAt>::Elem;
723    })
724    .build())
725}
726
727/// A struct is `TryFromBytes` if:
728/// - all fields are `TryFromBytes`
729fn derive_try_from_bytes_struct(
730    ast: &DeriveInput,
731    strct: &DataStruct,
732    top_level: Trait,
733    zerocopy_crate: &Path,
734) -> Result<TokenStream, Error> {
735    let extras =
736        try_gen_trivial_is_bit_valid(ast, top_level, zerocopy_crate).unwrap_or_else(|| {
737            let fields = strct.fields();
738            let field_names = fields.iter().map(|(_vis, name, _ty)| name);
739            let field_tys = fields.iter().map(|(_vis, _name, ty)| ty);
740            quote!(
741                // SAFETY: We use `is_bit_valid` to validate that each field is
742                // bit-valid, and only return `true` if all of them are. The bit
743                // validity of a struct is just the composition of the bit
744                // validities of its fields, so this is a sound implementation of
745                // `is_bit_valid`.
746                fn is_bit_valid<___ZerocopyAliasing>(
747                    mut candidate: #zerocopy_crate::Maybe<Self, ___ZerocopyAliasing>,
748                ) -> #zerocopy_crate::util::macro_util::core_reexport::primitive::bool
749                where
750                    ___ZerocopyAliasing: #zerocopy_crate::pointer::invariant::Reference,
751                {
752                    use #zerocopy_crate::util::macro_util::core_reexport;
753
754                    true #(&& {
755                        // SAFETY:
756                        // - `project` is a field projection, and so it addresses a
757                        //   subset of the bytes addressed by `slf`
758                        // - ..., and so it preserves provenance
759                        // - ..., and `*slf` is a struct, so `UnsafeCell`s exist at
760                        //   the same byte ranges in the returned pointer's referent
761                        //   as they do in `*slf`
762                        let field_candidate = unsafe {
763                            let project = |slf: core_reexport::ptr::NonNull<Self>| {
764                                let slf = slf.as_ptr();
765                                let field = core_reexport::ptr::addr_of_mut!((*slf).#field_names);
766                                // SAFETY: `cast_unsized_unchecked` promises that
767                                // `slf` will either reference a zero-sized byte
768                                // range, or else will reference a byte range that
769                                // is entirely contained withing an allocated
770                                // object. In either case, this guarantees that
771                                // field projection will not wrap around the address
772                                // space, and so `field` will be non-null.
773                                unsafe { core_reexport::ptr::NonNull::new_unchecked(field) }
774                            };
775
776                            candidate.reborrow().cast_unsized_unchecked(project)
777                        };
778
779                        <#field_tys as #zerocopy_crate::TryFromBytes>::is_bit_valid(field_candidate)
780                    })*
781                }
782            )
783        });
784    Ok(ImplBlockBuilder::new(
785        ast,
786        strct,
787        Trait::TryFromBytes,
788        FieldBounds::ALL_SELF,
789        zerocopy_crate,
790    )
791    .inner_extras(extras)
792    .build())
793}
794
795/// A union is `TryFromBytes` if:
796/// - all of its fields are `TryFromBytes` and `Immutable`
797fn derive_try_from_bytes_union(
798    ast: &DeriveInput,
799    unn: &DataUnion,
800    top_level: Trait,
801    zerocopy_crate: &Path,
802) -> TokenStream {
803    // TODO(#5): Remove the `Immutable` bound.
804    let field_type_trait_bounds =
805        FieldBounds::All(&[TraitBound::Slf, TraitBound::Other(Trait::Immutable)]);
806    let extras =
807        try_gen_trivial_is_bit_valid(ast, top_level, zerocopy_crate).unwrap_or_else(|| {
808            let fields = unn.fields();
809            let field_names = fields.iter().map(|(_vis, name, _ty)| name);
810            let field_tys = fields.iter().map(|(_vis, _name, ty)| ty);
811            quote!(
812                // SAFETY: We use `is_bit_valid` to validate that any field is
813                // bit-valid; we only return `true` if at least one of them is. The
814                // bit validity of a union is not yet well defined in Rust, but it
815                // is guaranteed to be no more strict than this definition. See #696
816                // for a more in-depth discussion.
817                fn is_bit_valid<___ZerocopyAliasing>(
818                    mut candidate: #zerocopy_crate::Maybe<'_, Self,___ZerocopyAliasing>
819                ) -> #zerocopy_crate::util::macro_util::core_reexport::primitive::bool
820                where
821                    ___ZerocopyAliasing: #zerocopy_crate::pointer::invariant::Reference,
822                {
823                    use #zerocopy_crate::util::macro_util::core_reexport;
824
825                    false #(|| {
826                        // SAFETY:
827                        // - `project` is a field projection, and so it addresses a
828                        //   subset of the bytes addressed by `slf`
829                        // - ..., and so it preserves provenance
830                        // - Since `Self: Immutable` is enforced by
831                        //   `self_type_trait_bounds`, neither `*slf` nor the
832                        //   returned pointer's referent contain any `UnsafeCell`s
833                        let field_candidate = unsafe {
834                            let project = |slf: core_reexport::ptr::NonNull<Self>| {
835                                let slf = slf.as_ptr();
836                                let field = core_reexport::ptr::addr_of_mut!((*slf).#field_names);
837                                // SAFETY: `cast_unsized_unchecked` promises that
838                                // `slf` will either reference a zero-sized byte
839                                // range, or else will reference a byte range that
840                                // is entirely contained withing an allocated
841                                // object. In either case, this guarantees that
842                                // field projection will not wrap around the address
843                                // space, and so `field` will be non-null.
844                                unsafe { core_reexport::ptr::NonNull::new_unchecked(field) }
845                            };
846
847                            candidate.reborrow().cast_unsized_unchecked(project)
848                        };
849
850                        <#field_tys as #zerocopy_crate::TryFromBytes>::is_bit_valid(field_candidate)
851                    })*
852                }
853            )
854        });
855    ImplBlockBuilder::new(ast, unn, Trait::TryFromBytes, field_type_trait_bounds, zerocopy_crate)
856        .inner_extras(extras)
857        .build()
858}
859
860fn derive_try_from_bytes_enum(
861    ast: &DeriveInput,
862    enm: &DataEnum,
863    top_level: Trait,
864    zerocopy_crate: &Path,
865) -> Result<TokenStream, Error> {
866    let repr = EnumRepr::from_attrs(&ast.attrs)?;
867
868    // If an enum has no fields, it has a well-defined integer representation,
869    // and every possible bit pattern corresponds to a valid discriminant tag,
870    // then it *could* be `FromBytes` (even if the user hasn't derived
871    // `FromBytes`). This holds if, for `repr(uN)` or `repr(iN)`, there are 2^N
872    // variants.
873    let could_be_from_bytes = enum_size_from_repr(&repr)
874        .map(|size| enm.fields().is_empty() && enm.variants.len() == 1usize << size)
875        .unwrap_or(false);
876
877    let trivial_is_bit_valid = try_gen_trivial_is_bit_valid(ast, top_level, zerocopy_crate);
878    let extra = match (trivial_is_bit_valid, could_be_from_bytes) {
879        (Some(is_bit_valid), _) => is_bit_valid,
880        // SAFETY: It would be sound for the enum to implement `FomBytes`, as
881        // required by `gen_trivial_is_bit_valid_unchecked`.
882        (None, true) => unsafe { gen_trivial_is_bit_valid_unchecked(zerocopy_crate) },
883        (None, false) => {
884            r#enum::derive_is_bit_valid(&ast.ident, &repr, &ast.generics, enm, zerocopy_crate)?
885        }
886    };
887
888    Ok(ImplBlockBuilder::new(ast, enm, Trait::TryFromBytes, FieldBounds::ALL_SELF, zerocopy_crate)
889        .inner_extras(extra)
890        .build())
891}
892
893/// Attempts to generate a `TryFromBytes::is_bit_valid` instance that
894/// unconditionally returns true.
895///
896/// This is possible when the `top_level` trait is `FromBytes` and there are no
897/// generic type parameters. In this case, we know that compilation will succeed
898/// only if the type is unconditionally `FromBytes`. Type parameters are not
899/// supported because a type with type parameters could be `TryFromBytes` but
900/// not `FromBytes` depending on its type parameters, and so deriving a trivial
901/// `is_bit_valid` would be either unsound or, assuming we add a defensive
902/// `Self: FromBytes` bound (as we currently do), overly restrictive. Consider,
903/// for example, that `Foo<bool>` ought to be `TryFromBytes` but not `FromBytes`
904/// in this example:
905///
906/// ```rust,ignore
907/// #[derive(FromBytes)]
908/// #[repr(transparent)]
909/// struct Foo<T>(T);
910/// ```
911///
912/// This should be used where possible. Using this impl is faster to codegen,
913/// faster to compile, and is friendlier on the optimizer.
914fn try_gen_trivial_is_bit_valid(
915    ast: &DeriveInput,
916    top_level: Trait,
917    zerocopy_crate: &Path,
918) -> Option<proc_macro2::TokenStream> {
919    // If the top-level trait is `FromBytes` and `Self` has no type parameters,
920    // then the `FromBytes` derive will fail compilation if `Self` is not
921    // actually soundly `FromBytes`, and so we can rely on that for our
922    // `is_bit_valid` impl. It's plausible that we could make changes - or Rust
923    // could make changes (such as the "trivial bounds" language feature) - that
924    // make this no longer true. To hedge against these, we include an explicit
925    // `Self: FromBytes` check in the generated `is_bit_valid`, which is
926    // bulletproof.
927    if top_level == Trait::FromBytes && ast.generics.params.is_empty() {
928        Some(quote!(
929            // SAFETY: See inline.
930            fn is_bit_valid<___ZerocopyAliasing>(
931                _candidate: #zerocopy_crate::Maybe<Self, ___ZerocopyAliasing>,
932            ) -> #zerocopy_crate::util::macro_util::core_reexport::primitive::bool
933            where
934                ___ZerocopyAliasing: #zerocopy_crate::pointer::invariant::Reference,
935            {
936                if false {
937                    fn assert_is_from_bytes<T>()
938                    where
939                        T: #zerocopy_crate::FromBytes,
940                        T: ?#zerocopy_crate::util::macro_util::core_reexport::marker::Sized,
941                    {
942                    }
943
944                    assert_is_from_bytes::<Self>();
945                }
946
947                // SAFETY: The preceding code only compiles if `Self:
948                // FromBytes`. Thus, this code only compiles if all initialized
949                // byte sequences represent valid instances of `Self`.
950                true
951            }
952        ))
953    } else {
954        None
955    }
956}
957
958/// Generates a `TryFromBytes::is_bit_valid` instance that unconditionally
959/// returns true.
960///
961/// This should be used where possible, (although `try_gen_trivial_is_bit_valid`
962/// should be preferred over this for safety reasons). Using this impl is faster
963/// to codegen, faster to compile, and is friendlier on the optimizer.
964///
965/// # Safety
966///
967/// The caller must ensure that all initialized bit patterns are valid for
968/// `Self`.
969unsafe fn gen_trivial_is_bit_valid_unchecked(zerocopy_crate: &Path) -> proc_macro2::TokenStream {
970    quote!(
971        // SAFETY: The caller of `gen_trivial_is_bit_valid_unchecked` has
972        // promised that all initialized bit patterns are valid for `Self`.
973        fn is_bit_valid<___ZerocopyAliasing>(
974            _candidate: #zerocopy_crate::Maybe<Self, ___ZerocopyAliasing>,
975        ) -> #zerocopy_crate::util::macro_util::core_reexport::primitive::bool
976        where
977            ___ZerocopyAliasing: #zerocopy_crate::pointer::invariant::Reference,
978        {
979            true
980        }
981    )
982}
983
984/// A struct is `FromZeros` if:
985/// - all fields are `FromZeros`
986fn derive_from_zeros_struct(
987    ast: &DeriveInput,
988    strct: &DataStruct,
989    zerocopy_crate: &Path,
990) -> TokenStream {
991    ImplBlockBuilder::new(ast, strct, Trait::FromZeros, FieldBounds::ALL_SELF, zerocopy_crate)
992        .build()
993}
994
995/// Returns `Ok(index)` if variant `index` of the enum has a discriminant of
996/// zero. If `Err(bool)` is returned, the boolean is true if the enum has
997/// unknown discriminants (e.g. discriminants set to const expressions which we
998/// can't evaluate in a proc macro). If the enum has unknown discriminants, then
999/// it might have a zero variant that we just can't detect.
1000fn find_zero_variant(enm: &DataEnum) -> Result<usize, bool> {
1001    // Discriminants can be anywhere in the range [i128::MIN, u128::MAX] because
1002    // the discriminant type may be signed or unsigned. Since we only care about
1003    // tracking the discriminant when it's less than or equal to zero, we can
1004    // avoid u128 -> i128 conversions and bounds checking by making the "next
1005    // discriminant" value implicitly negative.
1006    // Technically 64 bits is enough, but 128 is better for future compatibility
1007    // with https://github.com/rust-lang/rust/issues/56071
1008    let mut next_negative_discriminant = Some(0);
1009
1010    // Sometimes we encounter explicit discriminants that we can't know the
1011    // value of (e.g. a constant expression that requires evaluation). These
1012    // could evaluate to zero or a negative number, but we can't assume that
1013    // they do (no false positives allowed!). So we treat them like strictly-
1014    // positive values that can't result in any zero variants, and track whether
1015    // we've encountered any unknown discriminants.
1016    let mut has_unknown_discriminants = false;
1017
1018    for (i, v) in enm.variants.iter().enumerate() {
1019        match v.discriminant.as_ref() {
1020            // Implicit discriminant
1021            None => {
1022                match next_negative_discriminant.as_mut() {
1023                    Some(0) => return Ok(i),
1024                    // n is nonzero so subtraction is always safe
1025                    Some(n) => *n -= 1,
1026                    None => (),
1027                }
1028            }
1029            // Explicit positive discriminant
1030            Some((_, Expr::Lit(ExprLit { lit: Lit::Int(int), .. }))) => {
1031                match int.base10_parse::<u128>().ok() {
1032                    Some(0) => return Ok(i),
1033                    Some(_) => next_negative_discriminant = None,
1034                    None => {
1035                        // Numbers should never fail to parse, but just in case:
1036                        has_unknown_discriminants = true;
1037                        next_negative_discriminant = None;
1038                    }
1039                }
1040            }
1041            // Explicit negative discriminant
1042            Some((_, Expr::Unary(ExprUnary { op: UnOp::Neg(_), expr, .. }))) => match &**expr {
1043                Expr::Lit(ExprLit { lit: Lit::Int(int), .. }) => {
1044                    match int.base10_parse::<u128>().ok() {
1045                        Some(0) => return Ok(i),
1046                        // x is nonzero so subtraction is always safe
1047                        Some(x) => next_negative_discriminant = Some(x - 1),
1048                        None => {
1049                            // Numbers should never fail to parse, but just in
1050                            // case:
1051                            has_unknown_discriminants = true;
1052                            next_negative_discriminant = None;
1053                        }
1054                    }
1055                }
1056                // Unknown negative discriminant (e.g. const repr)
1057                _ => {
1058                    has_unknown_discriminants = true;
1059                    next_negative_discriminant = None;
1060                }
1061            },
1062            // Unknown discriminant (e.g. const expr)
1063            _ => {
1064                has_unknown_discriminants = true;
1065                next_negative_discriminant = None;
1066            }
1067        }
1068    }
1069
1070    Err(has_unknown_discriminants)
1071}
1072
1073/// An enum is `FromZeros` if:
1074/// - one of the variants has a discriminant of `0`
1075/// - that variant's fields are all `FromZeros`
1076fn derive_from_zeros_enum(
1077    ast: &DeriveInput,
1078    enm: &DataEnum,
1079    zerocopy_crate: &Path,
1080) -> Result<TokenStream, Error> {
1081    let repr = EnumRepr::from_attrs(&ast.attrs)?;
1082
1083    // We don't actually care what the repr is; we just care that it's one of
1084    // the allowed ones.
1085    match repr {
1086         Repr::Compound(
1087            Spanned { t: CompoundRepr::C | CompoundRepr::Primitive(_), span: _ },
1088            _,
1089        ) => {}
1090        Repr::Transparent(_)
1091        | Repr::Compound(Spanned { t: CompoundRepr::Rust, span: _ }, _) => return Err(Error::new(Span::call_site(), "must have #[repr(C)] or #[repr(Int)] attribute in order to guarantee this type's memory layout")),
1092    }
1093
1094    let zero_variant = match find_zero_variant(enm) {
1095        Ok(index) => enm.variants.iter().nth(index).unwrap(),
1096        // Has unknown variants
1097        Err(true) => {
1098            return Err(Error::new_spanned(
1099                ast,
1100                "FromZeros only supported on enums with a variant that has a discriminant of `0`\n\
1101                help: This enum has discriminants which are not literal integers. One of those may \
1102                define or imply which variant has a discriminant of zero. Use a literal integer to \
1103                define or imply the variant with a discriminant of zero.",
1104            ));
1105        }
1106        // Does not have unknown variants
1107        Err(false) => {
1108            return Err(Error::new_spanned(
1109                ast,
1110                "FromZeros only supported on enums with a variant that has a discriminant of `0`",
1111            ));
1112        }
1113    };
1114
1115    let explicit_bounds = zero_variant
1116        .fields
1117        .iter()
1118        .map(|field| {
1119            let ty = &field.ty;
1120            parse_quote! { #ty: #zerocopy_crate::FromZeros }
1121        })
1122        .collect::<Vec<WherePredicate>>();
1123
1124    Ok(ImplBlockBuilder::new(
1125        ast,
1126        enm,
1127        Trait::FromZeros,
1128        FieldBounds::Explicit(explicit_bounds),
1129        zerocopy_crate,
1130    )
1131    .build())
1132}
1133
1134/// Unions are `FromZeros` if
1135/// - all fields are `FromZeros` and `Immutable`
1136fn derive_from_zeros_union(
1137    ast: &DeriveInput,
1138    unn: &DataUnion,
1139    zerocopy_crate: &Path,
1140) -> TokenStream {
1141    // TODO(#5): Remove the `Immutable` bound. It's only necessary for
1142    // compatibility with `derive(TryFromBytes)` on unions; not for soundness.
1143    let field_type_trait_bounds =
1144        FieldBounds::All(&[TraitBound::Slf, TraitBound::Other(Trait::Immutable)]);
1145    ImplBlockBuilder::new(ast, unn, Trait::FromZeros, field_type_trait_bounds, zerocopy_crate)
1146        .build()
1147}
1148
1149/// A struct is `FromBytes` if:
1150/// - all fields are `FromBytes`
1151fn derive_from_bytes_struct(
1152    ast: &DeriveInput,
1153    strct: &DataStruct,
1154    zerocopy_crate: &Path,
1155) -> TokenStream {
1156    ImplBlockBuilder::new(ast, strct, Trait::FromBytes, FieldBounds::ALL_SELF, zerocopy_crate)
1157        .build()
1158}
1159
1160/// An enum is `FromBytes` if:
1161/// - Every possible bit pattern must be valid, which means that every bit
1162///   pattern must correspond to a different enum variant. Thus, for an enum
1163///   whose layout takes up N bytes, there must be 2^N variants.
1164/// - Since we must know N, only representations which guarantee the layout's
1165///   size are allowed. These are `repr(uN)` and `repr(iN)` (`repr(C)` implies an
1166///   implementation-defined size). `usize` and `isize` technically guarantee the
1167///   layout's size, but would require us to know how large those are on the
1168///   target platform. This isn't terribly difficult - we could emit a const
1169///   expression that could call `core::mem::size_of` in order to determine the
1170///   size and check against the number of enum variants, but a) this would be
1171///   platform-specific and, b) even on Rust's smallest bit width platform (32),
1172///   this would require ~4 billion enum variants, which obviously isn't a thing.
1173/// - All fields of all variants are `FromBytes`.
1174fn derive_from_bytes_enum(
1175    ast: &DeriveInput,
1176    enm: &DataEnum,
1177    zerocopy_crate: &Path,
1178) -> Result<TokenStream, Error> {
1179    let repr = EnumRepr::from_attrs(&ast.attrs)?;
1180
1181    let variants_required = 1usize << enum_size_from_repr(&repr)?;
1182    if enm.variants.len() != variants_required {
1183        return Err(Error::new_spanned(
1184            ast,
1185            format!(
1186                "FromBytes only supported on {} enum with {} variants",
1187                repr.repr_type_name(),
1188                variants_required
1189            ),
1190        ));
1191    }
1192
1193    Ok(ImplBlockBuilder::new(ast, enm, Trait::FromBytes, FieldBounds::ALL_SELF, zerocopy_crate)
1194        .build())
1195}
1196
1197// Returns `None` if the enum's size is not guaranteed by the repr.
1198fn enum_size_from_repr(repr: &EnumRepr) -> Result<usize, Error> {
1199    use {CompoundRepr::*, PrimitiveRepr::*, Repr::*};
1200    match repr {
1201        Transparent(span)
1202        | Compound(
1203            Spanned { t: C | Rust | Primitive(U32 | I32 | U64 | I64 | Usize | Isize), span },
1204            _,
1205        ) => Err(Error::new(*span, "`FromBytes` only supported on enums with `#[repr(...)]` attributes `u8`, `i8`, `u16`, or `i16`")),
1206        Compound(Spanned { t: Primitive(U8 | I8), span: _ }, _align) => Ok(8),
1207        Compound(Spanned { t: Primitive(U16 | I16), span: _ }, _align) => Ok(16),
1208    }
1209}
1210
1211/// Unions are `FromBytes` if
1212/// - all fields are `FromBytes` and `Immutable`
1213fn derive_from_bytes_union(
1214    ast: &DeriveInput,
1215    unn: &DataUnion,
1216    zerocopy_crate: &Path,
1217) -> TokenStream {
1218    // TODO(#5): Remove the `Immutable` bound. It's only necessary for
1219    // compatibility with `derive(TryFromBytes)` on unions; not for soundness.
1220    let field_type_trait_bounds =
1221        FieldBounds::All(&[TraitBound::Slf, TraitBound::Other(Trait::Immutable)]);
1222    ImplBlockBuilder::new(ast, unn, Trait::FromBytes, field_type_trait_bounds, zerocopy_crate)
1223        .build()
1224}
1225
1226fn derive_into_bytes_struct(
1227    ast: &DeriveInput,
1228    strct: &DataStruct,
1229    zerocopy_crate: &Path,
1230) -> Result<TokenStream, Error> {
1231    let repr = StructUnionRepr::from_attrs(&ast.attrs)?;
1232
1233    let is_transparent = repr.is_transparent();
1234    let is_c = repr.is_c();
1235    let is_packed_1 = repr.is_packed_1();
1236    let num_fields = strct.fields().len();
1237
1238    let (padding_check, require_unaligned_fields) = if is_transparent || is_packed_1 {
1239        // No padding check needed.
1240        // - repr(transparent): The layout and ABI of the whole struct is the
1241        //   same as its only non-ZST field (meaning there's no padding outside
1242        //   of that field) and we require that field to be `IntoBytes` (meaning
1243        //   there's no padding in that field).
1244        // - repr(packed): Any inter-field padding bytes are removed, meaning
1245        //   that any padding bytes would need to come from the fields, all of
1246        //   which we require to be `IntoBytes` (meaning they don't have any
1247        //   padding). Note that this holds regardless of other `repr`
1248        //   attributes, including `repr(Rust)`. [1]
1249        //
1250        // [1] Per https://doc.rust-lang.org/1.81.0/reference/type-layout.html#the-alignment-modifiers:
1251        //
1252        //   An important consequence of these rules is that a type with
1253        //   `#[repr(packed(1))]`` (or `#[repr(packed)]``) will have no
1254        //   inter-field padding.
1255        (None, false)
1256    } else if is_c && !repr.is_align_gt_1() && num_fields <= 1 {
1257        // No padding check needed. A repr(C) struct with zero or one field has
1258        // no padding unless #[repr(align)] explicitly adds padding, which we
1259        // check for in this branch's condition.
1260        (None, false)
1261    } else if ast.generics.params.is_empty() {
1262        // Since there are no generics, we can emit a padding check. All reprs
1263        // guarantee that fields won't overlap [1], so the padding check is
1264        // sound. This is more permissive than the next case, which requires
1265        // that all field types implement `Unaligned`.
1266        //
1267        // [1] Per https://doc.rust-lang.org/1.81.0/reference/type-layout.html#the-rust-representation:
1268        //
1269        //   The only data layout guarantees made by [`repr(Rust)`] are those
1270        //   required for soundness. They are:
1271        //   ...
1272        //   2. The fields do not overlap.
1273        //   ...
1274        (Some(PaddingCheck::Struct), false)
1275    } else if is_c && !repr.is_align_gt_1() {
1276        // We can't use a padding check since there are generic type arguments.
1277        // Instead, we require all field types to implement `Unaligned`. This
1278        // ensures that the `repr(C)` layout algorithm will not insert any
1279        // padding unless #[repr(align)] explicitly adds padding, which we check
1280        // for in this branch's condition.
1281        //
1282        // TODO(#10): Support type parameters for non-transparent, non-packed
1283        // structs without requiring `Unaligned`.
1284        (None, true)
1285    } else {
1286        return Err(Error::new(Span::call_site(), "must have a non-align #[repr(...)] attribute in order to guarantee this type's memory layout"));
1287    };
1288
1289    let field_bounds = if require_unaligned_fields {
1290        FieldBounds::All(&[TraitBound::Slf, TraitBound::Other(Trait::Unaligned)])
1291    } else {
1292        FieldBounds::ALL_SELF
1293    };
1294
1295    Ok(ImplBlockBuilder::new(ast, strct, Trait::IntoBytes, field_bounds, zerocopy_crate)
1296        .padding_check(padding_check)
1297        .build())
1298}
1299
1300/// If the type is an enum:
1301/// - It must have a defined representation (`repr`s `C`, `u8`, `u16`, `u32`,
1302///   `u64`, `usize`, `i8`, `i16`, `i32`, `i64`, or `isize`).
1303/// - It must have no padding bytes.
1304/// - Its fields must be `IntoBytes`.
1305fn derive_into_bytes_enum(
1306    ast: &DeriveInput,
1307    enm: &DataEnum,
1308    zerocopy_crate: &Path,
1309) -> Result<TokenStream, Error> {
1310    let repr = EnumRepr::from_attrs(&ast.attrs)?;
1311    if !repr.is_c() && !repr.is_primitive() {
1312        return Err(Error::new(Span::call_site(), "must have #[repr(C)] or #[repr(Int)] attribute in order to guarantee this type's memory layout"));
1313    }
1314
1315    let tag_type_definition = r#enum::generate_tag_enum(&repr, enm);
1316    Ok(ImplBlockBuilder::new(ast, enm, Trait::IntoBytes, FieldBounds::ALL_SELF, zerocopy_crate)
1317        .padding_check(PaddingCheck::Enum { tag_type_definition })
1318        .build())
1319}
1320
1321/// A union is `IntoBytes` if:
1322/// - all fields are `IntoBytes`
1323/// - `repr(C)`, `repr(transparent)`, or `repr(packed)`
1324/// - no padding (size of union equals size of each field type)
1325fn derive_into_bytes_union(
1326    ast: &DeriveInput,
1327    unn: &DataUnion,
1328    zerocopy_crate: &Path,
1329) -> Result<TokenStream, Error> {
1330    // See #1792 for more context.
1331    //
1332    // By checking for `zerocopy_derive_union_into_bytes` both here and in the
1333    // generated code, we ensure that `--cfg zerocopy_derive_union_into_bytes`
1334    // need only be passed *either* when compiling this crate *or* when
1335    // compiling the user's crate. The former is preferable, but in some
1336    // situations (such as when cross-compiling using `cargo build --target`),
1337    // it doesn't get propagated to this crate's build by default.
1338    let cfg_compile_error = if cfg!(zerocopy_derive_union_into_bytes) {
1339        quote!()
1340    } else {
1341        let error_message = "requires --cfg zerocopy_derive_union_into_bytes;
1342please let us know you use this feature: https://github.com/google/zerocopy/discussions/1802";
1343        quote!(
1344            const _: () = {
1345                #[cfg(not(zerocopy_derive_union_into_bytes))]
1346                #zerocopy_crate::util::macro_util::core_reexport::compile_error!(#error_message);
1347            };
1348        )
1349    };
1350
1351    // TODO(#10): Support type parameters.
1352    if !ast.generics.params.is_empty() {
1353        return Err(Error::new(Span::call_site(), "unsupported on types with type parameters"));
1354    }
1355
1356    // Because we don't support generics, we don't need to worry about
1357    // special-casing different reprs. So long as there is *some* repr which
1358    // guarantees the layout, our `PaddingCheck::Union` guarantees that there is
1359    // no padding.
1360    let repr = StructUnionRepr::from_attrs(&ast.attrs)?;
1361    if !repr.is_c() && !repr.is_transparent() && !repr.is_packed_1() {
1362        return Err(Error::new(
1363            Span::call_site(),
1364            "must be #[repr(C)], #[repr(packed)], or #[repr(transparent)]",
1365        ));
1366    }
1367
1368    let impl_block =
1369        ImplBlockBuilder::new(ast, unn, Trait::IntoBytes, FieldBounds::ALL_SELF, zerocopy_crate)
1370            .padding_check(PaddingCheck::Union)
1371            .build();
1372    Ok(quote!(#cfg_compile_error #impl_block))
1373}
1374
1375/// A struct is `Unaligned` if:
1376/// - `repr(align)` is no more than 1 and either
1377///   - `repr(C)` or `repr(transparent)` and
1378///     - all fields `Unaligned`
1379///   - `repr(packed)`
1380fn derive_unaligned_struct(
1381    ast: &DeriveInput,
1382    strct: &DataStruct,
1383    zerocopy_crate: &Path,
1384) -> Result<TokenStream, Error> {
1385    let repr = StructUnionRepr::from_attrs(&ast.attrs)?;
1386    repr.unaligned_validate_no_align_gt_1()?;
1387
1388    let field_bounds = if repr.is_packed_1() {
1389        FieldBounds::None
1390    } else if repr.is_c() || repr.is_transparent() {
1391        FieldBounds::ALL_SELF
1392    } else {
1393        return Err(Error::new(Span::call_site(), "must have #[repr(C)], #[repr(transparent)], or #[repr(packed)] attribute in order to guarantee this type's alignment"));
1394    };
1395
1396    Ok(ImplBlockBuilder::new(ast, strct, Trait::Unaligned, field_bounds, zerocopy_crate).build())
1397}
1398
1399/// An enum is `Unaligned` if:
1400/// - No `repr(align(N > 1))`
1401/// - `repr(u8)` or `repr(i8)`
1402fn derive_unaligned_enum(
1403    ast: &DeriveInput,
1404    enm: &DataEnum,
1405    zerocopy_crate: &Path,
1406) -> Result<TokenStream, Error> {
1407    let repr = EnumRepr::from_attrs(&ast.attrs)?;
1408    repr.unaligned_validate_no_align_gt_1()?;
1409
1410    if !repr.is_u8() && !repr.is_i8() {
1411        return Err(Error::new(Span::call_site(), "must have #[repr(u8)] or #[repr(i8)] attribute in order to guarantee this type's alignment"));
1412    }
1413
1414    Ok(ImplBlockBuilder::new(ast, enm, Trait::Unaligned, FieldBounds::ALL_SELF, zerocopy_crate)
1415        .build())
1416}
1417
1418/// Like structs, a union is `Unaligned` if:
1419/// - `repr(align)` is no more than 1 and either
1420///   - `repr(C)` or `repr(transparent)` and
1421///     - all fields `Unaligned`
1422///   - `repr(packed)`
1423fn derive_unaligned_union(
1424    ast: &DeriveInput,
1425    unn: &DataUnion,
1426    zerocopy_crate: &Path,
1427) -> Result<TokenStream, Error> {
1428    let repr = StructUnionRepr::from_attrs(&ast.attrs)?;
1429    repr.unaligned_validate_no_align_gt_1()?;
1430
1431    let field_type_trait_bounds = if repr.is_packed_1() {
1432        FieldBounds::None
1433    } else if repr.is_c() || repr.is_transparent() {
1434        FieldBounds::ALL_SELF
1435    } else {
1436        return Err(Error::new(Span::call_site(), "must have #[repr(C)], #[repr(transparent)], or #[repr(packed)] attribute in order to guarantee this type's alignment"));
1437    };
1438
1439    Ok(ImplBlockBuilder::new(ast, unn, Trait::Unaligned, field_type_trait_bounds, zerocopy_crate)
1440        .build())
1441}
1442
1443/// This enum describes what kind of padding check needs to be generated for the
1444/// associated impl.
1445enum PaddingCheck {
1446    /// Check that the sum of the fields' sizes exactly equals the struct's
1447    /// size.
1448    Struct,
1449    /// Check that the size of each field exactly equals the union's size.
1450    Union,
1451    /// Check that every variant of the enum contains no padding.
1452    ///
1453    /// Because doing so requires a tag enum, this padding check requires an
1454    /// additional `TokenStream` which defines the tag enum as `___ZerocopyTag`.
1455    Enum { tag_type_definition: TokenStream },
1456}
1457
1458impl PaddingCheck {
1459    /// Returns the ident of the macro to call in order to validate that a type
1460    /// passes the padding check encoded by `PaddingCheck`.
1461    fn validator_macro_ident(&self) -> Ident {
1462        let s = match self {
1463            PaddingCheck::Struct => "struct_has_padding",
1464            PaddingCheck::Union => "union_has_padding",
1465            PaddingCheck::Enum { .. } => "enum_has_padding",
1466        };
1467
1468        Ident::new(s, Span::call_site())
1469    }
1470
1471    /// Sometimes performing the padding check requires some additional
1472    /// "context" code. For enums, this is the definition of the tag enum.
1473    fn validator_macro_context(&self) -> Option<&TokenStream> {
1474        match self {
1475            PaddingCheck::Struct | PaddingCheck::Union => None,
1476            PaddingCheck::Enum { tag_type_definition } => Some(tag_type_definition),
1477        }
1478    }
1479}
1480
1481#[derive(Copy, Clone, Debug, Eq, PartialEq)]
1482enum Trait {
1483    KnownLayout,
1484    Immutable,
1485    TryFromBytes,
1486    FromZeros,
1487    FromBytes,
1488    IntoBytes,
1489    Unaligned,
1490    Sized,
1491    ByteHash,
1492    ByteEq,
1493    SplitAt,
1494}
1495
1496impl ToTokens for Trait {
1497    fn to_tokens(&self, tokens: &mut TokenStream) {
1498        // According to [1], the format of the derived `Debug`` output is not
1499        // stable and therefore not guaranteed to represent the variant names.
1500        // Indeed with the (unstable) `fmt-debug` compiler flag [2], it can
1501        // return only a minimalized output or empty string. To make sure this
1502        // code will work in the future and independet of the compiler flag, we
1503        // translate the variants to their names manually here.
1504        //
1505        // [1] https://doc.rust-lang.org/1.81.0/std/fmt/trait.Debug.html#stability
1506        // [2] https://doc.rust-lang.org/beta/unstable-book/compiler-flags/fmt-debug.html
1507        let s = match self {
1508            Trait::KnownLayout => "KnownLayout",
1509            Trait::Immutable => "Immutable",
1510            Trait::TryFromBytes => "TryFromBytes",
1511            Trait::FromZeros => "FromZeros",
1512            Trait::FromBytes => "FromBytes",
1513            Trait::IntoBytes => "IntoBytes",
1514            Trait::Unaligned => "Unaligned",
1515            Trait::Sized => "Sized",
1516            Trait::ByteHash => "ByteHash",
1517            Trait::ByteEq => "ByteEq",
1518            Trait::SplitAt => "SplitAt",
1519        };
1520        let ident = Ident::new(s, Span::call_site());
1521        tokens.extend(core::iter::once(TokenTree::Ident(ident)));
1522    }
1523}
1524
1525impl Trait {
1526    fn crate_path(&self, zerocopy_crate: &Path) -> Path {
1527        match self {
1528            Self::Sized => {
1529                parse_quote!(#zerocopy_crate::util::macro_util::core_reexport::marker::#self)
1530            }
1531            _ => parse_quote!(#zerocopy_crate::#self),
1532        }
1533    }
1534}
1535
1536#[derive(Debug, Eq, PartialEq)]
1537enum TraitBound {
1538    Slf,
1539    Other(Trait),
1540}
1541
1542enum FieldBounds<'a> {
1543    None,
1544    All(&'a [TraitBound]),
1545    Trailing(&'a [TraitBound]),
1546    Explicit(Vec<WherePredicate>),
1547}
1548
1549impl<'a> FieldBounds<'a> {
1550    const ALL_SELF: FieldBounds<'a> = FieldBounds::All(&[TraitBound::Slf]);
1551    const TRAILING_SELF: FieldBounds<'a> = FieldBounds::Trailing(&[TraitBound::Slf]);
1552}
1553
1554#[derive(Debug, Eq, PartialEq)]
1555enum SelfBounds<'a> {
1556    None,
1557    All(&'a [Trait]),
1558}
1559
1560// TODO(https://github.com/rust-lang/rust-clippy/issues/12908): This is a false positive.
1561// Explicit lifetimes are actually necessary here.
1562#[allow(clippy::needless_lifetimes)]
1563impl<'a> SelfBounds<'a> {
1564    const SIZED: Self = Self::All(&[Trait::Sized]);
1565}
1566
1567/// Normalizes a slice of bounds by replacing [`TraitBound::Slf`] with `slf`.
1568fn normalize_bounds(slf: Trait, bounds: &[TraitBound]) -> impl '_ + Iterator<Item = Trait> {
1569    bounds.iter().map(move |bound| match bound {
1570        TraitBound::Slf => slf,
1571        TraitBound::Other(trt) => *trt,
1572    })
1573}
1574
1575struct ImplBlockBuilder<'a, D: DataExt> {
1576    input: &'a DeriveInput,
1577    data: &'a D,
1578    trt: Trait,
1579    field_type_trait_bounds: FieldBounds<'a>,
1580    zerocopy_crate: &'a Path,
1581    self_type_trait_bounds: SelfBounds<'a>,
1582    padding_check: Option<PaddingCheck>,
1583    inner_extras: Option<TokenStream>,
1584    outer_extras: Option<TokenStream>,
1585}
1586
1587impl<'a, D: DataExt> ImplBlockBuilder<'a, D> {
1588    fn new(
1589        input: &'a DeriveInput,
1590        data: &'a D,
1591        trt: Trait,
1592        field_type_trait_bounds: FieldBounds<'a>,
1593        zerocopy_crate: &'a Path,
1594    ) -> Self {
1595        Self {
1596            input,
1597            data,
1598            trt,
1599            field_type_trait_bounds,
1600            zerocopy_crate,
1601            self_type_trait_bounds: SelfBounds::None,
1602            padding_check: None,
1603            inner_extras: None,
1604            outer_extras: None,
1605        }
1606    }
1607
1608    fn self_type_trait_bounds(mut self, self_type_trait_bounds: SelfBounds<'a>) -> Self {
1609        self.self_type_trait_bounds = self_type_trait_bounds;
1610        self
1611    }
1612
1613    fn padding_check<P: Into<Option<PaddingCheck>>>(mut self, padding_check: P) -> Self {
1614        self.padding_check = padding_check.into();
1615        self
1616    }
1617
1618    fn inner_extras(mut self, inner_extras: TokenStream) -> Self {
1619        self.inner_extras = Some(inner_extras);
1620        self
1621    }
1622
1623    fn outer_extras<T: Into<Option<TokenStream>>>(mut self, outer_extras: T) -> Self {
1624        self.outer_extras = outer_extras.into();
1625        self
1626    }
1627
1628    fn build(self) -> TokenStream {
1629        // In this documentation, we will refer to this hypothetical struct:
1630        //
1631        //   #[derive(FromBytes)]
1632        //   struct Foo<T, I: Iterator>
1633        //   where
1634        //       T: Copy,
1635        //       I: Clone,
1636        //       I::Item: Clone,
1637        //   {
1638        //       a: u8,
1639        //       b: T,
1640        //       c: I::Item,
1641        //   }
1642        //
1643        // We extract the field types, which in this case are `u8`, `T`, and
1644        // `I::Item`. We re-use the existing parameters and where clauses. If
1645        // `require_trait_bound == true` (as it is for `FromBytes), we add where
1646        // bounds for each field's type:
1647        //
1648        //   impl<T, I: Iterator> FromBytes for Foo<T, I>
1649        //   where
1650        //       T: Copy,
1651        //       I: Clone,
1652        //       I::Item: Clone,
1653        //       T: FromBytes,
1654        //       I::Item: FromBytes,
1655        //   {
1656        //   }
1657        //
1658        // NOTE: It is standard practice to only emit bounds for the type
1659        // parameters themselves, not for field types based on those parameters
1660        // (e.g., `T` vs `T::Foo`). For a discussion of why this is standard
1661        // practice, see https://github.com/rust-lang/rust/issues/26925.
1662        //
1663        // The reason we diverge from this standard is that doing it that way
1664        // for us would be unsound. E.g., consider a type, `T` where `T:
1665        // FromBytes` but `T::Foo: !FromBytes`. It would not be sound for us to
1666        // accept a type with a `T::Foo` field as `FromBytes` simply because `T:
1667        // FromBytes`.
1668        //
1669        // While there's no getting around this requirement for us, it does have
1670        // the pretty serious downside that, when lifetimes are involved, the
1671        // trait solver ties itself in knots:
1672        //
1673        //     #[derive(Unaligned)]
1674        //     #[repr(C)]
1675        //     struct Dup<'a, 'b> {
1676        //         a: PhantomData<&'a u8>,
1677        //         b: PhantomData<&'b u8>,
1678        //     }
1679        //
1680        //     error[E0283]: type annotations required: cannot resolve `core::marker::PhantomData<&'a u8>: zerocopy::Unaligned`
1681        //      --> src/main.rs:6:10
1682        //       |
1683        //     6 | #[derive(Unaligned)]
1684        //       |          ^^^^^^^^^
1685        //       |
1686        //       = note: required by `zerocopy::Unaligned`
1687
1688        let type_ident = &self.input.ident;
1689        let trait_path = self.trt.crate_path(self.zerocopy_crate);
1690        let fields = self.data.fields();
1691        let variants = self.data.variants();
1692        let tag = self.data.tag();
1693        let zerocopy_crate = self.zerocopy_crate;
1694
1695        fn bound_tt(
1696            ty: &Type,
1697            traits: impl Iterator<Item = Trait>,
1698            zerocopy_crate: &Path,
1699        ) -> WherePredicate {
1700            let traits = traits.map(|t| t.crate_path(zerocopy_crate));
1701            parse_quote!(#ty: #(#traits)+*)
1702        }
1703        let field_type_bounds: Vec<_> = match (self.field_type_trait_bounds, &fields[..]) {
1704            (FieldBounds::All(traits), _) => fields
1705                .iter()
1706                .map(|(_vis, _name, ty)| {
1707                    bound_tt(ty, normalize_bounds(self.trt, traits), zerocopy_crate)
1708                })
1709                .collect(),
1710            (FieldBounds::None, _) | (FieldBounds::Trailing(..), []) => vec![],
1711            (FieldBounds::Trailing(traits), [.., last]) => {
1712                vec![bound_tt(last.2, normalize_bounds(self.trt, traits), zerocopy_crate)]
1713            }
1714            (FieldBounds::Explicit(bounds), _) => bounds,
1715        };
1716
1717        // Don't bother emitting a padding check if there are no fields.
1718        #[allow(unstable_name_collisions)] // See `BoolExt` below
1719        let padding_check_bound = self
1720            .padding_check
1721            .and_then(|check| (!fields.is_empty()).then_some(check))
1722            .map(|check| {
1723                let variant_types = variants.iter().map(|var| {
1724                    let types = var.iter().map(|(_vis, _name, ty)| ty);
1725                    quote!([#(#types),*])
1726                });
1727                let validator_context = check.validator_macro_context();
1728                let validator_macro = check.validator_macro_ident();
1729                let t = tag.iter();
1730                parse_quote! {
1731                    (): #zerocopy_crate::util::macro_util::PaddingFree<
1732                        Self,
1733                        {
1734                            #validator_context
1735                            #zerocopy_crate::#validator_macro!(Self, #(#t,)* #(#variant_types),*)
1736                        }
1737                    >
1738                }
1739            });
1740
1741        let self_bounds: Option<WherePredicate> = match self.self_type_trait_bounds {
1742            SelfBounds::None => None,
1743            SelfBounds::All(traits) => {
1744                Some(bound_tt(&parse_quote!(Self), traits.iter().copied(), zerocopy_crate))
1745            }
1746        };
1747
1748        let bounds = self
1749            .input
1750            .generics
1751            .where_clause
1752            .as_ref()
1753            .map(|where_clause| where_clause.predicates.iter())
1754            .into_iter()
1755            .flatten()
1756            .chain(field_type_bounds.iter())
1757            .chain(padding_check_bound.iter())
1758            .chain(self_bounds.iter());
1759
1760        // The parameters with trait bounds, but without type defaults.
1761        let params = self.input.generics.params.clone().into_iter().map(|mut param| {
1762            match &mut param {
1763                GenericParam::Type(ty) => ty.default = None,
1764                GenericParam::Const(cnst) => cnst.default = None,
1765                GenericParam::Lifetime(_) => {}
1766            }
1767            quote!(#param)
1768        });
1769
1770        // The identifiers of the parameters without trait bounds or type
1771        // defaults.
1772        let param_idents = self.input.generics.params.iter().map(|param| match param {
1773            GenericParam::Type(ty) => {
1774                let ident = &ty.ident;
1775                quote!(#ident)
1776            }
1777            GenericParam::Lifetime(l) => {
1778                let ident = &l.lifetime;
1779                quote!(#ident)
1780            }
1781            GenericParam::Const(cnst) => {
1782                let ident = &cnst.ident;
1783                quote!({#ident})
1784            }
1785        });
1786
1787        let inner_extras = self.inner_extras;
1788        let impl_tokens = quote! {
1789            // TODO(#553): Add a test that generates a warning when
1790            // `#[allow(deprecated)]` isn't present.
1791            #[allow(deprecated)]
1792            // While there are not currently any warnings that this suppresses
1793            // (that we're aware of), it's good future-proofing hygiene.
1794            #[automatically_derived]
1795            unsafe impl < #(#params),* > #trait_path for #type_ident < #(#param_idents),* >
1796            where
1797                #(#bounds,)*
1798            {
1799                fn only_derive_is_allowed_to_implement_this_trait() {}
1800
1801                #inner_extras
1802            }
1803        };
1804
1805        if let Some(outer_extras) = self.outer_extras {
1806            // So that any items defined in `#outer_extras` don't conflict with
1807            // existing names defined in this scope.
1808            quote! {
1809                const _: () = {
1810                    #impl_tokens
1811
1812                    #outer_extras
1813                };
1814            }
1815        } else {
1816            impl_tokens
1817        }
1818    }
1819}
1820
1821// A polyfill for `Option::then_some`, which was added after our MSRV.
1822//
1823// The `#[allow(unused)]` is necessary because, on sufficiently recent toolchain
1824// versions, `b.then_some(...)` resolves to the inherent method rather than to
1825// this trait, and so this trait is considered unused.
1826//
1827// TODO(#67): Remove this once our MSRV is >= 1.62.
1828#[allow(unused)]
1829trait BoolExt {
1830    fn then_some<T>(self, t: T) -> Option<T>;
1831}
1832
1833impl BoolExt for bool {
1834    fn then_some<T>(self, t: T) -> Option<T> {
1835        if self {
1836            Some(t)
1837        } else {
1838            None
1839        }
1840    }
1841}