]> git.lizzy.rs Git - rust.git/blob - src/libsyntax_ext/deriving/default.rs
Do not abort compilation if expansion produces errors
[rust.git] / src / libsyntax_ext / deriving / default.rs
1 use deriving::path_std;
2 use deriving::generic::*;
3 use deriving::generic::ty::*;
4
5 use syntax::ast::{Expr, MetaItem};
6 use syntax::ext::base::{Annotatable, DummyResult, ExtCtxt};
7 use syntax::ext::build::AstBuilder;
8 use syntax::ptr::P;
9 use syntax::symbol::Symbol;
10 use syntax_pos::Span;
11
12 pub fn expand_deriving_default(cx: &mut ExtCtxt,
13                                span: Span,
14                                mitem: &MetaItem,
15                                item: &Annotatable,
16                                push: &mut dyn FnMut(Annotatable)) {
17     let inline = cx.meta_word(span, Symbol::intern("inline"));
18     let attrs = vec![cx.attribute(span, inline)];
19     let trait_def = TraitDef {
20         span,
21         attributes: Vec::new(),
22         path: path_std!(cx, default::Default),
23         additional_bounds: Vec::new(),
24         generics: LifetimeBounds::empty(),
25         is_unsafe: false,
26         supports_unions: false,
27         methods: vec![MethodDef {
28                           name: "default",
29                           generics: LifetimeBounds::empty(),
30                           explicit_self: None,
31                           args: Vec::new(),
32                           ret_ty: Self_,
33                           attributes: attrs,
34                           is_unsafe: false,
35                           unify_fieldless_variants: false,
36                           combine_substructure: combine_substructure(Box::new(|a, b, c| {
37                               default_substructure(a, b, c)
38                           })),
39                       }],
40         associated_types: Vec::new(),
41     };
42     trait_def.expand(cx, mitem, item, push)
43 }
44
45 fn default_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure) -> P<Expr> {
46     let default_ident = cx.std_path(&["default", "Default", "default"]);
47     let default_call = |span| cx.expr_call_global(span, default_ident.clone(), Vec::new());
48
49     return match *substr.fields {
50         StaticStruct(_, ref summary) => {
51             match *summary {
52                 Unnamed(ref fields, is_tuple) => {
53                     if !is_tuple {
54                         cx.expr_ident(trait_span, substr.type_ident)
55                     } else {
56                         let exprs = fields.iter().map(|sp| default_call(*sp)).collect();
57                         cx.expr_call_ident(trait_span, substr.type_ident, exprs)
58                     }
59                 }
60                 Named(ref fields) => {
61                     let default_fields = fields.iter()
62                         .map(|&(ident, span)| cx.field_imm(span, ident, default_call(span)))
63                         .collect();
64                     cx.expr_struct_ident(trait_span, substr.type_ident, default_fields)
65                 }
66             }
67         }
68         StaticEnum(..) => {
69             span_err!(cx, trait_span, E0665,
70                       "`Default` cannot be derived for enums, only structs");
71             // let compilation continue
72             DummyResult::raw_expr(trait_span)
73         }
74         _ => cx.span_bug(trait_span, "Non-static method in `derive(Default)`"),
75     };
76 }