11
11
use deriving:: generic:: * ;
12
12
use deriving:: generic:: ty:: * ;
13
13
14
- use syntax:: ast:: { Expr , ItemKind , Generics , MetaItem , VariantData } ;
14
+ use syntax:: ast:: { Expr , Generics , ItemKind , MetaItem , VariantData } ;
15
15
use syntax:: attr;
16
- use syntax:: ext:: base:: { ExtCtxt , Annotatable } ;
16
+ use syntax:: ext:: base:: { Annotatable , ExtCtxt } ;
17
17
use syntax:: ext:: build:: AstBuilder ;
18
18
use syntax:: parse:: token:: InternedString ;
19
19
use syntax:: ptr:: P ;
20
20
use syntax_pos:: Span ;
21
21
22
22
#[ derive( PartialEq ) ]
23
- enum Mode { Deep , Shallow }
23
+ enum Mode {
24
+ Deep ,
25
+ Shallow ,
26
+ }
24
27
25
28
pub fn expand_deriving_clone ( cx : & mut ExtCtxt ,
26
29
span : Span ,
27
30
mitem : & MetaItem ,
28
31
item : & Annotatable ,
29
- push : & mut FnMut ( Annotatable ) )
30
- {
32
+ push : & mut FnMut ( Annotatable ) ) {
31
33
// check if we can use a short form
32
34
//
33
35
// the short form is `fn clone(&self) -> Self { *self }`
@@ -46,8 +48,8 @@ pub fn expand_deriving_clone(cx: &mut ExtCtxt,
46
48
match annitem. node {
47
49
ItemKind :: Struct ( _, Generics { ref ty_params, .. } ) |
48
50
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" ) => {
51
53
52
54
bounds = vec ! [ Literal ( path_std!( cx, core:: marker:: Copy ) ) ] ;
53
55
unify_fieldless_variants = true ;
@@ -66,54 +68,53 @@ pub fn expand_deriving_clone(cx: &mut ExtCtxt,
66
68
}
67
69
}
68
70
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" ) ,
70
72
}
71
73
72
74
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) ] ;
74
76
let trait_def = TraitDef {
75
77
span : span,
76
78
attributes : Vec :: new ( ) ,
77
79
path : path_std ! ( cx, core:: clone:: Clone ) ,
78
80
additional_bounds : bounds,
79
81
generics : LifetimeBounds :: empty ( ) ,
80
82
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
+ } ] ,
94
94
associated_types : Vec :: new ( ) ,
95
95
} ;
96
96
97
97
trait_def. expand ( cx, mitem, item, push)
98
98
}
99
99
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 > {
105
106
let ctor_path;
106
107
let all_fields;
107
108
let fn_path = match mode {
108
109
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" ] ) ,
110
111
} ;
111
112
let subcall = |field : & FieldInfo | {
112
113
let args = vec ! [ cx. expr_addr_of( field. span, field. self_. clone( ) ) ] ;
113
114
114
115
let span = if mode == Mode :: Shallow {
115
116
// 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 }
117
118
} else {
118
119
field. span
119
120
} ;
@@ -131,15 +132,15 @@ fn cs_clone(
131
132
ctor_path = cx. path ( trait_span, vec ! [ substr. type_ident, variant. node. name] ) ;
132
133
all_fields = af;
133
134
vdata = & variant. node . data ;
134
- } ,
135
- EnumNonMatchingCollapsed ( ..) => {
135
+ }
136
+ EnumNonMatchingCollapsed ( ..) => {
136
137
cx. span_bug ( trait_span,
137
138
& format ! ( "non-matching enum variants in \
138
- `derive({})`", name) )
139
+ `derive({})`",
140
+ name) )
139
141
}
140
142
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) )
143
144
}
144
145
}
145
146
@@ -153,17 +154,20 @@ fn cs_clone(
153
154
Mode :: Deep => {
154
155
match * vdata {
155
156
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 < _ > > ( ) ;
167
171
168
172
cx. expr_struct ( trait_span, ctor_path, fields)
169
173
}
@@ -172,9 +176,7 @@ fn cs_clone(
172
176
let path = cx. expr_path ( ctor_path) ;
173
177
cx. expr_call ( trait_span, path, subcalls)
174
178
}
175
- VariantData :: Unit ( ..) => {
176
- cx. expr_path ( ctor_path)
177
- }
179
+ VariantData :: Unit ( ..) => cx. expr_path ( ctor_path) ,
178
180
}
179
181
}
180
182
}
0 commit comments