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