Skip to content

Commit bf8fc8a

Browse files
committed
syntax_ext: Improve and simplify code generated by #[global_allocator]
Instead of ``` mod allocator_abi { /* methods */ } ``` we now generate ``` const _: () = { /* methods */ } ``` and use `std_path` for paths referring to standard library entities. This way we no longer need to generate `use` and `extern crate` imports, and `#[global_allocator]` starts working inside unnamed blocks.
1 parent 76b1ffa commit bf8fc8a

File tree

3 files changed

+34
-61
lines changed

3 files changed

+34
-61
lines changed

src/libsyntax_ext/global_allocator.rs

Lines changed: 28 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
use syntax::ast::{ItemKind, Mutability, Ty, TyKind, Unsafety, VisibilityKind};
2-
use syntax::ast::{self, Arg, Attribute, Expr, FnHeader, Generics, Ident, Item};
1+
use syntax::ast::{ItemKind, Mutability, Stmt, Ty, TyKind, Unsafety};
2+
use syntax::ast::{self, Arg, Attribute, Expr, FnHeader, Generics, Ident};
33
use syntax::attr::check_builtin_macro_attribute;
44
use syntax::ext::allocator::{AllocatorKind, AllocatorMethod, AllocatorTy, ALLOCATOR_METHODS};
55
use syntax::ext::base::{Annotatable, ExtCtxt};
66
use syntax::ext::build::AstBuilder;
77
use syntax::ext::hygiene::SyntaxContext;
88
use syntax::ptr::P;
9-
use syntax::source_map::respan;
10-
use syntax::symbol::{kw, sym};
9+
use syntax::symbol::{kw, sym, Symbol};
1110
use syntax_pos::Span;
1211

1312
pub fn expand(
@@ -36,52 +35,31 @@ pub fn expand(
3635
span,
3736
kind: AllocatorKind::Global,
3837
global: item.ident,
39-
core: Ident::with_empty_ctxt(sym::core),
4038
cx: ecx,
4139
};
4240

43-
// We will generate a new submodule. To `use` the static from that module, we need to get
44-
// the `super::...` path.
45-
let super_path = f.cx.path(f.span, vec![Ident::with_empty_ctxt(kw::Super), f.global]);
46-
47-
// Generate the items in the submodule
48-
let mut items = vec![
49-
// import `core` to use allocators
50-
f.cx.item_extern_crate(f.span, f.core),
51-
// `use` the `global_allocator` in `super`
52-
f.cx.item_use_simple(
53-
f.span,
54-
respan(f.span.shrink_to_lo(), VisibilityKind::Inherited),
55-
super_path,
56-
),
57-
];
58-
59-
// Add the allocator methods to the submodule
60-
items.extend(
61-
ALLOCATOR_METHODS
62-
.iter()
63-
.map(|method| f.allocator_fn(method)),
64-
);
41+
// Generate item statements for the allocator methods.
42+
let stmts = ALLOCATOR_METHODS.iter().map(|method| f.allocator_fn(method)).collect();
6543

66-
// Generate the submodule itself
67-
let name = f.kind.fn_name("allocator_abi");
68-
let allocator_abi = Ident::from_str(&name).gensym();
69-
let module = f.cx.item_mod(span, span, allocator_abi, Vec::new(), items);
44+
// Generate anonymous constant serving as container for the allocator methods.
45+
let const_ty = ecx.ty(span, TyKind::Tup(Vec::new()));
46+
let const_body = ecx.expr_block(ecx.block(span, stmts));
47+
let const_item =
48+
ecx.item_const(span, Ident::with_empty_ctxt(kw::Underscore), const_ty, const_body);
7049

71-
// Return the item and new submodule
72-
vec![Annotatable::Item(item), Annotatable::Item(module)]
50+
// Return the original item and the new methods.
51+
vec![Annotatable::Item(item), Annotatable::Item(const_item)]
7352
}
7453

7554
struct AllocFnFactory<'a, 'b> {
7655
span: Span,
7756
kind: AllocatorKind,
7857
global: Ident,
79-
core: Ident,
8058
cx: &'b ExtCtxt<'a>,
8159
}
8260

