@@ -4,7 +4,7 @@ use rustc_ast::expand::allocator::{
4
4
AllocatorKind , AllocatorMethod , AllocatorTy , ALLOCATOR_METHODS ,
5
5
} ;
6
6
use rustc_ast:: ptr:: P ;
7
- use rustc_ast:: { self as ast, Attribute , Expr , FnHeader , FnSig , Generics , Param } ;
7
+ use rustc_ast:: { self as ast, Attribute , Expr , FnHeader , FnSig , Generics , Param , StmtKind } ;
8
8
use rustc_ast:: { ItemKind , Mutability , Stmt , Ty , TyKind , Unsafe } ;
9
9
use rustc_expand:: base:: { Annotatable , ExtCtxt } ;
10
10
use rustc_span:: symbol:: { kw, sym, Ident , Symbol } ;
@@ -14,14 +14,25 @@ pub fn expand(
14
14
ecx : & mut ExtCtxt < ' _ > ,
15
15
_span : Span ,
16
16
meta_item : & ast:: MetaItem ,
17
- item : Annotatable ,
17
+ mut item : Annotatable ,
18
18
) -> Vec < Annotatable > {
19
19
check_builtin_macro_attribute ( ecx, meta_item, sym:: global_allocator) ;
20
20
21
21
let not_static = |item : Annotatable | {
22
22
ecx. sess . parse_sess . span_diagnostic . span_err ( item. span ( ) , "allocators must be statics" ) ;
23
23
vec ! [ item]
24
24
} ;
25
+ let orig_item = item. clone ( ) ;
26
+ let mut is_stmt = false ;
27
+
28
+ // Allow using `#[global_allocator]` on an item statement
29
+ if let Annotatable :: Stmt ( stmt) = & item {
30
+ if let StmtKind :: Item ( item_) = & stmt. kind {
31
+ item = Annotatable :: Item ( item_. clone ( ) ) ;
32
+ is_stmt = true ;
33
+ }
34
+ }
35
+
25
36
let item = match item {
26
37
Annotatable :: Item ( item) => match item. kind {
27
38
ItemKind :: Static ( ..) => item,
@@ -41,9 +52,14 @@ pub fn expand(
41
52
let const_ty = ecx. ty ( span, TyKind :: Tup ( Vec :: new ( ) ) ) ;
42
53
let const_body = ecx. expr_block ( ecx. block ( span, stmts) ) ;
43
54
let const_item = ecx. item_const ( span, Ident :: new ( kw:: Underscore , span) , const_ty, const_body) ;
55
+ let const_item = if is_stmt {
56
+ Annotatable :: Stmt ( P ( ecx. stmt_item ( span, const_item) ) )
57
+ } else {
58
+ Annotatable :: Item ( const_item)
59
+ } ;
44
60
45
61
// Return the original item and the new methods.
46
- vec ! [ Annotatable :: Item ( item ) , Annotatable :: Item ( const_item) ]
62
+ vec ! [ orig_item , const_item]
47
63
}
48
64
49
65
struct AllocFnFactory < ' a , ' b > {
0 commit comments