]> git.lizzy.rs Git - rust.git/blob - src/librustc_builtin_macros/deriving/default.rs
Rollup merge of #69577 - GuillaumeGomez:cleanup-e0375, r=Dylan-DPC
[rust.git] / src / librustc_builtin_macros / deriving / default.rs
1 use crate::deriving::generic::ty::*;
2 use crate::deriving::generic::*;
3 use crate::deriving::path_std;
4
5 use rustc_ast::ast::{Expr, MetaItem};
6 use rustc_ast::ptr::P;
7 use rustc_errors::struct_span_err;
8 use rustc_expand::base::{Annotatable, DummyResult, ExtCtxt};
9 use rustc_span::symbol::{kw, sym};
10 use rustc_span::Span;
11
12 pub fn expand_deriving_default(
13     cx: &mut ExtCtxt<'_>,
14     span: Span,
15     mitem: &MetaItem,
16     item: &Annotatable,
17     push: &mut dyn FnMut(Annotatable),
18 ) {
19     let inline = cx.meta_word(span, sym::inline);
20     let attrs = vec![cx.attribute(inline)];
21     let trait_def = TraitDef {
22         span,
23         attributes: Vec::new(),
24         path: path_std!(cx, default::Default),
25         additional_bounds: Vec::new(),
26         generics: LifetimeBounds::empty(),
27         is_unsafe: false,
28         supports_unions: false,
29         methods: vec![MethodDef {
30             name: "default",
31             generics: LifetimeBounds::empty(),
32             explicit_self: None,
33             args: Vec::new(),
34             ret_ty: Self_,
35             attributes: attrs,
36             is_unsafe: false,
37             unify_fieldless_variants: false,
38             combine_substructure: combine_substructure(Box::new(|a, b, c| {
39                 default_substructure(a, b, c)
40             })),
41         }],
42         associated_types: Vec::new(),
43     };
44     trait_def.expand(cx, mitem, item, push)
45 }
46
47 fn default_substructure(
48     cx: &mut ExtCtxt<'_>,
49     trait_span: Span,
50     substr: &Substructure<'_>,
51 ) -> P<Expr> {
52     // Note that `kw::Default` is "default" and `sym::Default` is "Default"!
53     let default_ident = cx.std_path(&[kw::Default, sym::Default, kw::Default]);
54     let default_call = |span| cx.expr_call_global(span, default_ident.clone(), Vec::new());
55
56     return match *substr.fields {
57         StaticStruct(_, ref summary) => match *summary {
58             Unnamed(ref fields, is_tuple) => {
59                 if !is_tuple {
60                     cx.expr_ident(trait_span, substr.type_ident)
61                 } else {
62                     let exprs = fields.iter().map(|sp| default_call(*sp)).collect();
63                     cx.expr_call_ident(trait_span, substr.type_ident, exprs)
64                 }
65             }
66             Named(ref fields) => {
67                 let default_fields = fields
68                     .iter()
69                     .map(|&(ident, span)| cx.field_imm(span, ident, default_call(span)))
70                     .collect();
71                 cx.expr_struct_ident(trait_span, substr.type_ident, default_fields)
72             }
73         },
74         StaticEnum(..) => {
75             struct_span_err!(
76                 cx.parse_sess.span_diagnostic,
77                 trait_span,
78                 E0665,
79                 "`Default` cannot be derived for enums, only structs"
80             )
81             .emit();
82             // let compilation continue
83             DummyResult::raw_expr(trait_span, true)
84         }
85         _ => cx.span_bug(trait_span, "method in `derive(Default)`"),
86     };
87 }