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