about summary refs log tree commit diff
path: root/src/librustc_allocator
diff options
context:
space:
mode:
authorMark Mansi <markm@cs.wisc.edu>2018-04-13 15:58:16 -0500
committermark <markm@cs.wisc.edu>2018-06-24 16:10:20 -0500
commit5da4ff8180b4ba67c6e206e3c910f2a7848c2d07 (patch)
tree91a0f13d45e1de8189316ca32a711902cc5a9a56 /src/librustc_allocator
parent01cc982e936120acb0424e41de14e42ba2d88c6f (diff)
Attempt to fix hygiene for global_allocator
Diffstat (limited to 'src/librustc_allocator')
-rw-r--r--src/librustc_allocator/expand.rs47
1 files changed, 27 insertions, 20 deletions
diff --git a/src/librustc_allocator/expand.rs b/src/librustc_allocator/expand.rs
index a9530964bff..cea1807cfba 100644
--- a/src/librustc_allocator/expand.rs
+++ b/src/librustc_allocator/expand.rs
@@ -8,6 +8,8 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+#![allow(unused_imports, unused_variables, dead_code)]
+
 use rustc::middle::allocator::AllocatorKind;
 use rustc_errors;
 use syntax::ast::{Attribute, Crate, LitKind, StrStyle};
@@ -34,6 +36,7 @@ pub fn modify(
     sess: &ParseSess,
     resolver: &mut Resolver,
     krate: Crate,
+    crate_name: String,
     handler: &rustc_errors::Handler,
 ) -> ast::Crate {
     ExpandAllocatorDirectives {
@@ -41,6 +44,7 @@ pub fn modify(
         sess,
         resolver,
         found: false,
+        crate_name: Some(crate_name),
     }.fold_crate(krate)
 }
 
@@ -49,6 +53,7 @@ struct ExpandAllocatorDirectives<'a> {
     handler: &'a rustc_errors::Handler,
     sess: &'a ParseSess,
     resolver: &'a mut Resolver,
+    crate_name: Option<String>,
 }
 
 impl<'a> Folder for ExpandAllocatorDirectives<'a> {
@@ -77,44 +82,44 @@ impl<'a> Folder for ExpandAllocatorDirectives<'a> {
         }
         self.found = true;
 
+        // Create a fresh Mark for the new macro expansion we are about to do
         let mark = Mark::fresh(Mark::root());
         mark.set_expn_info(ExpnInfo {
-            call_site: DUMMY_SP,
+            call_site: item.span,
             def_site: None,
             format: MacroAttribute(Symbol::intern(name)),
             allow_internal_unstable: true,
             allow_internal_unsafe: false,
             edition: hygiene::default_edition(),
         });
+
+        // Tie the span to the macro expansion info we just created
         let span = item.span.with_ctxt(SyntaxContext::empty().apply_mark(mark));
-        let ecfg = ExpansionConfig::default(name.to_string());
+
+        // Create an expansion config
+        let ecfg = ExpansionConfig::default(self.crate_name.take().unwrap());
+
+        // Generate a bunch of new items using the AllocFnFactory
         let mut f = AllocFnFactory {
             span,
             kind: AllocatorKind::Global,
             global: item.ident,
-            core: Ident::from_str("core"),
+            core: Ident::with_empty_ctxt(Symbol::gensym("core")),
             cx: ExtCtxt::new(self.sess, ecfg, self.resolver),
         };
-        let super_path = f.cx.path(f.span, vec![Ident::from_str("super"), f.global]);
-        let mut items = vec![
-            f.cx.item_extern_crate(f.span, f.core),
-            f.cx.item_use_simple(
-                f.span,
-                respan(f.span.shrink_to_lo(), VisibilityKind::Inherited),
-                super_path,
-            ),
-        ];
-        for method in ALLOCATOR_METHODS {
-            items.push(f.allocator_fn(method));
-        }
-        let name = f.kind.fn_name("allocator_abi");
-        let allocator_abi = Ident::with_empty_ctxt(Symbol::gensym(&name));
-        let module = f.cx.item_mod(span, span, allocator_abi, Vec::new(), items);
-        let module = f.cx.monotonic_expander().fold_item(module).pop().unwrap();
+
+        let extcore = {
+            let extcore = f.cx.item_extern_crate(item.span, f.core);
+            f.cx.monotonic_expander().fold_item(extcore).pop().unwrap()
+        };
 
         let mut ret = SmallVector::new();
         ret.push(item);
-        ret.push(module);
+        ret.push(extcore);
+        ret.extend(ALLOCATOR_METHODS.iter().map(|method| {
+            let method = f.allocator_fn(method);
+            f.cx.monotonic_expander().fold_item(method).pop().unwrap()
+        }));
         return ret;
     }
 
@@ -168,6 +173,7 @@ impl<'a> AllocFnFactory<'a> {
         let method = self.cx.path(
             self.span,
             vec![
+                Ident::from_str("self"),
                 self.core,
                 Ident::from_str("alloc"),
                 Ident::from_str("GlobalAlloc"),
@@ -218,6 +224,7 @@ impl<'a> AllocFnFactory<'a> {
                 let layout_new = self.cx.path(
                     self.span,
                     vec![
+                        Ident::from_str("self"),
                         self.core,
                         Ident::from_str("alloc"),
                         Ident::from_str("Layout"),