8361
impl AllocFnFactory<'_, '_> {
84-
fn allocator_fn(&self, method: &AllocatorMethod) -> P<Item> {
62+
fn allocator_fn(&self, method: &AllocatorMethod) -> Stmt {
8563
let mut abi_args = Vec::new();
8664
let mut i = 0;
8765
let ref mut mk = || {
@@ -105,25 +83,22 @@ impl AllocFnFactory<'_, '_> {
10583
Generics::default(),
10684
self.cx.block_expr(output_expr),
10785
);
108-
self.cx.item(
86+
let item = self.cx.item(
10987
self.span,
11088
Ident::from_str(&self.kind.fn_name(method.name)),
11189
self.attrs(),
11290
kind,
113-
)
91+
);
92+
self.cx.stmt_item(self.span, item)
11493
}
11594

11695
fn call_allocator(&self, method: &str, mut args: Vec<P<Expr>>) -> P<Expr> {
117-
let method = self.cx.path(
118-
self.span,
119-
vec![
120-
self.core,
121-
Ident::from_str("alloc"),
122-
Ident::from_str("GlobalAlloc"),
123-
Ident::from_str(method),
124-
],
125-
);
126-
let method = self.cx.expr_path(method);
96+
let method = self.cx.std_path(&[
97+
Symbol::intern("alloc"),
98+
Symbol::intern("GlobalAlloc"),
99+
Symbol::intern(method),
100+
]);
101+
let method = self.cx.expr_path(self.cx.path(self.span, method));
127102
let allocator = self.cx.path_ident(self.span, self.global);
128103
let allocator = self.cx.expr_path(allocator);
129104
let allocator = self.cx.expr_addr_of(self.span, allocator);
@@ -153,16 +128,12 @@ impl AllocFnFactory<'_, '_> {
153128
args.push(self.cx.arg(self.span, size, ty_usize.clone()));
154129
args.push(self.cx.arg(self.span, align, ty_usize));
155130

156-
let layout_new = self.cx.path(
157-
self.span,
158-
vec![
159-
self.core,
160-
Ident::from_str("alloc"),
161-
Ident::from_str("Layout"),
162-
Ident::from_str("from_size_align_unchecked"),
163-
],
164-
);
165-
let layout_new = self.cx.expr_path(layout_new);
131+
let layout_new = self.cx.std_path(&[
132+
Symbol::intern("alloc"),
133+
Symbol::intern("Layout"),
134+
Symbol::intern("from_size_align_unchecked"),
135+
]);
136+
let layout_new = self.cx.expr_path(self.cx.path(self.span, layout_new));
166137
let size = self.cx.expr_ident(self.span, size);
167138
let align = self.cx.expr_ident(self.span, align);
168139
let layout = self.cx.expr_call(self.span, layout_new, vec![size, align]);

src/test/run-pass/allocator/custom-in-block.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ fn main() {
1313
#[global_allocator]
1414
pub static GLOBAL: A = A(AtomicUsize::new(0));
1515

16+
let n = GLOBAL.0.load(Ordering::SeqCst);
1617
let s = Box::new(0);
1718
helper::work_with(&s);
18-
assert_eq!(GLOBAL.0.load(Ordering::SeqCst), 1);
19+
assert_eq!(GLOBAL.0.load(Ordering::SeqCst), n + 1);
1920
drop(s);
20-
assert_eq!(GLOBAL.0.load(Ordering::SeqCst), 2);
21+
assert_eq!(GLOBAL.0.load(Ordering::SeqCst), n + 2);
2122
}

src/test/run-pass/allocator/custom-in-submodule.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ mod submodule {
1717
}
1818

1919
fn main() {
20+
let n = submodule::GLOBAL.0.load(Ordering::SeqCst);
2021
let s = Box::new(0);
2122
helper::work_with(&s);
22-
assert_eq!(submodule::GLOBAL.0.load(Ordering::SeqCst), 1);
23+
assert_eq!(submodule::GLOBAL.0.load(Ordering::SeqCst), n + 1);
2324
drop(s);
24-
assert_eq!(submodule::GLOBAL.0.load(Ordering::SeqCst), 2);
25+
assert_eq!(submodule::GLOBAL.0.load(Ordering::SeqCst), n + 2);
2526
}

0 commit comments

Comments
 (0)