Skip to content

Commit e9546bd

Browse files
committed
Handle Annotatable::Stmt in some builtin macros
This is preparation for PR #78296, which will require us to handle statement items in addition to normal items.
1 parent 25a6910 commit e9546bd

File tree

3 files changed

+70
-14
lines changed

3 files changed

+70
-14
lines changed

compiler/rustc_builtin_macros/src/deriving/mod.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,27 @@ impl MultiItemModifier for BuiltinDerive {
5454
// so we are doing it here in a centralized way.
5555
let span = ecx.with_def_site_ctxt(span);
5656
let mut items = Vec::new();
57-
(self.0)(ecx, span, meta_item, &item, &mut |a| items.push(a));
57+
match item {
58+
Annotatable::Stmt(stmt) => {
59+
if let ast::StmtKind::Item(item) = stmt.into_inner().kind {
60+
(self.0)(ecx, span, meta_item, &Annotatable::Item(item), &mut |a| {
61+
// Cannot use 'ecx.stmt_item' here, because we need to pass 'ecx'
62+
// to the function
63+
items.push(Annotatable::Stmt(P(ast::Stmt {
64+
id: ast::DUMMY_NODE_ID,
65+
kind: ast::StmtKind::Item(a.expect_item()),
66+
span,
67+
tokens: None,
68+
})));
69+
});
70+
} else {
71+
unreachable!("should have already errored on non-item statement")
72+
}
73+
}
74+
_ => {
75+
(self.0)(ecx, span, meta_item, &item, &mut |a| items.push(a));
76+
}
77+
}
5878
ExpandResult::Ready(items)
5979
}
6080
}

compiler/rustc_builtin_macros/src/global_allocator.rs

+19-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_ast::expand::allocator::{
44
AllocatorKind, AllocatorMethod, AllocatorTy, ALLOCATOR_METHODS,
55
};
66
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};
88
use rustc_ast::{ItemKind, Mutability, Stmt, Ty, TyKind, Unsafe};
99
use rustc_expand::base::{Annotatable, ExtCtxt};
1010
use rustc_span::symbol::{kw, sym, Ident, Symbol};
@@ -14,14 +14,25 @@ pub fn expand(
1414
ecx: &mut ExtCtxt<'_>,
1515
_span: Span,
1616
meta_item: &ast::MetaItem,
17-
item: Annotatable,
17+
mut item: Annotatable,
1818
) -> Vec<Annotatable> {
1919
check_builtin_macro_attribute(ecx, meta_item, sym::global_allocator);
2020

2121
let not_static = |item: Annotatable| {
2222
ecx.sess.parse_sess.span_diagnostic.span_err(item.span(), "allocators must be statics");
2323
vec![item]
2424
};
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+
2536
let item = match item {
2637
Annotatable::Item(item) => match item.kind {
2738
ItemKind::Static(..) => item,
@@ -41,9 +52,14 @@ pub fn expand(
4152
let const_ty = ecx.ty(span, TyKind::Tup(Vec::new()));
4253
let const_body = ecx.expr_block(ecx.block(span, stmts));
4354
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+
};
4460

4561
// Return the original item and the new methods.
46-
vec![Annotatable::Item(item), Annotatable::Item(const_item)]
62+
vec![orig_item, const_item]
4763
}
4864

4965
struct AllocFnFactory<'a, 'b> {

compiler/rustc_builtin_macros/src/test.rs

+30-10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::util::check_builtin_macro_attribute;
44

55
use rustc_ast as ast;
66
use rustc_ast::attr;
7+
use rustc_ast::ptr::P;
78
use rustc_ast_pretty::pprust;
89
use rustc_expand::base::*;
910
use rustc_session::Session;
@@ -78,8 +79,16 @@ pub fn expand_test_or_bench(
7879
return vec![];
7980
}
8081

81-
let item = match item {
82-
Annotatable::Item(i) => i,
82+
let (item, is_stmt) = match item {
83+
Annotatable::Item(i) => (i, false),
84+
Annotatable::Stmt(stmt) if matches!(stmt.kind, ast::StmtKind::Item(_)) => {
85+
// FIXME: Use an 'if let' guard once they are implemented
86+
if let ast::StmtKind::Item(i) = stmt.into_inner().kind {
87+
(i, true)
88+
} else {
89+
unreachable!()
90+
}
91+
}
8392
other => {
8493
cx.struct_span_err(
8594
other.span(),
@@ -304,14 +313,25 @@ pub fn expand_test_or_bench(
304313

305314
tracing::debug!("synthetic test item:\n{}\n", pprust::item_to_string(&test_const));
306315

307-
vec![
308-
// Access to libtest under a hygienic name
309-
Annotatable::Item(test_extern),
310-
// The generated test case
311-
Annotatable::Item(test_const),
312-
// The original item
313-
Annotatable::Item(item),
314-
]
316+
if is_stmt {
317+
vec![
318+
// Access to libtest under a hygienic name
319+
Annotatable::Stmt(P(cx.stmt_item(sp, test_extern))),
320+
// The generated test case
321+
Annotatable::Stmt(P(cx.stmt_item(sp, test_const))),
322+
// The original item
323+
Annotatable::Stmt(P(cx.stmt_item(sp, item))),
324+
]
325+
} else {
326+
vec![
327+
// Access to libtest under a hygienic name
328+
Annotatable::Item(test_extern),
329+
// The generated test case
330+
Annotatable::Item(test_const),
331+
// The original item
332+
Annotatable::Item(item),
333+
]
334+
}
315335
}
316336

317337
fn item_path(mod_path: &[Ident], item_ident: &Ident) -> String {

0 commit comments

Comments
 (0)