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