Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Allow global_allocators in submodules #49320

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 28 additions & 20 deletions src/librustc_allocator/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 rustc_target::spec::abi::Abi;
Expand Down Expand Up @@ -35,13 +37,15 @@ pub fn modify(
sess: &ParseSess,
resolver: &mut Resolver,
krate: Crate,
crate_name: String,
handler: &rustc_errors::Handler,
) -> ast::Crate {
ExpandAllocatorDirectives {
handler,
sess,
resolver,
found: false,
crate_name: Some(crate_name),
}.fold_crate(krate)
}

Expand All @@ -50,6 +54,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> {
Expand Down Expand Up @@ -78,45 +83,45 @@ 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,
callee: NameAndSpan {
format: MacroAttribute(Symbol::intern(name)),
span: None,
allow_internal_unstable: true,
allow_internal_unsafe: false,
},
});

// 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;
}

Expand Down Expand Up @@ -169,6 +174,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"),
Expand Down Expand Up @@ -219,6 +225,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"),
Expand Down Expand Up @@ -285,6 +292,7 @@ impl<'a> AllocFnFactory<'a> {
let opaque = self.cx.path(
self.span,
vec![
Ident::from_str("self"),
self.core,
Ident::from_str("alloc"),
Ident::from_str("Opaque"),
Expand Down
11 changes: 10 additions & 1 deletion src/librustc_driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -929,10 +929,19 @@ where
});
}

// Expand global allocators, which are treated as an in-tree proc macro
krate = time(sess, "creating allocators", || {
allocator::expand::modify(&sess.parse_sess, &mut resolver, krate, sess.diagnostic())
allocator::expand::modify(
&sess.parse_sess,
&mut resolver,
krate,
crate_name.to_string(),
sess.diagnostic(),
)
});

// Done with macro expansion!

after_expand(&krate)?;

if sess.opts.debugging_opts.input_stats {
Expand Down
37 changes: 37 additions & 0 deletions src/test/run-pass/allocator/submodule.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// Tests that it is possible to create a global allocator in a submodule, rather than in the crate
// root.

#![feature(alloc, allocator_api, global_allocator)]

extern crate alloc;

use std::alloc::{GlobalAlloc, Layout, Opaque};

struct MyAlloc;

unsafe impl GlobalAlloc for MyAlloc {
unsafe fn alloc(&self, layout: Layout) -> *mut Opaque {
0 as usize as *mut Opaque
}

unsafe fn dealloc(&self, ptr: *mut Opaque, layout: Layout) {}
}

mod submod {
use super::MyAlloc;

#[global_allocator]
static MY_HEAP: MyAlloc = MyAlloc;
}

fn main() {}