88// option. This file may not be copied, modified, or distributed
99// except according to those terms.
1010
11+ #![ allow( unused_imports, unused_variables, dead_code) ]
12+
1113use rustc:: middle:: allocator:: AllocatorKind ;
1214use rustc_errors;
1315use syntax:: ast:: { Attribute , Crate , LitKind , StrStyle } ;
@@ -34,13 +36,15 @@ pub fn modify(
3436 sess : & ParseSess ,
3537 resolver : & mut Resolver ,
3638 krate : Crate ,
39+ crate_name : String ,
3740 handler : & rustc_errors:: Handler ,
3841) -> ast:: Crate {
3942 ExpandAllocatorDirectives {
4043 handler,
4144 sess,
4245 resolver,
4346 found : false ,
47+ crate_name : Some ( crate_name) ,
4448 } . fold_crate ( krate)
4549}
4650
@@ -49,6 +53,7 @@ struct ExpandAllocatorDirectives<'a> {
4953 handler : & ' a rustc_errors:: Handler ,
5054 sess : & ' a ParseSess ,
5155 resolver : & ' a mut Resolver ,
56+ crate_name : Option < String > ,
5257}
5358
5459impl < ' a > Folder for ExpandAllocatorDirectives < ' a > {
@@ -77,44 +82,44 @@ impl<'a> Folder for ExpandAllocatorDirectives<'a> {
7782 }
7883 self . found = true ;
7984
85+ // Create a fresh Mark for the new macro expansion we are about to do
8086 let mark = Mark :: fresh ( Mark :: root ( ) ) ;
8187 mark. set_expn_info ( ExpnInfo {
82- call_site : DUMMY_SP ,
88+ call_site : item . span ,
8389 def_site : None ,
8490 format : MacroAttribute ( Symbol :: intern ( name) ) ,
8591 allow_internal_unstable : true ,
8692 allow_internal_unsafe : false ,
8793 edition : hygiene:: default_edition ( ) ,
8894 } ) ;
95+
96+ // Tie the span to the macro expansion info we just created
8997 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
91103 let mut f = AllocFnFactory {
92104 span,
93105 kind : AllocatorKind :: Global ,
94106 global : item. ident ,
95- core : Ident :: from_str ( "core" ) ,
107+ core : Ident :: with_empty_ctxt ( Symbol :: gensym ( "core" ) ) ,
96108 cx : ExtCtxt :: new ( self . sess , ecfg, self . resolver ) ,
97109 } ;
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+ } ;
114115
115116 let mut ret = SmallVector :: new ( ) ;
116117 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+ } ) ) ;
118123 return ret;
119124 }
120125
@@ -168,6 +173,7 @@ impl<'a> AllocFnFactory<'a> {
168173 let method = self . cx . path (
169174 self . span ,
170175 vec ! [
176+ Ident :: from_str( "self" ) ,
171177 self . core,
172178 Ident :: from_str( "alloc" ) ,
173179 Ident :: from_str( "GlobalAlloc" ) ,
@@ -218,6 +224,7 @@ impl<'a> AllocFnFactory<'a> {
218224 let layout_new = self . cx . path (
219225 self . span ,
220226 vec ! [
227+ Ident :: from_str( "self" ) ,
221228 self . core,
222229 Ident :: from_str( "alloc" ) ,
223230 Ident :: from_str( "Layout" ) ,
0 commit comments