rustc_builtin_macros/deriving/generic/
mod.rs

1//! Some code that abstracts away much of the boilerplate of writing
2//! `derive` instances for traits. Among other things it manages getting
3//! access to the fields of the 4 different sorts of structs and enum
4//! variants, as well as creating the method and impl ast instances.
5//!
6//! Supported features (fairly exhaustive):
7//!
8//! - Methods taking any number of parameters of any type, and returning
9//!   any type, other than vectors, bottom and closures.
10//! - Generating `impl`s for types with type parameters and lifetimes
11//!   (e.g., `Option<T>`), the parameters are automatically given the
12//!   current trait as a bound. (This includes separate type parameters
13//!   and lifetimes for methods.)
14//! - Additional bounds on the type parameters (`TraitDef.additional_bounds`)
15//!
16//! The most important thing for implementors is the `Substructure` and
17//! `SubstructureFields` objects. The latter groups 5 possibilities of the
18//! arguments:
19//!
20//! - `Struct`, when `Self` is a struct (including tuple structs, e.g
21//!   `struct T(i32, char)`).
22//! - `EnumMatching`, when `Self` is an enum and all the arguments are the
23//!   same variant of the enum (e.g., `Some(1)`, `Some(3)` and `Some(4)`)
24//! - `EnumDiscr` when `Self` is an enum, for comparing the enum discriminants.
25//! - `StaticEnum` and `StaticStruct` for static methods, where the type
26//!   being derived upon is either an enum or struct respectively. (Any
27//!   argument with type Self is just grouped among the non-self
28//!   arguments.)
29//!
30//! In the first two cases, the values from the corresponding fields in
31//! all the arguments are grouped together.
32//!
33//! The non-static cases have `Option<ident>` in several places associated
34//! with field `expr`s. This represents the name of the field it is
35//! associated with. It is only not `None` when the associated field has
36//! an identifier in the source code. For example, the `x`s in the
37//! following snippet
38//!
39//! ```rust
40//! struct A {
41//!     x: i32,
42//! }
43//!
44//! struct B(i32);
45//!
46//! enum C {
47//!     C0(i32),
48//!     C1 { x: i32 }
49//! }
50//! ```
51//!
52//! The `i32`s in `B` and `C0` don't have an identifier, so the
53//! `Option<ident>`s would be `None` for them.
54//!
55//! In the static cases, the structure is summarized, either into the just
56//! spans of the fields or a list of spans and the field idents (for tuple
57//! structs and record structs, respectively), or a list of these, for
58//! enums (one for each variant). For empty struct and empty enum
59//! variants, it is represented as a count of 0.
60//!
61//! # "`cs`" functions
62//!
63//! The `cs_...` functions ("combine substructure") are designed to
64//! make life easier by providing some pre-made recipes for common
65//! threads; mostly calling the function being derived on all the
66//! arguments and then combining them back together in some way (or
67//! letting the user chose that). They are not meant to be the only
68//! way to handle the structures that this code creates.
69//!
70//! # Examples
71//!
72//! The following simplified `PartialEq` is used for in-code examples:
73//!
74//! ```rust
75//! trait PartialEq {
76//!     fn eq(&self, other: &Self) -> bool;
77//! }
78//!
79//! impl PartialEq for i32 {
80//!     fn eq(&self, other: &i32) -> bool {
81//!         *self == *other
82//!     }
83//! }
84//! ```
85//!
86//! Some examples of the values of `SubstructureFields` follow, using the
87//! above `PartialEq`, `A`, `B` and `C`.
88//!
89//! ## Structs
90//!
91//! When generating the `expr` for the `A` impl, the `SubstructureFields` is
92//!
93//! ```text
94//! Struct(vec![FieldInfo {
95//!     span: <span of x>,
96//!     name: Some(<ident of x>),
97//!     self_: <expr for &self.x>,
98//!     other: vec![<expr for &other.x>],
99//! }])
100//! ```
101//!
102//! For the `B` impl, called with `B(a)` and `B(b)`,
103//!
104//! ```text
105//! Struct(vec![FieldInfo {
106//!     span: <span of i32>,
107//!     name: None,
108//!     self_: <expr for &a>,
109//!     other: vec![<expr for &b>],
110//! }])
111//! ```
112//!
113//! ## Enums
114//!
115//! When generating the `expr` for a call with `self == C0(a)` and `other
116//! == C0(b)`, the SubstructureFields is
117//!
118//! ```text
119//! EnumMatching(
120//!     0,
121//!     <ast::Variant for C0>,
122//!     vec![FieldInfo {
123//!         span: <span of i32>,
124//!         name: None,
125//!         self_: <expr for &a>,
126//!         other: vec![<expr for &b>],
127//!     }],
128//! )
129//! ```
130//!
131//! For `C1 {x}` and `C1 {x}`,
132//!
133//! ```text
134//! EnumMatching(
135//!     1,
136//!     <ast::Variant for C1>,
137//!     vec![FieldInfo {
138//!         span: <span of x>,
139//!         name: Some(<ident of x>),
140//!         self_: <expr for &self.x>,
141//!         other: vec![<expr for &other.x>],
142//!     }],
143//! )
144//! ```
145//!
146//! For the discriminants,
147//!
148//! ```text
149//! EnumDiscr(
150//!     &[<ident of self discriminant>, <ident of other discriminant>],
151//!     <expr to combine with>,
152//! )
153//! ```
154//!
155//! Note that this setup doesn't allow for the brute-force "match every variant
156//! against every other variant" approach, which is bad because it produces a
157//! quadratic amount of code (see #15375).
158//!
159//! ## Static
160//!
161//! A static method on the types above would result in,
162//!
163//! ```text
164//! StaticStruct(<ast::VariantData of A>, Named(vec![(<ident of x>, <span of x>)]))
165//!
166//! StaticStruct(<ast::VariantData of B>, Unnamed(vec![<span of x>]))
167//!
168//! StaticEnum(
169//!     <ast::EnumDef of C>,
170//!     vec![
171//!         (<ident of C0>, <span of C0>, Unnamed(vec![<span of i32>])),
172//!         (<ident of C1>, <span of C1>, Named(vec![(<ident of x>, <span of x>)])),
173//!     ],
174//! )
175//! ```
176
177use std::cell::RefCell;
178use std::ops::Not;
179use std::{iter, vec};
180
181pub(crate) use StaticFields::*;
182pub(crate) use SubstructureFields::*;
183use rustc_ast::ptr::P;
184use rustc_ast::{
185    self as ast, AnonConst, BindingMode, ByRef, EnumDef, Expr, GenericArg, GenericParamKind,
186    Generics, Mutability, PatKind, VariantData,
187};
188use rustc_attr_data_structures::{AttributeKind, ReprPacked};
189use rustc_attr_parsing::AttributeParser;
190use rustc_expand::base::{Annotatable, ExtCtxt};
191use rustc_hir::Attribute;
192use rustc_span::{DUMMY_SP, Ident, Span, Symbol, kw, sym};
193use thin_vec::{ThinVec, thin_vec};
194use ty::{Bounds, Path, Ref, Self_, Ty};
195
196use crate::{deriving, errors};
197
198pub(crate) mod ty;
199
200pub(crate) struct TraitDef<'a> {
201    /// The span for the current #[derive(Foo)] header.
202    pub span: Span,
203
204    /// Path of the trait, including any type parameters
205    pub path: Path,
206
207    /// Whether to skip adding the current trait as a bound to the type parameters of the type.
208    pub skip_path_as_bound: bool,
209
210    /// Whether `Copy` is needed as an additional bound on type parameters in a packed struct.
211    pub needs_copy_as_bound_if_packed: bool,
212
213    /// Additional bounds required of any type parameters of the type,
214    /// other than the current trait
215    pub additional_bounds: Vec<Ty>,
216
217    /// Can this trait be derived for unions?
218    pub supports_unions: bool,
219
220    pub methods: Vec<MethodDef<'a>>,
221
222    pub associated_types: Vec<(Ident, Ty)>,
223
224    pub is_const: bool,
225}
226
227pub(crate) struct MethodDef<'a> {
228    /// name of the method
229    pub name: Symbol,
230    /// List of generics, e.g., `R: rand::Rng`
231    pub generics: Bounds,
232
233    /// Is there is a `&self` argument? If not, it is a static function.
234    pub explicit_self: bool,
235
236    /// Arguments other than the self argument.
237    pub nonself_args: Vec<(Ty, Symbol)>,
238
239    /// Returns type
240    pub ret_ty: Ty,
241
242    pub attributes: ast::AttrVec,
243
244    pub fieldless_variants_strategy: FieldlessVariantsStrategy,
245
246    pub combine_substructure: RefCell<CombineSubstructureFunc<'a>>,
247}
248
249/// How to handle fieldless enum variants.
250#[derive(PartialEq)]
251pub(crate) enum FieldlessVariantsStrategy {
252    /// Combine fieldless variants into a single match arm.
253    /// This assumes that relevant information has been handled
254    /// by looking at the enum's discriminant.
255    Unify,
256    /// Don't do anything special about fieldless variants. They are
257    /// handled like any other variant.
258    Default,
259    /// If all variants of the enum are fieldless, expand the special
260    /// `AllFieldLessEnum` substructure, so that the entire enum can be handled
261    /// at once.
262    SpecializeIfAllVariantsFieldless,
263}
264
265/// All the data about the data structure/method being derived upon.
266pub(crate) struct Substructure<'a> {
267    /// ident of self
268    pub type_ident: Ident,
269    /// Verbatim access to any non-selflike arguments, i.e. arguments that
270    /// don't have type `&Self`.
271    pub nonselflike_args: &'a [P<Expr>],
272    pub fields: &'a SubstructureFields<'a>,
273}
274
275/// Summary of the relevant parts of a struct/enum field.
276pub(crate) struct FieldInfo {
277    pub span: Span,
278    /// None for tuple structs/normal enum variants, Some for normal
279    /// structs/struct enum variants.
280    pub name: Option<Ident>,
281    /// The expression corresponding to this field of `self`
282    /// (specifically, a reference to it).
283    pub self_expr: P<Expr>,
284    /// The expressions corresponding to references to this field in
285    /// the other selflike arguments.
286    pub other_selflike_exprs: Vec<P<Expr>>,
287}
288
289#[derive(Copy, Clone)]
290pub(crate) enum IsTuple {
291    No,
292    Yes,
293}
294
295/// Fields for a static method
296pub(crate) enum StaticFields {
297    /// Tuple and unit structs/enum variants like this.
298    Unnamed(Vec<Span>, IsTuple),
299    /// Normal structs/struct variants.
300    Named(Vec<(Ident, Span, Option<AnonConst>)>),
301}
302
303/// A summary of the possible sets of fields.
304pub(crate) enum SubstructureFields<'a> {
305    /// A non-static method where `Self` is a struct.
306    Struct(&'a ast::VariantData, Vec<FieldInfo>),
307
308    /// A non-static method handling the entire enum at once
309    /// (after it has been determined that none of the enum
310    /// variants has any fields).
311    AllFieldlessEnum(&'a ast::EnumDef),
312
313    /// Matching variants of the enum: variant index, ast::Variant,
314    /// fields: the field name is only non-`None` in the case of a struct
315    /// variant.
316    EnumMatching(&'a ast::Variant, Vec<FieldInfo>),
317
318    /// The discriminant of an enum. The first field is a `FieldInfo` for the discriminants, as
319    /// if they were fields. The second field is the expression to combine the
320    /// discriminant expression with; it will be `None` if no match is necessary.
321    EnumDiscr(FieldInfo, Option<P<Expr>>),
322
323    /// A static method where `Self` is a struct.
324    StaticStruct(&'a ast::VariantData, StaticFields),
325
326    /// A static method where `Self` is an enum.
327    StaticEnum(&'a ast::EnumDef),
328}
329
330/// Combine the values of all the fields together. The last argument is
331/// all the fields of all the structures.
332pub(crate) type CombineSubstructureFunc<'a> =
333    Box<dyn FnMut(&ExtCtxt<'_>, Span, &Substructure<'_>) -> BlockOrExpr + 'a>;
334
335pub(crate) fn combine_substructure(
336    f: CombineSubstructureFunc<'_>,
337) -> RefCell<CombineSubstructureFunc<'_>> {
338    RefCell::new(f)
339}
340
341struct TypeParameter {
342    bound_generic_params: ThinVec<ast::GenericParam>,
343    ty: P<ast::Ty>,
344}
345
346/// The code snippets built up for derived code are sometimes used as blocks
347/// (e.g. in a function body) and sometimes used as expressions (e.g. in a match
348/// arm). This structure avoids committing to either form until necessary,
349/// avoiding the insertion of any unnecessary blocks.
350///
351/// The statements come before the expression.
352pub(crate) struct BlockOrExpr(ThinVec<ast::Stmt>, Option<P<Expr>>);
353
354impl BlockOrExpr {
355    pub(crate) fn new_stmts(stmts: ThinVec<ast::Stmt>) -> BlockOrExpr {
356        BlockOrExpr(stmts, None)
357    }
358
359    pub(crate) fn new_expr(expr: P<Expr>) -> BlockOrExpr {
360        BlockOrExpr(ThinVec::new(), Some(expr))
361    }
362
363    pub(crate) fn new_mixed(stmts: ThinVec<ast::Stmt>, expr: Option<P<Expr>>) -> BlockOrExpr {
364        BlockOrExpr(stmts, expr)
365    }
366
367    // Converts it into a block.
368    fn into_block(mut self, cx: &ExtCtxt<'_>, span: Span) -> P<ast::Block> {
369        if let Some(expr) = self.1 {
370            self.0.push(cx.stmt_expr(expr));
371        }
372        cx.block(span, self.0)
373    }
374
375    // Converts it into an expression.
376    fn into_expr(self, cx: &ExtCtxt<'_>, span: Span) -> P<Expr> {
377        if self.0.is_empty() {
378            match self.1 {
379                None => cx.expr_block(cx.block(span, ThinVec::new())),
380                Some(expr) => expr,
381            }
382        } else if let [stmt] = self.0.as_slice()
383            && let ast::StmtKind::Expr(expr) = &stmt.kind
384            && self.1.is_none()
385        {
386            // There's only a single statement expression. Pull it out.
387            expr.clone()
388        } else {
389            // Multiple statements and/or expressions.
390            cx.expr_block(self.into_block(cx, span))
391        }
392    }
393}
394
395/// This method helps to extract all the type parameters referenced from a
396/// type. For a type parameter `<T>`, it looks for either a `TyPath` that
397/// is not global and starts with `T`, or a `TyQPath`.
398/// Also include bound generic params from the input type.
399fn find_type_parameters(
400    ty: &ast::Ty,
401    ty_param_names: &[Symbol],
402    cx: &ExtCtxt<'_>,
403) -> Vec<TypeParameter> {
404    use rustc_ast::visit;
405
406    struct Visitor<'a, 'b> {
407        cx: &'a ExtCtxt<'b>,
408        ty_param_names: &'a [Symbol],
409        bound_generic_params_stack: ThinVec<ast::GenericParam>,
410        type_params: Vec<TypeParameter>,
411    }
412
413    impl<'a, 'b> visit::Visitor<'a> for Visitor<'a, 'b> {
414        fn visit_ty(&mut self, ty: &'a ast::Ty) {
415            let stack_len = self.bound_generic_params_stack.len();
416            if let ast::TyKind::BareFn(bare_fn) = &ty.kind
417                && !bare_fn.generic_params.is_empty()
418            {
419                // Given a field `x: for<'a> fn(T::SomeType<'a>)`, we wan't to account for `'a` so
420                // that we generate `where for<'a> T::SomeType<'a>: ::core::clone::Clone`. #122622
421                self.bound_generic_params_stack.extend(bare_fn.generic_params.iter().cloned());
422            }
423
424            if let ast::TyKind::Path(_, path) = &ty.kind
425                && let Some(segment) = path.segments.first()
426                && self.ty_param_names.contains(&segment.ident.name)
427            {
428                self.type_params.push(TypeParameter {
429                    bound_generic_params: self.bound_generic_params_stack.clone(),
430                    ty: P(ty.clone()),
431                });
432            }
433
434            visit::walk_ty(self, ty);
435            self.bound_generic_params_stack.truncate(stack_len);
436        }
437
438        // Place bound generic params on a stack, to extract them when a type is encountered.
439        fn visit_poly_trait_ref(&mut self, trait_ref: &'a ast::PolyTraitRef) {
440            let stack_len = self.bound_generic_params_stack.len();
441            self.bound_generic_params_stack.extend(trait_ref.bound_generic_params.iter().cloned());
442
443            visit::walk_poly_trait_ref(self, trait_ref);
444
445            self.bound_generic_params_stack.truncate(stack_len);
446        }
447
448        fn visit_mac_call(&mut self, mac: &ast::MacCall) {
449            self.cx.dcx().emit_err(errors::DeriveMacroCall { span: mac.span() });
450        }
451    }
452
453    let mut visitor = Visitor {
454        cx,
455        ty_param_names,
456        bound_generic_params_stack: ThinVec::new(),
457        type_params: Vec::new(),
458    };
459    visit::Visitor::visit_ty(&mut visitor, ty);
460
461    visitor.type_params
462}
463
464impl<'a> TraitDef<'a> {
465    pub(crate) fn expand(
466        self,
467        cx: &ExtCtxt<'_>,
468        mitem: &ast::MetaItem,
469        item: &'a Annotatable,
470        push: &mut dyn FnMut(Annotatable),
471    ) {
472        self.expand_ext(cx, mitem, item, push, false);
473    }
474
475    pub(crate) fn expand_ext(
476        self,
477        cx: &ExtCtxt<'_>,
478        mitem: &ast::MetaItem,
479        item: &'a Annotatable,
480        push: &mut dyn FnMut(Annotatable),
481        from_scratch: bool,
482    ) {
483        match item {
484            Annotatable::Item(item) => {
485                let is_packed = matches!(
486                    AttributeParser::parse_limited(cx.sess, &item.attrs, sym::repr, item.span, true),
487                    Some(Attribute::Parsed(AttributeKind::Repr(r))) if r.iter().any(|(x, _)| matches!(x, ReprPacked(..)))
488                );
489
490                let newitem = match &item.kind {
491                    ast::ItemKind::Struct(ident, generics, struct_def) => self.expand_struct_def(
492                        cx,
493                        struct_def,
494                        *ident,
495                        generics,
496                        from_scratch,
497                        is_packed,
498                    ),
499                    ast::ItemKind::Enum(ident, generics, enum_def) => {
500                        // We ignore `is_packed` here, because `repr(packed)`
501                        // enums cause an error later on.
502                        //
503                        // This can only cause further compilation errors
504                        // downstream in blatantly illegal code, so it is fine.
505                        self.expand_enum_def(cx, enum_def, *ident, generics, from_scratch)
506                    }
507                    ast::ItemKind::Union(ident, generics, struct_def) => {
508                        if self.supports_unions {
509                            self.expand_struct_def(
510                                cx,
511                                struct_def,
512                                *ident,
513                                generics,
514                                from_scratch,
515                                is_packed,
516                            )
517                        } else {
518                            cx.dcx().emit_err(errors::DeriveUnion { span: mitem.span });
519                            return;
520                        }
521                    }
522                    _ => unreachable!(),
523                };
524                // Keep the lint attributes of the previous item to control how the
525                // generated implementations are linted
526                let mut attrs = newitem.attrs.clone();
527                attrs.extend(
528                    item.attrs
529                        .iter()
530                        .filter(|a| {
531                            a.has_any_name(&[
532                                sym::allow,
533                                sym::warn,
534                                sym::deny,
535                                sym::forbid,
536                                sym::stable,
537                                sym::unstable,
538                            ])
539                        })
540                        .cloned(),
541                );
542                push(Annotatable::Item(P(ast::Item { attrs, ..(*newitem).clone() })))
543            }
544            _ => unreachable!(),
545        }
546    }
547
548    /// Given that we are deriving a trait `DerivedTrait` for a type like:
549    ///
550    /// ```ignore (only-for-syntax-highlight)
551    /// struct Struct<'a, ..., 'z, A, B: DeclaredTrait, C, ..., Z>
552    /// where
553    ///     C: WhereTrait,
554    /// {
555    ///     a: A,
556    ///     b: B::Item,
557    ///     b1: <B as DeclaredTrait>::Item,
558    ///     c1: <C as WhereTrait>::Item,
559    ///     c2: Option<<C as WhereTrait>::Item>,
560    ///     ...
561    /// }
562    /// ```
563    ///
564    /// create an impl like:
565    ///
566    /// ```ignore (only-for-syntax-highlight)
567    /// impl<'a, ..., 'z, A, B: DeclaredTrait, C, ..., Z>
568    /// where
569    ///     C: WhereTrait,
570    ///     A: DerivedTrait + B1 + ... + BN,
571    ///     B: DerivedTrait + B1 + ... + BN,
572    ///     C: DerivedTrait + B1 + ... + BN,
573    ///     B::Item: DerivedTrait + B1 + ... + BN,
574    ///     <C as WhereTrait>::Item: DerivedTrait + B1 + ... + BN,
575    ///     ...
576    /// {
577    ///     ...
578    /// }
579    /// ```
580    ///
581    /// where B1, ..., BN are the bounds given by `bounds_paths`.'. Z is a phantom type, and
582    /// therefore does not get bound by the derived trait.
583    fn create_derived_impl(
584        &self,
585        cx: &ExtCtxt<'_>,
586        type_ident: Ident,
587        generics: &Generics,
588        field_tys: Vec<P<ast::Ty>>,
589        methods: Vec<P<ast::AssocItem>>,
590        is_packed: bool,
591    ) -> P<ast::Item> {
592        let trait_path = self.path.to_path(cx, self.span, type_ident, generics);
593
594        // Transform associated types from `deriving::ty::Ty` into `ast::AssocItem`
595        let associated_types = self.associated_types.iter().map(|&(ident, ref type_def)| {
596            P(ast::AssocItem {
597                id: ast::DUMMY_NODE_ID,
598                span: self.span,
599                vis: ast::Visibility {
600                    span: self.span.shrink_to_lo(),
601                    kind: ast::VisibilityKind::Inherited,
602                    tokens: None,
603                },
604                attrs: ast::AttrVec::new(),
605                kind: ast::AssocItemKind::Type(Box::new(ast::TyAlias {
606                    defaultness: ast::Defaultness::Final,
607                    ident,
608                    generics: Generics::default(),
609                    where_clauses: ast::TyAliasWhereClauses::default(),
610                    bounds: Vec::new(),
611                    ty: Some(type_def.to_ty(cx, self.span, type_ident, generics)),
612                })),
613                tokens: None,
614            })
615        });
616
617        let mut where_clause = ast::WhereClause::default();
618        where_clause.span = generics.where_clause.span;
619        let ctxt = self.span.ctxt();
620        let span = generics.span.with_ctxt(ctxt);
621
622        // Create the generic parameters
623        let params: ThinVec<_> = generics
624            .params
625            .iter()
626            .map(|param| match &param.kind {
627                GenericParamKind::Lifetime { .. } => param.clone(),
628                GenericParamKind::Type { .. } => {
629                    // Extra restrictions on the generics parameters to the
630                    // type being derived upon.
631                    let bounds: Vec<_> = self
632                        .additional_bounds
633                        .iter()
634                        .map(|p| {
635                            cx.trait_bound(
636                                p.to_path(cx, self.span, type_ident, generics),
637                                self.is_const,
638                            )
639                        })
640                        .chain(
641                            // Add a bound for the current trait.
642                            self.skip_path_as_bound
643                                .not()
644                                .then(|| cx.trait_bound(trait_path.clone(), self.is_const)),
645                        )
646                        .chain({
647                            // Add a `Copy` bound if required.
648                            if is_packed && self.needs_copy_as_bound_if_packed {
649                                let p = deriving::path_std!(marker::Copy);
650                                Some(cx.trait_bound(
651                                    p.to_path(cx, self.span, type_ident, generics),
652                                    self.is_const,
653                                ))
654                            } else {
655                                None
656                            }
657                        })
658                        .chain(
659                            // Also add in any bounds from the declaration.
660                            param.bounds.iter().cloned(),
661                        )
662                        .collect();
663
664                    cx.typaram(param.ident.span.with_ctxt(ctxt), param.ident, bounds, None)
665                }
666                GenericParamKind::Const { ty, kw_span, .. } => {
667                    let const_nodefault_kind = GenericParamKind::Const {
668                        ty: ty.clone(),
669                        kw_span: kw_span.with_ctxt(ctxt),
670
671                        // We can't have default values inside impl block
672                        default: None,
673                    };
674                    let mut param_clone = param.clone();
675                    param_clone.kind = const_nodefault_kind;
676                    param_clone
677                }
678            })
679            .map(|mut param| {
680                // Remove all attributes, because there might be helper attributes
681                // from other macros that will not be valid in the expanded implementation.
682                param.attrs.clear();
683                param
684            })
685            .collect();
686
687        // and similarly for where clauses
688        where_clause.predicates.extend(generics.where_clause.predicates.iter().map(|clause| {
689            ast::WherePredicate {
690                attrs: clause.attrs.clone(),
691                kind: clause.kind.clone(),
692                id: ast::DUMMY_NODE_ID,
693                span: clause.span.with_ctxt(ctxt),
694                is_placeholder: false,
695            }
696        }));
697
698        let ty_param_names: Vec<Symbol> = params
699            .iter()
700            .filter(|param| matches!(param.kind, ast::GenericParamKind::Type { .. }))
701            .map(|ty_param| ty_param.ident.name)
702            .collect();
703
704        if !ty_param_names.is_empty() {
705            for field_ty in field_tys {
706                let field_ty_params = find_type_parameters(&field_ty, &ty_param_names, cx);
707
708                for field_ty_param in field_ty_params {
709                    // if we have already handled this type, skip it
710                    if let ast::TyKind::Path(_, p) = &field_ty_param.ty.kind
711                        && let [sole_segment] = &*p.segments
712                        && ty_param_names.contains(&sole_segment.ident.name)
713                    {
714                        continue;
715                    }
716                    let mut bounds: Vec<_> = self
717                        .additional_bounds
718                        .iter()
719                        .map(|p| {
720                            cx.trait_bound(
721                                p.to_path(cx, self.span, type_ident, generics),
722                                self.is_const,
723                            )
724                        })
725                        .collect();
726
727                    // Require the current trait.
728                    if !self.skip_path_as_bound {
729                        bounds.push(cx.trait_bound(trait_path.clone(), self.is_const));
730                    }
731
732                    // Add a `Copy` bound if required.
733                    if is_packed && self.needs_copy_as_bound_if_packed {
734                        let p = deriving::path_std!(marker::Copy);
735                        bounds.push(cx.trait_bound(
736                            p.to_path(cx, self.span, type_ident, generics),
737                            self.is_const,
738                        ));
739                    }
740
741                    if !bounds.is_empty() {
742                        let predicate = ast::WhereBoundPredicate {
743                            bound_generic_params: field_ty_param.bound_generic_params,
744                            bounded_ty: field_ty_param.ty,
745                            bounds,
746                        };
747
748                        let kind = ast::WherePredicateKind::BoundPredicate(predicate);
749                        let predicate = ast::WherePredicate {
750                            attrs: ThinVec::new(),
751                            kind,
752                            id: ast::DUMMY_NODE_ID,
753                            span: self.span,
754                            is_placeholder: false,
755                        };
756                        where_clause.predicates.push(predicate);
757                    }
758                }
759            }
760        }
761
762        let trait_generics = Generics { params, where_clause, span };
763
764        // Create the reference to the trait.
765        let trait_ref = cx.trait_ref(trait_path);
766
767        let self_params: Vec<_> = generics
768            .params
769            .iter()
770            .map(|param| match param.kind {
771                GenericParamKind::Lifetime { .. } => {
772                    GenericArg::Lifetime(cx.lifetime(param.ident.span.with_ctxt(ctxt), param.ident))
773                }
774                GenericParamKind::Type { .. } => {
775                    GenericArg::Type(cx.ty_ident(param.ident.span.with_ctxt(ctxt), param.ident))
776                }
777                GenericParamKind::Const { .. } => {
778                    GenericArg::Const(cx.const_ident(param.ident.span.with_ctxt(ctxt), param.ident))
779                }
780            })
781            .collect();
782
783        // Create the type of `self`.
784        let path = cx.path_all(self.span, false, vec![type_ident], self_params);
785        let self_type = cx.ty_path(path);
786
787        let attrs = thin_vec![cx.attr_word(sym::automatically_derived, self.span),];
788        let opt_trait_ref = Some(trait_ref);
789
790        cx.item(
791            self.span,
792            attrs,
793            ast::ItemKind::Impl(Box::new(ast::Impl {
794                safety: ast::Safety::Default,
795                polarity: ast::ImplPolarity::Positive,
796                defaultness: ast::Defaultness::Final,
797                constness: if self.is_const { ast::Const::Yes(DUMMY_SP) } else { ast::Const::No },
798                generics: trait_generics,
799                of_trait: opt_trait_ref,
800                self_ty: self_type,
801                items: methods.into_iter().chain(associated_types).collect(),
802            })),
803        )
804    }
805
806    fn expand_struct_def(
807        &self,
808        cx: &ExtCtxt<'_>,
809        struct_def: &'a VariantData,
810        type_ident: Ident,
811        generics: &Generics,
812        from_scratch: bool,
813        is_packed: bool,
814    ) -> P<ast::Item> {
815        let field_tys: Vec<P<ast::Ty>> =
816            struct_def.fields().iter().map(|field| field.ty.clone()).collect();
817
818        let methods = self
819            .methods
820            .iter()
821            .map(|method_def| {
822                let (explicit_self, selflike_args, nonselflike_args, nonself_arg_tys) =
823                    method_def.extract_arg_details(cx, self, type_ident, generics);
824
825                let body = if from_scratch || method_def.is_static() {
826                    method_def.expand_static_struct_method_body(
827                        cx,
828                        self,
829                        struct_def,
830                        type_ident,
831                        &nonselflike_args,
832                    )
833                } else {
834                    method_def.expand_struct_method_body(
835                        cx,
836                        self,
837                        struct_def,
838                        type_ident,
839                        &selflike_args,
840                        &nonselflike_args,
841                        is_packed,
842                    )
843                };
844
845                method_def.create_method(
846                    cx,
847                    self,
848                    type_ident,
849                    generics,
850                    explicit_self,
851                    nonself_arg_tys,
852                    body,
853                )
854            })
855            .collect();
856
857        self.create_derived_impl(cx, type_ident, generics, field_tys, methods, is_packed)
858    }
859
860    fn expand_enum_def(
861        &self,
862        cx: &ExtCtxt<'_>,
863        enum_def: &'a EnumDef,
864        type_ident: Ident,
865        generics: &Generics,
866        from_scratch: bool,
867    ) -> P<ast::Item> {
868        let mut field_tys = Vec::new();
869
870        for variant in &enum_def.variants {
871            field_tys.extend(variant.data.fields().iter().map(|field| field.ty.clone()));
872        }
873
874        let methods = self
875            .methods
876            .iter()
877            .map(|method_def| {
878                let (explicit_self, selflike_args, nonselflike_args, nonself_arg_tys) =
879                    method_def.extract_arg_details(cx, self, type_ident, generics);
880
881                let body = if from_scratch || method_def.is_static() {
882                    method_def.expand_static_enum_method_body(
883                        cx,
884                        self,
885                        enum_def,
886                        type_ident,
887                        &nonselflike_args,
888                    )
889                } else {
890                    method_def.expand_enum_method_body(
891                        cx,
892                        self,
893                        enum_def,
894                        type_ident,
895                        selflike_args,
896                        &nonselflike_args,
897                    )
898                };
899
900                method_def.create_method(
901                    cx,
902                    self,
903                    type_ident,
904                    generics,
905                    explicit_self,
906                    nonself_arg_tys,
907                    body,
908                )
909            })
910            .collect();
911
912        let is_packed = false; // enums are never packed
913        self.create_derived_impl(cx, type_ident, generics, field_tys, methods, is_packed)
914    }
915}
916
917impl<'a> MethodDef<'a> {
918    fn call_substructure_method(
919        &self,
920        cx: &ExtCtxt<'_>,
921        trait_: &TraitDef<'_>,
922        type_ident: Ident,
923        nonselflike_args: &[P<Expr>],
924        fields: &SubstructureFields<'_>,
925    ) -> BlockOrExpr {
926        let span = trait_.span;
927        let substructure = Substructure { type_ident, nonselflike_args, fields };
928        let mut f = self.combine_substructure.borrow_mut();
929        let f: &mut CombineSubstructureFunc<'_> = &mut *f;
930        f(cx, span, &substructure)
931    }
932
933    fn get_ret_ty(
934        &self,
935        cx: &ExtCtxt<'_>,
936        trait_: &TraitDef<'_>,
937        generics: &Generics,
938        type_ident: Ident,
939    ) -> P<ast::Ty> {
940        self.ret_ty.to_ty(cx, trait_.span, type_ident, generics)
941    }
942
943    fn is_static(&self) -> bool {
944        !self.explicit_self
945    }
946
947    // The return value includes:
948    // - explicit_self: The `&self` arg, if present.
949    // - selflike_args: Expressions for `&self` (if present) and also any other
950    //   args with the same type (e.g. the `other` arg in `PartialEq::eq`).
951    // - nonselflike_args: Expressions for all the remaining args.
952    // - nonself_arg_tys: Additional information about all the args other than
953    //   `&self`.
954    fn extract_arg_details(
955        &self,
956        cx: &ExtCtxt<'_>,
957        trait_: &TraitDef<'_>,
958        type_ident: Ident,
959        generics: &Generics,
960    ) -> (Option<ast::ExplicitSelf>, ThinVec<P<Expr>>, Vec<P<Expr>>, Vec<(Ident, P<ast::Ty>)>) {
961        let mut selflike_args = ThinVec::new();
962        let mut nonselflike_args = Vec::new();
963        let mut nonself_arg_tys = Vec::new();
964        let span = trait_.span;
965
966        let explicit_self = self.explicit_self.then(|| {
967            let (self_expr, explicit_self) = ty::get_explicit_self(cx, span);
968            selflike_args.push(self_expr);
969            explicit_self
970        });
971
972        for (ty, name) in self.nonself_args.iter() {
973            let ast_ty = ty.to_ty(cx, span, type_ident, generics);
974            let ident = Ident::new(*name, span);
975            nonself_arg_tys.push((ident, ast_ty));
976
977            let arg_expr = cx.expr_ident(span, ident);
978
979            match ty {
980                // Selflike (`&Self`) arguments only occur in non-static methods.
981                Ref(box Self_, _) if !self.is_static() => selflike_args.push(arg_expr),
982                Self_ => cx.dcx().span_bug(span, "`Self` in non-return position"),
983                _ => nonselflike_args.push(arg_expr),
984            }
985        }
986
987        (explicit_self, selflike_args, nonselflike_args, nonself_arg_tys)
988    }
989
990    fn create_method(
991        &self,
992        cx: &ExtCtxt<'_>,
993        trait_: &TraitDef<'_>,
994        type_ident: Ident,
995        generics: &Generics,
996        explicit_self: Option<ast::ExplicitSelf>,
997        nonself_arg_tys: Vec<(Ident, P<ast::Ty>)>,
998        body: BlockOrExpr,
999    ) -> P<ast::AssocItem> {
1000        let span = trait_.span;
1001        // Create the generics that aren't for `Self`.
1002        let fn_generics = self.generics.to_generics(cx, span, type_ident, generics);
1003
1004        let args = {
1005            let self_arg = explicit_self.map(|explicit_self| {
1006                let ident = Ident::with_dummy_span(kw::SelfLower).with_span_pos(span);
1007                ast::Param::from_self(ast::AttrVec::default(), explicit_self, ident)
1008            });
1009            let nonself_args =
1010                nonself_arg_tys.into_iter().map(|(name, ty)| cx.param(span, name, ty));
1011            self_arg.into_iter().chain(nonself_args).collect()
1012        };
1013
1014        let ret_type = self.get_ret_ty(cx, trait_, generics, type_ident);
1015
1016        let method_ident = Ident::new(self.name, span);
1017        let fn_decl = cx.fn_decl(args, ast::FnRetTy::Ty(ret_type));
1018        let body_block = body.into_block(cx, span);
1019
1020        let trait_lo_sp = span.shrink_to_lo();
1021
1022        let sig = ast::FnSig { header: ast::FnHeader::default(), decl: fn_decl, span };
1023        let defaultness = ast::Defaultness::Final;
1024
1025        // Create the method.
1026        P(ast::AssocItem {
1027            id: ast::DUMMY_NODE_ID,
1028            attrs: self.attributes.clone(),
1029            span,
1030            vis: ast::Visibility {
1031                span: trait_lo_sp,
1032                kind: ast::VisibilityKind::Inherited,
1033                tokens: None,
1034            },
1035            kind: ast::AssocItemKind::Fn(Box::new(ast::Fn {
1036                defaultness,
1037                sig,
1038                ident: method_ident,
1039                generics: fn_generics,
1040                contract: None,
1041                body: Some(body_block),
1042                define_opaque: None,
1043            })),
1044            tokens: None,
1045        })
1046    }
1047
1048    /// The normal case uses field access.
1049    ///
1050    /// ```
1051    /// #[derive(PartialEq)]
1052    /// # struct Dummy;
1053    /// struct A { x: u8, y: u8 }
1054    ///
1055    /// // equivalent to:
1056    /// impl PartialEq for A {
1057    ///     fn eq(&self, other: &A) -> bool {
1058    ///         self.x == other.x && self.y == other.y
1059    ///     }
1060    /// }
1061    /// ```
1062    ///
1063    /// But if the struct is `repr(packed)`, we can't use something like
1064    /// `&self.x` because that might cause an unaligned ref. So for any trait
1065    /// method that takes a reference, we use a local block to force a copy.
1066    /// This requires that the field impl `Copy`.
1067    ///
1068    /// ```rust,ignore (example)
1069    /// # struct A { x: u8, y: u8 }
1070    /// impl PartialEq for A {
1071    ///     fn eq(&self, other: &A) -> bool {
1072    ///         // Desugars to `{ self.x }.eq(&{ other.y }) && ...`
1073    ///         { self.x } == { other.y } && { self.y } == { other.y }
1074    ///     }
1075    /// }
1076    /// impl Hash for A {
1077    ///     fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () {
1078    ///         ::core::hash::Hash::hash(&{ self.x }, state);
1079    ///         ::core::hash::Hash::hash(&{ self.y }, state);
1080    ///     }
1081    /// }
1082    /// ```
1083    fn expand_struct_method_body<'b>(
1084        &self,
1085        cx: &ExtCtxt<'_>,
1086        trait_: &TraitDef<'b>,
1087        struct_def: &'b VariantData,
1088        type_ident: Ident,
1089        selflike_args: &[P<Expr>],
1090        nonselflike_args: &[P<Expr>],
1091        is_packed: bool,
1092    ) -> BlockOrExpr {
1093        assert!(selflike_args.len() == 1 || selflike_args.len() == 2);
1094
1095        let selflike_fields =
1096            trait_.create_struct_field_access_fields(cx, selflike_args, struct_def, is_packed);
1097        self.call_substructure_method(
1098            cx,
1099            trait_,
1100            type_ident,
1101            nonselflike_args,
1102            &Struct(struct_def, selflike_fields),
1103        )
1104    }
1105
1106    fn expand_static_struct_method_body(
1107        &self,
1108        cx: &ExtCtxt<'_>,
1109        trait_: &TraitDef<'_>,
1110        struct_def: &VariantData,
1111        type_ident: Ident,
1112        nonselflike_args: &[P<Expr>],
1113    ) -> BlockOrExpr {
1114        let summary = trait_.summarise_struct(cx, struct_def);
1115
1116        self.call_substructure_method(
1117            cx,
1118            trait_,
1119            type_ident,
1120            nonselflike_args,
1121            &StaticStruct(struct_def, summary),
1122        )
1123    }
1124
1125    /// ```
1126    /// #[derive(PartialEq)]
1127    /// # struct Dummy;
1128    /// enum A {
1129    ///     A1,
1130    ///     A2(i32)
1131    /// }
1132    /// ```
1133    ///
1134    /// is equivalent to:
1135    ///
1136    /// ```
1137    /// #![feature(core_intrinsics)]
1138    /// enum A {
1139    ///     A1,
1140    ///     A2(i32)
1141    /// }
1142    /// impl ::core::cmp::PartialEq for A {
1143    ///     #[inline]
1144    ///     fn eq(&self, other: &A) -> bool {
1145    ///         let __self_discr = ::core::intrinsics::discriminant_value(self);
1146    ///         let __arg1_discr = ::core::intrinsics::discriminant_value(other);
1147    ///         __self_discr == __arg1_discr
1148    ///             && match (self, other) {
1149    ///                 (A::A2(__self_0), A::A2(__arg1_0)) => *__self_0 == *__arg1_0,
1150    ///                 _ => true,
1151    ///             }
1152    ///     }
1153    /// }
1154    /// ```
1155    ///
1156    /// Creates a discriminant check combined with a match for a tuple of all
1157    /// `selflike_args`, with an arm for each variant with fields, possibly an
1158    /// arm for each fieldless variant (if `unify_fieldless_variants` is not
1159    /// `Unify`), and possibly a default arm.
1160    fn expand_enum_method_body<'b>(
1161        &self,
1162        cx: &ExtCtxt<'_>,
1163        trait_: &TraitDef<'b>,
1164        enum_def: &'b EnumDef,
1165        type_ident: Ident,
1166        mut selflike_args: ThinVec<P<Expr>>,
1167        nonselflike_args: &[P<Expr>],
1168    ) -> BlockOrExpr {
1169        assert!(
1170            !selflike_args.is_empty(),
1171            "static methods must use `expand_static_enum_method_body`",
1172        );
1173
1174        let span = trait_.span;
1175        let variants = &enum_def.variants;
1176
1177        // Traits that unify fieldless variants always use the discriminant(s).
1178        let unify_fieldless_variants =
1179            self.fieldless_variants_strategy == FieldlessVariantsStrategy::Unify;
1180
1181        // For zero-variant enum, this function body is unreachable. Generate
1182        // `match *self {}`. This produces machine code identical to `unsafe {
1183        // core::intrinsics::unreachable() }` while being safe and stable.
1184        if variants.is_empty() {
1185            selflike_args.truncate(1);
1186            let match_arg = cx.expr_deref(span, selflike_args.pop().unwrap());
1187            let match_arms = ThinVec::new();
1188            let expr = cx.expr_match(span, match_arg, match_arms);
1189            return BlockOrExpr(ThinVec::new(), Some(expr));
1190        }
1191
1192        let prefixes = iter::once("__self".to_string())
1193            .chain(
1194                selflike_args
1195                    .iter()
1196                    .enumerate()
1197                    .skip(1)
1198                    .map(|(arg_count, _selflike_arg)| format!("__arg{arg_count}")),
1199            )
1200            .collect::<Vec<String>>();
1201
1202        // Build a series of let statements mapping each selflike_arg
1203        // to its discriminant value.
1204        //
1205        // e.g. for `PartialEq::eq` builds two statements:
1206        // ```
1207        // let __self_discr = ::core::intrinsics::discriminant_value(self);
1208        // let __arg1_discr = ::core::intrinsics::discriminant_value(other);
1209        // ```
1210        let get_discr_pieces = |cx: &ExtCtxt<'_>| {
1211            let discr_idents: Vec<_> = prefixes
1212                .iter()
1213                .map(|name| Ident::from_str_and_span(&format!("{name}_discr"), span))
1214                .collect();
1215
1216            let mut discr_exprs: Vec<_> = discr_idents
1217                .iter()
1218                .map(|&ident| cx.expr_addr_of(span, cx.expr_ident(span, ident)))
1219                .collect();
1220
1221            let self_expr = discr_exprs.remove(0);
1222            let other_selflike_exprs = discr_exprs;
1223            let discr_field = FieldInfo { span, name: None, self_expr, other_selflike_exprs };
1224
1225            let discr_let_stmts: ThinVec<_> = iter::zip(&discr_idents, &selflike_args)
1226                .map(|(&ident, selflike_arg)| {
1227                    let variant_value = deriving::call_intrinsic(
1228                        cx,
1229                        span,
1230                        sym::discriminant_value,
1231                        thin_vec![selflike_arg.clone()],
1232                    );
1233                    cx.stmt_let(span, false, ident, variant_value)
1234                })
1235                .collect();
1236
1237            (discr_field, discr_let_stmts)
1238        };
1239
1240        // There are some special cases involving fieldless enums where no
1241        // match is necessary.
1242        let all_fieldless = variants.iter().all(|v| v.data.fields().is_empty());
1243        if all_fieldless {
1244            if variants.len() > 1 {
1245                match self.fieldless_variants_strategy {
1246                    FieldlessVariantsStrategy::Unify => {
1247                        // If the type is fieldless and the trait uses the discriminant and
1248                        // there are multiple variants, we need just an operation on
1249                        // the discriminant(s).
1250                        let (discr_field, mut discr_let_stmts) = get_discr_pieces(cx);
1251                        let mut discr_check = self.call_substructure_method(
1252                            cx,
1253                            trait_,
1254                            type_ident,
1255                            nonselflike_args,
1256                            &EnumDiscr(discr_field, None),
1257                        );
1258                        discr_let_stmts.append(&mut discr_check.0);
1259                        return BlockOrExpr(discr_let_stmts, discr_check.1);
1260                    }
1261                    FieldlessVariantsStrategy::SpecializeIfAllVariantsFieldless => {
1262                        return self.call_substructure_method(
1263                            cx,
1264                            trait_,
1265                            type_ident,
1266                            nonselflike_args,
1267                            &AllFieldlessEnum(enum_def),
1268                        );
1269                    }
1270                    FieldlessVariantsStrategy::Default => (),
1271                }
1272            } else if let [variant] = variants.as_slice() {
1273                // If there is a single variant, we don't need an operation on
1274                // the discriminant(s). Just use the most degenerate result.
1275                return self.call_substructure_method(
1276                    cx,
1277                    trait_,
1278                    type_ident,
1279                    nonselflike_args,
1280                    &EnumMatching(variant, Vec::new()),
1281                );
1282            }
1283        }
1284
1285        // These arms are of the form:
1286        // (Variant1, Variant1, ...) => Body1
1287        // (Variant2, Variant2, ...) => Body2
1288        // ...
1289        // where each tuple has length = selflike_args.len()
1290        let mut match_arms: ThinVec<ast::Arm> = variants
1291            .iter()
1292            .filter(|&v| !(unify_fieldless_variants && v.data.fields().is_empty()))
1293            .map(|variant| {
1294                // A single arm has form (&VariantK, &VariantK, ...) => BodyK
1295                // (see "Final wrinkle" note below for why.)
1296
1297                let fields = trait_.create_struct_pattern_fields(cx, &variant.data, &prefixes);
1298
1299                let sp = variant.span.with_ctxt(trait_.span.ctxt());
1300                let variant_path = cx.path(sp, vec![type_ident, variant.ident]);
1301                let by_ref = ByRef::No; // because enums can't be repr(packed)
1302                let mut subpats = trait_.create_struct_patterns(
1303                    cx,
1304                    variant_path,
1305                    &variant.data,
1306                    &prefixes,
1307                    by_ref,
1308                );
1309
1310                // `(VariantK, VariantK, ...)` or just `VariantK`.
1311                let single_pat = if subpats.len() == 1 {
1312                    subpats.pop().unwrap()
1313                } else {
1314                    cx.pat_tuple(span, subpats)
1315                };
1316
1317                // For the BodyK, we need to delegate to our caller,
1318                // passing it an EnumMatching to indicate which case
1319                // we are in.
1320                //
1321                // Now, for some given VariantK, we have built up
1322                // expressions for referencing every field of every
1323                // Self arg, assuming all are instances of VariantK.
1324                // Build up code associated with such a case.
1325                let substructure = EnumMatching(variant, fields);
1326                let arm_expr = self
1327                    .call_substructure_method(
1328                        cx,
1329                        trait_,
1330                        type_ident,
1331                        nonselflike_args,
1332                        &substructure,
1333                    )
1334                    .into_expr(cx, span);
1335
1336                cx.arm(span, single_pat, arm_expr)
1337            })
1338            .collect();
1339
1340        // Add a default arm to the match, if necessary.
1341        let first_fieldless = variants.iter().find(|v| v.data.fields().is_empty());
1342        let default = match first_fieldless {
1343            Some(v) if unify_fieldless_variants => {
1344                // We need a default case that handles all the fieldless
1345                // variants. The index and actual variant aren't meaningful in
1346                // this case, so just use dummy values.
1347                Some(
1348                    self.call_substructure_method(
1349                        cx,
1350                        trait_,
1351                        type_ident,
1352                        nonselflike_args,
1353                        &EnumMatching(v, Vec::new()),
1354                    )
1355                    .into_expr(cx, span),
1356                )
1357            }
1358            _ if variants.len() > 1 && selflike_args.len() > 1 => {
1359                // Because we know that all the arguments will match if we reach
1360                // the match expression we add the unreachable intrinsics as the
1361                // result of the default which should help llvm in optimizing it.
1362                Some(deriving::call_unreachable(cx, span))
1363            }
1364            _ => None,
1365        };
1366        if let Some(arm) = default {
1367            match_arms.push(cx.arm(span, cx.pat_wild(span), arm));
1368        }
1369
1370        // Create a match expression with one arm per discriminant plus
1371        // possibly a default arm, e.g.:
1372        //      match (self, other) {
1373        //          (Variant1, Variant1, ...) => Body1
1374        //          (Variant2, Variant2, ...) => Body2,
1375        //          ...
1376        //          _ => ::core::intrinsics::unreachable(),
1377        //      }
1378        let get_match_expr = |mut selflike_args: ThinVec<P<Expr>>| {
1379            let match_arg = if selflike_args.len() == 1 {
1380                selflike_args.pop().unwrap()
1381            } else {
1382                cx.expr(span, ast::ExprKind::Tup(selflike_args))
1383            };
1384            cx.expr_match(span, match_arg, match_arms)
1385        };
1386
1387        // If the trait uses the discriminant and there are multiple variants, we need
1388        // to add a discriminant check operation before the match. Otherwise, the match
1389        // is enough.
1390        if unify_fieldless_variants && variants.len() > 1 {
1391            let (discr_field, mut discr_let_stmts) = get_discr_pieces(cx);
1392
1393            // Combine a discriminant check with the match.
1394            let mut discr_check_plus_match = self.call_substructure_method(
1395                cx,
1396                trait_,
1397                type_ident,
1398                nonselflike_args,
1399                &EnumDiscr(discr_field, Some(get_match_expr(selflike_args))),
1400            );
1401            discr_let_stmts.append(&mut discr_check_plus_match.0);
1402            BlockOrExpr(discr_let_stmts, discr_check_plus_match.1)
1403        } else {
1404            BlockOrExpr(ThinVec::new(), Some(get_match_expr(selflike_args)))
1405        }
1406    }
1407
1408    fn expand_static_enum_method_body(
1409        &self,
1410        cx: &ExtCtxt<'_>,
1411        trait_: &TraitDef<'_>,
1412        enum_def: &EnumDef,
1413        type_ident: Ident,
1414        nonselflike_args: &[P<Expr>],
1415    ) -> BlockOrExpr {
1416        self.call_substructure_method(
1417            cx,
1418            trait_,
1419            type_ident,
1420            nonselflike_args,
1421            &StaticEnum(enum_def),
1422        )
1423    }
1424}
1425
1426// general helper methods.
1427impl<'a> TraitDef<'a> {
1428    fn summarise_struct(&self, cx: &ExtCtxt<'_>, struct_def: &VariantData) -> StaticFields {
1429        let mut named_idents = Vec::new();
1430        let mut just_spans = Vec::new();
1431        for field in struct_def.fields() {
1432            let sp = field.span.with_ctxt(self.span.ctxt());
1433            match field.ident {
1434                Some(ident) => named_idents.push((ident, sp, field.default.clone())),
1435                _ => just_spans.push(sp),
1436            }
1437        }
1438
1439        let is_tuple = match struct_def {
1440            ast::VariantData::Tuple(..) => IsTuple::Yes,
1441            _ => IsTuple::No,
1442        };
1443        match (just_spans.is_empty(), named_idents.is_empty()) {
1444            (false, false) => cx
1445                .dcx()
1446                .span_bug(self.span, "a struct with named and unnamed fields in generic `derive`"),
1447            // named fields
1448            (_, false) => Named(named_idents),
1449            // unnamed fields
1450            (false, _) => Unnamed(just_spans, is_tuple),
1451            // empty
1452            _ => Named(Vec::new()),
1453        }
1454    }
1455
1456    fn create_struct_patterns(
1457        &self,
1458        cx: &ExtCtxt<'_>,
1459        struct_path: ast::Path,
1460        struct_def: &'a VariantData,
1461        prefixes: &[String],
1462        by_ref: ByRef,
1463    ) -> ThinVec<P<ast::Pat>> {
1464        prefixes
1465            .iter()
1466            .map(|prefix| {
1467                let pieces_iter =
1468                    struct_def.fields().iter().enumerate().map(|(i, struct_field)| {
1469                        let sp = struct_field.span.with_ctxt(self.span.ctxt());
1470                        let ident = self.mk_pattern_ident(prefix, i);
1471                        let path = ident.with_span_pos(sp);
1472                        (
1473                            sp,
1474                            struct_field.ident,
1475                            cx.pat(
1476                                path.span,
1477                                PatKind::Ident(BindingMode(by_ref, Mutability::Not), path, None),
1478                            ),
1479                        )
1480                    });
1481
1482                let struct_path = struct_path.clone();
1483                match *struct_def {
1484                    VariantData::Struct { .. } => {
1485                        let field_pats = pieces_iter
1486                            .map(|(sp, ident, pat)| {
1487                                if ident.is_none() {
1488                                    cx.dcx().span_bug(
1489                                        sp,
1490                                        "a braced struct with unnamed fields in `derive`",
1491                                    );
1492                                }
1493                                ast::PatField {
1494                                    ident: ident.unwrap(),
1495                                    is_shorthand: false,
1496                                    attrs: ast::AttrVec::new(),
1497                                    id: ast::DUMMY_NODE_ID,
1498                                    span: pat.span.with_ctxt(self.span.ctxt()),
1499                                    pat,
1500                                    is_placeholder: false,
1501                                }
1502                            })
1503                            .collect();
1504                        cx.pat_struct(self.span, struct_path, field_pats)
1505                    }
1506                    VariantData::Tuple(..) => {
1507                        let subpats = pieces_iter.map(|(_, _, subpat)| subpat).collect();
1508                        cx.pat_tuple_struct(self.span, struct_path, subpats)
1509                    }
1510                    VariantData::Unit(..) => cx.pat_path(self.span, struct_path),
1511                }
1512            })
1513            .collect()
1514    }
1515
1516    fn create_fields<F>(&self, struct_def: &'a VariantData, mk_exprs: F) -> Vec<FieldInfo>
1517    where
1518        F: Fn(usize, &ast::FieldDef, Span) -> Vec<P<ast::Expr>>,
1519    {
1520        struct_def
1521            .fields()
1522            .iter()
1523            .enumerate()
1524            .map(|(i, struct_field)| {
1525                // For this field, get an expr for each selflike_arg. E.g. for
1526                // `PartialEq::eq`, one for each of `&self` and `other`.
1527                let sp = struct_field.span.with_ctxt(self.span.ctxt());
1528                let mut exprs: Vec<_> = mk_exprs(i, struct_field, sp);
1529                let self_expr = exprs.remove(0);
1530                let other_selflike_exprs = exprs;
1531                FieldInfo {
1532                    span: sp.with_ctxt(self.span.ctxt()),
1533                    name: struct_field.ident,
1534                    self_expr,
1535                    other_selflike_exprs,
1536                }
1537            })
1538            .collect()
1539    }
1540
1541    fn mk_pattern_ident(&self, prefix: &str, i: usize) -> Ident {
1542        Ident::from_str_and_span(&format!("{prefix}_{i}"), self.span)
1543    }
1544
1545    fn create_struct_pattern_fields(
1546        &self,
1547        cx: &ExtCtxt<'_>,
1548        struct_def: &'a VariantData,
1549        prefixes: &[String],
1550    ) -> Vec<FieldInfo> {
1551        self.create_fields(struct_def, |i, _struct_field, sp| {
1552            prefixes
1553                .iter()
1554                .map(|prefix| {
1555                    let ident = self.mk_pattern_ident(prefix, i);
1556                    cx.expr_path(cx.path_ident(sp, ident))
1557                })
1558                .collect()
1559        })
1560    }
1561
1562    fn create_struct_field_access_fields(
1563        &self,
1564        cx: &ExtCtxt<'_>,
1565        selflike_args: &[P<Expr>],
1566        struct_def: &'a VariantData,
1567        is_packed: bool,
1568    ) -> Vec<FieldInfo> {
1569        self.create_fields(struct_def, |i, struct_field, sp| {
1570            selflike_args
1571                .iter()
1572                .map(|selflike_arg| {
1573                    // Note: we must use `struct_field.span` rather than `sp` in the
1574                    // `unwrap_or_else` case otherwise the hygiene is wrong and we get
1575                    // "field `0` of struct `Point` is private" errors on tuple
1576                    // structs.
1577                    let mut field_expr = cx.expr(
1578                        sp,
1579                        ast::ExprKind::Field(
1580                            selflike_arg.clone(),
1581                            struct_field.ident.unwrap_or_else(|| {
1582                                Ident::from_str_and_span(&i.to_string(), struct_field.span)
1583                            }),
1584                        ),
1585                    );
1586                    if is_packed {
1587                        // Fields in packed structs are wrapped in a block, e.g. `&{self.0}`,
1588                        // causing a copy instead of a (potentially misaligned) reference.
1589                        field_expr = cx.expr_block(
1590                            cx.block(struct_field.span, thin_vec![cx.stmt_expr(field_expr)]),
1591                        );
1592                    }
1593                    cx.expr_addr_of(sp, field_expr)
1594                })
1595                .collect()
1596        })
1597    }
1598}
1599
1600/// The function passed to `cs_fold` is called repeatedly with a value of this
1601/// type. It describes one part of the code generation. The result is always an
1602/// expression.
1603pub(crate) enum CsFold<'a> {
1604    /// The basic case: a field expression for one or more selflike args. E.g.
1605    /// for `PartialEq::eq` this is something like `self.x == other.x`.
1606    Single(&'a FieldInfo),
1607
1608    /// The combination of two field expressions. E.g. for `PartialEq::eq` this
1609    /// is something like `<field1 equality> && <field2 equality>`.
1610    Combine(Span, P<Expr>, P<Expr>),
1611
1612    // The fallback case for a struct or enum variant with no fields.
1613    Fieldless,
1614}
1615
1616/// Folds over fields, combining the expressions for each field in a sequence.
1617/// Statics may not be folded over.
1618pub(crate) fn cs_fold<F>(
1619    use_foldl: bool,
1620    cx: &ExtCtxt<'_>,
1621    trait_span: Span,
1622    substructure: &Substructure<'_>,
1623    mut f: F,
1624) -> P<Expr>
1625where
1626    F: FnMut(&ExtCtxt<'_>, CsFold<'_>) -> P<Expr>,
1627{
1628    match substructure.fields {
1629        EnumMatching(.., all_fields) | Struct(_, all_fields) => {
1630            if all_fields.is_empty() {
1631                return f(cx, CsFold::Fieldless);
1632            }
1633
1634            let (base_field, rest) = if use_foldl {
1635                all_fields.split_first().unwrap()
1636            } else {
1637                all_fields.split_last().unwrap()
1638            };
1639
1640            let base_expr = f(cx, CsFold::Single(base_field));
1641
1642            let op = |old, field: &FieldInfo| {
1643                let new = f(cx, CsFold::Single(field));
1644                f(cx, CsFold::Combine(field.span, old, new))
1645            };
1646
1647            if use_foldl {
1648                rest.iter().fold(base_expr, op)
1649            } else {
1650                rest.iter().rfold(base_expr, op)
1651            }
1652        }
1653        EnumDiscr(discr_field, match_expr) => {
1654            let discr_check_expr = f(cx, CsFold::Single(discr_field));
1655            if let Some(match_expr) = match_expr {
1656                if use_foldl {
1657                    f(cx, CsFold::Combine(trait_span, discr_check_expr, match_expr.clone()))
1658                } else {
1659                    f(cx, CsFold::Combine(trait_span, match_expr.clone(), discr_check_expr))
1660                }
1661            } else {
1662                discr_check_expr
1663            }
1664        }
1665        StaticEnum(..) | StaticStruct(..) => {
1666            cx.dcx().span_bug(trait_span, "static function in `derive`")
1667        }
1668        AllFieldlessEnum(..) => cx.dcx().span_bug(trait_span, "fieldless enum in `derive`"),
1669    }
1670}