Skip to content

Commit 2bfdc3c

Browse files
committed
Use call-site hygiene for allocator expansion
1 parent 0a223d1 commit 2bfdc3c

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/librustc_allocator/expand.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
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+
1113
use rustc::middle::allocator::AllocatorKind;
1214
use rustc_errors;
1315
use rustc_target::spec::abi::Abi;
@@ -35,13 +37,15 @@ pub fn modify(
3537
sess: &ParseSess,
3638
resolver: &mut Resolver,
3739
krate: Crate,
40+
crate_name: String,
3841
handler: &rustc_errors::Handler,
3942
) -> ast::Crate {
4043
ExpandAllocatorDirectives {
4144
handler,
4245
sess,
4346
resolver,
4447
found: false,
48+
crate_name: Some(crate_name),
4549
}.fold_crate(krate)
4650
}
4751

@@ -50,6 +54,7 @@ struct ExpandAllocatorDirectives<'a> {
5054
handler: &'a rustc_errors::Handler,
5155
sess: &'a ParseSess,
5256
resolver: &'a mut Resolver,
57+
crate_name: Option<String>,
5358
}
5459

5560
impl<'a> Folder for ExpandAllocatorDirectives<'a> {
@@ -78,26 +83,34 @@ impl<'a> Folder for ExpandAllocatorDirectives<'a> {
7883
}
7984
self.found = true;
8085

86+
// Create a fresh Mark for the new macro expansion we are about to do
8187
let mark = Mark::fresh(Mark::root());
8288
mark.set_expn_info(ExpnInfo {
83-
call_site: DUMMY_SP,
89+
call_site: item.span,
8490
callee: NameAndSpan {
8591
format: MacroAttribute(Symbol::intern(name)),
8692
span: None,
8793
allow_internal_unstable: true,
8894
allow_internal_unsafe: false,
8995
},
9096
});
97+
98+
// Tie the span to the macro expansion info we just created
9199
let span = item.span.with_ctxt(SyntaxContext::empty().apply_mark(mark));
92-
let ecfg = ExpansionConfig::default(name.to_string());
100+
101+
// Create an expansion config
102+
let ecfg = ExpansionConfig::default(self.crate_name.take().unwrap());
103+
104+
// Generate a bunch of new items using the AllocFnFactory
93105
let mut f = AllocFnFactory {
94106
span,
95107
kind: AllocatorKind::Global,
96108
global: item.ident,
97109
core: Ident::from_str("core"),
98110
cx: ExtCtxt::new(self.sess, ecfg, self.resolver),
99111
};
100-
let super_path = f.cx.path(f.span, vec![Ident::from_str("super"), f.global]);
112+
//let super_path = f.cx.path(f.span, vec![Ident::from_str("super"), f.global]);
113+
let super_path = f.cx.path(f.span, vec![f.global]);
101114
let mut items = vec![
102115
f.cx.item_extern_crate(f.span, f.core),
103116
f.cx.item_use_simple(

src/librustc_driver/driver.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -929,10 +929,19 @@ where
929929
});
930930
}
931931

932+
// Expand global allocators, which are treated as an in-tree proc macro
932933
krate = time(sess, "creating allocators", || {
933-
allocator::expand::modify(&sess.parse_sess, &mut resolver, krate, sess.diagnostic())
934+
allocator::expand::modify(
935+
&sess.parse_sess,
936+
&mut resolver,
937+
krate,
938+
crate_name.to_string(),
939+
sess.diagnostic(),
940+
)
934941
});
935942

943+
// Done with macro expansion!
944+
936945
after_expand(&krate)?;
937946

938947
if sess.opts.debugging_opts.input_stats {

0 commit comments

Comments
 (0)