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