1
1
use rustc:: middle:: allocator:: AllocatorKind ;
2
2
use rustc_errors;
3
- use smallvec:: SmallVec ;
4
3
use syntax:: {
5
4
ast:: {
6
5
self , Arg , Attribute , Crate , Expr , FnHeader , Generics , Ident , Item , ItemKind ,
7
- Mac , Mod , Mutability , Ty , TyKind , Unsafety , VisibilityKind ,
6
+ Mod , Mutability , Ty , TyKind , Unsafety , VisibilityKind , NodeId ,
8
7
} ,
9
8
attr,
10
9
source_map:: {
@@ -16,7 +15,8 @@ use syntax::{
16
15
expand:: ExpansionConfig ,
17
16
hygiene:: { self , Mark , SyntaxContext } ,
18
17
} ,
19
- fold:: { self , Folder } ,
18
+ fold:: Folder ,
19
+ visit_mut:: { self , MutVisitor } ,
20
20
parse:: ParseSess ,
21
21
ptr:: P ,
22
22
symbol:: Symbol
@@ -28,7 +28,7 @@ use {AllocatorMethod, AllocatorTy, ALLOCATOR_METHODS};
28
28
pub fn modify (
29
29
sess : & ParseSess ,
30
30
resolver : & mut dyn Resolver ,
31
- krate : Crate ,
31
+ mut krate : Crate ,
32
32
crate_name : String ,
33
33
handler : & rustc_errors:: Handler ,
34
34
) -> ast:: Crate {
@@ -39,7 +39,8 @@ pub fn modify(
39
39
found : false ,
40
40
crate_name : Some ( crate_name) ,
41
41
in_submod : -1 , // -1 to account for the "root" module
42
- } . fold_crate ( krate)
42
+ } . visit_crate ( & mut krate) ;
43
+ krate
43
44
}
44
45
45
46
struct ExpandAllocatorDirectives < ' a > {
@@ -54,34 +55,35 @@ struct ExpandAllocatorDirectives<'a> {
54
55
in_submod : isize ,
55
56
}
56
57
57
- impl < ' a > Folder for ExpandAllocatorDirectives < ' a > {
58
- fn fold_item ( & mut self , item : P < Item > ) -> SmallVec < [ P < Item > ; 1 ] > {
58
+ impl < ' a > MutVisitor for ExpandAllocatorDirectives < ' a > {
59
+ fn visit_item ( & mut self , item : & mut P < Item > ) -> Option < Vec < P < Item > > > {
59
60
debug ! ( "in submodule {}" , self . in_submod) ;
60
61
61
62
let name = if attr:: contains_name ( & item. attrs , "global_allocator" ) {
62
63
"global_allocator"
63
64
} else {
64
- return fold:: noop_fold_item ( item, self ) ;
65
+ visit_mut:: walk_item ( self , item) ;
66
+ return None ;
65
67
} ;
66
68
match item. node {
67
69
ItemKind :: Static ( ..) => { }
68
70
_ => {
69
71
self . handler
70
72
. span_err ( item. span , "allocators must be statics" ) ;
71
- return smallvec ! [ item ] ;
73
+ return None ;
72
74
}
73
75
}
74
76
75
77
if self . in_submod > 0 {
76
78
self . handler
77
79
. span_err ( item. span , "`global_allocator` cannot be used in submodules" ) ;
78
- return smallvec ! [ item ] ;
80
+ return None ;
79
81
}
80
82
81
83
if self . found {
82
84
self . handler
83
85
. span_err ( item. span , "cannot define more than one #[global_allocator]" ) ;
84
- return smallvec ! [ item ] ;
86
+ return None ;
85
87
}
86
88
self . found = true ;
87
89
@@ -142,23 +144,18 @@ impl<'a> Folder for ExpandAllocatorDirectives<'a> {
142
144
let module = f. cx . monotonic_expander ( ) . fold_item ( module) . pop ( ) . unwrap ( ) ;
143
145
144
146
// Return the item and new submodule
145
- smallvec ! [ item, module]
147
+ Some ( vec ! [ item. clone ( ) , module] )
146
148
}
147
149
148
150
// If we enter a submodule, take note.
149
- fn fold_mod ( & mut self , m : Mod ) -> Mod {
151
+ fn visit_mod ( & mut self , m : & mut Mod , _s : Span , _attrs : & [ Attribute ] , _n : NodeId ) {
150
152
debug ! ( "enter submodule" ) ;
151
153
self . in_submod += 1 ;
152
- let ret = fold :: noop_fold_mod ( m , self ) ;
154
+ let ret = visit_mut :: walk_mod ( self , m ) ;
153
155
self . in_submod -= 1 ;
154
156
debug ! ( "exit submodule" ) ;
155
157
ret
156
158
}
157
-
158
- // `fold_mac` is disabled by default. Enable it here.
159
- fn fold_mac ( & mut self , mac : Mac ) -> Mac {
160
- fold:: noop_fold_mac ( mac, self )
161
- }
162
159
}
163
160
164
161
struct AllocFnFactory < ' a > {
0 commit comments