Skip to content

Commit 4d1ebe4

Browse files
committed
Still trying to make core work
1 parent 650d5d9 commit 4d1ebe4

File tree

2 files changed

+30
-23
lines changed

2 files changed

+30
-23
lines changed

src/librustc_allocator/expand.rs

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -106,30 +106,22 @@ impl<'a> Folder for ExpandAllocatorDirectives<'a> {
106106
span,
107107
kind: AllocatorKind::Global,
108108
global: item.ident,
109-
core: Ident::from_str("core"),
109+
core: Ident::with_empty_ctxt(Symbol::gensym("core")),
110110
cx: ExtCtxt::new(self.sess, ecfg, self.resolver),
111111
};
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]);
114-
let mut items = vec![
115-
f.cx.item_extern_crate(f.span, f.core),
116-
f.cx.item_use_simple(
117-
f.span,
118-
respan(f.span.shrink_to_lo(), VisibilityKind::Inherited),
119-
super_path,
120-
),
121-
];
122-
for method in ALLOCATOR_METHODS {
123-
items.push(f.allocator_fn(method));
124-
}
125-
let name = f.kind.fn_name("allocator_abi");
126-
let allocator_abi = Ident::with_empty_ctxt(Symbol::gensym(&name));
127-
let module = f.cx.item_mod(span, span, allocator_abi, Vec::new(), items);
128-
let module = f.cx.monotonic_expander().fold_item(module).pop().unwrap();
112+
113+
let extcore = {
114+
let extcore = f.cx.item_extern_crate(item.span, f.core);
115+
f.cx.monotonic_expander().fold_item(extcore).pop().unwrap()
116+
};
129117

130118
let mut ret = SmallVector::new();
131119
ret.push(item);
132-
ret.push(module);
120+
ret.push(extcore);
121+
ret.extend(ALLOCATOR_METHODS.iter().map(|method| {
122+
let method = f.allocator_fn(method);
123+
f.cx.monotonic_expander().fold_item(method).pop().unwrap()
124+
}));
133125
return ret;
134126
}
135127

@@ -182,6 +174,7 @@ impl<'a> AllocFnFactory<'a> {
182174
let method = self.cx.path(
183175
self.span,
184176
vec![
177+
Ident::from_str("self"),
185178
self.core,
186179
Ident::from_str("alloc"),
187180
Ident::from_str("GlobalAlloc"),
@@ -232,6 +225,7 @@ impl<'a> AllocFnFactory<'a> {
232225
let layout_new = self.cx.path(
233226
self.span,
234227
vec![
228+
Ident::from_str("self"),
235229
self.core,
236230
Ident::from_str("alloc"),
237231
Ident::from_str("Layout"),
@@ -298,6 +292,7 @@ impl<'a> AllocFnFactory<'a> {
298292
let opaque = self.cx.path(
299293
self.span,
300294
vec![
295+
Ident::from_str("self"),
301296
self.core,
302297
Ident::from_str("alloc"),
303298
Ident::from_str("Opaque"),

src/test/ui/allocator-submodule.rs renamed to src/test/run-pass/allocator/submodule.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,27 @@
1111
// Tests that it is possible to create a global allocator in a submodule, rather than in the crate
1212
// root.
1313

14-
#![feature(global_allocator, alloc, allocator_api)]
14+
#![feature(alloc, allocator_api, global_allocator)]
1515

1616
extern crate alloc;
1717

18-
mod mymod {
19-
use alloc::heap::Global;
18+
use std::alloc::{GlobalAlloc, Layout, Opaque};
19+
20+
struct MyAlloc;
21+
22+
unsafe impl GlobalAlloc for MyAlloc {
23+
unsafe fn alloc(&self, layout: Layout) -> *mut Opaque {
24+
0 as usize as *mut Opaque
25+
}
26+
27+
unsafe fn dealloc(&self, ptr: *mut Opaque, layout: Layout) {}
28+
}
29+
30+
mod submod {
31+
use super::MyAlloc;
2032

2133
#[global_allocator]
22-
static MY_HEAP: Global = Global::default(); //~ ERROR
34+
static MY_HEAP: MyAlloc = MyAlloc;
2335
}
2436

2537
fn main() {}

0 commit comments

Comments
 (0)