1111use deriving:: generic:: * ;
1212use deriving:: generic:: ty:: * ;
1313
14- use syntax:: ast:: { Expr , ItemKind , Generics , MetaItem , VariantData } ;
14+ use syntax:: ast:: { Expr , Generics , ItemKind , MetaItem , VariantData } ;
1515use syntax:: attr;
16- use syntax:: ext:: base:: { ExtCtxt , Annotatable } ;
16+ use syntax:: ext:: base:: { Annotatable , ExtCtxt } ;
1717use syntax:: ext:: build:: AstBuilder ;
1818use syntax:: parse:: token:: InternedString ;
1919use syntax:: ptr:: P ;
2020use syntax_pos:: Span ;
2121
2222#[ derive( PartialEq ) ]
23- enum Mode { Deep , Shallow }
23+ enum Mode {
24+ Deep ,
25+ Shallow ,
26+ }
2427
2528pub fn expand_deriving_clone ( cx : & mut ExtCtxt ,
2629 span : Span ,
2730 mitem : & MetaItem ,
2831 item : & Annotatable ,
29- push : & mut FnMut ( Annotatable ) )
30- {
32+ push : & mut FnMut ( Annotatable ) ) {
3133 // check if we can use a short form
3234 //
3335 // the short form is `fn clone(&self) -> Self { *self }`
@@ -46,8 +48,8 @@ pub fn expand_deriving_clone(cx: &mut ExtCtxt,
4648 match annitem. node {
4749 ItemKind :: Struct ( _, Generics { ref ty_params, .. } ) |
4850 ItemKind :: Enum ( _, Generics { ref ty_params, .. } )
49- if ty_params. is_empty ( )
50- && attr:: contains_name ( & annitem. attrs , "derive_Copy" ) => {
51+ if ty_params. is_empty ( ) &&
52+ attr:: contains_name ( & annitem. attrs , "derive_Copy" ) => {
5153
5254 bounds = vec ! [ Literal ( path_std!( cx, core:: marker:: Copy ) ) ] ;
5355 unify_fieldless_variants = true ;
@@ -66,54 +68,53 @@ pub fn expand_deriving_clone(cx: &mut ExtCtxt,
6668 }
6769 }
6870
69- _ => cx. span_bug ( span, "#[derive(Clone)] on trait item or impl item" )
71+ _ => cx. span_bug ( span, "#[derive(Clone)] on trait item or impl item" ) ,
7072 }
7173
7274 let inline = cx. meta_word ( span, InternedString :: new ( "inline" ) ) ;
73- let attrs = vec ! ( cx. attribute( span, inline) ) ;
75+ let attrs = vec ! [ cx. attribute( span, inline) ] ;
7476 let trait_def = TraitDef {
7577 span : span,
7678 attributes : Vec :: new ( ) ,
7779 path : path_std ! ( cx, core:: clone:: Clone ) ,
7880 additional_bounds : bounds,
7981 generics : LifetimeBounds :: empty ( ) ,
8082 is_unsafe : false ,
81- methods : vec ! (
82- MethodDef {
83- name: "clone" ,
84- generics: LifetimeBounds :: empty( ) ,
85- explicit_self: borrowed_explicit_self( ) ,
86- args: Vec :: new( ) ,
87- ret_ty: Self_ ,
88- attributes: attrs,
89- is_unsafe: false ,
90- unify_fieldless_variants: unify_fieldless_variants,
91- combine_substructure: substructure,
92- }
93- ) ,
83+ methods : vec ! [ MethodDef {
84+ name: "clone" ,
85+ generics: LifetimeBounds :: empty( ) ,
86+ explicit_self: borrowed_explicit_self( ) ,
87+ args: Vec :: new( ) ,
88+ ret_ty: Self_ ,
89+ attributes: attrs,
90+ is_unsafe: false ,
91+ unify_fieldless_variants: unify_fieldless_variants,
92+ combine_substructure: substructure,
93+ } ] ,
9494 associated_types : Vec :: new ( ) ,
9595 } ;
9696
9797 trait_def. expand ( cx, mitem, item, push)
9898}
9999
100- fn cs_clone (
101- name : & str ,
102- cx : & mut ExtCtxt , trait_span : Span ,
103- substr : & Substructure ,
104- mode : Mode ) -> P < Expr > {
100+ fn cs_clone ( name : & str ,
101+ cx : & mut ExtCtxt ,
102+ trait_span : Span ,
103+ substr : & Substructure ,
104+ mode : Mode )
105+ -> P < Expr > {
105106 let ctor_path;
106107 let all_fields;
107108 let fn_path = match mode {
108109 Mode :: Shallow => cx. std_path ( & [ "clone" , "assert_receiver_is_clone" ] ) ,
109- Mode :: Deep => cx. std_path ( & [ "clone" , "Clone" , "clone" ] ) ,
110+ Mode :: Deep => cx. std_path ( & [ "clone" , "Clone" , "clone" ] ) ,
110111 } ;
111112 let subcall = |field : & FieldInfo | {
112113 let args = vec ! [ cx. expr_addr_of( field. span, field. self_. clone( ) ) ] ;
113114
114115 let span = if mode == Mode :: Shallow {
115116 // set the expn ID so we can call the unstable method
116- Span { expn_id : cx. backtrace ( ) , .. trait_span }
117+ Span { expn_id : cx. backtrace ( ) , ..trait_span }
117118 } else {
118119 field. span
119120 } ;
@@ -131,15 +132,15 @@ fn cs_clone(
131132 ctor_path = cx. path ( trait_span, vec ! [ substr. type_ident, variant. node. name] ) ;
132133 all_fields = af;
133134 vdata = & variant. node . data ;
134- } ,
135- EnumNonMatchingCollapsed ( ..) => {
135+ }
136+ EnumNonMatchingCollapsed ( ..) => {
136137 cx. span_bug ( trait_span,
137138 & format ! ( "non-matching enum variants in \
138- `derive({})`", name) )
139+ `derive({})`",
140+ name) )
139141 }
140142 StaticEnum ( ..) | StaticStruct ( ..) => {
141- cx. span_bug ( trait_span,
142- & format ! ( "static method in `derive({})`" , name) )
143+ cx. span_bug ( trait_span, & format ! ( "static method in `derive({})`" , name) )
143144 }
144145 }
145146
@@ -153,17 +154,20 @@ fn cs_clone(
153154 Mode :: Deep => {
154155 match * vdata {
155156 VariantData :: Struct ( ..) => {
156- let fields = all_fields. iter ( ) . map ( |field| {
157- let ident = match field. name {
158- Some ( i) => i,
159- None => {
160- cx. span_bug ( trait_span,
161- & format ! ( "unnamed field in normal struct in \
162- `derive({})`", name) )
163- }
164- } ;
165- cx. field_imm ( field. span , ident, subcall ( field) )
166- } ) . collect :: < Vec < _ > > ( ) ;
157+ let fields = all_fields. iter ( )
158+ . map ( |field| {
159+ let ident = match field. name {
160+ Some ( i) => i,
161+ None => {
162+ cx. span_bug ( trait_span,
163+ & format ! ( "unnamed field in normal struct in \
164+ `derive({})`",
165+ name) )
166+ }
167+ } ;
168+ cx. field_imm ( field. span , ident, subcall ( field) )
169+ } )
170+ . collect :: < Vec < _ > > ( ) ;
167171
168172 cx. expr_struct ( trait_span, ctor_path, fields)
169173 }
@@ -172,9 +176,7 @@ fn cs_clone(
172176 let path = cx. expr_path ( ctor_path) ;
173177 cx. expr_call ( trait_span, path, subcalls)
174178 }
175- VariantData :: Unit ( ..) => {
176- cx. expr_path ( ctor_path)
177- }
179+ VariantData :: Unit ( ..) => cx. expr_path ( ctor_path) ,
178180 }
179181 }
180182 }
0 commit comments