Skip to content

Commit 84408ca

Browse files
committed
Point at type when a static #[global_allocator] doesn't impl GlobalAlloc
1 parent 06a6674 commit 84408ca

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

compiler/rustc_builtin_macros/src/global_allocator.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ pub fn expand(
2626

2727
// Allow using `#[global_allocator]` on an item statement
2828
// FIXME - if we get deref patterns, use them to reduce duplication here
29-
let (item, is_stmt) = match &item {
29+
let (item, is_stmt, ty_span) = match &item {
3030
Annotatable::Item(item) => match item.kind {
31-
ItemKind::Static(..) => (item, false),
31+
ItemKind::Static(ref ty, ..) => (item, false, ecx.with_def_site_ctxt(ty.span)),
3232
_ => return not_static(),
3333
},
3434
Annotatable::Stmt(stmt) => match &stmt.kind {
3535
StmtKind::Item(item_) => match item_.kind {
36-
ItemKind::Static(..) => (item_, true),
36+
ItemKind::Static(ref ty, ..) => (item_, true, ecx.with_def_site_ctxt(ty.span)),
3737
_ => return not_static(),
3838
},
3939
_ => return not_static(),
@@ -43,13 +43,14 @@ pub fn expand(
4343

4444
// Generate a bunch of new items using the AllocFnFactory
4545
let span = ecx.with_def_site_ctxt(item.span);
46-
let f = AllocFnFactory { span, kind: AllocatorKind::Global, global: item.ident, cx: ecx };
46+
let f =
47+
AllocFnFactory { span, ty_span, kind: AllocatorKind::Global, global: item.ident, cx: ecx };
4748

4849
// Generate item statements for the allocator methods.
4950
let stmts = ALLOCATOR_METHODS.iter().map(|method| f.allocator_fn(method)).collect();
5051

5152
// Generate anonymous constant serving as container for the allocator methods.
52-
let const_ty = ecx.ty(span, TyKind::Tup(Vec::new()));
53+
let const_ty = ecx.ty(ty_span, TyKind::Tup(Vec::new()));
5354
let const_body = ecx.expr_block(ecx.block(span, stmts));
5455
let const_item = ecx.item_const(span, Ident::new(kw::Underscore, span), const_ty, const_body);
5556
let const_item = if is_stmt {
@@ -64,6 +65,7 @@ pub fn expand(
6465

6566
struct AllocFnFactory<'a, 'b> {
6667
span: Span,
68+
ty_span: Span,
6769
kind: AllocatorKind,
6870
global: Ident,
6971
cx: &'b ExtCtxt<'a>,
@@ -97,18 +99,18 @@ impl AllocFnFactory<'_, '_> {
9799
self.attrs(),
98100
kind,
99101
);
100-
self.cx.stmt_item(self.span, item)
102+
self.cx.stmt_item(self.ty_span, item)
101103
}
102104

103105
fn call_allocator(&self, method: Symbol, mut args: Vec<P<Expr>>) -> P<Expr> {
104106
let method = self.cx.std_path(&[sym::alloc, sym::GlobalAlloc, method]);
105-
let method = self.cx.expr_path(self.cx.path(self.span, method));
106-
let allocator = self.cx.path_ident(self.span, self.global);
107+
let method = self.cx.expr_path(self.cx.path(self.ty_span, method));
108+
let allocator = self.cx.path_ident(self.ty_span, self.global);
107109
let allocator = self.cx.expr_path(allocator);
108-
let allocator = self.cx.expr_addr_of(self.span, allocator);
110+
let allocator = self.cx.expr_addr_of(self.ty_span, allocator);
109111
args.insert(0, allocator);
110112

111-
self.cx.expr_call(self.span, method, args)
113+
self.cx.expr_call(self.ty_span, method, args)
112114
}
113115

114116
fn attrs(&self) -> Vec<Attribute> {

src/test/ui/allocator/not-an-allocator.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
11
error[E0277]: the trait bound `usize: GlobalAlloc` is not satisfied
2-
--> $DIR/not-an-allocator.rs:2:1
2+
--> $DIR/not-an-allocator.rs:2:11
33
|
44
LL | #[global_allocator]
55
| ------------------- in this procedural macro expansion
66
LL | static A: usize = 0;
7-
| ^^^^^^^^^^^^^^^^^^^^ the trait `GlobalAlloc` is not implemented for `usize`
7+
| ^^^^^ the trait `GlobalAlloc` is not implemented for `usize`
88
|
99
= note: this error originates in the attribute macro `global_allocator` (in Nightly builds, run with -Z macro-backtrace for more info)
1010

1111
error[E0277]: the trait bound `usize: GlobalAlloc` is not satisfied
12-
--> $DIR/not-an-allocator.rs:2:1
12+
--> $DIR/not-an-allocator.rs:2:11
1313
|
1414
LL | #[global_allocator]
1515
| ------------------- in this procedural macro expansion
1616
LL | static A: usize = 0;
17-
| ^^^^^^^^^^^^^^^^^^^^ the trait `GlobalAlloc` is not implemented for `usize`
17+
| ^^^^^ the trait `GlobalAlloc` is not implemented for `usize`
1818
|
1919
= note: this error originates in the attribute macro `global_allocator` (in Nightly builds, run with -Z macro-backtrace for more info)
2020

2121
error[E0277]: the trait bound `usize: GlobalAlloc` is not satisfied
22-
--> $DIR/not-an-allocator.rs:2:1
22+
--> $DIR/not-an-allocator.rs:2:11
2323
|
2424
LL | #[global_allocator]
2525
| ------------------- in this procedural macro expansion
2626
LL | static A: usize = 0;
27-
| ^^^^^^^^^^^^^^^^^^^^ the trait `GlobalAlloc` is not implemented for `usize`
27+
| ^^^^^ the trait `GlobalAlloc` is not implemented for `usize`
2828
|
2929
= note: this error originates in the attribute macro `global_allocator` (in Nightly builds, run with -Z macro-backtrace for more info)
3030

3131
error[E0277]: the trait bound `usize: GlobalAlloc` is not satisfied
32-
--> $DIR/not-an-allocator.rs:2:1
32+
--> $DIR/not-an-allocator.rs:2:11
3333
|
3434
LL | #[global_allocator]
3535
| ------------------- in this procedural macro expansion
3636
LL | static A: usize = 0;
37-
| ^^^^^^^^^^^^^^^^^^^^ the trait `GlobalAlloc` is not implemented for `usize`
37+
| ^^^^^ the trait `GlobalAlloc` is not implemented for `usize`
3838
|
3939
= note: this error originates in the attribute macro `global_allocator` (in Nightly builds, run with -Z macro-backtrace for more info)
4040

0 commit comments

Comments
 (0)