Skip to content

Commit

Permalink
Invoke attributes on the statement for statement items
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron1011 committed Nov 24, 2020
1 parent e9546bd commit 9c9f406
Show file tree
Hide file tree
Showing 11 changed files with 574 additions and 9 deletions.
9 changes: 9 additions & 0 deletions compiler/rustc_expand/src/base.rs
Expand Up @@ -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
Expand Down
25 changes: 21 additions & 4 deletions compiler/rustc_expand/src/expand.rs
Expand Up @@ -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(..)
Expand Down Expand Up @@ -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
Expand Down
21 changes: 20 additions & 1 deletion compiler/rustc_expand/src/proc_macro.rs
@@ -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, *};
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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;
Expand Down
59 changes: 59 additions & 0 deletions src/test/ui/proc-macro/allowed-attr-stmt-expr.rs
@@ -0,0 +1,59 @@
// aux-build:attr-stmt-expr.rs
// aux-build:test-macros.rs
// compile-flags: -Z span-debug
// check-pass

#![feature(proc_macro_hygiene)]
#![feature(stmt_expr_attributes)]
#![feature(rustc_attrs)]
#![allow(dead_code)]

#![no_std] // Don't load unnecessary hygiene information from std
extern crate std;

extern crate attr_stmt_expr;
extern crate test_macros;
use attr_stmt_expr::{expect_let, expect_print_stmt, expect_expr, expect_print_expr};
use test_macros::print_attr;
use std::println;

fn print_str(string: &'static str) {
// macros are handled a bit differently
#[expect_print_expr]
println!("{}", string)
}

macro_rules! make_stmt {
($stmt:stmt) => {
$stmt
}
}

macro_rules! second_make_stmt {
($stmt:stmt) => {
make_stmt!($stmt);
}
}


