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