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