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