1
Fork 0

Invoke attributes on the statement for statement items

This commit is contained in:
Aaron Hill 2020-11-24 14:47:49 -05:00
parent e9546bdbaf
commit 9c9f40656d
No known key found for this signature in database
GPG key ID: B4087E510E98B164
11 changed files with 574 additions and 9 deletions

View file

@ -234,6 +234,15 @@ impl Annotatable {
pub fn derive_allowed(&self) -> bool {
match *self {
Annotatable::Stmt(ref stmt) => match stmt.kind {
ast::StmtKind::Item(ref item) => match item.kind {
ast::ItemKind::Struct(..)
| ast::ItemKind::Enum(..)
| ast::ItemKind::Union(..) => true,
_ => false,
},
_ => false,
},
Annotatable::Item(ref item) => match item.kind {
ast::ItemKind::Struct(..) | ast::ItemKind::Enum(..) | ast::ItemKind::Union(..) => {
true

View file

@ -795,7 +795,14 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
| Annotatable::TraitItem(_)
| Annotatable::ImplItem(_)
| Annotatable::ForeignItem(_) => return,
Annotatable::Stmt(_) => "statements",
Annotatable::Stmt(stmt) => {
// Attributes are stable on item statements,
// but unstable on all other kinds of statements
if stmt.is_item() {
return;
}
"statements"
}
Annotatable::Expr(_) => "expressions",
Annotatable::Arm(..)
| Annotatable::Field(..)
@ -1266,9 +1273,19 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
// we'll expand attributes on expressions separately
if !stmt.is_expr() {
// FIXME: Handle custom attributes on statements (#15701).
let attr =
if stmt.is_item() { None } else { self.take_first_attr_no_derive(&mut stmt) };
let attr = if stmt.is_item() {
// FIXME: Implement proper token collection for statements
if let StmtKind::Item(item) = &mut stmt.kind {
stmt.tokens = item.tokens.take()
} else {
unreachable!()
};
self.take_first_attr(&mut stmt)
} else {
// Ignore derives on non-item statements for backwards compatibility.
// This will result in a unused attribute warning
self.take_first_attr_no_derive(&mut stmt)
};
if let Some(attr) = attr {
return self

View file

@ -1,6 +1,7 @@
use crate::base::{self, *};
use crate::proc_macro_server;
use rustc_ast::ptr::P;
use rustc_ast::token;
use rustc_ast::tokenstream::{TokenStream, TokenTree};
use rustc_ast::{self as ast, *};
@ -74,8 +75,20 @@ impl MultiItemModifier for ProcMacroDerive {
_meta_item: &ast::MetaItem,
item: Annotatable,
) -> ExpandResult<Vec<Annotatable>, Annotatable> {
// We need special handling for statement items
// (e.g. `fn foo() { #[derive(Debug)] struct Bar; }`)
let mut is_stmt = false;
let item = match item {
Annotatable::Item(item) => token::NtItem(item),
Annotatable::Stmt(stmt) => {
is_stmt = true;
assert!(stmt.is_item());
// A proc macro can't observe the fact that we're passing
// them an `NtStmt` - it can only see the underlying tokens
// of the wrapped item
token::NtStmt(stmt.into_inner())
}
_ => unreachable!(),
};
let input = if item.pretty_printing_compatibility_hack() {
@ -106,7 +119,13 @@ impl MultiItemModifier for ProcMacroDerive {
loop {
match parser.parse_item() {
Ok(None) => break,
Ok(Some(item)) => items.push(Annotatable::Item(item)),
Ok(Some(item)) => {
if is_stmt {
items.push(Annotatable::Stmt(P(ecx.stmt_item(span, item))));
} else {
items.push(Annotatable::Item(item));
}
}
Err(mut err) => {
err.emit();
break;