Skip to content

Commit 192c7db

Browse files
committed
Auto merge of #79326 - Aaron1011:fix/builtin-macro-stmt, r=petrochenkov
Always invoke statement attributes on the statement itself This is preparation for PR #78296, which will require us to handle statement items in addition to normal items.
2 parents db79d2f + baefba8 commit 192c7db

15 files changed

+658
-30
lines changed

compiler/rustc_ast/src/token.rs

+14-7
Original file line numberDiff line numberDiff line change
@@ -785,13 +785,20 @@ impl Nonterminal {
785785
/// See issue #73345 for more details.
786786
/// FIXME(#73933): Remove this eventually.
787787
pub fn pretty_printing_compatibility_hack(&self) -> bool {
788-
if let NtItem(item) = self {
789-
let name = item.ident.name;
790-
if name == sym::ProceduralMasqueradeDummyType || name == sym::ProcMacroHack {
791-
if let ast::ItemKind::Enum(enum_def, _) = &item.kind {
792-
if let [variant] = &*enum_def.variants {
793-
return variant.ident.name == sym::Input;
794-
}
788+
let item = match self {
789+
NtItem(item) => item,
790+
NtStmt(stmt) => match &stmt.kind {
791+
ast::StmtKind::Item(item) => item,
792+
_ => return false,
793+
},
794+
_ => return false,
795+
};
796+
797+
let name = item.ident.name;
798+
if name == sym::ProceduralMasqueradeDummyType || name == sym::ProcMacroHack {
799+
if let ast::ItemKind::Enum(enum_def, _) = &item.kind {
800+
if let [variant] = &*enum_def.variants {
801+
return variant.ident.name == sym::Input;
795802
}
796803
}
797804
}

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 {

compiler/rustc_expand/src/base.rs

+9
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,15 @@ impl Annotatable {
234234

235235
pub fn derive_allowed(&self) -> bool {
236236
match *self {
237+
Annotatable::Stmt(ref stmt) => match stmt.kind {
238+
ast::StmtKind::Item(ref item) => match item.kind {
239+
ast::ItemKind::Struct(..)
240+
| ast::ItemKind::Enum(..)
241+
| ast::ItemKind::Union(..) => true,
242+
_ => false,
243+
},
244+
_ => false,
245+
},
237246
Annotatable::Item(ref item) => match item.kind {
238247
ast::ItemKind::Struct(..) | ast::ItemKind::Enum(..) | ast::ItemKind::Union(..) => {
239248
true

compiler/rustc_expand/src/expand.rs

+21-4
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,14 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
795795
| Annotatable::TraitItem(_)
796796
| Annotatable::ImplItem(_)
797797
| Annotatable::ForeignItem(_) => return,
798-
Annotatable::Stmt(_) => "statements",
798+
Annotatable::Stmt(stmt) => {
799+
// Attributes are stable on item statements,
800+
// but unstable on all other kinds of statements
801+
if stmt.is_item() {
802+
return;
803+
}
804+
"statements"
805+
}
799806
Annotatable::Expr(_) => "expressions",
800807
Annotatable::Arm(..)
801808
| Annotatable::Field(..)
@@ -1266,9 +1273,19 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
12661273

12671274
// we'll expand attributes on expressions separately
12681275
if !stmt.is_expr() {
1269-
// FIXME: Handle custom attributes on statements (#15701).
1270-
let attr =
1271-
if stmt.is_item() { None } else { self.take_first_attr_no_derive(&mut stmt) };
1276+
let attr = if stmt.is_item() {
1277+
// FIXME: Implement proper token collection for statements
1278+
if let StmtKind::Item(item) = &mut stmt.kind {
1279+
stmt.tokens = item.tokens.take()
1280+
} else {
1281+
unreachable!()
1282+
};
1283+
self.take_first_attr(&mut stmt)
1284+
} else {
1285+
// Ignore derives on non-item statements for backwards compatibility.
1286+
// This will result in a unused attribute warning
1287+
self.take_first_attr_no_derive(&mut stmt)
1288+
};
12721289

12731290
if let Some(attr) = attr {
12741291
return self

compiler/rustc_expand/src/proc_macro.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::base::{self, *};
22
use crate::proc_macro_server;
33

4+
use rustc_ast::ptr::P;
45
use rustc_ast::token;
56
use rustc_ast::tokenstream::{TokenStream, TokenTree};
67
use rustc_ast::{self as ast, *};
@@ -74,8 +75,20 @@ impl MultiItemModifier for ProcMacroDerive {
7475
_meta_item: &ast::MetaItem,
7576
item: Annotatable,
7677
) -> ExpandResult<Vec<Annotatable>, Annotatable> {
78+
// We need special handling for statement items
79+
// (e.g. `fn foo() { #[derive(Debug)] struct Bar; }`)
80+
let mut is_stmt = false;
7781
let item = match item {
7882
Annotatable::Item(item) => token::NtItem(item),
83+
Annotatable::Stmt(stmt) => {
84+
is_stmt = true;
85+
assert!(stmt.is_item());
86+
87+
// A proc macro can't observe the fact that we're passing
88+
// them an `NtStmt` - it can only see the underlying tokens
89+
// of the wrapped item
90+
token::NtStmt(stmt.into_inner())
91+
}
7992
_ => unreachable!(),
8093
};
8194
let input = if item.pretty_printing_compatibility_hack() {
@@ -106,7 +119,13 @@ impl MultiItemModifier for ProcMacroDerive {
106119
loop {
107120
match parser.parse_item() {
108121
Ok(None) => break,
109-
Ok(Some(item)) => items.push(Annotatable::Item(item)),
122+
Ok(Some(item)) => {
123+
if is_stmt {
124+
items.push(Annotatable::Stmt(P(ecx.stmt_item(span, item))));
125+
} else {
126+
items.push(Annotatable::Item(item));
127+
}
128+
}
110129
Err(mut err) => {
111130
err.emit();
112131
break;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// aux-build:attr-stmt-expr.rs
2+
// aux-build:test-macros.rs
3+
// compile-flags: -Z span-debug
4+
// check-pass
5+
6+
#![feature(proc_macro_hygiene)]
7+
#![feature(stmt_expr_attributes)]
8+
#![feature(rustc_attrs)]
9+
#![allow(dead_code)]
10+
11+
#![no_std] // Don't load unnecessary hygiene information from std
12+
extern crate std;
13+
14+
extern crate attr_stmt_expr;
15+
extern crate test_macros;
16+
use attr_stmt_expr::{expect_let, expect_print_stmt, expect_expr, expect_print_expr};
17+
use test_macros::print_attr;
18+
use std::println;
19+
20+
fn print_str(string: &'static str) {
21+
// macros are handled a bit differently
22+
#[expect_print_expr]
23+
println!("{}", string)
24+
}
25+
26+
macro_rules! make_stmt {
27+
($stmt:stmt) => {
28+
$stmt
29+
}
30+
}
31+
32+
macro_rules! second_make_stmt {
33+
($stmt:stmt) => {
34+
make_stmt!($stmt);
35+
}
36+
}
37+
38+
39+
fn main() {
40+
make_stmt!(struct Foo {});
41+
42+
#[print_attr]
43+
#[expect_let]
44+
let string = "Hello, world!";
45+
46+
#[print_attr]
47+
#[expect_print_stmt]
48+
println!("{}", string);
49+
50+
#[print_attr]
51+
second_make_stmt!(#[allow(dead_code)] struct Bar {});
52+
53+
#[print_attr]
54+
#[rustc_dummy]
55+
struct Other {};
56+
57+
#[expect_expr]
58+
print_str("string")
59+
}

0 commit comments

Comments
 (0)