2019-02-04 13:49:54 +01:00
|
|
|
use crate::deriving::generic::ty::*;
|
2019-12-22 23:42:04 +01:00
|
|
|
use crate::deriving::generic::*;
|
|
|
|
use crate::deriving::path_std;
|
2015-12-10 15:23:14 +01:00
|
|
|
|
2019-12-31 21:25:16 +01:00
|
|
|
use errors::struct_span_err;
|
2019-12-29 15:23:55 +01:00
|
|
|
use rustc_expand::base::{Annotatable, DummyResult, ExtCtxt};
|
2020-01-01 19:30:57 +01:00
|
|
|
use rustc_span::symbol::{kw, sym};
|
2019-12-31 18:15:40 +01:00
|
|
|
use rustc_span::Span;
|
2016-07-19 19:32:06 +02:00
|
|
|
use syntax::ast::{Expr, MetaItem};
|
2015-12-10 15:23:14 +01:00
|
|
|
use syntax::ptr::P;
|
2014-05-16 09:16:13 +02:00
|
|
|
|
2019-11-11 22:46:56 +01:00
|
|
|
use rustc_error_codes::*;
|
|
|
|
|
2019-12-22 23:42:04 +01:00
|
|
|
pub fn expand_deriving_default(
|
|
|
|
cx: &mut ExtCtxt<'_>,
|
|
|
|
span: Span,
|
|
|
|
mitem: &MetaItem,
|
|
|
|
item: &Annotatable,
|
|
|
|
push: &mut dyn FnMut(Annotatable),
|
|
|
|
) {
|
2019-05-22 04:42:23 +02:00
|
|
|
let inline = cx.meta_word(span, sym::inline);
|
2019-07-30 20:12:52 +02:00
|
|
|
let attrs = vec![cx.attribute(inline)];
|
2013-09-12 06:51:13 +02:00
|
|
|
let trait_def = TraitDef {
|
2017-08-07 07:54:09 +02:00
|
|
|
span,
|
2014-02-28 22:09:09 +01:00
|
|
|
attributes: Vec::new(),
|
2017-12-06 19:50:55 +01:00
|
|
|
path: path_std!(cx, default::Default),
|
2014-02-28 22:09:09 +01:00
|
|
|
additional_bounds: Vec::new(),
|
2013-09-12 06:51:13 +02:00
|
|
|
generics: LifetimeBounds::empty(),
|
2015-08-29 20:50:05 +02:00
|
|
|
is_unsafe: false,
|
2016-08-29 13:14:25 +02:00
|
|
|
supports_unions: false,
|
2016-07-19 19:32:06 +02:00
|
|
|
methods: vec![MethodDef {
|
2019-12-22 23:42:04 +01:00
|
|
|
name: "default",
|
|
|
|
generics: LifetimeBounds::empty(),
|
|
|
|
explicit_self: None,
|
|
|
|
args: Vec::new(),
|
|
|
|
ret_ty: Self_,
|
|
|
|
attributes: attrs,
|
|
|
|
is_unsafe: false,
|
|
|
|
unify_fieldless_variants: false,
|
|
|
|
combine_substructure: combine_substructure(Box::new(|a, b, c| {
|
|
|
|
default_substructure(a, b, c)
|
|
|
|
})),
|
|
|
|
}],
|
2015-01-25 06:29:24 +01:00
|
|
|
associated_types: Vec::new(),
|
2013-09-12 06:51:13 +02:00
|
|
|
};
|
2015-05-22 17:40:14 +02:00
|
|
|
trait_def.expand(cx, mitem, item, push)
|
2013-09-12 06:51:13 +02:00
|
|
|
}
|
|
|
|
|
2019-12-22 23:42:04 +01:00
|
|
|
fn default_substructure(
|
|
|
|
cx: &mut ExtCtxt<'_>,
|
|
|
|
trait_span: Span,
|
|
|
|
substr: &Substructure<'_>,
|
|
|
|
) -> P<Expr> {
|
2019-05-22 06:41:15 +02:00
|
|
|
// Note that `kw::Default` is "default" and `sym::Default` is "Default"!
|
|
|
|
let default_ident = cx.std_path(&[kw::Default, sym::Default, kw::Default]);
|
2015-02-01 18:44:15 +01:00
|
|
|
let default_call = |span| cx.expr_call_global(span, default_ident.clone(), Vec::new());
|
2013-09-12 06:51:13 +02:00
|
|
|
|
|
|
|
return match *substr.fields {
|
2019-12-22 23:42:04 +01:00
|
|
|
StaticStruct(_, ref summary) => match *summary {
|
|
|
|
Unnamed(ref fields, is_tuple) => {
|
|
|
|
if !is_tuple {
|
|
|
|
cx.expr_ident(trait_span, substr.type_ident)
|
|
|
|
} else {
|
|
|
|
let exprs = fields.iter().map(|sp| default_call(*sp)).collect();
|
|
|
|
cx.expr_call_ident(trait_span, substr.type_ident, exprs)
|
2013-09-12 06:51:13 +02:00
|
|
|
}
|
|
|
|
}
|
2019-12-22 23:42:04 +01:00
|
|
|
Named(ref fields) => {
|
|
|
|
let default_fields = fields
|
|
|
|
.iter()
|
|
|
|
.map(|&(ident, span)| cx.field_imm(span, ident, default_call(span)))
|
|
|
|
.collect();
|
|
|
|
cx.expr_struct_ident(trait_span, substr.type_ident, default_fields)
|
|
|
|
}
|
|
|
|
},
|
2013-11-28 21:22:53 +01:00
|
|
|
StaticEnum(..) => {
|
2019-12-31 21:25:16 +01:00
|
|
|
struct_span_err!(
|
|
|
|
cx.parse_sess.span_diagnostic,
|
|
|
|
trait_span,
|
|
|
|
E0665,
|
|
|
|
"`Default` cannot be derived for enums, only structs"
|
|
|
|
)
|
|
|
|
.emit();
|
2014-01-17 15:53:10 +01:00
|
|
|
// let compilation continue
|
2018-12-20 01:57:48 +01:00
|
|
|
DummyResult::raw_expr(trait_span, true)
|
2013-09-12 06:51:13 +02:00
|
|
|
}
|
2019-10-18 04:00:19 +02:00
|
|
|
_ => cx.span_bug(trait_span, "method in `derive(Default)`"),
|
2013-09-12 06:51:13 +02:00
|
|
|
};
|
|
|
|
}
|