8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
11
+ #![ allow( unused_imports, unused_variables, dead_code) ]
12
+
11
13
use rustc:: middle:: allocator:: AllocatorKind ;
12
14
use rustc_errors;
13
15
use rustc_target:: spec:: abi:: Abi ;
@@ -35,13 +37,15 @@ pub fn modify(
35
37
sess : & ParseSess ,
36
38
resolver : & mut Resolver ,
37
39
krate : Crate ,
40
+ crate_name : String ,
38
41
handler : & rustc_errors:: Handler ,
39
42
) -> ast:: Crate {
40
43
ExpandAllocatorDirectives {
41
44
handler,
42
45
sess,
43
46
resolver,
44
47
found : false ,
48
+ crate_name : Some ( crate_name) ,
45
49
} . fold_crate ( krate)
46
50
}
47
51
@@ -50,6 +54,7 @@ struct ExpandAllocatorDirectives<'a> {
50
54
handler : & ' a rustc_errors:: Handler ,
51
55
sess : & ' a ParseSess ,
52
56
resolver : & ' a mut Resolver ,
57
+ crate_name : Option < String > ,
53
58
}
54
59
55
60
impl < ' a > Folder for ExpandAllocatorDirectives < ' a > {
@@ -78,9 +83,10 @@ impl<'a> Folder for ExpandAllocatorDirectives<'a> {
78
83
}
79
84
self . found = true ;
80
85
86
+ // Create a fresh Mark for the new macro expansion we are about to do
81
87
let mark = Mark :: fresh ( Mark :: root ( ) ) ;
82
88
mark. set_expn_info ( ExpnInfo {
83
- call_site : DUMMY_SP ,
89
+ call_site : item . span ,
84
90
callee : NameAndSpan {
85
91
format : MacroAttribute ( Symbol :: intern ( name) ) ,
86
92
span : None ,
@@ -89,35 +95,34 @@ impl<'a> Folder for ExpandAllocatorDirectives<'a> {
89
95
edition : hygiene:: default_edition ( ) ,
90
96
} ,
91
97
} ) ;
98
+
99
+ // Tie the span to the macro expansion info we just created
92
100
let span = item. span . with_ctxt ( SyntaxContext :: empty ( ) . apply_mark ( mark) ) ;
93
- let ecfg = ExpansionConfig :: default ( name. to_string ( ) ) ;
101
+
102
+ // Create an expansion config
103
+ let ecfg = ExpansionConfig :: default ( self . crate_name . take ( ) . unwrap ( ) ) ;
104
+
105
+ // Generate a bunch of new items using the AllocFnFactory
94
106
let mut f = AllocFnFactory {
95
107
span,
96
108
kind : AllocatorKind :: Global ,
97
109
global : item. ident ,
98
- core : Ident :: from_str ( "core" ) ,
110
+ core : Ident :: with_empty_ctxt ( Symbol :: gensym ( "core" ) ) ,
99
111
cx : ExtCtxt :: new ( self . sess , ecfg, self . resolver ) ,
100
112
} ;
101
- let super_path = f. cx . path ( f. span , vec ! [ Ident :: from_str( "super" ) , f. global] ) ;
102
- let mut items = vec ! [
103
- f. cx. item_extern_crate( f. span, f. core) ,
104
- f. cx. item_use_simple(
105
- f. span,
106
- respan( f. span. shrink_to_lo( ) , VisibilityKind :: Inherited ) ,
107
- super_path,
108
- ) ,
109
- ] ;
110
- for method in ALLOCATOR_METHODS {
111
- items. push ( f. allocator_fn ( method) ) ;
112
- }
113
- let name = f. kind . fn_name ( "allocator_abi" ) ;
114
- let allocator_abi = Ident :: with_empty_ctxt ( Symbol :: gensym ( & name) ) ;
115
- let module = f. cx . item_mod ( span, span, allocator_abi, Vec :: new ( ) , items) ;
116
- let module = f. cx . monotonic_expander ( ) . fold_item ( module) . pop ( ) . unwrap ( ) ;
113
+
114
+ let extcore = {
115
+ let extcore = f. cx . item_extern_crate ( item. span , f. core ) ;
116
+ f. cx . monotonic_expander ( ) . fold_item ( extcore) . pop ( ) . unwrap ( )
117
+ } ;
117
118
118
119
let mut ret = SmallVector :: new ( ) ;
119
120
ret. push ( item) ;
120
- ret. push ( module) ;
121
+ ret. push ( extcore) ;
122
+ ret. extend ( ALLOCATOR_METHODS . iter ( ) . map ( |method| {
123
+ let method = f. allocator_fn ( method) ;
124
+ f. cx . monotonic_expander ( ) . fold_item ( method) . pop ( ) . unwrap ( )
125
+ } ) ) ;
121
126
return ret;
122
127
}
123
128
@@ -170,6 +175,7 @@ impl<'a> AllocFnFactory<'a> {
170
175
let method = self . cx . path (
171
176
self . span ,
172
177
vec ! [
178
+ Ident :: from_str( "self" ) ,
173
179
self . core,
174
180
Ident :: from_str( "alloc" ) ,
175
181
Ident :: from_str( "GlobalAlloc" ) ,
@@ -220,6 +226,7 @@ impl<'a> AllocFnFactory<'a> {
220
226
let layout_new = self . cx . path (
221
227
self . span ,
222
228
vec ! [
229
+ Ident :: from_str( "self" ) ,
223
230
self . core,
224
231
Ident :: from_str( "alloc" ) ,
225
232
Ident :: from_str( "Layout" ) ,
0 commit comments