fn main() {
make_stmt!(struct Foo {});

#[print_attr]
#[expect_let]
let string = "Hello, world!";

#[print_attr]
#[expect_print_stmt]
println!("{}", string);

#[print_attr]
second_make_stmt!(#[allow(dead_code)] struct Bar {});

#[print_attr]
#[rustc_dummy]
struct Other {};

#[expect_expr]
print_str("string")
}
187 changes: 187 additions & 0 deletions src/test/ui/proc-macro/allowed-attr-stmt-expr.stdout
@@ -0,0 +1,187 @@
PRINT-ATTR INPUT (DISPLAY): #[expect_let] let string = "Hello, world!" ;
PRINT-ATTR INPUT (DEBUG): TokenStream [
Punct {
ch: '#',
spacing: Alone,
span: $DIR/allowed-attr-stmt-expr.rs:1:1: 1:1 (#0),
},
Group {
delimiter: Bracket,
stream: TokenStream [
Ident {
ident: "expect_let",
span: $DIR/allowed-attr-stmt-expr.rs:1:1: 1:1 (#0),
},
],
span: $DIR/allowed-attr-stmt-expr.rs:1:1: 1:1 (#0),
},
Ident {
ident: "let",
span: $DIR/allowed-attr-stmt-expr.rs:1:1: 1:1 (#0),
},
Ident {
ident: "string",
span: $DIR/allowed-attr-stmt-expr.rs:1:1: 1:1 (#0),
},
Punct {
ch: '=',
spacing: Alone,
span: $DIR/allowed-attr-stmt-expr.rs:1:1: 1:1 (#0),
},
Literal {
kind: Str,
symbol: "Hello, world!",
suffix: None,
span: $DIR/allowed-attr-stmt-expr.rs:1:1: 1:1 (#0),
},
Punct {
ch: ';',
spacing: Alone,
span: $DIR/allowed-attr-stmt-expr.rs:1:1: 1:1 (#0),
},
]
PRINT-ATTR INPUT (DISPLAY): #[expect_print_stmt] println ! ("{}", string) ;
PRINT-ATTR INPUT (DEBUG): TokenStream [
Punct {
ch: '#',
spacing: Alone,
span: $DIR/allowed-attr-stmt-expr.rs:1:1: 1:1 (#0),
},
Group {
delimiter: Bracket,
stream: TokenStream [
Ident {
ident: "expect_print_stmt",
span: $DIR/allowed-attr-stmt-expr.rs:1:1: 1:1 (#0),
},
],
span: $DIR/allowed-attr-stmt-expr.rs:1:1: 1:1 (#0),
},
Ident {
ident: "println",
span: $DIR/allowed-attr-stmt-expr.rs:1:1: 1:1 (#0),
},
Punct {
ch: '!',
spacing: Alone,
span: $DIR/allowed-attr-stmt-expr.rs:1:1: 1:1 (#0),
},
Group {
delimiter: Parenthesis,
stream: TokenStream [
Literal {
kind: Str,
symbol: "{}",
suffix: None,
span: $DIR/allowed-attr-stmt-expr.rs:1:1: 1:1 (#0),
},
Punct {
ch: ',',
spacing: Alone,
span: $DIR/allowed-attr-stmt-expr.rs:1:1: 1:1 (#0),
},
Ident {
ident: "string",
span: $DIR/allowed-attr-stmt-expr.rs:1:1: 1:1 (#0),
},
],
span: $DIR/allowed-attr-stmt-expr.rs:1:1: 1:1 (#0),
},
Punct {
ch: ';',
spacing: Alone,
span: $DIR/allowed-attr-stmt-expr.rs:1:1: 1:1 (#0),
},
]
PRINT-ATTR INPUT (DISPLAY): second_make_stmt ! (#[allow(dead_code)] struct Bar { }) ;
PRINT-ATTR INPUT (DEBUG): TokenStream [
Ident {
ident: "second_make_stmt",
span: $DIR/allowed-attr-stmt-expr.rs:1:1: 1:1 (#0),
},
Punct {
ch: '!',
spacing: Alone,
span: $DIR/allowed-attr-stmt-expr.rs:1:1: 1:1 (#0),
},
Group {
delimiter: Parenthesis,
stream: TokenStream [
Punct {
ch: '#',
spacing: Alone,
span: $DIR/allowed-attr-stmt-expr.rs:1:1: 1:1 (#0),
},
Group {
delimiter: Bracket,
stream: TokenStream [
Ident {
ident: "allow",
span: $DIR/allowed-attr-stmt-expr.rs:1:1: 1:1 (#0),
},
Group {
delimiter: Parenthesis,
stream: TokenStream [
Ident {
ident: "dead_code",
span: $DIR/allowed-attr-stmt-expr.rs:1:1: 1:1 (#0),
},
],
span: $DIR/allowed-attr-stmt-expr.rs:1:1: 1:1 (#0),
},
],
span: $DIR/allowed-attr-stmt-expr.rs:1:1: 1:1 (#0),
},
Ident {
ident: "struct",
span: $DIR/allowed-attr-stmt-expr.rs:1:1: 1:1 (#0),
},
Ident {
ident: "Bar",
span: $DIR/allowed-attr-stmt-expr.rs:1:1: 1:1 (#0),
},
Group {
delimiter: Brace,
stream: TokenStream [],
span: $DIR/allowed-attr-stmt-expr.rs:1:1: 1:1 (#0),
},
],
span: $DIR/allowed-attr-stmt-expr.rs:1:1: 1:1 (#0),
},
Punct {
ch: ';',
spacing: Alone,
span: $DIR/allowed-attr-stmt-expr.rs:1:1: 1:1 (#0),
},
]
PRINT-ATTR INPUT (DISPLAY): #[rustc_dummy] struct Other { }
PRINT-ATTR INPUT (DEBUG): TokenStream [
Punct {
ch: '#',
spacing: Alone,
span: $DIR/allowed-attr-stmt-expr.rs:1:1: 1:1 (#0),
},
Group {
delimiter: Bracket,
stream: TokenStream [
Ident {
ident: "rustc_dummy",
span: $DIR/allowed-attr-stmt-expr.rs:1:1: 1:1 (#0),
},
],
span: $DIR/allowed-attr-stmt-expr.rs:1:1: 1:1 (#0),
},
Ident {
ident: "struct",
span: $DIR/allowed-attr-stmt-expr.rs:1:1: 1:1 (#0),
},
Ident {
ident: "Other",
span: $DIR/allowed-attr-stmt-expr.rs:1:1: 1:1 (#0),
},
Group {
delimiter: Brace,
stream: TokenStream [],
span: $DIR/allowed-attr-stmt-expr.rs:1:1: 1:1 (#0),
},
]

0 comments on commit 9c9f406

Please sign in to comment.