Skip to content

Commit

Permalink
Run rustfmt on libsyntax_ext/deriving folder
Browse files Browse the repository at this point in the history
  • Loading branch information
srinivasreddy committed Jul 19, 2016
1 parent 27e766d commit 9652fcb
Show file tree
Hide file tree
Showing 13 changed files with 932 additions and 927 deletions.
8 changes: 3 additions & 5 deletions src/libsyntax_ext/deriving/bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,22 @@ use deriving::generic::*;
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");
}

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");
Expand Down
98 changes: 50 additions & 48 deletions src/libsyntax_ext/deriving/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }`
Expand All @@ -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;
Expand All @@ -66,54 +68,53 @@ 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(),
path: path_std!(cx, core::clone::Clone),
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
};
Expand All @@ -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))
}
}

Expand All @@ -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)
}
Expand All @@ -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),
}
}
}
Expand Down
67 changes: 31 additions & 36 deletions src/libsyntax_ext/deriving/cmp/eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,52 +22,47 @@ 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(),
path: path_std!(cx, core::cmp::Eq),
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)
Expand Down
Loading

0 comments on commit 9652fcb

Please sign in to comment.