mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-16 21:15:18 +03:00
Run rustfmt on libsyntax_ext/deriving folder
This commit is contained in:
@@ -12,15 +12,14 @@
|
||||
use deriving::generic::ty::*;
|
||||
|
||||
use syntax::ast::MetaItem;
|
||||
use syntax::ext::base::{ExtCtxt, Annotatable};
|
||||
use syntax::ext::base::{Annotatable, ExtCtxt};
|
||||
use syntax_pos::Span;
|
||||
|
||||
pub fn expand_deriving_unsafe_bound(cx: &mut ExtCtxt,
|
||||
span: Span,
|
||||
_: &MetaItem,
|
||||
_: &Annotatable,
|
||||
_: &mut FnMut(Annotatable))
|
||||
{
|
||||
_: &mut FnMut(Annotatable)) {
|
||||
cx.span_err(span, "this unsafe trait should be implemented explicitly");
|
||||
}
|
||||
|
||||
@@ -28,8 +27,7 @@ pub fn expand_deriving_copy(cx: &mut ExtCtxt,
|
||||
span: Span,
|
||||
mitem: &MetaItem,
|
||||
item: &Annotatable,
|
||||
push: &mut FnMut(Annotatable))
|
||||
{
|
||||
push: &mut FnMut(Annotatable)) {
|
||||
let mut v = cx.crate_root.map(|s| vec![s]).unwrap_or(Vec::new());
|
||||
v.push("marker");
|
||||
v.push("Copy");
|
||||
|
||||
@@ -11,23 +11,25 @@
|
||||
use deriving::generic::*;
|
||||
use deriving::generic::ty::*;
|
||||
|
||||
use syntax::ast::{Expr, ItemKind, Generics, MetaItem, VariantData};
|
||||
use syntax::ast::{Expr, Generics, ItemKind, MetaItem, VariantData};
|
||||
use syntax::attr;
|
||||
use syntax::ext::base::{ExtCtxt, Annotatable};
|
||||
use syntax::ext::base::{Annotatable, ExtCtxt};
|
||||
use syntax::ext::build::AstBuilder;
|
||||
use syntax::parse::token::InternedString;
|
||||
use syntax::ptr::P;
|
||||
use syntax_pos::Span;
|
||||
|
||||
#[derive(PartialEq)]
|
||||
enum Mode { Deep, Shallow }
|
||||
enum Mode {
|
||||
Deep,
|
||||
Shallow,
|
||||
}
|
||||
|
||||
pub fn expand_deriving_clone(cx: &mut ExtCtxt,
|
||||
span: Span,
|
||||
mitem: &MetaItem,
|
||||
item: &Annotatable,
|
||||
push: &mut FnMut(Annotatable))
|
||||
{
|
||||
push: &mut FnMut(Annotatable)) {
|
||||
// check if we can use a short form
|
||||
//
|
||||
// the short form is `fn clone(&self) -> Self { *self }`
|
||||
@@ -46,8 +48,8 @@ pub fn expand_deriving_clone(cx: &mut ExtCtxt,
|
||||
match annitem.node {
|
||||
ItemKind::Struct(_, Generics { ref ty_params, .. }) |
|
||||
ItemKind::Enum(_, Generics { ref ty_params, .. })
|
||||
if ty_params.is_empty()
|
||||
&& attr::contains_name(&annitem.attrs, "derive_Copy") => {
|
||||
if ty_params.is_empty() &&
|
||||
attr::contains_name(&annitem.attrs, "derive_Copy") => {
|
||||
|
||||
bounds = vec![Literal(path_std!(cx, core::marker::Copy))];
|
||||
unify_fieldless_variants = true;
|
||||
@@ -66,11 +68,11 @@ pub fn expand_deriving_clone(cx: &mut ExtCtxt,
|
||||
}
|
||||
}
|
||||
|
||||
_ => cx.span_bug(span, "#[derive(Clone)] on trait item or impl item")
|
||||
_ => cx.span_bug(span, "#[derive(Clone)] on trait item or impl item"),
|
||||
}
|
||||
|
||||
let inline = cx.meta_word(span, InternedString::new("inline"));
|
||||
let attrs = vec!(cx.attribute(span, inline));
|
||||
let attrs = vec![cx.attribute(span, inline)];
|
||||
let trait_def = TraitDef {
|
||||
span: span,
|
||||
attributes: Vec::new(),
|
||||
@@ -78,42 +80,41 @@ pub fn expand_deriving_clone(cx: &mut ExtCtxt,
|
||||
additional_bounds: bounds,
|
||||
generics: LifetimeBounds::empty(),
|
||||
is_unsafe: false,
|
||||
methods: vec!(
|
||||
MethodDef {
|
||||
name: "clone",
|
||||
generics: LifetimeBounds::empty(),
|
||||
explicit_self: borrowed_explicit_self(),
|
||||
args: Vec::new(),
|
||||
ret_ty: Self_,
|
||||
attributes: attrs,
|
||||
is_unsafe: false,
|
||||
unify_fieldless_variants: unify_fieldless_variants,
|
||||
combine_substructure: substructure,
|
||||
}
|
||||
),
|
||||
methods: vec![MethodDef {
|
||||
name: "clone",
|
||||
generics: LifetimeBounds::empty(),
|
||||
explicit_self: borrowed_explicit_self(),
|
||||
args: Vec::new(),
|
||||
ret_ty: Self_,
|
||||
attributes: attrs,
|
||||
is_unsafe: false,
|
||||
unify_fieldless_variants: unify_fieldless_variants,
|
||||
combine_substructure: substructure,
|
||||
}],
|
||||
associated_types: Vec::new(),
|
||||
};
|
||||
|
||||
trait_def.expand(cx, mitem, item, push)
|
||||
}
|
||||
|
||||
fn cs_clone(
|
||||
name: &str,
|
||||
cx: &mut ExtCtxt, trait_span: Span,
|
||||
substr: &Substructure,
|
||||
mode: Mode) -> P<Expr> {
|
||||
fn cs_clone(name: &str,
|
||||
cx: &mut ExtCtxt,
|
||||
trait_span: Span,
|
||||
substr: &Substructure,
|
||||
mode: Mode)
|
||||
-> P<Expr> {
|
||||
let ctor_path;
|
||||
let all_fields;
|
||||
let fn_path = match mode {
|
||||
Mode::Shallow => cx.std_path(&["clone", "assert_receiver_is_clone"]),
|
||||
Mode::Deep => cx.std_path(&["clone", "Clone", "clone"]),
|
||||
Mode::Deep => cx.std_path(&["clone", "Clone", "clone"]),
|
||||
};
|
||||
let subcall = |field: &FieldInfo| {
|
||||
let args = vec![cx.expr_addr_of(field.span, field.self_.clone())];
|
||||
|
||||
let span = if mode == Mode::Shallow {
|
||||
// set the expn ID so we can call the unstable method
|
||||
Span { expn_id: cx.backtrace(), .. trait_span }
|
||||
Span { expn_id: cx.backtrace(), ..trait_span }
|
||||
} else {
|
||||
field.span
|
||||
};
|
||||
@@ -131,15 +132,15 @@ fn cs_clone(
|
||||
ctor_path = cx.path(trait_span, vec![substr.type_ident, variant.node.name]);
|
||||
all_fields = af;
|
||||
vdata = &variant.node.data;
|
||||
},
|
||||
EnumNonMatchingCollapsed (..) => {
|
||||
}
|
||||
EnumNonMatchingCollapsed(..) => {
|
||||
cx.span_bug(trait_span,
|
||||
&format!("non-matching enum variants in \
|
||||
`derive({})`", name))
|
||||
`derive({})`",
|
||||
name))
|
||||
}
|
||||
StaticEnum(..) | StaticStruct(..) => {
|
||||
cx.span_bug(trait_span,
|
||||
&format!("static method in `derive({})`", name))
|
||||
cx.span_bug(trait_span, &format!("static method in `derive({})`", name))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -153,17 +154,20 @@ fn cs_clone(
|
||||
Mode::Deep => {
|
||||
match *vdata {
|
||||
VariantData::Struct(..) => {
|
||||
let fields = all_fields.iter().map(|field| {
|
||||
let ident = match field.name {
|
||||
Some(i) => i,
|
||||
None => {
|
||||
cx.span_bug(trait_span,
|
||||
&format!("unnamed field in normal struct in \
|
||||
`derive({})`", name))
|
||||
}
|
||||
};
|
||||
cx.field_imm(field.span, ident, subcall(field))
|
||||
}).collect::<Vec<_>>();
|
||||
let fields = all_fields.iter()
|
||||
.map(|field| {
|
||||
let ident = match field.name {
|
||||
Some(i) => i,
|
||||
None => {
|
||||
cx.span_bug(trait_span,
|
||||
&format!("unnamed field in normal struct in \
|
||||
`derive({})`",
|
||||
name))
|
||||
}
|
||||
};
|
||||
cx.field_imm(field.span, ident, subcall(field))
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
cx.expr_struct(trait_span, ctor_path, fields)
|
||||
}
|
||||
@@ -172,9 +176,7 @@ fn cs_clone(
|
||||
let path = cx.expr_path(ctor_path);
|
||||
cx.expr_call(trait_span, path, subcalls)
|
||||
}
|
||||
VariantData::Unit(..) => {
|
||||
cx.expr_path(ctor_path)
|
||||
}
|
||||
VariantData::Unit(..) => cx.expr_path(ctor_path),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,8 +11,8 @@
|
||||
use deriving::generic::*;
|
||||
use deriving::generic::ty::*;
|
||||
|
||||
use syntax::ast::{MetaItem, Expr};
|
||||
use syntax::ext::base::{ExtCtxt, Annotatable};
|
||||
use syntax::ast::{Expr, MetaItem};
|
||||
use syntax::ext::base::{Annotatable, ExtCtxt};
|
||||
use syntax::ext::build::AstBuilder;
|
||||
use syntax::parse::token::InternedString;
|
||||
use syntax::ptr::P;
|
||||
@@ -22,30 +22,27 @@ pub fn expand_deriving_eq(cx: &mut ExtCtxt,
|
||||
span: Span,
|
||||
mitem: &MetaItem,
|
||||
item: &Annotatable,
|
||||
push: &mut FnMut(Annotatable))
|
||||
{
|
||||
push: &mut FnMut(Annotatable)) {
|
||||
fn cs_total_eq_assert(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P<Expr> {
|
||||
cs_same_method(
|
||||
|cx, span, exprs| {
|
||||
// create `a.<method>(); b.<method>(); c.<method>(); ...`
|
||||
// (where method is `assert_receiver_is_total_eq`)
|
||||
let stmts = exprs.into_iter().map(|e| cx.stmt_expr(e)).collect();
|
||||
let block = cx.block(span, stmts);
|
||||
cx.expr_block(block)
|
||||
},
|
||||
Box::new(|cx, sp, _, _| {
|
||||
cx.span_bug(sp, "non matching enums in derive(Eq)?") }),
|
||||
cx,
|
||||
span,
|
||||
substr
|
||||
)
|
||||
cs_same_method(|cx, span, exprs| {
|
||||
// create `a.<method>(); b.<method>(); c.<method>(); ...`
|
||||
// (where method is `assert_receiver_is_total_eq`)
|
||||
let stmts = exprs.into_iter().map(|e| cx.stmt_expr(e)).collect();
|
||||
let block = cx.block(span, stmts);
|
||||
cx.expr_block(block)
|
||||
},
|
||||
Box::new(|cx, sp, _, _| {
|
||||
cx.span_bug(sp, "non matching enums in derive(Eq)?")
|
||||
}),
|
||||
cx,
|
||||
span,
|
||||
substr)
|
||||
}
|
||||
|
||||
let inline = cx.meta_word(span, InternedString::new("inline"));
|
||||
let hidden = cx.meta_word(span, InternedString::new("hidden"));
|
||||
let doc = cx.meta_list(span, InternedString::new("doc"), vec!(hidden));
|
||||
let attrs = vec!(cx.attribute(span, inline),
|
||||
cx.attribute(span, doc));
|
||||
let doc = cx.meta_list(span, InternedString::new("doc"), vec![hidden]);
|
||||
let attrs = vec![cx.attribute(span, inline), cx.attribute(span, doc)];
|
||||
let trait_def = TraitDef {
|
||||
span: span,
|
||||
attributes: Vec::new(),
|
||||
@@ -53,21 +50,19 @@ fn cs_total_eq_assert(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P<
|
||||
additional_bounds: Vec::new(),
|
||||
generics: LifetimeBounds::empty(),
|
||||
is_unsafe: false,
|
||||
methods: vec!(
|
||||
MethodDef {
|
||||
name: "assert_receiver_is_total_eq",
|
||||
generics: LifetimeBounds::empty(),
|
||||
explicit_self: borrowed_explicit_self(),
|
||||
args: vec!(),
|
||||
ret_ty: nil_ty(),
|
||||
attributes: attrs,
|
||||
is_unsafe: false,
|
||||
unify_fieldless_variants: true,
|
||||
combine_substructure: combine_substructure(Box::new(|a, b, c| {
|
||||
cs_total_eq_assert(a, b, c)
|
||||
}))
|
||||
}
|
||||
),
|
||||
methods: vec![MethodDef {
|
||||
name: "assert_receiver_is_total_eq",
|
||||
generics: LifetimeBounds::empty(),
|
||||
explicit_self: borrowed_explicit_self(),
|
||||
args: vec![],
|
||||
ret_ty: nil_ty(),
|
||||
attributes: attrs,
|
||||
is_unsafe: false,
|
||||
unify_fieldless_variants: true,
|
||||
combine_substructure: combine_substructure(Box::new(|a, b, c| {
|
||||
cs_total_eq_assert(a, b, c)
|
||||
})),
|
||||
}],
|
||||
associated_types: Vec::new(),
|
||||
};
|
||||
trait_def.expand(cx, mitem, item, push)
|
||||
|
||||
@@ -11,8 +11,8 @@
|
||||
use deriving::generic::*;
|
||||
use deriving::generic::ty::*;
|
||||
|
||||
use syntax::ast::{MetaItem, Expr, self};
|
||||
use syntax::ext::base::{ExtCtxt, Annotatable};
|
||||
use syntax::ast::{self, Expr, MetaItem};
|
||||
use syntax::ext::base::{Annotatable, ExtCtxt};
|
||||
use syntax::ext::build::AstBuilder;
|
||||
use syntax::parse::token::InternedString;
|
||||
use syntax::ptr::P;
|
||||
@@ -22,10 +22,9 @@ pub fn expand_deriving_ord(cx: &mut ExtCtxt,
|
||||
span: Span,
|
||||
mitem: &MetaItem,
|
||||
item: &Annotatable,
|
||||
push: &mut FnMut(Annotatable))
|
||||
{
|
||||
push: &mut FnMut(Annotatable)) {
|
||||
let inline = cx.meta_word(span, InternedString::new("inline"));
|
||||
let attrs = vec!(cx.attribute(span, inline));
|
||||
let attrs = vec![cx.attribute(span, inline)];
|
||||
let trait_def = TraitDef {
|
||||
span: span,
|
||||
attributes: Vec::new(),
|
||||
@@ -33,21 +32,19 @@ pub fn expand_deriving_ord(cx: &mut ExtCtxt,
|
||||
additional_bounds: Vec::new(),
|
||||
generics: LifetimeBounds::empty(),
|
||||
is_unsafe: false,
|
||||
methods: vec!(
|
||||
MethodDef {
|
||||
name: "cmp",
|
||||
generics: LifetimeBounds::empty(),
|
||||
explicit_self: borrowed_explicit_self(),
|
||||
args: vec!(borrowed_self()),
|
||||
ret_ty: Literal(path_std!(cx, core::cmp::Ordering)),
|
||||
attributes: attrs,
|
||||
is_unsafe: false,
|
||||
unify_fieldless_variants: true,
|
||||
combine_substructure: combine_substructure(Box::new(|a, b, c| {
|
||||
cs_cmp(a, b, c)
|
||||
})),
|
||||
}
|
||||
),
|
||||
methods: vec![MethodDef {
|
||||
name: "cmp",
|
||||
generics: LifetimeBounds::empty(),
|
||||
explicit_self: borrowed_explicit_self(),
|
||||
args: vec![borrowed_self()],
|
||||
ret_ty: Literal(path_std!(cx, core::cmp::Ordering)),
|
||||
attributes: attrs,
|
||||
is_unsafe: false,
|
||||
unify_fieldless_variants: true,
|
||||
combine_substructure: combine_substructure(Box::new(|a, b, c| {
|
||||
cs_cmp(a, b, c)
|
||||
})),
|
||||
}],
|
||||
associated_types: Vec::new(),
|
||||
};
|
||||
|
||||
@@ -57,76 +54,73 @@ pub fn expand_deriving_ord(cx: &mut ExtCtxt,
|
||||
|
||||
pub fn ordering_collapsed(cx: &mut ExtCtxt,
|
||||
span: Span,
|
||||
self_arg_tags: &[ast::Ident]) -> P<ast::Expr> {
|
||||
self_arg_tags: &[ast::Ident])
|
||||
-> P<ast::Expr> {
|
||||
let lft = cx.expr_ident(span, self_arg_tags[0]);
|
||||
let rgt = cx.expr_addr_of(span, cx.expr_ident(span, self_arg_tags[1]));
|
||||
cx.expr_method_call(span, lft, cx.ident_of("cmp"), vec![rgt])
|
||||
}
|
||||
|
||||
pub fn cs_cmp(cx: &mut ExtCtxt, span: Span,
|
||||
substr: &Substructure) -> P<Expr> {
|
||||
pub fn cs_cmp(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P<Expr> {
|
||||
let test_id = cx.ident_of("__cmp");
|
||||
let equals_path = cx.path_global(span,
|
||||
cx.std_path(&["cmp", "Ordering", "Equal"]));
|
||||
let equals_path = cx.path_global(span, cx.std_path(&["cmp", "Ordering", "Equal"]));
|
||||
|
||||
let cmp_path = cx.std_path(&["cmp", "Ord", "cmp"]);
|
||||
|
||||
/*
|
||||
Builds:
|
||||
// Builds:
|
||||
//
|
||||
// match ::std::cmp::Ord::cmp(&self_field1, &other_field1) {
|
||||
// ::std::cmp::Ordering::Equal =>
|
||||
// match ::std::cmp::Ord::cmp(&self_field2, &other_field2) {
|
||||
// ::std::cmp::Ordering::Equal => {
|
||||
// ...
|
||||
// }
|
||||
// __cmp => __cmp
|
||||
// },
|
||||
// __cmp => __cmp
|
||||
// }
|
||||
//
|
||||
cs_fold(// foldr nests the if-elses correctly, leaving the first field
|
||||
// as the outermost one, and the last as the innermost.
|
||||
false,
|
||||
|cx, span, old, self_f, other_fs| {
|
||||
// match new {
|
||||
// ::std::cmp::Ordering::Equal => old,
|
||||
// __cmp => __cmp
|
||||
// }
|
||||
|
||||
match ::std::cmp::Ord::cmp(&self_field1, &other_field1) {
|
||||
::std::cmp::Ordering::Equal =>
|
||||
match ::std::cmp::Ord::cmp(&self_field2, &other_field2) {
|
||||
::std::cmp::Ordering::Equal => {
|
||||
...
|
||||
}
|
||||
__cmp => __cmp
|
||||
},
|
||||
__cmp => __cmp
|
||||
}
|
||||
*/
|
||||
cs_fold(
|
||||
// foldr nests the if-elses correctly, leaving the first field
|
||||
// as the outermost one, and the last as the innermost.
|
||||
false,
|
||||
|cx, span, old, self_f, other_fs| {
|
||||
// match new {
|
||||
// ::std::cmp::Ordering::Equal => old,
|
||||
// __cmp => __cmp
|
||||
// }
|
||||
let new = {
|
||||
let other_f = match (other_fs.len(), other_fs.get(0)) {
|
||||
(1, Some(o_f)) => o_f,
|
||||
_ => cx.span_bug(span, "not exactly 2 arguments in `derive(Ord)`"),
|
||||
};
|
||||
|
||||
let new = {
|
||||
let other_f = match (other_fs.len(), other_fs.get(0)) {
|
||||
(1, Some(o_f)) => o_f,
|
||||
_ => cx.span_bug(span, "not exactly 2 arguments in `derive(Ord)`"),
|
||||
};
|
||||
|
||||
let args = vec![
|
||||
let args = vec![
|
||||
cx.expr_addr_of(span, self_f),
|
||||
cx.expr_addr_of(span, other_f.clone()),
|
||||
];
|
||||
|
||||
cx.expr_call_global(span, cmp_path.clone(), args)
|
||||
};
|
||||
cx.expr_call_global(span, cmp_path.clone(), args)
|
||||
};
|
||||
|
||||
let eq_arm = cx.arm(span,
|
||||
vec![cx.pat_enum(span,
|
||||
equals_path.clone(),
|
||||
vec![])],
|
||||
old);
|
||||
let neq_arm = cx.arm(span,
|
||||
vec![cx.pat_ident(span, test_id)],
|
||||
cx.expr_ident(span, test_id));
|
||||
let eq_arm = cx.arm(span,
|
||||
vec![cx.pat_enum(span, equals_path.clone(), vec![])],
|
||||
old);
|
||||
let neq_arm = cx.arm(span,
|
||||
vec![cx.pat_ident(span, test_id)],
|
||||
cx.expr_ident(span, test_id));
|
||||
|
||||
cx.expr_match(span, new, vec![eq_arm, neq_arm])
|
||||
},
|
||||
cx.expr_path(equals_path.clone()),
|
||||
Box::new(|cx, span, (self_args, tag_tuple), _non_self_args| {
|
||||
if self_args.len() != 2 {
|
||||
cx.span_bug(span, "not exactly 2 arguments in `derive(Ord)`")
|
||||
} else {
|
||||
ordering_collapsed(cx, span, tag_tuple)
|
||||
}
|
||||
}),
|
||||
cx, span, substr)
|
||||
cx.expr_match(span, new, vec![eq_arm, neq_arm])
|
||||
},
|
||||
cx.expr_path(equals_path.clone()),
|
||||
Box::new(|cx, span, (self_args, tag_tuple), _non_self_args| {
|
||||
if self_args.len() != 2 {
|
||||
cx.span_bug(span, "not exactly 2 arguments in `derive(Ord)`")
|
||||
} else {
|
||||
ordering_collapsed(cx, span, tag_tuple)
|
||||
}
|
||||
}),
|
||||
cx,
|
||||
span,
|
||||
substr)
|
||||
}
|
||||
|
||||
@@ -11,8 +11,8 @@
|
||||
use deriving::generic::*;
|
||||
use deriving::generic::ty::*;
|
||||
|
||||
use syntax::ast::{MetaItem, Expr, BinOpKind};
|
||||
use syntax::ext::base::{ExtCtxt, Annotatable};
|
||||
use syntax::ast::{BinOpKind, Expr, MetaItem};
|
||||
use syntax::ext::base::{Annotatable, ExtCtxt};
|
||||
use syntax::ext::build::AstBuilder;
|
||||
use syntax::parse::token::InternedString;
|
||||
use syntax::ptr::P;
|
||||
@@ -22,43 +22,44 @@ pub fn expand_deriving_partial_eq(cx: &mut ExtCtxt,
|
||||
span: Span,
|
||||
mitem: &MetaItem,
|
||||
item: &Annotatable,
|
||||
push: &mut FnMut(Annotatable))
|
||||
{
|
||||
push: &mut FnMut(Annotatable)) {
|
||||
// structures are equal if all fields are equal, and non equal, if
|
||||
// any fields are not equal or if the enum variants are different
|
||||
fn cs_eq(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P<Expr> {
|
||||
cs_fold(
|
||||
true, // use foldl
|
||||
|cx, span, subexpr, self_f, other_fs| {
|
||||
let other_f = match (other_fs.len(), other_fs.get(0)) {
|
||||
(1, Some(o_f)) => o_f,
|
||||
_ => cx.span_bug(span, "not exactly 2 arguments in `derive(PartialEq)`")
|
||||
};
|
||||
cs_fold(true, // use foldl
|
||||
|cx, span, subexpr, self_f, other_fs| {
|
||||
let other_f = match (other_fs.len(), other_fs.get(0)) {
|
||||
(1, Some(o_f)) => o_f,
|
||||
_ => cx.span_bug(span, "not exactly 2 arguments in `derive(PartialEq)`"),
|
||||
};
|
||||
|
||||
let eq = cx.expr_binary(span, BinOpKind::Eq, self_f, other_f.clone());
|
||||
let eq = cx.expr_binary(span, BinOpKind::Eq, self_f, other_f.clone());
|
||||
|
||||
cx.expr_binary(span, BinOpKind::And, subexpr, eq)
|
||||
},
|
||||
cx.expr_bool(span, true),
|
||||
Box::new(|cx, span, _, _| cx.expr_bool(span, false)),
|
||||
cx, span, substr)
|
||||
cx.expr_binary(span, BinOpKind::And, subexpr, eq)
|
||||
},
|
||||
cx.expr_bool(span, true),
|
||||
Box::new(|cx, span, _, _| cx.expr_bool(span, false)),
|
||||
cx,
|
||||
span,
|
||||
substr)
|
||||
}
|
||||
fn cs_ne(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P<Expr> {
|
||||
cs_fold(
|
||||
true, // use foldl
|
||||
|cx, span, subexpr, self_f, other_fs| {
|
||||
let other_f = match (other_fs.len(), other_fs.get(0)) {
|
||||
(1, Some(o_f)) => o_f,
|
||||
_ => cx.span_bug(span, "not exactly 2 arguments in `derive(PartialEq)`")
|
||||
};
|
||||
cs_fold(true, // use foldl
|
||||
|cx, span, subexpr, self_f, other_fs| {
|
||||
let other_f = match (other_fs.len(), other_fs.get(0)) {
|
||||
(1, Some(o_f)) => o_f,
|
||||
_ => cx.span_bug(span, "not exactly 2 arguments in `derive(PartialEq)`"),
|
||||
};
|
||||
|
||||
let eq = cx.expr_binary(span, BinOpKind::Ne, self_f, other_f.clone());
|
||||
let eq = cx.expr_binary(span, BinOpKind::Ne, self_f, other_f.clone());
|
||||
|
||||
cx.expr_binary(span, BinOpKind::Or, subexpr, eq)
|
||||
},
|
||||
cx.expr_bool(span, false),
|
||||
Box::new(|cx, span, _, _| cx.expr_bool(span, true)),
|
||||
cx, span, substr)
|
||||
cx.expr_binary(span, BinOpKind::Or, subexpr, eq)
|
||||
},
|
||||
cx.expr_bool(span, false),
|
||||
Box::new(|cx, span, _, _| cx.expr_bool(span, true)),
|
||||
cx,
|
||||
span,
|
||||
substr)
|
||||
}
|
||||
|
||||
macro_rules! md {
|
||||
|
||||
@@ -13,8 +13,8 @@
|
||||
use deriving::generic::*;
|
||||
use deriving::generic::ty::*;
|
||||
|
||||
use syntax::ast::{MetaItem, Expr, BinOpKind, self};
|
||||
use syntax::ext::base::{ExtCtxt, Annotatable};
|
||||
use syntax::ast::{self, BinOpKind, Expr, MetaItem};
|
||||
use syntax::ext::base::{Annotatable, ExtCtxt};
|
||||
use syntax::ext::build::AstBuilder;
|
||||
use syntax::parse::token::InternedString;
|
||||
use syntax::ptr::P;
|
||||
@@ -24,8 +24,7 @@ pub fn expand_deriving_partial_ord(cx: &mut ExtCtxt,
|
||||
span: Span,
|
||||
mitem: &MetaItem,
|
||||
item: &Annotatable,
|
||||
push: &mut FnMut(Annotatable))
|
||||
{
|
||||
push: &mut FnMut(Annotatable)) {
|
||||
macro_rules! md {
|
||||
($name:expr, $op:expr, $equal:expr) => { {
|
||||
let inline = cx.meta_word(span, InternedString::new("inline"));
|
||||
@@ -53,7 +52,7 @@ macro_rules! md {
|
||||
true));
|
||||
|
||||
let inline = cx.meta_word(span, InternedString::new("inline"));
|
||||
let attrs = vec!(cx.attribute(span, inline));
|
||||
let attrs = vec![cx.attribute(span, inline)];
|
||||
|
||||
let partial_cmp_def = MethodDef {
|
||||
name: "partial_cmp",
|
||||
@@ -66,7 +65,7 @@ macro_rules! md {
|
||||
unify_fieldless_variants: true,
|
||||
combine_substructure: combine_substructure(Box::new(|cx, span, substr| {
|
||||
cs_partial_cmp(cx, span, substr)
|
||||
}))
|
||||
})),
|
||||
};
|
||||
|
||||
// avoid defining extra methods if we can
|
||||
@@ -75,13 +74,11 @@ macro_rules! md {
|
||||
let methods = if is_type_without_fields(item) {
|
||||
vec![partial_cmp_def]
|
||||
} else {
|
||||
vec![
|
||||
partial_cmp_def,
|
||||
md!("lt", true, false),
|
||||
md!("le", true, true),
|
||||
md!("gt", false, false),
|
||||
md!("ge", false, true)
|
||||
]
|
||||
vec![partial_cmp_def,
|
||||
md!("lt", true, false),
|
||||
md!("le", true, true),
|
||||
md!("gt", false, false),
|
||||
md!("ge", false, true)]
|
||||
};
|
||||
|
||||
let trait_def = TraitDef {
|
||||
@@ -99,142 +96,146 @@ macro_rules! md {
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub enum OrderingOp {
|
||||
PartialCmpOp, LtOp, LeOp, GtOp, GeOp,
|
||||
PartialCmpOp,
|
||||
LtOp,
|
||||
LeOp,
|
||||
GtOp,
|
||||
GeOp,
|
||||
}
|
||||
|
||||
pub fn some_ordering_collapsed(cx: &mut ExtCtxt,
|
||||
span: Span,
|
||||
op: OrderingOp,
|
||||
self_arg_tags: &[ast::Ident]) -> P<ast::Expr> {
|
||||
self_arg_tags: &[ast::Ident])
|
||||
-> P<ast::Expr> {
|
||||
let lft = cx.expr_ident(span, self_arg_tags[0]);
|
||||
let rgt = cx.expr_addr_of(span, cx.expr_ident(span, self_arg_tags[1]));
|
||||
let op_str = match op {
|
||||
PartialCmpOp => "partial_cmp",
|
||||
LtOp => "lt", LeOp => "le",
|
||||
GtOp => "gt", GeOp => "ge",
|
||||
LtOp => "lt",
|
||||
LeOp => "le",
|
||||
GtOp => "gt",
|
||||
GeOp => "ge",
|
||||
};
|
||||
cx.expr_method_call(span, lft, cx.ident_of(op_str), vec![rgt])
|
||||
}
|
||||
|
||||
pub fn cs_partial_cmp(cx: &mut ExtCtxt, span: Span,
|
||||
substr: &Substructure) -> P<Expr> {
|
||||
pub fn cs_partial_cmp(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P<Expr> {
|
||||
let test_id = cx.ident_of("__cmp");
|
||||
let ordering = cx.path_global(span,
|
||||
cx.std_path(&["cmp", "Ordering", "Equal"]));
|
||||
let ordering = cx.path_global(span, cx.std_path(&["cmp", "Ordering", "Equal"]));
|
||||
let ordering_expr = cx.expr_path(ordering.clone());
|
||||
let equals_expr = cx.expr_some(span, ordering_expr);
|
||||
|
||||
let partial_cmp_path = cx.std_path(&["cmp", "PartialOrd", "partial_cmp"]);
|
||||
|
||||
/*
|
||||
Builds:
|
||||
// Builds:
|
||||
//
|
||||
// match ::std::cmp::PartialOrd::partial_cmp(&self_field1, &other_field1) {
|
||||
// ::std::option::Option::Some(::std::cmp::Ordering::Equal) =>
|
||||
// match ::std::cmp::PartialOrd::partial_cmp(&self_field2, &other_field2) {
|
||||
// ::std::option::Option::Some(::std::cmp::Ordering::Equal) => {
|
||||
// ...
|
||||
// }
|
||||
// __cmp => __cmp
|
||||
// },
|
||||
// __cmp => __cmp
|
||||
// }
|
||||
//
|
||||
cs_fold(// foldr nests the if-elses correctly, leaving the first field
|
||||
// as the outermost one, and the last as the innermost.
|
||||
false,
|
||||
|cx, span, old, self_f, other_fs| {
|
||||
// match new {
|
||||
// Some(::std::cmp::Ordering::Equal) => old,
|
||||
// __cmp => __cmp
|
||||
// }
|
||||
|
||||
match ::std::cmp::PartialOrd::partial_cmp(&self_field1, &other_field1) {
|
||||
::std::option::Option::Some(::std::cmp::Ordering::Equal) =>
|
||||
match ::std::cmp::PartialOrd::partial_cmp(&self_field2, &other_field2) {
|
||||
::std::option::Option::Some(::std::cmp::Ordering::Equal) => {
|
||||
...
|
||||
}
|
||||
__cmp => __cmp
|
||||
},
|
||||
__cmp => __cmp
|
||||
}
|
||||
*/
|
||||
cs_fold(
|
||||
// foldr nests the if-elses correctly, leaving the first field
|
||||
// as the outermost one, and the last as the innermost.
|
||||
false,
|
||||
|cx, span, old, self_f, other_fs| {
|
||||
// match new {
|
||||
// Some(::std::cmp::Ordering::Equal) => old,
|
||||
// __cmp => __cmp
|
||||
// }
|
||||
let new = {
|
||||
let other_f = match (other_fs.len(), other_fs.get(0)) {
|
||||
(1, Some(o_f)) => o_f,
|
||||
_ => cx.span_bug(span, "not exactly 2 arguments in `derive(PartialOrd)`"),
|
||||
};
|
||||
|
||||
let new = {
|
||||
let other_f = match (other_fs.len(), other_fs.get(0)) {
|
||||
(1, Some(o_f)) => o_f,
|
||||
_ => cx.span_bug(span, "not exactly 2 arguments in `derive(PartialOrd)`"),
|
||||
};
|
||||
|
||||
let args = vec![
|
||||
let args = vec![
|
||||
cx.expr_addr_of(span, self_f),
|
||||
cx.expr_addr_of(span, other_f.clone()),
|
||||
];
|
||||
|
||||
cx.expr_call_global(span, partial_cmp_path.clone(), args)
|
||||
};
|
||||
cx.expr_call_global(span, partial_cmp_path.clone(), args)
|
||||
};
|
||||
|
||||
let eq_arm = cx.arm(span,
|
||||
vec![cx.pat_some(span,
|
||||
cx.pat_enum(span,
|
||||
ordering.clone(),
|
||||
vec![]))],
|
||||
old);
|
||||
let neq_arm = cx.arm(span,
|
||||
vec![cx.pat_ident(span, test_id)],
|
||||
cx.expr_ident(span, test_id));
|
||||
let eq_arm = cx.arm(span,
|
||||
vec![cx.pat_some(span, cx.pat_enum(span, ordering.clone(), vec![]))],
|
||||
old);
|
||||
let neq_arm = cx.arm(span,
|
||||
vec![cx.pat_ident(span, test_id)],
|
||||
cx.expr_ident(span, test_id));
|
||||
|
||||
cx.expr_match(span, new, vec![eq_arm, neq_arm])
|
||||
},
|
||||
equals_expr.clone(),
|
||||
Box::new(|cx, span, (self_args, tag_tuple), _non_self_args| {
|
||||
if self_args.len() != 2 {
|
||||
cx.span_bug(span, "not exactly 2 arguments in `derive(PartialOrd)`")
|
||||
} else {
|
||||
some_ordering_collapsed(cx, span, PartialCmpOp, tag_tuple)
|
||||
}
|
||||
}),
|
||||
cx, span, substr)
|
||||
cx.expr_match(span, new, vec![eq_arm, neq_arm])
|
||||
},
|
||||
equals_expr.clone(),
|
||||
Box::new(|cx, span, (self_args, tag_tuple), _non_self_args| {
|
||||
if self_args.len() != 2 {
|
||||
cx.span_bug(span, "not exactly 2 arguments in `derive(PartialOrd)`")
|
||||
} else {
|
||||
some_ordering_collapsed(cx, span, PartialCmpOp, tag_tuple)
|
||||
}
|
||||
}),
|
||||
cx,
|
||||
span,
|
||||
substr)
|
||||
}
|
||||
|
||||
/// Strict inequality.
|
||||
fn cs_op(less: bool, equal: bool, cx: &mut ExtCtxt,
|
||||
span: Span, substr: &Substructure) -> P<Expr> {
|
||||
fn cs_op(less: bool, equal: bool, cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P<Expr> {
|
||||
let op = if less { BinOpKind::Lt } else { BinOpKind::Gt };
|
||||
cs_fold(
|
||||
false, // need foldr,
|
||||
|cx, span, subexpr, self_f, other_fs| {
|
||||
/*
|
||||
build up a series of chain ||'s and &&'s from the inside
|
||||
out (hence foldr) to get lexical ordering, i.e. for op ==
|
||||
`ast::lt`
|
||||
cs_fold(false, // need foldr,
|
||||
|cx, span, subexpr, self_f, other_fs| {
|
||||
// build up a series of chain ||'s and &&'s from the inside
|
||||
// out (hence foldr) to get lexical ordering, i.e. for op ==
|
||||
// `ast::lt`
|
||||
//
|
||||
// ```
|
||||
// self.f1 < other.f1 || (!(other.f1 < self.f1) &&
|
||||
// (self.f2 < other.f2 || (!(other.f2 < self.f2) &&
|
||||
// (false)
|
||||
// ))
|
||||
// )
|
||||
// ```
|
||||
//
|
||||
// The optimiser should remove the redundancy. We explicitly
|
||||
// get use the binops to avoid auto-deref dereferencing too many
|
||||
// layers of pointers, if the type includes pointers.
|
||||
//
|
||||
let other_f = match (other_fs.len(), other_fs.get(0)) {
|
||||
(1, Some(o_f)) => o_f,
|
||||
_ => cx.span_bug(span, "not exactly 2 arguments in `derive(PartialOrd)`"),
|
||||
};
|
||||
|
||||
```
|
||||
self.f1 < other.f1 || (!(other.f1 < self.f1) &&
|
||||
(self.f2 < other.f2 || (!(other.f2 < self.f2) &&
|
||||
(false)
|
||||
))
|
||||
)
|
||||
```
|
||||
let cmp = cx.expr_binary(span, op, self_f.clone(), other_f.clone());
|
||||
|
||||
The optimiser should remove the redundancy. We explicitly
|
||||
get use the binops to avoid auto-deref dereferencing too many
|
||||
layers of pointers, if the type includes pointers.
|
||||
*/
|
||||
let other_f = match (other_fs.len(), other_fs.get(0)) {
|
||||
(1, Some(o_f)) => o_f,
|
||||
_ => cx.span_bug(span, "not exactly 2 arguments in `derive(PartialOrd)`")
|
||||
let not_cmp = cx.expr_unary(span,
|
||||
ast::UnOp::Not,
|
||||
cx.expr_binary(span, op, other_f.clone(), self_f));
|
||||
|
||||
let and = cx.expr_binary(span, BinOpKind::And, not_cmp, subexpr);
|
||||
cx.expr_binary(span, BinOpKind::Or, cmp, and)
|
||||
},
|
||||
cx.expr_bool(span, equal),
|
||||
Box::new(|cx, span, (self_args, tag_tuple), _non_self_args| {
|
||||
if self_args.len() != 2 {
|
||||
cx.span_bug(span, "not exactly 2 arguments in `derive(PartialOrd)`")
|
||||
} else {
|
||||
let op = match (less, equal) {
|
||||
(true, true) => LeOp,
|
||||
(true, false) => LtOp,
|
||||
(false, true) => GeOp,
|
||||
(false, false) => GtOp,
|
||||
};
|
||||
|
||||
let cmp = cx.expr_binary(span, op, self_f.clone(), other_f.clone());
|
||||
|
||||
let not_cmp = cx.expr_unary(span, ast::UnOp::Not,
|
||||
cx.expr_binary(span, op, other_f.clone(), self_f));
|
||||
|
||||
let and = cx.expr_binary(span, BinOpKind::And, not_cmp, subexpr);
|
||||
cx.expr_binary(span, BinOpKind::Or, cmp, and)
|
||||
},
|
||||
cx.expr_bool(span, equal),
|
||||
Box::new(|cx, span, (self_args, tag_tuple), _non_self_args| {
|
||||
if self_args.len() != 2 {
|
||||
cx.span_bug(span, "not exactly 2 arguments in `derive(PartialOrd)`")
|
||||
} else {
|
||||
let op = match (less, equal) {
|
||||
(true, true) => LeOp, (true, false) => LtOp,
|
||||
(false, true) => GeOp, (false, false) => GtOp,
|
||||
};
|
||||
some_ordering_collapsed(cx, span, op, tag_tuple)
|
||||
}
|
||||
}),
|
||||
cx, span, substr)
|
||||
some_ordering_collapsed(cx, span, op, tag_tuple)
|
||||
}
|
||||
}),
|
||||
cx,
|
||||
span,
|
||||
substr)
|
||||
}
|
||||
|
||||
@@ -12,19 +12,18 @@
|
||||
use deriving::generic::ty::*;
|
||||
|
||||
use syntax::ast;
|
||||
use syntax::ast::{MetaItem, Expr};
|
||||
use syntax::ext::base::{ExtCtxt, Annotatable};
|
||||
use syntax::ast::{Expr, MetaItem};
|
||||
use syntax::ext::base::{Annotatable, ExtCtxt};
|
||||
use syntax::ext::build::AstBuilder;
|
||||
use syntax::parse::token;
|
||||
use syntax::ptr::P;
|
||||
use syntax_pos::{Span, DUMMY_SP};
|
||||
use syntax_pos::{DUMMY_SP, Span};
|
||||
|
||||
pub fn expand_deriving_debug(cx: &mut ExtCtxt,
|
||||
span: Span,
|
||||
mitem: &MetaItem,
|
||||
item: &Annotatable,
|
||||
push: &mut FnMut(Annotatable))
|
||||
{
|
||||
span: Span,
|
||||
mitem: &MetaItem,
|
||||
item: &Annotatable,
|
||||
push: &mut FnMut(Annotatable)) {
|
||||
// &mut ::std::fmt::Formatter
|
||||
let fmtr = Ptr(Box::new(Literal(path_std!(cx, core::fmt::Formatter))),
|
||||
Borrowed(None, ast::Mutability::Mutable));
|
||||
@@ -36,57 +35,54 @@ pub fn expand_deriving_debug(cx: &mut ExtCtxt,
|
||||
additional_bounds: Vec::new(),
|
||||
generics: LifetimeBounds::empty(),
|
||||
is_unsafe: false,
|
||||
methods: vec![
|
||||
MethodDef {
|
||||
name: "fmt",
|
||||
generics: LifetimeBounds::empty(),
|
||||
explicit_self: borrowed_explicit_self(),
|
||||
args: vec!(fmtr),
|
||||
ret_ty: Literal(path_std!(cx, core::fmt::Result)),
|
||||
attributes: Vec::new(),
|
||||
is_unsafe: false,
|
||||
unify_fieldless_variants: false,
|
||||
combine_substructure: combine_substructure(Box::new(|a, b, c| {
|
||||
show_substructure(a, b, c)
|
||||
}))
|
||||
}
|
||||
],
|
||||
methods: vec![MethodDef {
|
||||
name: "fmt",
|
||||
generics: LifetimeBounds::empty(),
|
||||
explicit_self: borrowed_explicit_self(),
|
||||
args: vec![fmtr],
|
||||
ret_ty: Literal(path_std!(cx, core::fmt::Result)),
|
||||
attributes: Vec::new(),
|
||||
is_unsafe: false,
|
||||
unify_fieldless_variants: false,
|
||||
combine_substructure: combine_substructure(Box::new(|a, b, c| {
|
||||
show_substructure(a, b, c)
|
||||
})),
|
||||
}],
|
||||
associated_types: Vec::new(),
|
||||
};
|
||||
trait_def.expand(cx, mitem, item, push)
|
||||
}
|
||||
|
||||
/// We use the debug builders to do the heavy lifting here
|
||||
fn show_substructure(cx: &mut ExtCtxt, span: Span,
|
||||
substr: &Substructure) -> P<Expr> {
|
||||
fn show_substructure(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P<Expr> {
|
||||
// build fmt.debug_struct(<name>).field(<fieldname>, &<fieldval>)....build()
|
||||
// or fmt.debug_tuple(<name>).field(&<fieldval>)....build()
|
||||
// based on the "shape".
|
||||
let (ident, is_struct) = match *substr.fields {
|
||||
Struct(vdata, _) => (substr.type_ident, vdata.is_struct()),
|
||||
EnumMatching(_, v, _) => (v.node.name, v.node.data.is_struct()),
|
||||
EnumNonMatchingCollapsed(..) | StaticStruct(..) | StaticEnum(..) => {
|
||||
cx.span_bug(span, "nonsensical .fields in `#[derive(Debug)]`")
|
||||
}
|
||||
EnumNonMatchingCollapsed(..) |
|
||||
StaticStruct(..) |
|
||||
StaticEnum(..) => cx.span_bug(span, "nonsensical .fields in `#[derive(Debug)]`"),
|
||||
};
|
||||
|
||||
// We want to make sure we have the expn_id set so that we can use unstable methods
|
||||
let span = Span { expn_id: cx.backtrace(), .. span };
|
||||
let name = cx.expr_lit(span, ast::LitKind::Str(ident.name.as_str(), ast::StrStyle::Cooked));
|
||||
let span = Span { expn_id: cx.backtrace(), ..span };
|
||||
let name = cx.expr_lit(span,
|
||||
ast::LitKind::Str(ident.name.as_str(), ast::StrStyle::Cooked));
|
||||
let builder = token::str_to_ident("builder");
|
||||
let builder_expr = cx.expr_ident(span, builder.clone());
|
||||
|
||||
let fmt = substr.nonself_args[0].clone();
|
||||
|
||||
let mut stmts = match *substr.fields {
|
||||
Struct(_, ref fields) | EnumMatching(_, _, ref fields) => {
|
||||
Struct(_, ref fields) |
|
||||
EnumMatching(_, _, ref fields) => {
|
||||
let mut stmts = vec![];
|
||||
if !is_struct {
|
||||
// tuple struct/"normal" variant
|
||||
let expr = cx.expr_method_call(span,
|
||||
fmt,
|
||||
token::str_to_ident("debug_tuple"),
|
||||
vec![name]);
|
||||
let expr =
|
||||
cx.expr_method_call(span, fmt, token::str_to_ident("debug_tuple"), vec![name]);
|
||||
stmts.push(cx.stmt_let(DUMMY_SP, true, builder, expr));
|
||||
|
||||
for field in fields {
|
||||
@@ -105,16 +101,14 @@ fn show_substructure(cx: &mut ExtCtxt, span: Span,
|
||||
}
|
||||
} else {
|
||||
// normal struct/struct variant
|
||||
let expr = cx.expr_method_call(span,
|
||||
fmt,
|
||||
token::str_to_ident("debug_struct"),
|
||||
vec![name]);
|
||||
let expr =
|
||||
cx.expr_method_call(span, fmt, token::str_to_ident("debug_struct"), vec![name]);
|
||||
stmts.push(cx.stmt_let(DUMMY_SP, true, builder, expr));
|
||||
|
||||
for field in fields {
|
||||
let name = cx.expr_lit(field.span, ast::LitKind::Str(
|
||||
field.name.unwrap().name.as_str(),
|
||||
ast::StrStyle::Cooked));
|
||||
let name = cx.expr_lit(field.span,
|
||||
ast::LitKind::Str(field.name.unwrap().name.as_str(),
|
||||
ast::StrStyle::Cooked));
|
||||
|
||||
// Use double indirection to make sure this works for unsized types
|
||||
let field = cx.expr_addr_of(field.span, field.self_.clone());
|
||||
@@ -128,22 +122,17 @@ fn show_substructure(cx: &mut ExtCtxt, span: Span,
|
||||
}
|
||||
stmts
|
||||
}
|
||||
_ => unreachable!()
|
||||
_ => unreachable!(),
|
||||
};
|
||||
|
||||
let expr = cx.expr_method_call(span,
|
||||
builder_expr,
|
||||
token::str_to_ident("finish"),
|
||||
vec![]);
|
||||
let expr = cx.expr_method_call(span, builder_expr, token::str_to_ident("finish"), vec![]);
|
||||
|
||||
stmts.push(cx.stmt_expr(expr));
|
||||
let block = cx.block(span, stmts);
|
||||
cx.expr_block(block)
|
||||
}
|
||||
|
||||
fn stmt_let_undescore(cx: &mut ExtCtxt,
|
||||
sp: Span,
|
||||
expr: P<ast::Expr>) -> ast::Stmt {
|
||||
fn stmt_let_undescore(cx: &mut ExtCtxt, sp: Span, expr: P<ast::Expr>) -> ast::Stmt {
|
||||
let local = P(ast::Local {
|
||||
pat: cx.pat_wild(sp),
|
||||
ty: None,
|
||||
|
||||
@@ -15,8 +15,8 @@
|
||||
use deriving::generic::ty::*;
|
||||
|
||||
use syntax::ast;
|
||||
use syntax::ast::{MetaItem, Expr, Mutability};
|
||||
use syntax::ext::base::{ExtCtxt, Annotatable};
|
||||
use syntax::ast::{Expr, MetaItem, Mutability};
|
||||
use syntax::ext::base::{Annotatable, ExtCtxt};
|
||||
use syntax::ext::build::AstBuilder;
|
||||
use syntax::parse::token::InternedString;
|
||||
use syntax::parse::token;
|
||||
@@ -27,8 +27,7 @@ pub fn expand_deriving_rustc_decodable(cx: &mut ExtCtxt,
|
||||
span: Span,
|
||||
mitem: &MetaItem,
|
||||
item: &Annotatable,
|
||||
push: &mut FnMut(Annotatable))
|
||||
{
|
||||
push: &mut FnMut(Annotatable)) {
|
||||
expand_deriving_decodable_imp(cx, span, mitem, item, push, "rustc_serialize")
|
||||
}
|
||||
|
||||
@@ -36,8 +35,7 @@ pub fn expand_deriving_decodable(cx: &mut ExtCtxt,
|
||||
span: Span,
|
||||
mitem: &MetaItem,
|
||||
item: &Annotatable,
|
||||
push: &mut FnMut(Annotatable))
|
||||
{
|
||||
push: &mut FnMut(Annotatable)) {
|
||||
expand_deriving_decodable_imp(cx, span, mitem, item, push, "serialize")
|
||||
}
|
||||
|
||||
@@ -46,13 +44,13 @@ fn expand_deriving_decodable_imp(cx: &mut ExtCtxt,
|
||||
mitem: &MetaItem,
|
||||
item: &Annotatable,
|
||||
push: &mut FnMut(Annotatable),
|
||||
krate: &'static str)
|
||||
{
|
||||
krate: &'static str) {
|
||||
if cx.crate_root != Some("std") {
|
||||
// FIXME(#21880): lift this requirement.
|
||||
cx.span_err(span, "this trait cannot be derived with #![no_std] \
|
||||
cx.span_err(span,
|
||||
"this trait cannot be derived with #![no_std] \
|
||||
or #![no_core]");
|
||||
return
|
||||
return;
|
||||
}
|
||||
|
||||
let typaram = &*deriving::hygienic_type_parameter(item, "__D");
|
||||
@@ -60,50 +58,50 @@ fn expand_deriving_decodable_imp(cx: &mut ExtCtxt,
|
||||
let trait_def = TraitDef {
|
||||
span: span,
|
||||
attributes: Vec::new(),
|
||||
path: Path::new_(vec!(krate, "Decodable"), None, vec!(), true),
|
||||
path: Path::new_(vec![krate, "Decodable"], None, vec![], true),
|
||||
additional_bounds: Vec::new(),
|
||||
generics: LifetimeBounds::empty(),
|
||||
is_unsafe: false,
|
||||
methods: vec!(
|
||||
MethodDef {
|
||||
name: "decode",
|
||||
generics: LifetimeBounds {
|
||||
lifetimes: Vec::new(),
|
||||
bounds: vec![(typaram,
|
||||
vec![Path::new_(vec!(krate, "Decoder"), None, vec!(), true)])]
|
||||
},
|
||||
explicit_self: None,
|
||||
args: vec!(Ptr(Box::new(Literal(Path::new_local(typaram))),
|
||||
Borrowed(None, Mutability::Mutable))),
|
||||
ret_ty: Literal(Path::new_(
|
||||
pathvec_std!(cx, core::result::Result),
|
||||
None,
|
||||
vec!(Box::new(Self_), Box::new(Literal(Path::new_(
|
||||
methods: vec![MethodDef {
|
||||
name: "decode",
|
||||
generics: LifetimeBounds {
|
||||
lifetimes: Vec::new(),
|
||||
bounds: vec![(typaram,
|
||||
vec![Path::new_(vec![krate, "Decoder"],
|
||||
None,
|
||||
vec![],
|
||||
true)])],
|
||||
},
|
||||
explicit_self: None,
|
||||
args: vec![Ptr(Box::new(Literal(Path::new_local(typaram))),
|
||||
Borrowed(None, Mutability::Mutable))],
|
||||
ret_ty:
|
||||
Literal(Path::new_(pathvec_std!(cx, core::result::Result),
|
||||
None,
|
||||
vec!(Box::new(Self_), Box::new(Literal(Path::new_(
|
||||
vec![typaram, "Error"], None, vec![], false
|
||||
)))),
|
||||
true
|
||||
)),
|
||||
attributes: Vec::new(),
|
||||
is_unsafe: false,
|
||||
unify_fieldless_variants: false,
|
||||
combine_substructure: combine_substructure(Box::new(|a, b, c| {
|
||||
decodable_substructure(a, b, c, krate)
|
||||
})),
|
||||
}
|
||||
),
|
||||
true)),
|
||||
attributes: Vec::new(),
|
||||
is_unsafe: false,
|
||||
unify_fieldless_variants: false,
|
||||
combine_substructure: combine_substructure(Box::new(|a, b, c| {
|
||||
decodable_substructure(a, b, c, krate)
|
||||
})),
|
||||
}],
|
||||
associated_types: Vec::new(),
|
||||
};
|
||||
|
||||
trait_def.expand(cx, mitem, item, push)
|
||||
}
|
||||
|
||||
fn decodable_substructure(cx: &mut ExtCtxt, trait_span: Span,
|
||||
fn decodable_substructure(cx: &mut ExtCtxt,
|
||||
trait_span: Span,
|
||||
substr: &Substructure,
|
||||
krate: &str) -> P<Expr> {
|
||||
krate: &str)
|
||||
-> P<Expr> {
|
||||
let decoder = substr.nonself_args[0].clone();
|
||||
let recurse = vec!(cx.ident_of(krate),
|
||||
cx.ident_of("Decodable"),
|
||||
cx.ident_of("decode"));
|
||||
let recurse = vec![cx.ident_of(krate), cx.ident_of("Decodable"), cx.ident_of("decode")];
|
||||
let exprdecode = cx.expr_path(cx.path_global(trait_span, recurse));
|
||||
// throw an underscore in front to suppress unused variable warnings
|
||||
let blkarg = cx.ident_of("_d");
|
||||
@@ -113,31 +111,28 @@ fn decodable_substructure(cx: &mut ExtCtxt, trait_span: Span,
|
||||
StaticStruct(_, ref summary) => {
|
||||
let nfields = match *summary {
|
||||
Unnamed(ref fields) => fields.len(),
|
||||
Named(ref fields) => fields.len()
|
||||
Named(ref fields) => fields.len(),
|
||||
};
|
||||
let read_struct_field = cx.ident_of("read_struct_field");
|
||||
|
||||
let path = cx.path_ident(trait_span, substr.type_ident);
|
||||
let result = decode_static_fields(cx,
|
||||
trait_span,
|
||||
path,
|
||||
summary,
|
||||
|cx, span, name, field| {
|
||||
cx.expr_try(span,
|
||||
cx.expr_method_call(span, blkdecoder.clone(), read_struct_field,
|
||||
vec!(cx.expr_str(span, name),
|
||||
cx.expr_usize(span, field),
|
||||
exprdecode.clone())))
|
||||
});
|
||||
let result =
|
||||
decode_static_fields(cx, trait_span, path, summary, |cx, span, name, field| {
|
||||
cx.expr_try(span,
|
||||
cx.expr_method_call(span,
|
||||
blkdecoder.clone(),
|
||||
read_struct_field,
|
||||
vec![cx.expr_str(span, name),
|
||||
cx.expr_usize(span, field),
|
||||
exprdecode.clone()]))
|
||||
});
|
||||
let result = cx.expr_ok(trait_span, result);
|
||||
cx.expr_method_call(trait_span,
|
||||
decoder,
|
||||
cx.ident_of("read_struct"),
|
||||
vec!(
|
||||
cx.expr_str(trait_span, substr.type_ident.name.as_str()),
|
||||
cx.expr_usize(trait_span, nfields),
|
||||
cx.lambda_expr_1(trait_span, result, blkarg)
|
||||
))
|
||||
vec![cx.expr_str(trait_span, substr.type_ident.name.as_str()),
|
||||
cx.expr_usize(trait_span, nfields),
|
||||
cx.lambda_expr_1(trait_span, result, blkarg)])
|
||||
}
|
||||
StaticEnum(_, ref fields) => {
|
||||
let variant = cx.ident_of("i");
|
||||
@@ -150,42 +145,39 @@ fn decodable_substructure(cx: &mut ExtCtxt, trait_span: Span,
|
||||
variants.push(cx.expr_str(v_span, ident.name.as_str()));
|
||||
|
||||
let path = cx.path(trait_span, vec![substr.type_ident, ident]);
|
||||
let decoded = decode_static_fields(cx,
|
||||
v_span,
|
||||
path,
|
||||
parts,
|
||||
|cx, span, _, field| {
|
||||
let decoded = decode_static_fields(cx, v_span, path, parts, |cx, span, _, field| {
|
||||
let idx = cx.expr_usize(span, field);
|
||||
cx.expr_try(span,
|
||||
cx.expr_method_call(span, blkdecoder.clone(), rvariant_arg,
|
||||
vec!(idx, exprdecode.clone())))
|
||||
cx.expr_method_call(span,
|
||||
blkdecoder.clone(),
|
||||
rvariant_arg,
|
||||
vec![idx, exprdecode.clone()]))
|
||||
});
|
||||
|
||||
arms.push(cx.arm(v_span,
|
||||
vec!(cx.pat_lit(v_span, cx.expr_usize(v_span, i))),
|
||||
vec![cx.pat_lit(v_span, cx.expr_usize(v_span, i))],
|
||||
decoded));
|
||||
}
|
||||
|
||||
arms.push(cx.arm_unreachable(trait_span));
|
||||
|
||||
let result = cx.expr_ok(trait_span,
|
||||
cx.expr_match(trait_span,
|
||||
cx.expr_ident(trait_span, variant), arms));
|
||||
let lambda = cx.lambda_expr(trait_span, vec!(blkarg, variant), result);
|
||||
let result =
|
||||
cx.expr_ok(trait_span,
|
||||
cx.expr_match(trait_span, cx.expr_ident(trait_span, variant), arms));
|
||||
let lambda = cx.lambda_expr(trait_span, vec![blkarg, variant], result);
|
||||
let variant_vec = cx.expr_vec(trait_span, variants);
|
||||
let variant_vec = cx.expr_addr_of(trait_span, variant_vec);
|
||||
let result = cx.expr_method_call(trait_span, blkdecoder,
|
||||
let result = cx.expr_method_call(trait_span,
|
||||
blkdecoder,
|
||||
cx.ident_of("read_enum_variant"),
|
||||
vec!(variant_vec, lambda));
|
||||
vec![variant_vec, lambda]);
|
||||
cx.expr_method_call(trait_span,
|
||||
decoder,
|
||||
cx.ident_of("read_enum"),
|
||||
vec!(
|
||||
cx.expr_str(trait_span, substr.type_ident.name.as_str()),
|
||||
cx.lambda_expr_1(trait_span, result, blkarg)
|
||||
))
|
||||
vec![cx.expr_str(trait_span, substr.type_ident.name.as_str()),
|
||||
cx.lambda_expr_1(trait_span, result, blkarg)])
|
||||
}
|
||||
_ => cx.bug("expected StaticEnum or StaticStruct in derive(Decodable)")
|
||||
_ => cx.bug("expected StaticEnum or StaticStruct in derive(Decodable)"),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -197,8 +189,8 @@ fn decode_static_fields<F>(cx: &mut ExtCtxt,
|
||||
outer_pat_path: ast::Path,
|
||||
fields: &StaticFields,
|
||||
mut getarg: F)
|
||||
-> P<Expr> where
|
||||
F: FnMut(&mut ExtCtxt, Span, InternedString, usize) -> P<Expr>,
|
||||
-> P<Expr>
|
||||
where F: FnMut(&mut ExtCtxt, Span, InternedString, usize) -> P<Expr>
|
||||
{
|
||||
match *fields {
|
||||
Unnamed(ref fields) => {
|
||||
@@ -206,21 +198,28 @@ fn decode_static_fields<F>(cx: &mut ExtCtxt,
|
||||
if fields.is_empty() {
|
||||
path_expr
|
||||
} else {
|
||||
let fields = fields.iter().enumerate().map(|(i, &span)| {
|
||||
getarg(cx, span,
|
||||
token::intern_and_get_ident(&format!("_field{}", i)),
|
||||
i)
|
||||
}).collect();
|
||||
let fields = fields.iter()
|
||||
.enumerate()
|
||||
.map(|(i, &span)| {
|
||||
getarg(cx,
|
||||
span,
|
||||
token::intern_and_get_ident(&format!("_field{}", i)),
|
||||
i)
|
||||
})
|
||||
.collect();
|
||||
|
||||
cx.expr_call(trait_span, path_expr, fields)
|
||||
}
|
||||
}
|
||||
Named(ref fields) => {
|
||||
// use the field's span to get nicer error messages.
|
||||
let fields = fields.iter().enumerate().map(|(i, &(ident, span))| {
|
||||
let arg = getarg(cx, span, ident.name.as_str(), i);
|
||||
cx.field_imm(span, ident, arg)
|
||||
}).collect();
|
||||
let fields = fields.iter()
|
||||
.enumerate()
|
||||
.map(|(i, &(ident, span))| {
|
||||
let arg = getarg(cx, span, ident.name.as_str(), i);
|
||||
cx.field_imm(span, ident, arg)
|
||||
})
|
||||
.collect();
|
||||
cx.expr_struct(trait_span, outer_pat_path, fields)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,8 +11,8 @@
|
||||
use deriving::generic::*;
|
||||
use deriving::generic::ty::*;
|
||||
|
||||
use syntax::ast::{MetaItem, Expr};
|
||||
use syntax::ext::base::{ExtCtxt, Annotatable};
|
||||
use syntax::ast::{Expr, MetaItem};
|
||||
use syntax::ext::base::{Annotatable, ExtCtxt};
|
||||
use syntax::ext::build::AstBuilder;
|
||||
use syntax::parse::token::InternedString;
|
||||
use syntax::ptr::P;
|
||||
@@ -22,10 +22,9 @@ pub fn expand_deriving_default(cx: &mut ExtCtxt,
|
||||
span: Span,
|
||||
mitem: &MetaItem,
|
||||
item: &Annotatable,
|
||||
push: &mut FnMut(Annotatable))
|
||||
{
|
||||
push: &mut FnMut(Annotatable)) {
|
||||
let inline = cx.meta_word(span, InternedString::new("inline"));
|
||||
let attrs = vec!(cx.attribute(span, inline));
|
||||
let attrs = vec![cx.attribute(span, inline)];
|
||||
let trait_def = TraitDef {
|
||||
span: span,
|
||||
attributes: Vec::new(),
|
||||
@@ -33,21 +32,19 @@ pub fn expand_deriving_default(cx: &mut ExtCtxt,
|
||||
additional_bounds: Vec::new(),
|
||||
generics: LifetimeBounds::empty(),
|
||||
is_unsafe: false,
|
||||
methods: vec!(
|
||||
MethodDef {
|
||||
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)
|
||||
}))
|
||||
}
|
||||
),
|
||||
methods: vec![MethodDef {
|
||||
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)
|
||||
})),
|
||||
}],
|
||||
associated_types: Vec::new(),
|
||||
};
|
||||
trait_def.expand(cx, mitem, item, push)
|
||||
@@ -69,18 +66,19 @@ fn default_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructur
|
||||
}
|
||||
}
|
||||
Named(ref fields) => {
|
||||
let default_fields = fields.iter().map(|&(ident, span)| {
|
||||
cx.field_imm(span, ident, default_call(span))
|
||||
}).collect();
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
StaticEnum(..) => {
|
||||
cx.span_err(trait_span, "`Default` cannot be derived for enums, only structs");
|
||||
cx.span_err(trait_span,
|
||||
"`Default` cannot be derived for enums, only structs");
|
||||
// let compilation continue
|
||||
cx.expr_usize(trait_span, 0)
|
||||
}
|
||||
_ => cx.span_bug(trait_span, "Non-static method in `derive(Default)`")
|
||||
_ => cx.span_bug(trait_span, "Non-static method in `derive(Default)`"),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -92,8 +92,8 @@
|
||||
use deriving::generic::*;
|
||||
use deriving::generic::ty::*;
|
||||
|
||||
use syntax::ast::{MetaItem, Expr, ExprKind, Mutability};
|
||||
use syntax::ext::base::{ExtCtxt,Annotatable};
|
||||
use syntax::ast::{Expr, ExprKind, MetaItem, Mutability};
|
||||
use syntax::ext::base::{Annotatable, ExtCtxt};
|
||||
use syntax::ext::build::AstBuilder;
|
||||
use syntax::parse::token;
|
||||
use syntax::ptr::P;
|
||||
@@ -103,8 +103,7 @@ pub fn expand_deriving_rustc_encodable(cx: &mut ExtCtxt,
|
||||
span: Span,
|
||||
mitem: &MetaItem,
|
||||
item: &Annotatable,
|
||||
push: &mut FnMut(Annotatable))
|
||||
{
|
||||
push: &mut FnMut(Annotatable)) {
|
||||
expand_deriving_encodable_imp(cx, span, mitem, item, push, "rustc_serialize")
|
||||
}
|
||||
|
||||
@@ -112,8 +111,7 @@ pub fn expand_deriving_encodable(cx: &mut ExtCtxt,
|
||||
span: Span,
|
||||
mitem: &MetaItem,
|
||||
item: &Annotatable,
|
||||
push: &mut FnMut(Annotatable))
|
||||
{
|
||||
push: &mut FnMut(Annotatable)) {
|
||||
expand_deriving_encodable_imp(cx, span, mitem, item, push, "serialize")
|
||||
}
|
||||
|
||||
@@ -122,11 +120,11 @@ fn expand_deriving_encodable_imp(cx: &mut ExtCtxt,
|
||||
mitem: &MetaItem,
|
||||
item: &Annotatable,
|
||||
push: &mut FnMut(Annotatable),
|
||||
krate: &'static str)
|
||||
{
|
||||
krate: &'static str) {
|
||||
if cx.crate_root != Some("std") {
|
||||
// FIXME(#21880): lift this requirement.
|
||||
cx.span_err(span, "this trait cannot be derived with #![no_std] \
|
||||
cx.span_err(span,
|
||||
"this trait cannot be derived with #![no_std] \
|
||||
or #![no_core]");
|
||||
return;
|
||||
}
|
||||
@@ -136,7 +134,7 @@ fn expand_deriving_encodable_imp(cx: &mut ExtCtxt,
|
||||
let trait_def = TraitDef {
|
||||
span: span,
|
||||
attributes: Vec::new(),
|
||||
path: Path::new_(vec!(krate, "Encodable"), None, vec!(), true),
|
||||
path: Path::new_(vec![krate, "Encodable"], None, vec![], true),
|
||||
additional_bounds: Vec::new(),
|
||||
generics: LifetimeBounds::empty(),
|
||||
is_unsafe: false,
|
||||
@@ -173,40 +171,38 @@ fn expand_deriving_encodable_imp(cx: &mut ExtCtxt,
|
||||
trait_def.expand(cx, mitem, item, push)
|
||||
}
|
||||
|
||||
fn encodable_substructure(cx: &mut ExtCtxt, trait_span: Span,
|
||||
substr: &Substructure, krate: &'static str) -> P<Expr> {
|
||||
fn encodable_substructure(cx: &mut ExtCtxt,
|
||||
trait_span: Span,
|
||||
substr: &Substructure,
|
||||
krate: &'static str)
|
||||
-> P<Expr> {
|
||||
let encoder = substr.nonself_args[0].clone();
|
||||
// throw an underscore in front to suppress unused variable warnings
|
||||
let blkarg = cx.ident_of("_e");
|
||||
let blkencoder = cx.expr_ident(trait_span, blkarg);
|
||||
let fn_path = cx.expr_path(cx.path_global(trait_span, vec![cx.ident_of(krate),
|
||||
cx.ident_of("Encodable"),
|
||||
cx.ident_of("encode")]));
|
||||
let fn_path = cx.expr_path(cx.path_global(trait_span,
|
||||
vec![cx.ident_of(krate),
|
||||
cx.ident_of("Encodable"),
|
||||
cx.ident_of("encode")]));
|
||||
|
||||
return match *substr.fields {
|
||||
Struct(_, ref fields) => {
|
||||
let emit_struct_field = cx.ident_of("emit_struct_field");
|
||||
let mut stmts = Vec::new();
|
||||
for (i, &FieldInfo {
|
||||
name,
|
||||
ref self_,
|
||||
span,
|
||||
..
|
||||
}) in fields.iter().enumerate() {
|
||||
for (i, &FieldInfo { name, ref self_, span, .. }) in fields.iter().enumerate() {
|
||||
let name = match name {
|
||||
Some(id) => id.name.as_str(),
|
||||
None => {
|
||||
token::intern_and_get_ident(&format!("_field{}", i))
|
||||
}
|
||||
None => token::intern_and_get_ident(&format!("_field{}", i)),
|
||||
};
|
||||
let self_ref = cx.expr_addr_of(span, self_.clone());
|
||||
let enc = cx.expr_call(span, fn_path.clone(), vec![self_ref, blkencoder.clone()]);
|
||||
let lambda = cx.lambda_expr_1(span, enc, blkarg);
|
||||
let call = cx.expr_method_call(span, blkencoder.clone(),
|
||||
let call = cx.expr_method_call(span,
|
||||
blkencoder.clone(),
|
||||
emit_struct_field,
|
||||
vec!(cx.expr_str(span, name),
|
||||
cx.expr_usize(span, i),
|
||||
lambda));
|
||||
vec![cx.expr_str(span, name),
|
||||
cx.expr_usize(span, i),
|
||||
lambda]);
|
||||
|
||||
// last call doesn't need a try!
|
||||
let last = fields.len() - 1;
|
||||
@@ -229,11 +225,9 @@ fn encodable_substructure(cx: &mut ExtCtxt, trait_span: Span,
|
||||
cx.expr_method_call(trait_span,
|
||||
encoder,
|
||||
cx.ident_of("emit_struct"),
|
||||
vec!(
|
||||
cx.expr_str(trait_span, substr.type_ident.name.as_str()),
|
||||
cx.expr_usize(trait_span, fields.len()),
|
||||
blk
|
||||
))
|
||||
vec![cx.expr_str(trait_span, substr.type_ident.name.as_str()),
|
||||
cx.expr_usize(trait_span, fields.len()),
|
||||
blk])
|
||||
}
|
||||
|
||||
EnumMatching(idx, variant, ref fields) => {
|
||||
@@ -248,14 +242,14 @@ fn encodable_substructure(cx: &mut ExtCtxt, trait_span: Span,
|
||||
if !fields.is_empty() {
|
||||
let last = fields.len() - 1;
|
||||
for (i, &FieldInfo { ref self_, span, .. }) in fields.iter().enumerate() {
|
||||
let self_ref = cx.expr_addr_of(span, self_.clone());
|
||||
let enc = cx.expr_call(span, fn_path.clone(), vec![self_ref,
|
||||
blkencoder.clone()]);
|
||||
let self_ref = cx.expr_addr_of(span, self_.clone());
|
||||
let enc =
|
||||
cx.expr_call(span, fn_path.clone(), vec![self_ref, blkencoder.clone()]);
|
||||
let lambda = cx.lambda_expr_1(span, enc, blkarg);
|
||||
let call = cx.expr_method_call(span, blkencoder.clone(),
|
||||
let call = cx.expr_method_call(span,
|
||||
blkencoder.clone(),
|
||||
emit_variant_arg,
|
||||
vec!(cx.expr_usize(span, i),
|
||||
lambda));
|
||||
vec![cx.expr_usize(span, i), lambda]);
|
||||
let call = if i != last {
|
||||
cx.expr_try(span, call)
|
||||
} else {
|
||||
@@ -271,23 +265,23 @@ fn encodable_substructure(cx: &mut ExtCtxt, trait_span: Span,
|
||||
|
||||
let blk = cx.lambda_stmts_1(trait_span, stmts, blkarg);
|
||||
let name = cx.expr_str(trait_span, variant.node.name.name.as_str());
|
||||
let call = cx.expr_method_call(trait_span, blkencoder,
|
||||
let call = cx.expr_method_call(trait_span,
|
||||
blkencoder,
|
||||
cx.ident_of("emit_enum_variant"),
|
||||
vec!(name,
|
||||
cx.expr_usize(trait_span, idx),
|
||||
cx.expr_usize(trait_span, fields.len()),
|
||||
blk));
|
||||
vec![name,
|
||||
cx.expr_usize(trait_span, idx),
|
||||
cx.expr_usize(trait_span, fields.len()),
|
||||
blk]);
|
||||
let blk = cx.lambda_expr_1(trait_span, call, blkarg);
|
||||
let ret = cx.expr_method_call(trait_span,
|
||||
encoder,
|
||||
cx.ident_of("emit_enum"),
|
||||
vec!(
|
||||
cx.expr_str(trait_span, substr.type_ident.name.as_str()),
|
||||
blk
|
||||
));
|
||||
vec![cx.expr_str(trait_span,
|
||||
substr.type_ident.name.as_str()),
|
||||
blk]);
|
||||
cx.expr_block(cx.block(trait_span, vec![me, cx.stmt_expr(ret)]))
|
||||
}
|
||||
|
||||
_ => cx.bug("expected Struct or EnumMatching in derive(Encodable)")
|
||||
_ => cx.bug("expected Struct or EnumMatching in derive(Encodable)"),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -192,16 +192,16 @@
|
||||
use std::vec;
|
||||
|
||||
use syntax::abi::Abi;
|
||||
use syntax::ast::{self, EnumDef, Expr, Ident, Generics, VariantData, BinOpKind, PatKind};
|
||||
use syntax::ast::{self, BinOpKind, EnumDef, Expr, Generics, Ident, PatKind, VariantData};
|
||||
use syntax::attr;
|
||||
use syntax::attr::AttrMetaMethods;
|
||||
use syntax::ext::base::{ExtCtxt, Annotatable};
|
||||
use syntax::ext::base::{Annotatable, ExtCtxt};
|
||||
use syntax::ext::build::AstBuilder;
|
||||
use syntax::codemap::{self, respan};
|
||||
use syntax::util::move_map::MoveMap;
|
||||
use syntax::parse::token::{keywords, InternedString};
|
||||
use syntax::parse::token::{InternedString, keywords};
|
||||
use syntax::ptr::P;
|
||||
use syntax_pos::{Span, DUMMY_SP};
|
||||
use syntax_pos::{DUMMY_SP, Span};
|
||||
use errors::Handler;
|
||||
|
||||
use self::ty::{LifetimeBounds, Path, Ptr, PtrTy, Self_, Ty};
|
||||
@@ -273,7 +273,7 @@ pub struct Substructure<'a> {
|
||||
pub self_args: &'a [P<Expr>],
|
||||
/// verbatim access to any other arguments
|
||||
pub nonself_args: &'a [P<Expr>],
|
||||
pub fields: &'a SubstructureFields<'a>
|
||||
pub fields: &'a SubstructureFields<'a>,
|
||||
}
|
||||
|
||||
/// Summary of the relevant parts of a struct/enum field.
|
||||
@@ -338,14 +338,17 @@ pub enum SubstructureFields<'a> {
|
||||
Box<FnMut(&mut ExtCtxt, Span, (&[Ident], &[Ident]), &[P<Expr>]) -> P<Expr> + 'a>;
|
||||
|
||||
pub fn combine_substructure<'a>(f: CombineSubstructureFunc<'a>)
|
||||
-> RefCell<CombineSubstructureFunc<'a>> {
|
||||
-> RefCell<CombineSubstructureFunc<'a>> {
|
||||
RefCell::new(f)
|
||||
}
|
||||
|
||||
/// This method helps to extract all the type parameters referenced from a
|
||||
/// type. For a type parameter `<T>`, it looks for either a `TyPath` that
|
||||
/// is not global and starts with `T`, or a `TyQPath`.
|
||||
fn find_type_parameters(ty: &ast::Ty, ty_param_names: &[ast::Name], span: Span, cx: &ExtCtxt)
|
||||
fn find_type_parameters(ty: &ast::Ty,
|
||||
ty_param_names: &[ast::Name],
|
||||
span: Span,
|
||||
cx: &ExtCtxt)
|
||||
-> Vec<P<ast::Ty>> {
|
||||
use syntax::visit;
|
||||
|
||||
@@ -395,23 +398,15 @@ pub fn expand(&self,
|
||||
cx: &mut ExtCtxt,
|
||||
mitem: &ast::MetaItem,
|
||||
item: &'a Annotatable,
|
||||
push: &mut FnMut(Annotatable))
|
||||
{
|
||||
push: &mut FnMut(Annotatable)) {
|
||||
match *item {
|
||||
Annotatable::Item(ref item) => {
|
||||
let newitem = match item.node {
|
||||
ast::ItemKind::Struct(ref struct_def, ref generics) => {
|
||||
self.expand_struct_def(cx,
|
||||
&struct_def,
|
||||
item.ident,
|
||||
generics)
|
||||
self.expand_struct_def(cx, &struct_def, item.ident, generics)
|
||||
}
|
||||
ast::ItemKind::Enum(ref enum_def, ref generics) => {
|
||||
self.expand_enum_def(cx,
|
||||
enum_def,
|
||||
&item.attrs,
|
||||
item.ident,
|
||||
generics)
|
||||
self.expand_enum_def(cx, enum_def, &item.attrs, item.ident, generics)
|
||||
}
|
||||
_ => {
|
||||
cx.span_err(mitem.span,
|
||||
@@ -422,19 +417,20 @@ pub fn expand(&self,
|
||||
// Keep the lint attributes of the previous item to control how the
|
||||
// generated implementations are linted
|
||||
let mut attrs = newitem.attrs.clone();
|
||||
attrs.extend(item.attrs.iter().filter(|a| {
|
||||
match &a.name()[..] {
|
||||
"allow" | "warn" | "deny" | "forbid" | "stable" | "unstable" => true,
|
||||
_ => false,
|
||||
}
|
||||
}).cloned());
|
||||
push(Annotatable::Item(P(ast::Item {
|
||||
attrs: attrs,
|
||||
..(*newitem).clone()
|
||||
})))
|
||||
attrs.extend(item.attrs
|
||||
.iter()
|
||||
.filter(|a| {
|
||||
match &a.name()[..] {
|
||||
"allow" | "warn" | "deny" | "forbid" | "stable" | "unstable" => true,
|
||||
_ => false,
|
||||
}
|
||||
})
|
||||
.cloned());
|
||||
push(Annotatable::Item(P(ast::Item { attrs: attrs, ..(*newitem).clone() })))
|
||||
}
|
||||
_ => {
|
||||
cx.span_err(mitem.span, "`derive` may only be applied to structs and enums");
|
||||
cx.span_err(mitem.span,
|
||||
"`derive` may only be applied to structs and enums");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -475,7 +471,8 @@ fn create_derived_impl(&self,
|
||||
type_ident: Ident,
|
||||
generics: &Generics,
|
||||
field_tys: Vec<P<ast::Ty>>,
|
||||
methods: Vec<ast::ImplItem>) -> P<ast::Item> {
|
||||
methods: Vec<ast::ImplItem>)
|
||||
-> P<ast::Item> {
|
||||
let trait_path = self.path.to_path(cx, self.span, type_ident, generics);
|
||||
|
||||
// Transform associated types from `deriving::ty::Ty` into `ast::ImplItem`
|
||||
@@ -487,16 +484,12 @@ fn create_derived_impl(&self,
|
||||
vis: ast::Visibility::Inherited,
|
||||
defaultness: ast::Defaultness::Final,
|
||||
attrs: Vec::new(),
|
||||
node: ast::ImplItemKind::Type(type_def.to_ty(cx,
|
||||
self.span,
|
||||
type_ident,
|
||||
generics
|
||||
)),
|
||||
node: ast::ImplItemKind::Type(type_def.to_ty(cx, self.span, type_ident, generics)),
|
||||
}
|
||||
});
|
||||
|
||||
let Generics { mut lifetimes, ty_params, mut where_clause } =
|
||||
self.generics.to_generics(cx, self.span, type_ident, generics);
|
||||
let Generics { mut lifetimes, ty_params, mut where_clause } = self.generics
|
||||
.to_generics(cx, self.span, type_ident, generics);
|
||||
let mut ty_params = ty_params.into_vec();
|
||||
|
||||
// Copy the lifetimes
|
||||
@@ -521,10 +514,7 @@ fn create_derived_impl(&self,
|
||||
bounds.push((*declared_bound).clone());
|
||||
}
|
||||
|
||||
cx.typaram(self.span,
|
||||
ty_param.ident,
|
||||
P::from_vec(bounds),
|
||||
None)
|
||||
cx.typaram(self.span, ty_param.ident, P::from_vec(bounds), None)
|
||||
}));
|
||||
|
||||
// and similarly for where clauses
|
||||
@@ -542,7 +532,7 @@ fn create_derived_impl(&self,
|
||||
ast::WherePredicate::RegionPredicate(ast::WhereRegionPredicate {
|
||||
span: self.span,
|
||||
lifetime: rb.lifetime,
|
||||
bounds: rb.bounds.iter().cloned().collect()
|
||||
bounds: rb.bounds.iter().cloned().collect(),
|
||||
})
|
||||
}
|
||||
ast::WherePredicate::EqPredicate(ref we) => {
|
||||
@@ -550,7 +540,7 @@ fn create_derived_impl(&self,
|
||||
id: ast::DUMMY_NODE_ID,
|
||||
span: self.span,
|
||||
path: we.path.clone(),
|
||||
ty: we.ty.clone()
|
||||
ty: we.ty.clone(),
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -568,16 +558,17 @@ fn create_derived_impl(&self,
|
||||
for ty in tys {
|
||||
// if we have already handled this type, skip it
|
||||
if let ast::TyKind::Path(_, ref p) = ty.node {
|
||||
if p.segments.len() == 1
|
||||
&& ty_param_names.contains(&p.segments[0].identifier.name)
|
||||
|| processed_field_types.contains(&p.segments) {
|
||||
if p.segments.len() == 1 &&
|
||||
ty_param_names.contains(&p.segments[0].identifier.name) ||
|
||||
processed_field_types.contains(&p.segments) {
|
||||
continue;
|
||||
};
|
||||
processed_field_types.insert(p.segments.clone());
|
||||
}
|
||||
let mut bounds: Vec<_> = self.additional_bounds.iter().map(|p| {
|
||||
cx.typarambound(p.to_path(cx, self.span, type_ident, generics))
|
||||
}).collect();
|
||||
let mut bounds: Vec<_> = self.additional_bounds
|
||||
.iter()
|
||||
.map(|p| cx.typarambound(p.to_path(cx, self.span, type_ident, generics)))
|
||||
.collect();
|
||||
|
||||
// require the current trait
|
||||
bounds.push(cx.typarambound(trait_path.clone()));
|
||||
@@ -598,40 +589,41 @@ fn create_derived_impl(&self,
|
||||
let trait_generics = Generics {
|
||||
lifetimes: lifetimes,
|
||||
ty_params: P::from_vec(ty_params),
|
||||
where_clause: where_clause
|
||||
where_clause: where_clause,
|
||||
};
|
||||
|
||||
// Create the reference to the trait.
|
||||
let trait_ref = cx.trait_ref(trait_path);
|
||||
|
||||
// Create the type parameters on the `self` path.
|
||||
let self_ty_params = generics.ty_params.iter().map(|ty_param| {
|
||||
cx.ty_ident(self.span, ty_param.ident)
|
||||
}).collect();
|
||||
let self_ty_params = generics.ty_params
|
||||
.iter()
|
||||
.map(|ty_param| cx.ty_ident(self.span, ty_param.ident))
|
||||
.collect();
|
||||
|
||||
let self_lifetimes: Vec<ast::Lifetime> =
|
||||
generics.lifetimes
|
||||
let self_lifetimes: Vec<ast::Lifetime> = generics.lifetimes
|
||||
.iter()
|
||||
.map(|ld| ld.lifetime)
|
||||
.collect();
|
||||
|
||||
// Create the type of `self`.
|
||||
let self_type = cx.ty_path(
|
||||
cx.path_all(self.span, false, vec!( type_ident ), self_lifetimes,
|
||||
self_ty_params, Vec::new()));
|
||||
let self_type = cx.ty_path(cx.path_all(self.span,
|
||||
false,
|
||||
vec![type_ident],
|
||||
self_lifetimes,
|
||||
self_ty_params,
|
||||
Vec::new()));
|
||||
|
||||
let attr = cx.attribute(
|
||||
self.span,
|
||||
cx.meta_word(self.span,
|
||||
InternedString::new("automatically_derived")));
|
||||
let attr = cx.attribute(self.span,
|
||||
cx.meta_word(self.span,
|
||||
InternedString::new("automatically_derived")));
|
||||
// Just mark it now since we know that it'll end up used downstream
|
||||
attr::mark_used(&attr);
|
||||
let opt_trait_ref = Some(trait_ref);
|
||||
let unused_qual = cx.attribute(
|
||||
self.span,
|
||||
cx.meta_list(self.span,
|
||||
InternedString::new("allow"),
|
||||
vec![cx.meta_word(self.span,
|
||||
let unused_qual = cx.attribute(self.span,
|
||||
cx.meta_list(self.span,
|
||||
InternedString::new("allow"),
|
||||
vec![cx.meta_word(self.span,
|
||||
InternedString::new("unused_qualifications"))]));
|
||||
let mut a = vec![attr, unused_qual];
|
||||
a.extend(self.attributes.iter().cloned());
|
||||
@@ -642,58 +634,60 @@ fn create_derived_impl(&self,
|
||||
ast::Unsafety::Normal
|
||||
};
|
||||
|
||||
cx.item(
|
||||
self.span,
|
||||
keywords::Invalid.ident(),
|
||||
a,
|
||||
ast::ItemKind::Impl(unsafety,
|
||||
ast::ImplPolarity::Positive,
|
||||
trait_generics,
|
||||
opt_trait_ref,
|
||||
self_type,
|
||||
methods.into_iter().chain(associated_types).collect()))
|
||||
cx.item(self.span,
|
||||
keywords::Invalid.ident(),
|
||||
a,
|
||||
ast::ItemKind::Impl(unsafety,
|
||||
ast::ImplPolarity::Positive,
|
||||
trait_generics,
|
||||
opt_trait_ref,
|
||||
self_type,
|
||||
methods.into_iter().chain(associated_types).collect()))
|
||||
}
|
||||
|
||||
fn expand_struct_def(&self,
|
||||
cx: &mut ExtCtxt,
|
||||
struct_def: &'a VariantData,
|
||||
type_ident: Ident,
|
||||
generics: &Generics) -> P<ast::Item> {
|
||||
let field_tys: Vec<P<ast::Ty>> = struct_def.fields().iter()
|
||||
generics: &Generics)
|
||||
-> P<ast::Item> {
|
||||
let field_tys: Vec<P<ast::Ty>> = struct_def.fields()
|
||||
.iter()
|
||||
.map(|field| field.ty.clone())
|
||||
.collect();
|
||||
|
||||
let methods = self.methods.iter().map(|method_def| {
|
||||
let (explicit_self, self_args, nonself_args, tys) =
|
||||
method_def.split_self_nonself_args(
|
||||
cx, self, type_ident, generics);
|
||||
let methods = self.methods
|
||||
.iter()
|
||||
.map(|method_def| {
|
||||
let (explicit_self, self_args, nonself_args, tys) =
|
||||
method_def.split_self_nonself_args(cx, self, type_ident, generics);
|
||||
|
||||
let body = if method_def.is_static() {
|
||||
method_def.expand_static_struct_method_body(
|
||||
cx,
|
||||
self,
|
||||
struct_def,
|
||||
type_ident,
|
||||
&self_args[..],
|
||||
&nonself_args[..])
|
||||
} else {
|
||||
method_def.expand_struct_method_body(cx,
|
||||
self,
|
||||
struct_def,
|
||||
type_ident,
|
||||
&self_args[..],
|
||||
&nonself_args[..])
|
||||
};
|
||||
let body = if method_def.is_static() {
|
||||
method_def.expand_static_struct_method_body(cx,
|
||||
self,
|
||||
struct_def,
|
||||
type_ident,
|
||||
&self_args[..],
|
||||
&nonself_args[..])
|
||||
} else {
|
||||
method_def.expand_struct_method_body(cx,
|
||||
self,
|
||||
struct_def,
|
||||
type_ident,
|
||||
&self_args[..],
|
||||
&nonself_args[..])
|
||||
};
|
||||
|
||||
method_def.create_method(cx,
|
||||
self,
|
||||
type_ident,
|
||||
generics,
|
||||
Abi::Rust,
|
||||
explicit_self,
|
||||
tys,
|
||||
body)
|
||||
}).collect();
|
||||
method_def.create_method(cx,
|
||||
self,
|
||||
type_ident,
|
||||
generics,
|
||||
Abi::Rust,
|
||||
explicit_self,
|
||||
tys,
|
||||
body)
|
||||
})
|
||||
.collect();
|
||||
|
||||
self.create_derived_impl(cx, type_ident, generics, field_tys, methods)
|
||||
}
|
||||
@@ -703,53 +697,57 @@ fn expand_enum_def(&self,
|
||||
enum_def: &'a EnumDef,
|
||||
type_attrs: &[ast::Attribute],
|
||||
type_ident: Ident,
|
||||
generics: &Generics) -> P<ast::Item> {
|
||||
generics: &Generics)
|
||||
-> P<ast::Item> {
|
||||
let mut field_tys = Vec::new();
|
||||
|
||||
for variant in &enum_def.variants {
|
||||
field_tys.extend(variant.node.data.fields().iter()
|
||||
field_tys.extend(variant.node
|
||||
.data
|
||||
.fields()
|
||||
.iter()
|
||||
.map(|field| field.ty.clone()));
|
||||
}
|
||||
|
||||
let methods = self.methods.iter().map(|method_def| {
|
||||
let (explicit_self, self_args, nonself_args, tys) =
|
||||
method_def.split_self_nonself_args(cx, self,
|
||||
type_ident, generics);
|
||||
let methods = self.methods
|
||||
.iter()
|
||||
.map(|method_def| {
|
||||
let (explicit_self, self_args, nonself_args, tys) =
|
||||
method_def.split_self_nonself_args(cx, self, type_ident, generics);
|
||||
|
||||
let body = if method_def.is_static() {
|
||||
method_def.expand_static_enum_method_body(
|
||||
cx,
|
||||
self,
|
||||
enum_def,
|
||||
type_ident,
|
||||
&self_args[..],
|
||||
&nonself_args[..])
|
||||
} else {
|
||||
method_def.expand_enum_method_body(cx,
|
||||
self,
|
||||
enum_def,
|
||||
type_attrs,
|
||||
type_ident,
|
||||
self_args,
|
||||
&nonself_args[..])
|
||||
};
|
||||
let body = if method_def.is_static() {
|
||||
method_def.expand_static_enum_method_body(cx,
|
||||
self,
|
||||
enum_def,
|
||||
type_ident,
|
||||
&self_args[..],
|
||||
&nonself_args[..])
|
||||
} else {
|
||||
method_def.expand_enum_method_body(cx,
|
||||
self,
|
||||
enum_def,
|
||||
type_attrs,
|
||||
type_ident,
|
||||
self_args,
|
||||
&nonself_args[..])
|
||||
};
|
||||
|
||||
method_def.create_method(cx,
|
||||
self,
|
||||
type_ident,
|
||||
generics,
|
||||
Abi::Rust,
|
||||
explicit_self,
|
||||
tys,
|
||||
body)
|
||||
}).collect();
|
||||
method_def.create_method(cx,
|
||||
self,
|
||||
type_ident,
|
||||
generics,
|
||||
Abi::Rust,
|
||||
explicit_self,
|
||||
tys,
|
||||
body)
|
||||
})
|
||||
.collect();
|
||||
|
||||
self.create_derived_impl(cx, type_ident, generics, field_tys, methods)
|
||||
}
|
||||
}
|
||||
|
||||
fn find_repr_type_name(diagnostic: &Handler,
|
||||
type_attrs: &[ast::Attribute]) -> &'static str {
|
||||
fn find_repr_type_name(diagnostic: &Handler, type_attrs: &[ast::Attribute]) -> &'static str {
|
||||
let mut repr_type_name = "isize";
|
||||
for a in type_attrs {
|
||||
for r in &attr::find_repr_attrs(diagnostic, a) {
|
||||
@@ -782,13 +780,13 @@ fn call_substructure_method(&self,
|
||||
self_args: &[P<Expr>],
|
||||
nonself_args: &[P<Expr>],
|
||||
fields: &SubstructureFields)
|
||||
-> P<Expr> {
|
||||
-> P<Expr> {
|
||||
let substructure = Substructure {
|
||||
type_ident: type_ident,
|
||||
method_ident: cx.ident_of(self.name),
|
||||
self_args: self_args,
|
||||
nonself_args: nonself_args,
|
||||
fields: fields
|
||||
fields: fields,
|
||||
};
|
||||
let mut f = self.combine_substructure.borrow_mut();
|
||||
let f: &mut CombineSubstructureFunc = &mut *f;
|
||||
@@ -808,12 +806,13 @@ fn is_static(&self) -> bool {
|
||||
self.explicit_self.is_none()
|
||||
}
|
||||
|
||||
fn split_self_nonself_args(&self,
|
||||
cx: &mut ExtCtxt,
|
||||
trait_: &TraitDef,
|
||||
type_ident: Ident,
|
||||
generics: &Generics)
|
||||
-> (Option<ast::ExplicitSelf>, Vec<P<Expr>>, Vec<P<Expr>>, Vec<(Ident, P<ast::Ty>)>) {
|
||||
fn split_self_nonself_args
|
||||
(&self,
|
||||
cx: &mut ExtCtxt,
|
||||
trait_: &TraitDef,
|
||||
type_ident: Ident,
|
||||
generics: &Generics)
|
||||
-> (Option<ast::ExplicitSelf>, Vec<P<Expr>>, Vec<P<Expr>>, Vec<(Ident, P<ast::Ty>)>) {
|
||||
|
||||
let mut self_args = Vec::new();
|
||||
let mut nonself_args = Vec::new();
|
||||
@@ -839,7 +838,7 @@ fn split_self_nonself_args(&self,
|
||||
match *ty {
|
||||
// for static methods, just treat any Self
|
||||
// arguments as a normal arg
|
||||
Self_ if nonstatic => {
|
||||
Self_ if nonstatic => {
|
||||
self_args.push(arg_expr);
|
||||
}
|
||||
Ptr(ref ty, _) if **ty == Self_ && nonstatic => {
|
||||
@@ -861,18 +860,20 @@ fn create_method(&self,
|
||||
generics: &Generics,
|
||||
abi: Abi,
|
||||
explicit_self: Option<ast::ExplicitSelf>,
|
||||
arg_types: Vec<(Ident, P<ast::Ty>)> ,
|
||||
body: P<Expr>) -> ast::ImplItem {
|
||||
arg_types: Vec<(Ident, P<ast::Ty>)>,
|
||||
body: P<Expr>)
|
||||
-> ast::ImplItem {
|
||||
|
||||
// create the generics that aren't for Self
|
||||
let fn_generics = self.generics.to_generics(cx, trait_.span, type_ident, generics);
|
||||
|
||||
let args = {
|
||||
let self_args = explicit_self.map(|explicit_self| {
|
||||
ast::Arg::from_self(explicit_self, respan(trait_.span, keywords::SelfValue.ident()))
|
||||
ast::Arg::from_self(explicit_self,
|
||||
respan(trait_.span, keywords::SelfValue.ident()))
|
||||
});
|
||||
let nonself_args = arg_types.into_iter()
|
||||
.map(|(name, ty)| cx.arg(trait_.span, name, ty));
|
||||
.map(|(name, ty)| cx.arg(trait_.span, name, ty));
|
||||
self_args.into_iter().chain(nonself_args).collect()
|
||||
};
|
||||
|
||||
@@ -897,12 +898,13 @@ fn create_method(&self,
|
||||
defaultness: ast::Defaultness::Final,
|
||||
ident: method_ident,
|
||||
node: ast::ImplItemKind::Method(ast::MethodSig {
|
||||
generics: fn_generics,
|
||||
abi: abi,
|
||||
unsafety: unsafety,
|
||||
constness: ast::Constness::NotConst,
|
||||
decl: fn_decl
|
||||
}, body_block)
|
||||
generics: fn_generics,
|
||||
abi: abi,
|
||||
unsafety: unsafety,
|
||||
constness: ast::Constness::NotConst,
|
||||
decl: fn_decl,
|
||||
},
|
||||
body_block),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -926,26 +928,24 @@ fn create_method(&self,
|
||||
/// }
|
||||
/// ```
|
||||
fn expand_struct_method_body<'b>(&self,
|
||||
cx: &mut ExtCtxt,
|
||||
trait_: &TraitDef<'b>,
|
||||
struct_def: &'b VariantData,
|
||||
type_ident: Ident,
|
||||
self_args: &[P<Expr>],
|
||||
nonself_args: &[P<Expr>])
|
||||
-> P<Expr> {
|
||||
cx: &mut ExtCtxt,
|
||||
trait_: &TraitDef<'b>,
|
||||
struct_def: &'b VariantData,
|
||||
type_ident: Ident,
|
||||
self_args: &[P<Expr>],
|
||||
nonself_args: &[P<Expr>])
|
||||
-> P<Expr> {
|
||||
|
||||
let mut raw_fields = Vec::new(); // Vec<[fields of self],
|
||||
// [fields of next Self arg], [etc]>
|
||||
let mut patterns = Vec::new();
|
||||
for i in 0..self_args.len() {
|
||||
let struct_path= cx.path(DUMMY_SP, vec!( type_ident ));
|
||||
let (pat, ident_expr) =
|
||||
trait_.create_struct_pattern(cx,
|
||||
struct_path,
|
||||
struct_def,
|
||||
&format!("__self_{}",
|
||||
i),
|
||||
ast::Mutability::Immutable);
|
||||
let struct_path = cx.path(DUMMY_SP, vec![type_ident]);
|
||||
let (pat, ident_expr) = trait_.create_struct_pattern(cx,
|
||||
struct_path,
|
||||
struct_def,
|
||||
&format!("__self_{}", i),
|
||||
ast::Mutability::Immutable);
|
||||
patterns.push(pat);
|
||||
raw_fields.push(ident_expr);
|
||||
}
|
||||
@@ -954,21 +954,23 @@ fn expand_struct_method_body<'b>(&self,
|
||||
let fields = if !raw_fields.is_empty() {
|
||||
let mut raw_fields = raw_fields.into_iter().map(|v| v.into_iter());
|
||||
let first_field = raw_fields.next().unwrap();
|
||||
let mut other_fields: Vec<vec::IntoIter<_>>
|
||||
= raw_fields.collect();
|
||||
let mut other_fields: Vec<vec::IntoIter<_>> = raw_fields.collect();
|
||||
first_field.map(|(span, opt_id, field, attrs)| {
|
||||
FieldInfo {
|
||||
span: span,
|
||||
name: opt_id,
|
||||
self_: field,
|
||||
other: other_fields.iter_mut().map(|l| {
|
||||
match l.next().unwrap() {
|
||||
(_, _, ex, _) => ex
|
||||
}
|
||||
}).collect(),
|
||||
attrs: attrs,
|
||||
}
|
||||
}).collect()
|
||||
FieldInfo {
|
||||
span: span,
|
||||
name: opt_id,
|
||||
self_: field,
|
||||
other: other_fields.iter_mut()
|
||||
.map(|l| {
|
||||
match l.next().unwrap() {
|
||||
(_, _, ex, _) => ex,
|
||||
}
|
||||
})
|
||||
.collect(),
|
||||
attrs: attrs,
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
} else {
|
||||
cx.span_bug(trait_.span,
|
||||
"no self arguments to non-static method in generic \
|
||||
@@ -976,20 +978,20 @@ fn expand_struct_method_body<'b>(&self,
|
||||
};
|
||||
|
||||
// body of the inner most destructuring match
|
||||
let mut body = self.call_substructure_method(
|
||||
cx,
|
||||
trait_,
|
||||
type_ident,
|
||||
self_args,
|
||||
nonself_args,
|
||||
&Struct(struct_def, fields));
|
||||
let mut body = self.call_substructure_method(cx,
|
||||
trait_,
|
||||
type_ident,
|
||||
self_args,
|
||||
nonself_args,
|
||||
&Struct(struct_def, fields));
|
||||
|
||||
// make a series of nested matches, to destructure the
|
||||
// structs. This is actually right-to-left, but it shouldn't
|
||||
// matter.
|
||||
for (arg_expr, pat) in self_args.iter().zip(patterns) {
|
||||
body = cx.expr_match(trait_.span, arg_expr.clone(),
|
||||
vec!( cx.arm(trait_.span, vec!(pat.clone()), body) ))
|
||||
body = cx.expr_match(trait_.span,
|
||||
arg_expr.clone(),
|
||||
vec![cx.arm(trait_.span, vec![pat.clone()], body)])
|
||||
}
|
||||
|
||||
body
|
||||
@@ -1002,13 +1004,14 @@ fn expand_static_struct_method_body(&self,
|
||||
type_ident: Ident,
|
||||
self_args: &[P<Expr>],
|
||||
nonself_args: &[P<Expr>])
|
||||
-> P<Expr> {
|
||||
-> P<Expr> {
|
||||
let summary = trait_.summarise_struct(cx, struct_def);
|
||||
|
||||
self.call_substructure_method(cx,
|
||||
trait_,
|
||||
type_ident,
|
||||
self_args, nonself_args,
|
||||
self_args,
|
||||
nonself_args,
|
||||
&StaticStruct(struct_def, summary))
|
||||
}
|
||||
|
||||
@@ -1042,16 +1045,21 @@ fn expand_static_struct_method_body(&self,
|
||||
/// as their results are unused. The point of `__self_vi` and
|
||||
/// `__arg_1_vi` is for `PartialOrd`; see #15503.)
|
||||
fn expand_enum_method_body<'b>(&self,
|
||||
cx: &mut ExtCtxt,
|
||||
trait_: &TraitDef<'b>,
|
||||
enum_def: &'b EnumDef,
|
||||
type_attrs: &[ast::Attribute],
|
||||
type_ident: Ident,
|
||||
self_args: Vec<P<Expr>>,
|
||||
nonself_args: &[P<Expr>])
|
||||
-> P<Expr> {
|
||||
self.build_enum_match_tuple(
|
||||
cx, trait_, enum_def, type_attrs, type_ident, self_args, nonself_args)
|
||||
cx: &mut ExtCtxt,
|
||||
trait_: &TraitDef<'b>,
|
||||
enum_def: &'b EnumDef,
|
||||
type_attrs: &[ast::Attribute],
|
||||
type_ident: Ident,
|
||||
self_args: Vec<P<Expr>>,
|
||||
nonself_args: &[P<Expr>])
|
||||
-> P<Expr> {
|
||||
self.build_enum_match_tuple(cx,
|
||||
trait_,
|
||||
enum_def,
|
||||
type_attrs,
|
||||
type_ident,
|
||||
self_args,
|
||||
nonself_args)
|
||||
}
|
||||
|
||||
|
||||
@@ -1090,20 +1098,21 @@ fn expand_enum_method_body<'b>(&self,
|
||||
/// ... // catch-all remainder can inspect above variant index values.
|
||||
/// }
|
||||
/// ```
|
||||
fn build_enum_match_tuple<'b>(
|
||||
&self,
|
||||
cx: &mut ExtCtxt,
|
||||
trait_: &TraitDef<'b>,
|
||||
enum_def: &'b EnumDef,
|
||||
type_attrs: &[ast::Attribute],
|
||||
type_ident: Ident,
|
||||
self_args: Vec<P<Expr>>,
|
||||
nonself_args: &[P<Expr>]) -> P<Expr> {
|
||||
fn build_enum_match_tuple<'b>(&self,
|
||||
cx: &mut ExtCtxt,
|
||||
trait_: &TraitDef<'b>,
|
||||
enum_def: &'b EnumDef,
|
||||
type_attrs: &[ast::Attribute],
|
||||
type_ident: Ident,
|
||||
self_args: Vec<P<Expr>>,
|
||||
nonself_args: &[P<Expr>])
|
||||
-> P<Expr> {
|
||||
|
||||
let sp = trait_.span;
|
||||
let variants = &enum_def.variants;
|
||||
|
||||
let self_arg_names = self_args.iter().enumerate()
|
||||
let self_arg_names = self_args.iter()
|
||||
.enumerate()
|
||||
.map(|(arg_count, _self_arg)| {
|
||||
if arg_count == 0 {
|
||||
"__self".to_string()
|
||||
@@ -1114,22 +1123,24 @@ fn build_enum_match_tuple<'b>(
|
||||
.collect::<Vec<String>>();
|
||||
|
||||
let self_arg_idents = self_arg_names.iter()
|
||||
.map(|name|cx.ident_of(&name[..]))
|
||||
.map(|name| cx.ident_of(&name[..]))
|
||||
.collect::<Vec<ast::Ident>>();
|
||||
|
||||
// The `vi_idents` will be bound, solely in the catch-all, to
|
||||
// a series of let statements mapping each self_arg to an int
|
||||
// value corresponding to its discriminant.
|
||||
let vi_idents: Vec<ast::Ident> = self_arg_names.iter()
|
||||
.map(|name| { let vi_suffix = format!("{}_vi", &name[..]);
|
||||
cx.ident_of(&vi_suffix[..]) })
|
||||
.map(|name| {
|
||||
let vi_suffix = format!("{}_vi", &name[..]);
|
||||
cx.ident_of(&vi_suffix[..])
|
||||
})
|
||||
.collect::<Vec<ast::Ident>>();
|
||||
|
||||
// Builds, via callback to call_substructure_method, the
|
||||
// delegated expression that handles the catch-all case,
|
||||
// using `__variants_tuple` to drive logic if necessary.
|
||||
let catch_all_substructure = EnumNonMatchingCollapsed(
|
||||
self_arg_idents, &variants[..], &vi_idents[..]);
|
||||
let catch_all_substructure =
|
||||
EnumNonMatchingCollapsed(self_arg_idents, &variants[..], &vi_idents[..]);
|
||||
|
||||
let first_fieldless = variants.iter().find(|v| v.node.data.fields().is_empty());
|
||||
|
||||
@@ -1138,15 +1149,16 @@ fn build_enum_match_tuple<'b>(
|
||||
// (Variant2, Variant2, ...) => Body2
|
||||
// ...
|
||||
// where each tuple has length = self_args.len()
|
||||
let mut match_arms: Vec<ast::Arm> = variants.iter().enumerate()
|
||||
let mut match_arms: Vec<ast::Arm> = variants.iter()
|
||||
.enumerate()
|
||||
.filter(|&(_, v)| !(self.unify_fieldless_variants && v.node.data.fields().is_empty()))
|
||||
.map(|(index, variant)| {
|
||||
let mk_self_pat = |cx: &mut ExtCtxt, self_arg_name: &str| {
|
||||
let (p, idents) = trait_.create_enum_variant_pattern(
|
||||
cx, type_ident,
|
||||
variant,
|
||||
self_arg_name,
|
||||
ast::Mutability::Immutable);
|
||||
let (p, idents) = trait_.create_enum_variant_pattern(cx,
|
||||
type_ident,
|
||||
variant,
|
||||
self_arg_name,
|
||||
ast::Mutability::Immutable);
|
||||
(cx.pat(sp, PatKind::Ref(p, ast::Mutability::Immutable)), idents)
|
||||
};
|
||||
|
||||
@@ -1213,24 +1225,29 @@ fn build_enum_match_tuple<'b>(
|
||||
// expressions for referencing every field of every
|
||||
// Self arg, assuming all are instances of VariantK.
|
||||
// Build up code associated with such a case.
|
||||
let substructure = EnumMatching(index,
|
||||
variant,
|
||||
field_tuples);
|
||||
let arm_expr = self.call_substructure_method(
|
||||
cx, trait_, type_ident, &self_args[..], nonself_args,
|
||||
&substructure);
|
||||
let substructure = EnumMatching(index, variant, field_tuples);
|
||||
let arm_expr = self.call_substructure_method(cx,
|
||||
trait_,
|
||||
type_ident,
|
||||
&self_args[..],
|
||||
nonself_args,
|
||||
&substructure);
|
||||
|
||||
cx.arm(sp, vec![single_pat], arm_expr)
|
||||
}).collect();
|
||||
})
|
||||
.collect();
|
||||
|
||||
let default = match first_fieldless {
|
||||
Some(v) if self.unify_fieldless_variants => {
|
||||
// We need a default case that handles the fieldless variants.
|
||||
// The index and actual variant aren't meaningful in this case,
|
||||
// so just use whatever
|
||||
Some(self.call_substructure_method(
|
||||
cx, trait_, type_ident, &self_args[..], nonself_args,
|
||||
&EnumMatching(0, v, Vec::new())))
|
||||
Some(self.call_substructure_method(cx,
|
||||
trait_,
|
||||
type_ident,
|
||||
&self_args[..],
|
||||
nonself_args,
|
||||
&EnumMatching(0, v, Vec::new())))
|
||||
}
|
||||
_ if variants.len() > 1 && self_args.len() > 1 => {
|
||||
// Since we know that all the arguments will match if we reach
|
||||
@@ -1238,7 +1255,7 @@ fn build_enum_match_tuple<'b>(
|
||||
// result of the catch all which should help llvm in optimizing it
|
||||
Some(deriving::call_intrinsic(cx, sp, "unreachable", vec![]))
|
||||
}
|
||||
_ => None
|
||||
_ => None,
|
||||
};
|
||||
if let Some(arm) = default {
|
||||
match_arms.push(cx.arm(sp, vec![cx.pat_wild(sp)], arm));
|
||||
@@ -1279,20 +1296,17 @@ fn build_enum_match_tuple<'b>(
|
||||
// ```
|
||||
let mut index_let_stmts: Vec<ast::Stmt> = Vec::new();
|
||||
|
||||
//We also build an expression which checks whether all discriminants are equal
|
||||
// We also build an expression which checks whether all discriminants are equal
|
||||
// discriminant_test = __self0_vi == __self1_vi && __self0_vi == __self2_vi && ...
|
||||
let mut discriminant_test = cx.expr_bool(sp, true);
|
||||
|
||||
let target_type_name =
|
||||
find_repr_type_name(&cx.parse_sess.span_diagnostic, type_attrs);
|
||||
let target_type_name = find_repr_type_name(&cx.parse_sess.span_diagnostic, type_attrs);
|
||||
|
||||
let mut first_ident = None;
|
||||
for (&ident, self_arg) in vi_idents.iter().zip(&self_args) {
|
||||
let self_addr = cx.expr_addr_of(sp, self_arg.clone());
|
||||
let variant_value = deriving::call_intrinsic(cx,
|
||||
sp,
|
||||
"discriminant_value",
|
||||
vec![self_addr]);
|
||||
let variant_value =
|
||||
deriving::call_intrinsic(cx, sp, "discriminant_value", vec![self_addr]);
|
||||
|
||||
let target_ty = cx.ty_ident(sp, cx.ident_of(target_type_name));
|
||||
let variant_disr = cx.expr_cast(sp, variant_value, target_ty);
|
||||
@@ -1304,8 +1318,8 @@ fn build_enum_match_tuple<'b>(
|
||||
let first_expr = cx.expr_ident(sp, first);
|
||||
let id = cx.expr_ident(sp, ident);
|
||||
let test = cx.expr_binary(sp, BinOpKind::Eq, first_expr, id);
|
||||
discriminant_test = cx.expr_binary(sp, BinOpKind::And,
|
||||
discriminant_test, test)
|
||||
discriminant_test =
|
||||
cx.expr_binary(sp, BinOpKind::And, discriminant_test, test)
|
||||
}
|
||||
None => {
|
||||
first_ident = Some(ident);
|
||||
@@ -1313,9 +1327,12 @@ fn build_enum_match_tuple<'b>(
|
||||
}
|
||||
}
|
||||
|
||||
let arm_expr = self.call_substructure_method(
|
||||
cx, trait_, type_ident, &self_args[..], nonself_args,
|
||||
&catch_all_substructure);
|
||||
let arm_expr = self.call_substructure_method(cx,
|
||||
trait_,
|
||||
type_ident,
|
||||
&self_args[..],
|
||||
nonself_args,
|
||||
&catch_all_substructure);
|
||||
|
||||
// Final wrinkle: the self_args are expressions that deref
|
||||
// down to desired l-values, but we cannot actually deref
|
||||
@@ -1325,7 +1342,7 @@ fn build_enum_match_tuple<'b>(
|
||||
let borrowed_self_args = self_args.move_map(|self_arg| cx.expr_addr_of(sp, self_arg));
|
||||
let match_arg = cx.expr(sp, ast::ExprKind::Tup(borrowed_self_args));
|
||||
|
||||
//Lastly we create an expression which branches on all discriminants being equal
|
||||
// Lastly we create an expression which branches on all discriminants being equal
|
||||
// if discriminant_test {
|
||||
// match (...) {
|
||||
// (Variant1, Variant1, ...) => Body1
|
||||
@@ -1392,8 +1409,7 @@ fn build_enum_match_tuple<'b>(
|
||||
// that needs the feature gate enabled.)
|
||||
|
||||
deriving::call_intrinsic(cx, sp, "unreachable", vec![])
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
|
||||
// Final wrinkle: the self_args are expressions that deref
|
||||
// down to desired l-values, but we cannot actually deref
|
||||
@@ -1413,26 +1429,30 @@ fn expand_static_enum_method_body(&self,
|
||||
type_ident: Ident,
|
||||
self_args: &[P<Expr>],
|
||||
nonself_args: &[P<Expr>])
|
||||
-> P<Expr> {
|
||||
let summary = enum_def.variants.iter().map(|v| {
|
||||
let ident = v.node.name;
|
||||
let summary = trait_.summarise_struct(cx, &v.node.data);
|
||||
(ident, v.span, summary)
|
||||
}).collect();
|
||||
self.call_substructure_method(cx, trait_, type_ident,
|
||||
self_args, nonself_args,
|
||||
-> P<Expr> {
|
||||
let summary = enum_def.variants
|
||||
.iter()
|
||||
.map(|v| {
|
||||
let ident = v.node.name;
|
||||
let summary = trait_.summarise_struct(cx, &v.node.data);
|
||||
(ident, v.span, summary)
|
||||
})
|
||||
.collect();
|
||||
self.call_substructure_method(cx,
|
||||
trait_,
|
||||
type_ident,
|
||||
self_args,
|
||||
nonself_args,
|
||||
&StaticEnum(enum_def, summary))
|
||||
}
|
||||
}
|
||||
|
||||
// general helper methods.
|
||||
impl<'a> TraitDef<'a> {
|
||||
fn summarise_struct(&self,
|
||||
cx: &mut ExtCtxt,
|
||||
struct_def: &VariantData) -> StaticFields {
|
||||
fn summarise_struct(&self, cx: &mut ExtCtxt, struct_def: &VariantData) -> StaticFields {
|
||||
let mut named_idents = Vec::new();
|
||||
let mut just_spans = Vec::new();
|
||||
for field in struct_def.fields(){
|
||||
for field in struct_def.fields() {
|
||||
let sp = Span { expn_id: self.span.expn_id, ..field.span };
|
||||
match field.ident {
|
||||
Some(ident) => named_idents.push((ident, sp)),
|
||||
@@ -1441,9 +1461,11 @@ fn summarise_struct(&self,
|
||||
}
|
||||
|
||||
match (just_spans.is_empty(), named_idents.is_empty()) {
|
||||
(false, false) => cx.span_bug(self.span,
|
||||
"a struct with named and unnamed \
|
||||
fields in generic `derive`"),
|
||||
(false, false) => {
|
||||
cx.span_bug(self.span,
|
||||
"a struct with named and unnamed \
|
||||
fields in generic `derive`")
|
||||
}
|
||||
// named fields
|
||||
(_, false) => Named(named_idents),
|
||||
// empty structs
|
||||
@@ -1454,46 +1476,57 @@ fn summarise_struct(&self,
|
||||
|
||||
fn create_subpatterns(&self,
|
||||
cx: &mut ExtCtxt,
|
||||
field_paths: Vec<ast::SpannedIdent> ,
|
||||
field_paths: Vec<ast::SpannedIdent>,
|
||||
mutbl: ast::Mutability)
|
||||
-> Vec<P<ast::Pat>> {
|
||||
field_paths.iter().map(|path| {
|
||||
cx.pat(path.span,
|
||||
PatKind::Ident(ast::BindingMode::ByRef(mutbl), (*path).clone(), None))
|
||||
}).collect()
|
||||
field_paths.iter()
|
||||
.map(|path| {
|
||||
cx.pat(path.span,
|
||||
PatKind::Ident(ast::BindingMode::ByRef(mutbl), (*path).clone(), None))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn create_struct_pattern(&self,
|
||||
cx: &mut ExtCtxt,
|
||||
struct_path: ast::Path,
|
||||
struct_def: &'a VariantData,
|
||||
prefix: &str,
|
||||
mutbl: ast::Mutability)
|
||||
-> (P<ast::Pat>, Vec<(Span, Option<Ident>,
|
||||
P<Expr>,
|
||||
&'a [ast::Attribute])>) {
|
||||
fn create_struct_pattern
|
||||
(&self,
|
||||
cx: &mut ExtCtxt,
|
||||
struct_path: ast::Path,
|
||||
struct_def: &'a VariantData,
|
||||
prefix: &str,
|
||||
mutbl: ast::Mutability)
|
||||
-> (P<ast::Pat>, Vec<(Span, Option<Ident>, P<Expr>, &'a [ast::Attribute])>) {
|
||||
let mut paths = Vec::new();
|
||||
let mut ident_exprs = Vec::new();
|
||||
for (i, struct_field) in struct_def.fields().iter().enumerate() {
|
||||
let sp = Span { expn_id: self.span.expn_id, ..struct_field.span };
|
||||
let ident = cx.ident_of(&format!("{}_{}", prefix, i));
|
||||
paths.push(codemap::Spanned{span: sp, node: ident});
|
||||
let val = cx.expr_deref(sp, cx.expr_path(cx.path_ident(sp,ident)));
|
||||
paths.push(codemap::Spanned {
|
||||
span: sp,
|
||||
node: ident,
|
||||
});
|
||||
let val = cx.expr_deref(sp, cx.expr_path(cx.path_ident(sp, ident)));
|
||||
let val = cx.expr(sp, ast::ExprKind::Paren(val));
|
||||
ident_exprs.push((sp, struct_field.ident, val, &struct_field.attrs[..]));
|
||||
}
|
||||
|
||||
let subpats = self.create_subpatterns(cx, paths, mutbl);
|
||||
let pattern = if struct_def.is_struct() {
|
||||
let field_pats = subpats.into_iter().zip(&ident_exprs).map(|(pat, &(sp, ident, _, _))| {
|
||||
if ident.is_none() {
|
||||
cx.span_bug(sp, "a braced struct with unnamed fields in `derive`");
|
||||
}
|
||||
codemap::Spanned {
|
||||
span: pat.span,
|
||||
node: ast::FieldPat { ident: ident.unwrap(), pat: pat, is_shorthand: false },
|
||||
}
|
||||
}).collect();
|
||||
let field_pats = subpats.into_iter()
|
||||
.zip(&ident_exprs)
|
||||
.map(|(pat, &(sp, ident, _, _))| {
|
||||
if ident.is_none() {
|
||||
cx.span_bug(sp, "a braced struct with unnamed fields in `derive`");
|
||||
}
|
||||
codemap::Spanned {
|
||||
span: pat.span,
|
||||
node: ast::FieldPat {
|
||||
ident: ident.unwrap(),
|
||||
pat: pat,
|
||||
is_shorthand: false,
|
||||
},
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
cx.pat_struct(self.span, struct_path, field_pats)
|
||||
} else {
|
||||
cx.pat_enum(self.span, struct_path, subpats)
|
||||
@@ -1502,20 +1535,21 @@ fn create_struct_pattern(&self,
|
||||
(pattern, ident_exprs)
|
||||
}
|
||||
|
||||
fn create_enum_variant_pattern(&self,
|
||||
cx: &mut ExtCtxt,
|
||||
enum_ident: ast::Ident,
|
||||
variant: &'a ast::Variant,
|
||||
prefix: &str,
|
||||
mutbl: ast::Mutability)
|
||||
-> (P<ast::Pat>, Vec<(Span, Option<Ident>, P<Expr>, &'a [ast::Attribute])>) {
|
||||
fn create_enum_variant_pattern
|
||||
(&self,
|
||||
cx: &mut ExtCtxt,
|
||||
enum_ident: ast::Ident,
|
||||
variant: &'a ast::Variant,
|
||||
prefix: &str,
|
||||
mutbl: ast::Mutability)
|
||||
-> (P<ast::Pat>, Vec<(Span, Option<Ident>, P<Expr>, &'a [ast::Attribute])>) {
|
||||
let variant_ident = variant.node.name;
|
||||
let variant_path = cx.path(variant.span, vec![enum_ident, variant_ident]);
|
||||
self.create_struct_pattern(cx, variant_path, &variant.node.data, prefix, mutbl)
|
||||
}
|
||||
}
|
||||
|
||||
/* helpful premade recipes */
|
||||
// helpful premade recipes
|
||||
|
||||
/// Fold the fields. `use_foldl` controls whether this is done
|
||||
/// left-to-right (`true`) or right-to-left (`false`).
|
||||
@@ -1526,35 +1560,29 @@ pub fn cs_fold<F>(use_foldl: bool,
|
||||
cx: &mut ExtCtxt,
|
||||
trait_span: Span,
|
||||
substructure: &Substructure)
|
||||
-> P<Expr> where
|
||||
F: FnMut(&mut ExtCtxt, Span, P<Expr>, P<Expr>, &[P<Expr>]) -> P<Expr>,
|
||||
-> P<Expr>
|
||||
where F: FnMut(&mut ExtCtxt, Span, P<Expr>, P<Expr>, &[P<Expr>]) -> P<Expr>
|
||||
{
|
||||
match *substructure.fields {
|
||||
EnumMatching(_, _, ref all_fields) | Struct(_, ref all_fields) => {
|
||||
EnumMatching(_, _, ref all_fields) |
|
||||
Struct(_, ref all_fields) => {
|
||||
if use_foldl {
|
||||
all_fields.iter().fold(base, |old, field| {
|
||||
f(cx,
|
||||
field.span,
|
||||
old,
|
||||
field.self_.clone(),
|
||||
&field.other)
|
||||
f(cx, field.span, old, field.self_.clone(), &field.other)
|
||||
})
|
||||
} else {
|
||||
all_fields.iter().rev().fold(base, |old, field| {
|
||||
f(cx,
|
||||
field.span,
|
||||
old,
|
||||
field.self_.clone(),
|
||||
&field.other)
|
||||
f(cx, field.span, old, field.self_.clone(), &field.other)
|
||||
})
|
||||
}
|
||||
},
|
||||
EnumNonMatchingCollapsed(ref all_args, _, tuple) =>
|
||||
enum_nonmatch_f(cx, trait_span, (&all_args[..], tuple),
|
||||
substructure.nonself_args),
|
||||
StaticEnum(..) | StaticStruct(..) => {
|
||||
cx.span_bug(trait_span, "static function in `derive`")
|
||||
}
|
||||
EnumNonMatchingCollapsed(ref all_args, _, tuple) => {
|
||||
enum_nonmatch_f(cx,
|
||||
trait_span,
|
||||
(&all_args[..], tuple),
|
||||
substructure.nonself_args)
|
||||
}
|
||||
StaticEnum(..) | StaticStruct(..) => cx.span_bug(trait_span, "static function in `derive`"),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1572,29 +1600,34 @@ pub fn cs_same_method<F>(f: F,
|
||||
cx: &mut ExtCtxt,
|
||||
trait_span: Span,
|
||||
substructure: &Substructure)
|
||||
-> P<Expr> where
|
||||
F: FnOnce(&mut ExtCtxt, Span, Vec<P<Expr>>) -> P<Expr>,
|
||||
-> P<Expr>
|
||||
where F: FnOnce(&mut ExtCtxt, Span, Vec<P<Expr>>) -> P<Expr>
|
||||
{
|
||||
match *substructure.fields {
|
||||
EnumMatching(_, _, ref all_fields) | Struct(_, ref all_fields) => {
|
||||
EnumMatching(_, _, ref all_fields) |
|
||||
Struct(_, ref all_fields) => {
|
||||
// call self_n.method(other_1_n, other_2_n, ...)
|
||||
let called = all_fields.iter().map(|field| {
|
||||
cx.expr_method_call(field.span,
|
||||
field.self_.clone(),
|
||||
substructure.method_ident,
|
||||
field.other.iter()
|
||||
.map(|e| cx.expr_addr_of(field.span, e.clone()))
|
||||
.collect())
|
||||
}).collect();
|
||||
let called = all_fields.iter()
|
||||
.map(|field| {
|
||||
cx.expr_method_call(field.span,
|
||||
field.self_.clone(),
|
||||
substructure.method_ident,
|
||||
field.other
|
||||
.iter()
|
||||
.map(|e| cx.expr_addr_of(field.span, e.clone()))
|
||||
.collect())
|
||||
})
|
||||
.collect();
|
||||
|
||||
f(cx, trait_span, called)
|
||||
},
|
||||
EnumNonMatchingCollapsed(ref all_self_args, _, tuple) =>
|
||||
enum_nonmatch_f(cx, trait_span, (&all_self_args[..], tuple),
|
||||
substructure.nonself_args),
|
||||
StaticEnum(..) | StaticStruct(..) => {
|
||||
cx.span_bug(trait_span, "static function in `derive`")
|
||||
}
|
||||
EnumNonMatchingCollapsed(ref all_self_args, _, tuple) => {
|
||||
enum_nonmatch_f(cx,
|
||||
trait_span,
|
||||
(&all_self_args[..], tuple),
|
||||
substructure.nonself_args)
|
||||
}
|
||||
StaticEnum(..) | StaticStruct(..) => cx.span_bug(trait_span, "static function in `derive`"),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1606,10 +1639,8 @@ pub fn is_type_without_fields(item: &Annotatable) -> bool {
|
||||
ast::ItemKind::Enum(ref enum_def, _) => {
|
||||
enum_def.variants.iter().all(|v| v.node.data.fields().is_empty())
|
||||
}
|
||||
ast::ItemKind::Struct(ref variant_data, _) => {
|
||||
variant_data.fields().is_empty()
|
||||
}
|
||||
_ => false
|
||||
ast::ItemKind::Struct(ref variant_data, _) => variant_data.fields().is_empty(),
|
||||
_ => false,
|
||||
}
|
||||
} else {
|
||||
false
|
||||
|
||||
@@ -12,8 +12,8 @@
|
||||
use deriving::generic::*;
|
||||
use deriving::generic::ty::*;
|
||||
|
||||
use syntax::ast::{MetaItem, Expr, Mutability};
|
||||
use syntax::ext::base::{ExtCtxt, Annotatable};
|
||||
use syntax::ast::{Expr, MetaItem, Mutability};
|
||||
use syntax::ext::base::{Annotatable, ExtCtxt};
|
||||
use syntax::ext::build::AstBuilder;
|
||||
use syntax::ptr::P;
|
||||
use syntax_pos::Span;
|
||||
@@ -22,11 +22,9 @@ pub fn expand_deriving_hash(cx: &mut ExtCtxt,
|
||||
span: Span,
|
||||
mitem: &MetaItem,
|
||||
item: &Annotatable,
|
||||
push: &mut FnMut(Annotatable))
|
||||
{
|
||||
push: &mut FnMut(Annotatable)) {
|
||||
|
||||
let path = Path::new_(pathvec_std!(cx, core::hash::Hash), None,
|
||||
vec!(), true);
|
||||
let path = Path::new_(pathvec_std!(cx, core::hash::Hash), None, vec![], true);
|
||||
|
||||
let typaram = &*deriving::hygienic_type_parameter(item, "__H");
|
||||
|
||||
@@ -38,25 +36,23 @@ pub fn expand_deriving_hash(cx: &mut ExtCtxt,
|
||||
additional_bounds: Vec::new(),
|
||||
generics: LifetimeBounds::empty(),
|
||||
is_unsafe: false,
|
||||
methods: vec!(
|
||||
MethodDef {
|
||||
name: "hash",
|
||||
generics: LifetimeBounds {
|
||||
lifetimes: Vec::new(),
|
||||
bounds: vec![(typaram,
|
||||
vec![path_std!(cx, core::hash::Hasher)])],
|
||||
},
|
||||
explicit_self: borrowed_explicit_self(),
|
||||
args: vec!(Ptr(Box::new(Literal(arg)), Borrowed(None, Mutability::Mutable))),
|
||||
ret_ty: nil_ty(),
|
||||
attributes: vec![],
|
||||
is_unsafe: false,
|
||||
unify_fieldless_variants: true,
|
||||
combine_substructure: combine_substructure(Box::new(|a, b, c| {
|
||||
hash_substructure(a, b, c)
|
||||
}))
|
||||
}
|
||||
),
|
||||
methods: vec![MethodDef {
|
||||
name: "hash",
|
||||
generics: LifetimeBounds {
|
||||
lifetimes: Vec::new(),
|
||||
bounds: vec![(typaram, vec![path_std!(cx, core::hash::Hasher)])],
|
||||
},
|
||||
explicit_self: borrowed_explicit_self(),
|
||||
args: vec![Ptr(Box::new(Literal(arg)),
|
||||
Borrowed(None, Mutability::Mutable))],
|
||||
ret_ty: nil_ty(),
|
||||
attributes: vec![],
|
||||
is_unsafe: false,
|
||||
unify_fieldless_variants: true,
|
||||
combine_substructure: combine_substructure(Box::new(|a, b, c| {
|
||||
hash_substructure(a, b, c)
|
||||
})),
|
||||
}],
|
||||
associated_types: Vec::new(),
|
||||
};
|
||||
|
||||
@@ -66,7 +62,10 @@ pub fn expand_deriving_hash(cx: &mut ExtCtxt,
|
||||
fn hash_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure) -> P<Expr> {
|
||||
let state_expr = match (substr.nonself_args.len(), substr.nonself_args.get(0)) {
|
||||
(1, Some(o_f)) => o_f,
|
||||
_ => cx.span_bug(trait_span, "incorrect number of arguments in `derive(Hash)`")
|
||||
_ => {
|
||||
cx.span_bug(trait_span,
|
||||
"incorrect number of arguments in `derive(Hash)`")
|
||||
}
|
||||
};
|
||||
let call_hash = |span, thing_expr| {
|
||||
let hash_path = {
|
||||
@@ -75,7 +74,7 @@ fn hash_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure)
|
||||
cx.expr_path(cx.path_global(span, strs))
|
||||
};
|
||||
let ref_thing = cx.expr_addr_of(span, thing_expr);
|
||||
let expr = cx.expr_call(span, hash_path, vec!(ref_thing, state_expr.clone()));
|
||||
let expr = cx.expr_call(span, hash_path, vec![ref_thing, state_expr.clone()]);
|
||||
cx.stmt_expr(expr)
|
||||
};
|
||||
let mut stmts = Vec::new();
|
||||
@@ -92,7 +91,7 @@ fn hash_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure)
|
||||
|
||||
fs
|
||||
}
|
||||
_ => cx.span_bug(trait_span, "impossible substructure in `derive(Hash)`")
|
||||
_ => cx.span_bug(trait_span, "impossible substructure in `derive(Hash)`"),
|
||||
};
|
||||
|
||||
for &FieldInfo { ref self_, span, .. } in fields {
|
||||
|
||||
@@ -10,9 +10,9 @@
|
||||
|
||||
//! The compiler code necessary to implement the `#[derive]` extensions.
|
||||
|
||||
use syntax::ast::{MetaItem, MetaItemKind, self};
|
||||
use syntax::ast::{self, MetaItem, MetaItemKind};
|
||||
use syntax::attr::AttrMetaMethods;
|
||||
use syntax::ext::base::{ExtCtxt, SyntaxEnv, Annotatable};
|
||||
use syntax::ext::base::{Annotatable, ExtCtxt, SyntaxEnv};
|
||||
use syntax::ext::base::{MultiDecorator, MultiItemDecorator, MultiModifier};
|
||||
use syntax::ext::build::AstBuilder;
|
||||
use syntax::feature_gate;
|
||||
@@ -123,7 +123,8 @@ fn expand_derive(cx: &mut ExtCtxt,
|
||||
span: Some(titem.span),
|
||||
allow_internal_unstable: true,
|
||||
},
|
||||
}), ..titem.span
|
||||
}),
|
||||
..titem.span
|
||||
};
|
||||
|
||||
if &tname[..] == "Eq" {
|
||||
@@ -133,8 +134,9 @@ fn expand_derive(cx: &mut ExtCtxt,
|
||||
}
|
||||
|
||||
// #[derive(Foo, Bar)] expands to #[derive_Foo] #[derive_Bar]
|
||||
item.attrs.push(cx.attribute(span, cx.meta_word(titem.span,
|
||||
intern_and_get_ident(&format!("derive_{}", tname)))));
|
||||
item.attrs.push(cx.attribute(span,
|
||||
cx.meta_word(titem.span,
|
||||
intern_and_get_ident(&format!("derive_{}", tname)))));
|
||||
}
|
||||
|
||||
// RFC #1445. `#[derive(PartialEq, Eq)]` adds a (trusted)
|
||||
@@ -142,18 +144,18 @@ fn expand_derive(cx: &mut ExtCtxt,
|
||||
if let Some(eq_span) = eq_span {
|
||||
if found_partial_eq {
|
||||
let structural_match = intern_and_get_ident("structural_match");
|
||||
item.attrs.push(cx.attribute(eq_span,
|
||||
cx.meta_word(eq_span,
|
||||
structural_match)));
|
||||
item.attrs.push(cx.attribute(eq_span, cx.meta_word(eq_span, structural_match)));
|
||||
}
|
||||
}
|
||||
|
||||
item
|
||||
})
|
||||
}, |a| {
|
||||
cx.span_err(span, "`derive` can only be applied to items");
|
||||
a
|
||||
});
|
||||
},
|
||||
|a| {
|
||||
cx.span_err(span,
|
||||
"`derive` can only be applied to items");
|
||||
a
|
||||
});
|
||||
debug!("expand_derive: annotatable output = {:?}", annot);
|
||||
annot
|
||||
}
|
||||
@@ -261,8 +263,10 @@ fn warn_if_deprecated(ecx: &mut ExtCtxt, sp: Span, name: &str) {
|
||||
"Decodable" => Some("RustcDecodable"),
|
||||
_ => None,
|
||||
} {
|
||||
ecx.span_warn(sp, &format!("derive({}) is deprecated in favor of derive({})",
|
||||
name, replacement));
|
||||
ecx.span_warn(sp,
|
||||
&format!("derive({}) is deprecated in favor of derive({})",
|
||||
name,
|
||||
replacement));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -275,8 +279,7 @@ fn hygienic_type_parameter(item: &Annotatable, base: &str) -> String {
|
||||
if let Annotatable::Item(ref item) = *item {
|
||||
match item.node {
|
||||
ast::ItemKind::Struct(_, ast::Generics { ref ty_params, .. }) |
|
||||
ast::ItemKind::Enum(_, ast::Generics { ref ty_params, .. }) => {
|
||||
|
||||
ast::ItemKind::Enum(_, ast::Generics { ref ty_params, .. }) => {
|
||||
for ty in ty_params.iter() {
|
||||
typaram.push_str(&ty.ident.name.as_str());
|
||||
}
|
||||
@@ -293,7 +296,8 @@ fn hygienic_type_parameter(item: &Annotatable, base: &str) -> String {
|
||||
fn call_intrinsic(cx: &ExtCtxt,
|
||||
span: Span,
|
||||
intrinsic: &str,
|
||||
args: Vec<P<ast::Expr>>) -> P<ast::Expr> {
|
||||
args: Vec<P<ast::Expr>>)
|
||||
-> P<ast::Expr> {
|
||||
let path = cx.std_path(&["intrinsics", intrinsic]);
|
||||
let call = cx.expr_call_global(span, path, args);
|
||||
|
||||
@@ -301,6 +305,6 @@ fn call_intrinsic(cx: &ExtCtxt,
|
||||
stmts: vec![cx.stmt_expr(call)],
|
||||
id: ast::DUMMY_NODE_ID,
|
||||
rules: ast::BlockCheckMode::Unsafe(ast::CompilerGenerated),
|
||||
span: span }))
|
||||
span: span,
|
||||
}))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user