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.
6 //! Supported features (fairly exhaustive):
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`)
16 //! The most important thing for implementors is the `Substructure` and
17 //! `SubstructureFields` objects. The latter groups 5 possibilities of the
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
30 //! In the first two cases, the values from the corresponding fields in
31 //! all the arguments are grouped together.
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
40 //! # #![allow(dead_code)]
41 //! struct A { x : i32 }
51 //! The `i32`s in `B` and `C0` don't have an identifier, so the
52 //! `Option<ident>`s would be `None` for them.
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.
60 //! # "`cs`" functions
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.
71 //! The following simplified `PartialEq` is used for in-code examples:
75 //! fn eq(&self, other: &Self) -> bool;
77 //! impl PartialEq for i32 {
78 //! fn eq(&self, other: &i32) -> bool {
84 //! Some examples of the values of `SubstructureFields` follow, using the
85 //! above `PartialEq`, `A`, `B` and `C`.
89 //! When generating the `expr` for the `A` impl, the `SubstructureFields` is
92 //! Struct(vec![FieldInfo {
94 //! name: Some(<ident of x>),
95 //! self_: <expr for &self.x>,
96 //! other: vec![<expr for &other.x]
100 //! For the `B` impl, called with `B(a)` and `B(b)`,
103 //! Struct(vec![FieldInfo {
104 //! span: <span of `i32`>,
106 //! self_: <expr for &a>
107 //! other: vec![<expr for &b>]
113 //! When generating the `expr` for a call with `self == C0(a)` and `other
114 //! == C0(b)`, the SubstructureFields is
117 //! EnumMatching(0, <ast::Variant for C0>,
119 //! span: <span of i32>
121 //! self_: <expr for &a>,
122 //! other: vec![<expr for &b>]
126 //! For `C1 {x}` and `C1 {x}`,
129 //! EnumMatching(1, <ast::Variant for C1>,
131 //! span: <span of x>
132 //! name: Some(<ident of x>),
133 //! self_: <expr for &self.x>,
134 //! other: vec![<expr for &other.x>]
142 //! &[<ident of self tag>, <ident of other tag>], <expr to combine with>)
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).
150 //! A static method on the types above would result in,
153 //! StaticStruct(<ast::VariantData of A>, Named(vec![(<ident of x>, <span of x>)]))
155 //! StaticStruct(<ast::VariantData of B>, Unnamed(vec![<span of x>]))
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>)]))])
162 pub use StaticFields::*;
163 pub use SubstructureFields::*;
165 use std::cell::RefCell;
169 use rustc_ast::ptr::P;
170 use rustc_ast::{self as ast, EnumDef, Expr, Generics, PatKind};
171 use rustc_ast::{GenericArg, GenericParamKind, VariantData};
172 use rustc_attr as attr;
173 use rustc_expand::base::{Annotatable, ExtCtxt};
174 use rustc_span::symbol::{kw, sym, Ident, Symbol};
175 use rustc_span::Span;
177 use ty::{Bounds, Path, Ref, Self_, Ty};
183 pub struct TraitDef<'a> {
184 /// The span for the current #[derive(Foo)] header.
187 /// Path of the trait, including any type parameters
190 /// Additional bounds required of any type parameters of the type,
191 /// other than the current trait
192 pub additional_bounds: Vec<Ty>,
194 /// Any extra lifetimes and/or bounds, e.g., `D: serialize::Decoder`
195 pub generics: Bounds,
197 /// Can this trait be derived for unions?
198 pub supports_unions: bool,
200 pub methods: Vec<MethodDef<'a>>,
202 pub associated_types: Vec<(Ident, Ty)>,
205 pub struct MethodDef<'a> {
206 /// name of the method
208 /// List of generics, e.g., `R: rand::Rng`
209 pub generics: Bounds,
211 /// Is there is a `&self` argument? If not, it is a static function.
212 pub explicit_self: bool,
214 /// Arguments other than the self argument.
215 pub nonself_args: Vec<(Ty, Symbol)>,
220 pub attributes: ast::AttrVec,
222 /// Can we combine fieldless variants for enums into a single match arm?
223 /// If true, indicates that the trait operation uses the enum tag in some
225 pub unify_fieldless_variants: bool,
227 pub combine_substructure: RefCell<CombineSubstructureFunc<'a>>,
230 /// All the data about the data structure/method being derived upon.
231 pub struct Substructure<'a> {
233 pub type_ident: Ident,
234 /// Verbatim access to any non-selflike arguments, i.e. arguments that
235 /// don't have type `&Self`.
236 pub nonselflike_args: &'a [P<Expr>],
237 pub fields: &'a SubstructureFields<'a>,
240 /// Summary of the relevant parts of a struct/enum field.
241 pub struct FieldInfo {
243 /// None for tuple structs/normal enum variants, Some for normal
244 /// structs/struct enum variants.
245 pub name: Option<Ident>,
246 /// The expression corresponding to this field of `self`
247 /// (specifically, a reference to it).
248 pub self_expr: P<Expr>,
249 /// The expressions corresponding to references to this field in
250 /// the other selflike arguments.
251 pub other_selflike_exprs: Vec<P<Expr>>,
254 /// Fields for a static method
255 pub enum StaticFields {
256 /// Tuple and unit structs/enum variants like this.
257 Unnamed(Vec<Span>, bool /*is tuple*/),
258 /// Normal structs/struct variants.
259 Named(Vec<(Ident, Span)>),
262 /// A summary of the possible sets of fields.
263 pub enum SubstructureFields<'a> {
264 /// A non-static method with `Self` is a struct.
265 Struct(&'a ast::VariantData, Vec<FieldInfo>),
267 /// Matching variants of the enum: variant index, variant count, ast::Variant,
268 /// fields: the field name is only non-`None` in the case of a struct
270 EnumMatching(usize, usize, &'a ast::Variant, Vec<FieldInfo>),
272 /// The tag of an enum. The first field is a `FieldInfo` for the tags, as
273 /// if they were fields. The second field is the expression to combine the
274 /// tag expression with; it will be `None` if no match is necessary.
275 EnumTag(FieldInfo, Option<P<Expr>>),
277 /// A static method where `Self` is a struct.
278 StaticStruct(&'a ast::VariantData, StaticFields),
280 /// A static method where `Self` is an enum.
281 StaticEnum(&'a ast::EnumDef, Vec<(Ident, Span, StaticFields)>),
284 /// Combine the values of all the fields together. The last argument is
285 /// all the fields of all the structures.
286 pub type CombineSubstructureFunc<'a> =
287 Box<dyn FnMut(&mut ExtCtxt<'_>, Span, &Substructure<'_>) -> BlockOrExpr + 'a>;
289 pub fn combine_substructure(
290 f: CombineSubstructureFunc<'_>,
291 ) -> RefCell<CombineSubstructureFunc<'_>> {
295 struct TypeParameter {
296 bound_generic_params: Vec<ast::GenericParam>,
300 // The code snippets built up for derived code are sometimes used as blocks
301 // (e.g. in a function body) and sometimes used as expressions (e.g. in a match
302 // arm). This structure avoids committing to either form until necessary,
303 // avoiding the insertion of any unnecessary blocks.
305 // The statements come before the expression.
306 pub struct BlockOrExpr(Vec<ast::Stmt>, Option<P<Expr>>);
309 pub fn new_stmts(stmts: Vec<ast::Stmt>) -> BlockOrExpr {
310 BlockOrExpr(stmts, None)
313 pub fn new_expr(expr: P<Expr>) -> BlockOrExpr {
314 BlockOrExpr(vec![], Some(expr))
317 pub fn new_mixed(stmts: Vec<ast::Stmt>, expr: Option<P<Expr>>) -> BlockOrExpr {
318 BlockOrExpr(stmts, expr)
321 // Converts it into a block.
322 fn into_block(mut self, cx: &ExtCtxt<'_>, span: Span) -> P<ast::Block> {
323 if let Some(expr) = self.1 {
324 self.0.push(cx.stmt_expr(expr));
326 cx.block(span, self.0)
329 // Converts it into an expression.
330 fn into_expr(self, cx: &ExtCtxt<'_>, span: Span) -> P<Expr> {
331 if self.0.is_empty() {
333 None => cx.expr_block(cx.block(span, vec![])),
336 } else if self.0.len() == 1
337 && let ast::StmtKind::Expr(expr) = &self.0[0].kind
340 // There's only a single statement expression. Pull it out.
343 // Multiple statements and/or expressions.
344 cx.expr_block(self.into_block(cx, span))
349 /// This method helps to extract all the type parameters referenced from a
350 /// type. For a type parameter `<T>`, it looks for either a `TyPath` that
351 /// is not global and starts with `T`, or a `TyQPath`.
352 /// Also include bound generic params from the input type.
353 fn find_type_parameters(
355 ty_param_names: &[Symbol],
357 ) -> Vec<TypeParameter> {
358 use rustc_ast::visit;
360 struct Visitor<'a, 'b> {
362 ty_param_names: &'a [Symbol],
363 bound_generic_params_stack: Vec<ast::GenericParam>,
364 type_params: Vec<TypeParameter>,
367 impl<'a, 'b> visit::Visitor<'a> for Visitor<'a, 'b> {
368 fn visit_ty(&mut self, ty: &'a ast::Ty) {
369 if let ast::TyKind::Path(_, ref path) = ty.kind {
370 if let Some(segment) = path.segments.first() {
371 if self.ty_param_names.contains(&segment.ident.name) {
372 self.type_params.push(TypeParameter {
373 bound_generic_params: self.bound_generic_params_stack.clone(),
380 visit::walk_ty(self, ty)
383 // Place bound generic params on a stack, to extract them when a type is encountered.
384 fn visit_poly_trait_ref(&mut self, trait_ref: &'a ast::PolyTraitRef) {
385 let stack_len = self.bound_generic_params_stack.len();
386 self.bound_generic_params_stack
387 .extend(trait_ref.bound_generic_params.clone().into_iter());
389 visit::walk_poly_trait_ref(self, trait_ref);
391 self.bound_generic_params_stack.truncate(stack_len);
394 fn visit_mac_call(&mut self, mac: &ast::MacCall) {
395 self.cx.span_err(mac.span(), "`derive` cannot be used on items with type macros");
399 let mut visitor = Visitor {
402 bound_generic_params_stack: Vec::new(),
403 type_params: Vec::new(),
405 visit::Visitor::visit_ty(&mut visitor, ty);
410 impl<'a> TraitDef<'a> {
413 cx: &mut ExtCtxt<'_>,
414 mitem: &ast::MetaItem,
415 item: &'a Annotatable,
416 push: &mut dyn FnMut(Annotatable),
418 self.expand_ext(cx, mitem, item, push, false);
423 cx: &mut ExtCtxt<'_>,
424 mitem: &ast::MetaItem,
425 item: &'a Annotatable,
426 push: &mut dyn FnMut(Annotatable),
430 Annotatable::Item(ref item) => {
431 let is_packed = item.attrs.iter().any(|attr| {
432 for r in attr::find_repr_attrs(&cx.sess, attr) {
433 if let attr::ReprPacked(_) = r {
439 let has_no_type_params = match item.kind {
440 ast::ItemKind::Struct(_, ref generics)
441 | ast::ItemKind::Enum(_, ref generics)
442 | ast::ItemKind::Union(_, ref generics) => !generics
445 .any(|param| matches!(param.kind, ast::GenericParamKind::Type { .. })),
448 let container_id = cx.current_expansion.id.expn_data().parent.expect_local();
449 let always_copy = has_no_type_params && cx.resolver.has_derive_copy(container_id);
451 let newitem = match item.kind {
452 ast::ItemKind::Struct(ref struct_def, ref generics) => self.expand_struct_def(
461 ast::ItemKind::Enum(ref enum_def, ref generics) => {
462 // We ignore `is_packed`/`always_copy` here, because
463 // `repr(packed)` enums cause an error later on.
465 // This can only cause further compilation errors
466 // downstream in blatantly illegal code, so it
468 self.expand_enum_def(cx, enum_def, item.ident, generics, from_scratch)
470 ast::ItemKind::Union(ref struct_def, ref generics) => {
471 if self.supports_unions {
472 self.expand_struct_def(
482 cx.span_err(mitem.span, "this trait cannot be derived for unions");
488 // Keep the lint attributes of the previous item to control how the
489 // generated implementations are linted
490 let mut attrs = newitem.attrs.clone();
503 .contains(&a.name_or_empty())
507 push(Annotatable::Item(P(ast::Item { attrs, ..(*newitem).clone() })))
513 /// Given that we are deriving a trait `DerivedTrait` for a type like:
515 /// ```ignore (only-for-syntax-highlight)
516 /// struct Struct<'a, ..., 'z, A, B: DeclaredTrait, C, ..., Z> where C: WhereTrait {
519 /// b1: <B as DeclaredTrait>::Item,
520 /// c1: <C as WhereTrait>::Item,
521 /// c2: Option<<C as WhereTrait>::Item>,
526 /// create an impl like:
528 /// ```ignore (only-for-syntax-highlight)
529 /// impl<'a, ..., 'z, A, B: DeclaredTrait, C, ... Z> where
531 /// A: DerivedTrait + B1 + ... + BN,
532 /// B: DerivedTrait + B1 + ... + BN,
533 /// C: DerivedTrait + B1 + ... + BN,
534 /// B::Item: DerivedTrait + B1 + ... + BN,
535 /// <C as WhereTrait>::Item: DerivedTrait + B1 + ... + BN,
542 /// where B1, ..., BN are the bounds given by `bounds_paths`.'. Z is a phantom type, and
543 /// therefore does not get bound by the derived trait.
544 fn create_derived_impl(
546 cx: &mut ExtCtxt<'_>,
549 field_tys: Vec<P<ast::Ty>>,
550 methods: Vec<P<ast::AssocItem>>,
552 let trait_path = self.path.to_path(cx, self.span, type_ident, generics);
554 // Transform associated types from `deriving::ty::Ty` into `ast::AssocItem`
555 let associated_types = self.associated_types.iter().map(|&(ident, ref type_def)| {
557 id: ast::DUMMY_NODE_ID,
560 vis: ast::Visibility {
561 span: self.span.shrink_to_lo(),
562 kind: ast::VisibilityKind::Inherited,
565 attrs: ast::AttrVec::new(),
566 kind: ast::AssocItemKind::TyAlias(Box::new(ast::TyAlias {
567 defaultness: ast::Defaultness::Final,
568 generics: Generics::default(),
570 ast::TyAliasWhereClause::default(),
571 ast::TyAliasWhereClause::default(),
573 where_predicates_split: 0,
575 ty: Some(type_def.to_ty(cx, self.span, type_ident, generics)),
581 let Generics { mut params, mut where_clause, .. } =
582 self.generics.to_generics(cx, self.span, type_ident, generics);
583 where_clause.span = generics.where_clause.span;
584 let ctxt = self.span.ctxt();
585 let span = generics.span.with_ctxt(ctxt);
587 // Create the generic parameters
588 params.extend(generics.params.iter().map(|param| match ¶m.kind {
589 GenericParamKind::Lifetime { .. } => param.clone(),
590 GenericParamKind::Type { .. } => {
591 // I don't think this can be moved out of the loop, since
592 // a GenericBound requires an ast id
594 // extra restrictions on the generics parameters to the
595 // type being derived upon
596 self.additional_bounds.iter().map(|p| {
597 cx.trait_bound(p.to_path(cx, self.span, type_ident, generics))
599 // require the current trait
600 iter::once(cx.trait_bound(trait_path.clone()))
602 // also add in any bounds from the declaration
603 param.bounds.iter().cloned()
606 cx.typaram(param.ident.span.with_ctxt(ctxt), param.ident, bounds, None)
608 GenericParamKind::Const { ty, kw_span, .. } => {
609 let const_nodefault_kind = GenericParamKind::Const {
611 kw_span: kw_span.with_ctxt(ctxt),
613 // We can't have default values inside impl block
616 let mut param_clone = param.clone();
617 param_clone.kind = const_nodefault_kind;
622 // and similarly for where clauses
623 where_clause.predicates.extend(generics.where_clause.predicates.iter().map(|clause| {
625 ast::WherePredicate::BoundPredicate(wb) => {
626 let span = wb.span.with_ctxt(ctxt);
627 ast::WherePredicate::BoundPredicate(ast::WhereBoundPredicate {
632 ast::WherePredicate::RegionPredicate(wr) => {
633 let span = wr.span.with_ctxt(ctxt);
634 ast::WherePredicate::RegionPredicate(ast::WhereRegionPredicate {
639 ast::WherePredicate::EqPredicate(we) => {
640 let span = we.span.with_ctxt(ctxt);
641 ast::WherePredicate::EqPredicate(ast::WhereEqPredicate { span, ..we.clone() })
647 // Extra scope required here so ty_params goes out of scope before params is moved
649 let mut ty_params = params
651 .filter(|param| matches!(param.kind, ast::GenericParamKind::Type { .. }))
654 if ty_params.peek().is_some() {
655 let ty_param_names: Vec<Symbol> =
656 ty_params.map(|ty_param| ty_param.ident.name).collect();
658 for field_ty in field_tys {
659 let field_ty_params = find_type_parameters(&field_ty, &ty_param_names, cx);
661 for field_ty_param in field_ty_params {
662 // if we have already handled this type, skip it
663 if let ast::TyKind::Path(_, ref p) = field_ty_param.ty.kind {
664 if p.segments.len() == 1
665 && ty_param_names.contains(&p.segments[0].ident.name)
670 let mut bounds: Vec<_> = self
673 .map(|p| cx.trait_bound(p.to_path(cx, self.span, type_ident, generics)))
676 // require the current trait
677 bounds.push(cx.trait_bound(trait_path.clone()));
679 let predicate = ast::WhereBoundPredicate {
681 bound_generic_params: field_ty_param.bound_generic_params,
682 bounded_ty: field_ty_param.ty,
686 let predicate = ast::WherePredicate::BoundPredicate(predicate);
687 where_clause.predicates.push(predicate);
693 let trait_generics = Generics { params, where_clause, span };
695 // Create the reference to the trait.
696 let trait_ref = cx.trait_ref(trait_path);
698 let self_params: Vec<_> = generics
701 .map(|param| match param.kind {
702 GenericParamKind::Lifetime { .. } => {
703 GenericArg::Lifetime(cx.lifetime(param.ident.span.with_ctxt(ctxt), param.ident))
705 GenericParamKind::Type { .. } => {
706 GenericArg::Type(cx.ty_ident(param.ident.span.with_ctxt(ctxt), param.ident))
708 GenericParamKind::Const { .. } => {
709 GenericArg::Const(cx.const_ident(param.ident.span.with_ctxt(ctxt), param.ident))
714 // Create the type of `self`.
715 let path = cx.path_all(self.span, false, vec![type_ident], self_params);
716 let self_type = cx.ty_path(path);
718 let attr = cx.attribute(cx.meta_word(self.span, sym::automatically_derived));
719 let attrs = vec![attr].into();
720 let opt_trait_ref = Some(trait_ref);
726 ast::ItemKind::Impl(Box::new(ast::Impl {
727 unsafety: ast::Unsafe::No,
728 polarity: ast::ImplPolarity::Positive,
729 defaultness: ast::Defaultness::Final,
730 constness: ast::Const::No,
731 generics: trait_generics,
732 of_trait: opt_trait_ref,
734 items: methods.into_iter().chain(associated_types).collect(),
739 fn expand_struct_def(
741 cx: &mut ExtCtxt<'_>,
742 struct_def: &'a VariantData,
749 let field_tys: Vec<P<ast::Ty>> =
750 struct_def.fields().iter().map(|field| field.ty.clone()).collect();
756 let (explicit_self, selflike_args, nonselflike_args, nonself_arg_tys) =
757 method_def.extract_arg_details(cx, self, type_ident, generics);
759 let body = if from_scratch || method_def.is_static() {
760 method_def.expand_static_struct_method_body(
768 method_def.expand_struct_method_body(
780 method_def.create_method(
792 self.create_derived_impl(cx, type_ident, generics, field_tys, methods)
797 cx: &mut ExtCtxt<'_>,
798 enum_def: &'a EnumDef,
803 let mut field_tys = Vec::new();
805 for variant in &enum_def.variants {
806 field_tys.extend(variant.data.fields().iter().map(|field| field.ty.clone()));
813 let (explicit_self, selflike_args, nonselflike_args, nonself_arg_tys) =
814 method_def.extract_arg_details(cx, self, type_ident, generics);
816 let body = if from_scratch || method_def.is_static() {
817 method_def.expand_static_enum_method_body(
825 method_def.expand_enum_method_body(
835 method_def.create_method(
847 self.create_derived_impl(cx, type_ident, generics, field_tys, methods)
851 impl<'a> MethodDef<'a> {
852 fn call_substructure_method(
854 cx: &mut ExtCtxt<'_>,
855 trait_: &TraitDef<'_>,
857 nonselflike_args: &[P<Expr>],
858 fields: &SubstructureFields<'_>,
860 let span = trait_.span;
861 let substructure = Substructure { type_ident, nonselflike_args, fields };
862 let mut f = self.combine_substructure.borrow_mut();
863 let f: &mut CombineSubstructureFunc<'_> = &mut *f;
864 f(cx, span, &substructure)
869 cx: &mut ExtCtxt<'_>,
870 trait_: &TraitDef<'_>,
874 self.ret_ty.to_ty(cx, trait_.span, type_ident, generics)
877 fn is_static(&self) -> bool {
881 // The return value includes:
882 // - explicit_self: The `&self` arg, if present.
883 // - selflike_args: Expressions for `&self` (if present) and also any other
884 // args with the same type (e.g. the `other` arg in `PartialEq::eq`).
885 // - nonselflike_args: Expressions for all the remaining args.
886 // - nonself_arg_tys: Additional information about all the args other than
888 fn extract_arg_details(
890 cx: &mut ExtCtxt<'_>,
891 trait_: &TraitDef<'_>,
894 ) -> (Option<ast::ExplicitSelf>, Vec<P<Expr>>, Vec<P<Expr>>, Vec<(Ident, P<ast::Ty>)>) {
895 let mut selflike_args = Vec::new();
896 let mut nonselflike_args = Vec::new();
897 let mut nonself_arg_tys = Vec::new();
898 let span = trait_.span;
900 let explicit_self = if self.explicit_self {
901 let (self_expr, explicit_self) = ty::get_explicit_self(cx, span);
902 selflike_args.push(self_expr);
908 for (ty, name) in self.nonself_args.iter() {
909 let ast_ty = ty.to_ty(cx, span, type_ident, generics);
910 let ident = Ident::new(*name, span);
911 nonself_arg_tys.push((ident, ast_ty));
913 let arg_expr = cx.expr_ident(span, ident);
916 // Selflike (`&Self`) arguments only occur in non-static methods.
917 Ref(box Self_, _) if !self.is_static() => selflike_args.push(arg_expr),
918 Self_ => cx.span_bug(span, "`Self` in non-return position"),
919 _ => nonselflike_args.push(arg_expr),
923 (explicit_self, selflike_args, nonselflike_args, nonself_arg_tys)
928 cx: &mut ExtCtxt<'_>,
929 trait_: &TraitDef<'_>,
932 explicit_self: Option<ast::ExplicitSelf>,
933 nonself_arg_tys: Vec<(Ident, P<ast::Ty>)>,
935 ) -> P<ast::AssocItem> {
936 let span = trait_.span;
937 // Create the generics that aren't for `Self`.
938 let fn_generics = self.generics.to_generics(cx, span, type_ident, generics);
941 let self_arg = explicit_self.map(|explicit_self| {
942 let ident = Ident::with_dummy_span(kw::SelfLower).with_span_pos(span);
943 ast::Param::from_self(ast::AttrVec::default(), explicit_self, ident)
946 nonself_arg_tys.into_iter().map(|(name, ty)| cx.param(span, name, ty));
947 self_arg.into_iter().chain(nonself_args).collect()
950 let ret_type = self.get_ret_ty(cx, trait_, generics, type_ident);
952 let method_ident = Ident::new(self.name, span);
953 let fn_decl = cx.fn_decl(args, ast::FnRetTy::Ty(ret_type));
954 let body_block = body.into_block(cx, span);
956 let trait_lo_sp = span.shrink_to_lo();
958 let sig = ast::FnSig { header: ast::FnHeader::default(), decl: fn_decl, span };
959 let defaultness = ast::Defaultness::Final;
961 // Create the method.
963 id: ast::DUMMY_NODE_ID,
964 attrs: self.attributes.clone(),
966 vis: ast::Visibility {
968 kind: ast::VisibilityKind::Inherited,
972 kind: ast::AssocItemKind::Fn(Box::new(ast::Fn {
975 generics: fn_generics,
976 body: Some(body_block),
982 /// The normal case uses field access.
984 /// #[derive(PartialEq)]
986 /// struct A { x: u8, y: u8 }
988 /// // equivalent to:
989 /// impl PartialEq for A {
990 /// fn eq(&self, other: &A) -> bool {
991 /// self.x == other.x && self.y == other.y
995 /// But if the struct is `repr(packed)`, we can't use something like
996 /// `&self.x` because that might cause an unaligned ref. So for any trait
997 /// method that takes a reference, if the struct impls `Copy` then we use a
998 /// local block to force a copy:
1000 /// # struct A { x: u8, y: u8 }
1001 /// impl PartialEq for A {
1002 /// fn eq(&self, other: &A) -> bool {
1003 /// // Desugars to `{ self.x }.eq(&{ other.y }) && ...`
1004 /// { self.x } == { other.y } && { self.y } == { other.y }
1007 /// impl Hash for A {
1008 /// fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () {
1009 /// ::core::hash::Hash::hash(&{ self.x }, state);
1010 /// ::core::hash::Hash::hash(&{ self.y }, state)
1014 /// If the struct doesn't impl `Copy`, we use let-destructuring with `ref`:
1016 /// # struct A { x: u8, y: u8 }
1017 /// impl PartialEq for A {
1018 /// fn eq(&self, other: &A) -> bool {
1019 /// let Self { x: ref __self_0_0, y: ref __self_0_1 } = *self;
1020 /// let Self { x: ref __self_1_0, y: ref __self_1_1 } = *other;
1021 /// *__self_0_0 == *__self_1_0 && *__self_0_1 == *__self_1_1
1025 /// This latter case only works if the fields match the alignment required
1026 /// by the `packed(N)` attribute. (We'll get errors later on if not.)
1027 fn expand_struct_method_body<'b>(
1029 cx: &mut ExtCtxt<'_>,
1030 trait_: &TraitDef<'b>,
1031 struct_def: &'b VariantData,
1033 selflike_args: &[P<Expr>],
1034 nonselflike_args: &[P<Expr>],
1038 let span = trait_.span;
1039 assert!(selflike_args.len() == 1 || selflike_args.len() == 2);
1041 let mk_body = |cx, selflike_fields| {
1042 self.call_substructure_method(
1047 &Struct(struct_def, selflike_fields),
1052 let selflike_fields =
1053 trait_.create_struct_field_access_fields(cx, selflike_args, struct_def, false);
1054 mk_body(cx, selflike_fields)
1055 } else if always_copy {
1056 let selflike_fields =
1057 trait_.create_struct_field_access_fields(cx, selflike_args, struct_def, true);
1058 mk_body(cx, selflike_fields)
1060 // Neither packed nor copy. Need to use ref patterns.
1061 let prefixes: Vec<_> =
1062 (0..selflike_args.len()).map(|i| format!("__self_{}", i)).collect();
1063 let addr_of = always_copy;
1064 let selflike_fields =
1065 trait_.create_struct_pattern_fields(cx, struct_def, &prefixes, addr_of);
1066 let mut body = mk_body(cx, selflike_fields);
1068 let struct_path = cx.path(span, vec![Ident::new(kw::SelfUpper, type_ident.span)]);
1069 let use_ref_pat = is_packed && !always_copy;
1071 trait_.create_struct_patterns(cx, struct_path, struct_def, &prefixes, use_ref_pat);
1073 // Do the let-destructuring.
1074 let mut stmts: Vec<_> = iter::zip(selflike_args, patterns)
1075 .map(|(selflike_arg_expr, pat)| {
1076 let selflike_arg_expr = cx.expr_deref(span, selflike_arg_expr.clone());
1077 cx.stmt_let_pat(span, pat, selflike_arg_expr)
1080 stmts.extend(std::mem::take(&mut body.0));
1081 BlockOrExpr(stmts, body.1)
1085 fn expand_static_struct_method_body(
1087 cx: &mut ExtCtxt<'_>,
1088 trait_: &TraitDef<'_>,
1089 struct_def: &VariantData,
1091 nonselflike_args: &[P<Expr>],
1093 let summary = trait_.summarise_struct(cx, struct_def);
1095 self.call_substructure_method(
1100 &StaticStruct(struct_def, summary),
1105 /// #[derive(PartialEq)]
1112 /// is equivalent to:
1114 /// impl ::core::cmp::PartialEq for A {
1116 /// fn eq(&self, other: &A) -> bool {
1117 /// let __self_tag = ::core::intrinsics::discriminant_value(self);
1118 /// let __arg1_tag = ::core::intrinsics::discriminant_value(other);
1119 /// __self_tag == __arg1_tag &&
1120 /// match (self, other) {
1121 /// (A::A2(__self_0), A::A2(__arg1_0)) =>
1122 /// *__self_0 == *__arg1_0,
1128 /// Creates a tag check combined with a match for a tuple of all
1129 /// `selflike_args`, with an arm for each variant with fields, possibly an
1130 /// arm for each fieldless variant (if `!unify_fieldless_variants` is not
1131 /// true), and possibly a default arm.
1132 fn expand_enum_method_body<'b>(
1134 cx: &mut ExtCtxt<'_>,
1135 trait_: &TraitDef<'b>,
1136 enum_def: &'b EnumDef,
1138 selflike_args: Vec<P<Expr>>,
1139 nonselflike_args: &[P<Expr>],
1141 let span = trait_.span;
1142 let variants = &enum_def.variants;
1144 // Traits that unify fieldless variants always use the tag(s).
1145 let uses_tags = self.unify_fieldless_variants;
1147 // There is no sensible code to be generated for *any* deriving on a
1148 // zero-variant enum. So we just generate a failing expression.
1149 if variants.is_empty() {
1150 return BlockOrExpr(vec![], Some(deriving::call_unreachable(cx, span)));
1153 let prefixes = iter::once("__self".to_string())
1159 .map(|(arg_count, _selflike_arg)| format!("__arg{}", arg_count)),
1161 .collect::<Vec<String>>();
1163 // Build a series of let statements mapping each selflike_arg
1164 // to its discriminant value.
1166 // e.g. for `PartialEq::eq` builds two statements:
1168 // let __self_tag = ::core::intrinsics::discriminant_value(self);
1169 // let __arg1_tag = ::core::intrinsics::discriminant_value(other);
1171 let get_tag_pieces = |cx: &ExtCtxt<'_>| {
1172 let tag_idents: Vec<_> = prefixes
1174 .map(|name| Ident::from_str_and_span(&format!("{}_tag", name), span))
1177 let mut tag_exprs: Vec<_> = tag_idents
1179 .map(|&ident| cx.expr_addr_of(span, cx.expr_ident(span, ident)))
1182 let self_expr = tag_exprs.remove(0);
1183 let other_selflike_exprs = tag_exprs;
1184 let tag_field = FieldInfo { span, name: None, self_expr, other_selflike_exprs };
1186 let tag_let_stmts: Vec<_> = iter::zip(&tag_idents, &selflike_args)
1187 .map(|(&ident, selflike_arg)| {
1188 let variant_value = deriving::call_intrinsic(
1191 sym::discriminant_value,
1192 vec![selflike_arg.clone()],
1194 cx.stmt_let(span, false, ident, variant_value)
1198 (tag_field, tag_let_stmts)
1201 // There are some special cases involving fieldless enums where no
1202 // match is necessary.
1203 let all_fieldless = variants.iter().all(|v| v.data.fields().is_empty());
1205 if uses_tags && variants.len() > 1 {
1206 // If the type is fieldless and the trait uses the tag and
1207 // there are multiple variants, we need just an operation on
1209 let (tag_field, mut tag_let_stmts) = get_tag_pieces(cx);
1210 let mut tag_check = self.call_substructure_method(
1215 &EnumTag(tag_field, None),
1217 tag_let_stmts.append(&mut tag_check.0);
1218 return BlockOrExpr(tag_let_stmts, tag_check.1);
1221 if variants.len() == 1 {
1222 // If there is a single variant, we don't need an operation on
1223 // the tag(s). Just use the most degenerate result.
1224 return self.call_substructure_method(
1229 &EnumMatching(0, 1, &variants[0], Vec::new()),
1234 // These arms are of the form:
1235 // (Variant1, Variant1, ...) => Body1
1236 // (Variant2, Variant2, ...) => Body2
1238 // where each tuple has length = selflike_args.len()
1239 let mut match_arms: Vec<ast::Arm> = variants
1242 .filter(|&(_, v)| !(self.unify_fieldless_variants && v.data.fields().is_empty()))
1243 .map(|(index, variant)| {
1244 // A single arm has form (&VariantK, &VariantK, ...) => BodyK
1245 // (see "Final wrinkle" note below for why.)
1247 let addr_of = false; // because enums can't be repr(packed)
1249 trait_.create_struct_pattern_fields(cx, &variant.data, &prefixes, addr_of);
1251 let sp = variant.span.with_ctxt(trait_.span.ctxt());
1252 let variant_path = cx.path(sp, vec![type_ident, variant.ident]);
1253 let use_ref_pat = false; // because enums can't be repr(packed)
1254 let mut subpats: Vec<_> = trait_.create_struct_patterns(
1262 // `(VariantK, VariantK, ...)` or just `VariantK`.
1263 let single_pat = if subpats.len() == 1 {
1264 subpats.pop().unwrap()
1266 cx.pat_tuple(span, subpats)
1269 // For the BodyK, we need to delegate to our caller,
1270 // passing it an EnumMatching to indicate which case
1273 // Now, for some given VariantK, we have built up
1274 // expressions for referencing every field of every
1275 // Self arg, assuming all are instances of VariantK.
1276 // Build up code associated with such a case.
1277 let substructure = EnumMatching(index, variants.len(), variant, fields);
1279 .call_substructure_method(
1286 .into_expr(cx, span);
1288 cx.arm(span, single_pat, arm_expr)
1292 // Add a default arm to the match, if necessary.
1293 let first_fieldless = variants.iter().find(|v| v.data.fields().is_empty());
1294 let default = match first_fieldless {
1295 Some(v) if self.unify_fieldless_variants => {
1296 // We need a default case that handles all the fieldless
1297 // variants. The index and actual variant aren't meaningful in
1298 // this case, so just use dummy values.
1300 self.call_substructure_method(
1305 &EnumMatching(0, variants.len(), v, Vec::new()),
1307 .into_expr(cx, span),
1310 _ if variants.len() > 1 && selflike_args.len() > 1 => {
1311 // Because we know that all the arguments will match if we reach
1312 // the match expression we add the unreachable intrinsics as the
1313 // result of the default which should help llvm in optimizing it.
1314 Some(deriving::call_unreachable(cx, span))
1318 if let Some(arm) = default {
1319 match_arms.push(cx.arm(span, cx.pat_wild(span), arm));
1322 // Create a match expression with one arm per discriminant plus
1323 // possibly a default arm, e.g.:
1324 // match (self, other) {
1325 // (Variant1, Variant1, ...) => Body1
1326 // (Variant2, Variant2, ...) => Body2,
1328 // _ => ::core::intrinsics::unreachable()
1330 let get_match_expr = |mut selflike_args: Vec<P<Expr>>| {
1331 let match_arg = if selflike_args.len() == 1 {
1332 selflike_args.pop().unwrap()
1334 cx.expr(span, ast::ExprKind::Tup(selflike_args))
1336 cx.expr_match(span, match_arg, match_arms)
1339 // If the trait uses the tag and there are multiple variants, we need
1340 // to add a tag check operation before the match. Otherwise, the match
1342 if uses_tags && variants.len() > 1 {
1343 let (tag_field, mut tag_let_stmts) = get_tag_pieces(cx);
1345 // Combine a tag check with the match.
1346 let mut tag_check_plus_match = self.call_substructure_method(
1351 &EnumTag(tag_field, Some(get_match_expr(selflike_args))),
1353 tag_let_stmts.append(&mut tag_check_plus_match.0);
1354 BlockOrExpr(tag_let_stmts, tag_check_plus_match.1)
1356 BlockOrExpr(vec![], Some(get_match_expr(selflike_args)))
1360 fn expand_static_enum_method_body(
1362 cx: &mut ExtCtxt<'_>,
1363 trait_: &TraitDef<'_>,
1366 nonselflike_args: &[P<Expr>],
1368 let summary = enum_def
1372 let sp = v.span.with_ctxt(trait_.span.ctxt());
1373 let summary = trait_.summarise_struct(cx, &v.data);
1374 (v.ident, sp, summary)
1377 self.call_substructure_method(
1382 &StaticEnum(enum_def, summary),
1387 // general helper methods.
1388 impl<'a> TraitDef<'a> {
1389 fn summarise_struct(&self, cx: &mut ExtCtxt<'_>, struct_def: &VariantData) -> StaticFields {
1390 let mut named_idents = Vec::new();
1391 let mut just_spans = Vec::new();
1392 for field in struct_def.fields() {
1393 let sp = field.span.with_ctxt(self.span.ctxt());
1395 Some(ident) => named_idents.push((ident, sp)),
1396 _ => just_spans.push(sp),
1400 let is_tuple = matches!(struct_def, ast::VariantData::Tuple(..));
1401 match (just_spans.is_empty(), named_idents.is_empty()) {
1403 cx.span_bug(self.span, "a struct with named and unnamed fields in generic `derive`")
1406 (_, false) => Named(named_idents),
1408 (false, _) => Unnamed(just_spans, is_tuple),
1410 _ => Named(Vec::new()),
1414 fn create_struct_patterns(
1416 cx: &mut ExtCtxt<'_>,
1417 struct_path: ast::Path,
1418 struct_def: &'a VariantData,
1419 prefixes: &[String],
1421 ) -> Vec<P<ast::Pat>> {
1426 struct_def.fields().iter().enumerate().map(|(i, struct_field)| {
1427 let sp = struct_field.span.with_ctxt(self.span.ctxt());
1428 let binding_mode = if use_ref_pat {
1429 ast::BindingMode::ByRef(ast::Mutability::Not)
1431 ast::BindingMode::ByValue(ast::Mutability::Not)
1433 let ident = self.mk_pattern_ident(prefix, i);
1434 let path = ident.with_span_pos(sp);
1438 cx.pat(path.span, PatKind::Ident(binding_mode, path, None)),
1442 let struct_path = struct_path.clone();
1444 VariantData::Struct(..) => {
1445 let field_pats = pieces_iter
1446 .map(|(sp, ident, pat)| {
1447 if ident.is_none() {
1450 "a braced struct with unnamed fields in `derive`",
1454 ident: ident.unwrap(),
1455 is_shorthand: false,
1456 attrs: ast::AttrVec::new(),
1457 id: ast::DUMMY_NODE_ID,
1458 span: pat.span.with_ctxt(self.span.ctxt()),
1460 is_placeholder: false,
1464 cx.pat_struct(self.span, struct_path, field_pats)
1466 VariantData::Tuple(..) => {
1467 let subpats = pieces_iter.map(|(_, _, subpat)| subpat).collect();
1468 cx.pat_tuple_struct(self.span, struct_path, subpats)
1470 VariantData::Unit(..) => cx.pat_path(self.span, struct_path),
1476 fn create_fields<F>(&self, struct_def: &'a VariantData, mk_exprs: F) -> Vec<FieldInfo>
1478 F: Fn(usize, &ast::FieldDef, Span) -> Vec<P<ast::Expr>>,
1484 .map(|(i, struct_field)| {
1485 // For this field, get an expr for each selflike_arg. E.g. for
1486 // `PartialEq::eq`, one for each of `&self` and `other`.
1487 let sp = struct_field.span.with_ctxt(self.span.ctxt());
1488 let mut exprs: Vec<_> = mk_exprs(i, struct_field, sp);
1489 let self_expr = exprs.remove(0);
1490 let other_selflike_exprs = exprs;
1492 span: sp.with_ctxt(self.span.ctxt()),
1493 name: struct_field.ident,
1495 other_selflike_exprs,
1501 fn mk_pattern_ident(&self, prefix: &str, i: usize) -> Ident {
1502 Ident::from_str_and_span(&format!("{}_{}", prefix, i), self.span)
1505 fn create_struct_pattern_fields(
1507 cx: &mut ExtCtxt<'_>,
1508 struct_def: &'a VariantData,
1509 prefixes: &[String],
1511 ) -> Vec<FieldInfo> {
1512 self.create_fields(struct_def, |i, _struct_field, sp| {
1516 let ident = self.mk_pattern_ident(prefix, i);
1517 let expr = cx.expr_path(cx.path_ident(sp, ident));
1518 if addr_of { cx.expr_addr_of(sp, expr) } else { expr }
1524 fn create_struct_field_access_fields(
1526 cx: &mut ExtCtxt<'_>,
1527 selflike_args: &[P<Expr>],
1528 struct_def: &'a VariantData,
1530 ) -> Vec<FieldInfo> {
1531 self.create_fields(struct_def, |i, struct_field, sp| {
1534 .map(|selflike_arg| {
1535 // Note: we must use `struct_field.span` rather than `sp` in the
1536 // `unwrap_or_else` case otherwise the hygiene is wrong and we get
1537 // "field `0` of struct `Point` is private" errors on tuple
1539 let mut field_expr = cx.expr(
1541 ast::ExprKind::Field(
1542 selflike_arg.clone(),
1543 struct_field.ident.unwrap_or_else(|| {
1544 Ident::from_str_and_span(&i.to_string(), struct_field.span)
1549 field_expr = cx.expr_block(
1550 cx.block(struct_field.span, vec![cx.stmt_expr(field_expr)]),
1553 cx.expr_addr_of(sp, field_expr)
1560 /// The function passed to `cs_fold` is called repeatedly with a value of this
1561 /// type. It describes one part of the code generation. The result is always an
1563 pub enum CsFold<'a> {
1564 /// The basic case: a field expression for one or more selflike args. E.g.
1565 /// for `PartialEq::eq` this is something like `self.x == other.x`.
1566 Single(&'a FieldInfo),
1568 /// The combination of two field expressions. E.g. for `PartialEq::eq` this
1569 /// is something like `<field1 equality> && <field2 equality>`.
1570 Combine(Span, P<Expr>, P<Expr>),
1572 // The fallback case for a struct or enum variant with no fields.
1576 /// Folds over fields, combining the expressions for each field in a sequence.
1577 /// Statics may not be folded over.
1580 cx: &mut ExtCtxt<'_>,
1582 substructure: &Substructure<'_>,
1586 F: FnMut(&mut ExtCtxt<'_>, CsFold<'_>) -> P<Expr>,
1588 match substructure.fields {
1589 EnumMatching(.., all_fields) | Struct(_, all_fields) => {
1590 if all_fields.is_empty() {
1591 return f(cx, CsFold::Fieldless);
1594 let (base_field, rest) = if use_foldl {
1595 all_fields.split_first().unwrap()
1597 all_fields.split_last().unwrap()
1600 let base_expr = f(cx, CsFold::Single(base_field));
1602 let op = |old, field: &FieldInfo| {
1603 let new = f(cx, CsFold::Single(field));
1604 f(cx, CsFold::Combine(field.span, old, new))
1608 rest.iter().fold(base_expr, op)
1610 rest.iter().rfold(base_expr, op)
1613 EnumTag(tag_field, match_expr) => {
1614 let tag_check_expr = f(cx, CsFold::Single(tag_field));
1615 if let Some(match_expr) = match_expr {
1617 f(cx, CsFold::Combine(trait_span, tag_check_expr, match_expr.clone()))
1619 f(cx, CsFold::Combine(trait_span, match_expr.clone(), tag_check_expr))
1625 StaticEnum(..) | StaticStruct(..) => cx.span_bug(trait_span, "static function in `derive`"),