Skip to content

Commit

Permalink
Auto merge of #84016 - Aaron1011:new-preexp-tests, r=petrochenkov
Browse files Browse the repository at this point in the history
Add some proc-macro attribute token handling tests

Split out from #82608

r? `@petrochenkov`
  • Loading branch information
bors committed Apr 10, 2021
2 parents 72c63de + 6c59111 commit a836d9b
Show file tree
Hide file tree
Showing 10 changed files with 1,199 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use proc_macro::{TokenStream, quote};
#[proc_macro_attribute]
pub fn first_attr(_: TokenStream, input: TokenStream) -> TokenStream {
let recollected: TokenStream = input.into_iter().collect();
println!("First recollected: {:#?}", recollected);
quote! {
#[second_attr]
$recollected
Expand All @@ -18,6 +19,7 @@ pub fn first_attr(_: TokenStream, input: TokenStream) -> TokenStream {

#[proc_macro_attribute]
pub fn second_attr(_: TokenStream, input: TokenStream) -> TokenStream {
let _recollected: TokenStream = input.into_iter().collect();
let recollected: TokenStream = input.into_iter().collect();
println!("Second recollected: {:#?}", recollected);
TokenStream::new()
}
39 changes: 39 additions & 0 deletions src/test/ui/proc-macro/cfg-eval-inner.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// compile-flags: -Z span-debug
// aux-build:test-macros.rs
// check-pass

#![feature(cfg_eval)]
#![feature(custom_inner_attributes)]
#![feature(stmt_expr_attributes)]
#![feature(rustc_attrs)]

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

#[macro_use]
extern crate test_macros;

struct Foo<T>(T);

impl Foo<[u8; {
#![cfg_attr(not(FALSE), rustc_dummy(cursed_inner))]
#![allow(unused)]
struct Inner {
field: [u8; {
#![cfg_attr(not(FALSE), rustc_dummy(another_cursed_inner))]
1
}]
}

0
}]> {
#![cfg_eval]
#![print_attr]
#![cfg_attr(not(FALSE), rustc_dummy(evaluated_attr))]

fn bar() {
#[cfg(FALSE)] let a = 1;
}
}

fn main() {}
252 changes: 252 additions & 0 deletions src/test/ui/proc-macro/cfg-eval-inner.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
PRINT-ATTR INPUT (DISPLAY): impl Foo <
[u8 ;
{
# ! [rustc_dummy(cursed_inner)] # ! [allow(unused)] struct Inner
{ field : [u8 ; { # ! [rustc_dummy(another_cursed_inner)] 1 }], } 0
}] > { # ! [rustc_dummy(evaluated_attr)] fn bar() { } }
PRINT-ATTR INPUT (DEBUG): TokenStream [
Ident {
ident: "impl",
span: $DIR/cfg-eval-inner.rs:18:1: 37:2 (#0),
},
Ident {
ident: "Foo",
span: $DIR/cfg-eval-inner.rs:18:1: 37:2 (#0),
},
Punct {
ch: '<',
spacing: Alone,
span: $DIR/cfg-eval-inner.rs:18:1: 37:2 (#0),
},
Group {
delimiter: Bracket,
stream: TokenStream [
Ident {
ident: "u8",
span: $DIR/cfg-eval-inner.rs:18:1: 37:2 (#0),
},
Punct {
ch: ';',
spacing: Alone,
span: $DIR/cfg-eval-inner.rs:18:1: 37:2 (#0),
},
Group {
delimiter: Brace,
stream: TokenStream [
Punct {
ch: '#',
spacing: Joint,
span: $DIR/cfg-eval-inner.rs:18:1: 37:2 (#0),
},
Punct {
ch: '!',
spacing: Alone,
span: $DIR/cfg-eval-inner.rs:18:1: 37:2 (#0),
},
Group {
delimiter: Bracket,
stream: TokenStream [
Ident {
ident: "rustc_dummy",
span: $DIR/cfg-eval-inner.rs:18:1: 37:2 (#0),
},
Group {
delimiter: Parenthesis,
stream: TokenStream [
Ident {
ident: "cursed_inner",
span: $DIR/cfg-eval-inner.rs:18:1: 37:2 (#0),
},
],
span: $DIR/cfg-eval-inner.rs:18:1: 37:2 (#0),
},
],
span: $DIR/cfg-eval-inner.rs:18:1: 37:2 (#0),
},
Punct {
ch: '#',
spacing: Joint,
span: $DIR/cfg-eval-inner.rs:18:1: 37:2 (#0),
},
Punct {
ch: '!',
spacing: Alone,
span: $DIR/cfg-eval-inner.rs:18:1: 37:2 (#0),
},
Group {
delimiter: Bracket,
stream: TokenStream [
Ident {
ident: "allow",
span: $DIR/cfg-eval-inner.rs:18:1: 37:2 (#0),
},
Group {
delimiter: Parenthesis,
stream: TokenStream [
Ident {
ident: "unused",
span: $DIR/cfg-eval-inner.rs:18:1: 37:2 (#0),
},
],
span: $DIR/cfg-eval-inner.rs:18:1: 37:2 (#0),
},
],
span: $DIR/cfg-eval-inner.rs:18:1: 37:2 (#0),
},
Ident {
ident: "struct",
span: $DIR/cfg-eval-inner.rs:18:1: 37:2 (#0),
},
Ident {
ident: "Inner",
span: $DIR/cfg-eval-inner.rs:18:1: 37:2 (#0),
},
Group {
delimiter: Brace,
stream: TokenStream [
Ident {
ident: "field",
span: $DIR/cfg-eval-inner.rs:18:1: 37:2 (#0),
},
Punct {
ch: ':',
spacing: Alone,
span: $DIR/cfg-eval-inner.rs:18:1: 37:2 (#0),
},
Group {
delimiter: Bracket,
stream: TokenStream [
Ident {
ident: "u8",
span: $DIR/cfg-eval-inner.rs:18:1: 37:2 (#0),
},
Punct {
ch: ';',
spacing: Alone,
span: $DIR/cfg-eval-inner.rs:18:1: 37:2 (#0),
},
Group {
delimiter: Brace,
stream: TokenStream [
Punct {
ch: '#',
spacing: Joint,
span: $DIR/cfg-eval-inner.rs:18:1: 37:2 (#0),
},
Punct {
ch: '!',
spacing: Alone,
span: $DIR/cfg-eval-inner.rs:18:1: 37:2 (#0),
},
Group {
delimiter: Bracket,
stream: TokenStream [
Ident {
ident: "rustc_dummy",
span: $DIR/cfg-eval-inner.rs:18:1: 37:2 (#0),
},
Group {
delimiter: Parenthesis,
stream: TokenStream [
Ident {
ident: "another_cursed_inner",
span: $DIR/cfg-eval-inner.rs:18:1: 37:2 (#0),
},
],
span: $DIR/cfg-eval-inner.rs:18:1: 37:2 (#0),
},
],
span: $DIR/cfg-eval-inner.rs:18:1: 37:2 (#0),
},
Literal {
kind: Integer,
symbol: "1",
suffix: None,
span: $DIR/cfg-eval-inner.rs:18:1: 37:2 (#0),
},
],
span: $DIR/cfg-eval-inner.rs:18:1: 37:2 (#0),
},
],
span: $DIR/cfg-eval-inner.rs:18:1: 37:2 (#0),
},
Punct {
ch: ',',
spacing: Alone,
span: $DIR/cfg-eval-inner.rs:18:1: 37:2 (#0),
},
],
span: $DIR/cfg-eval-inner.rs:18:1: 37:2 (#0),
},
Literal {
kind: Integer,
symbol: "0",
suffix: None,
span: $DIR/cfg-eval-inner.rs:18:1: 37:2 (#0),
},
],
span: $DIR/cfg-eval-inner.rs:18:1: 37:2 (#0),
},
],
span: $DIR/cfg-eval-inner.rs:18:1: 37:2 (#0),
},
Punct {
ch: '>',
spacing: Alone,
span: $DIR/cfg-eval-inner.rs:18:1: 37:2 (#0),
},
Group {
delimiter: Brace,
stream: TokenStream [
Punct {
ch: '#',
spacing: Joint,
span: $DIR/cfg-eval-inner.rs:18:1: 37:2 (#0),
},
Punct {
ch: '!',
spacing: Alone,
span: $DIR/cfg-eval-inner.rs:18:1: 37:2 (#0),
},
Group {
delimiter: Bracket,
stream: TokenStream [
Ident {
ident: "rustc_dummy",
span: $DIR/cfg-eval-inner.rs:18:1: 37:2 (#0),
},
Group {
delimiter: Parenthesis,
stream: TokenStream [
Ident {
ident: "evaluated_attr",
span: $DIR/cfg-eval-inner.rs:18:1: 37:2 (#0),
},
],
span: $DIR/cfg-eval-inner.rs:18:1: 37:2 (#0),
},
],
span: $DIR/cfg-eval-inner.rs:18:1: 37:2 (#0),
},
Ident {
ident: "fn",
span: $DIR/cfg-eval-inner.rs:18:1: 37:2 (#0),
},
Ident {
ident: "bar",
span: $DIR/cfg-eval-inner.rs:18:1: 37:2 (#0),
},
Group {
delimiter: Parenthesis,
stream: TokenStream [],
span: $DIR/cfg-eval-inner.rs:18:1: 37:2 (#0),
},
Group {
delimiter: Brace,
stream: TokenStream [],
span: $DIR/cfg-eval-inner.rs:18:1: 37:2 (#0),
},
],
span: $DIR/cfg-eval-inner.rs:18:1: 37:2 (#0),
},
]
9 changes: 7 additions & 2 deletions src/test/ui/proc-macro/cfg-eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#![feature(cfg_eval)]
#![feature(proc_macro_hygiene)]
#![feature(stmt_expr_attributes)]

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

Expand All @@ -28,5 +28,10 @@ struct S1 {
struct S2 {}

fn main() {
let _ = #[cfg_eval] #[print_attr](#[cfg(FALSE)] 0, #[cfg(all(/*true*/))] 1);
// Subtle - we need a trailing comma after the '1' - otherwise, `#[cfg_eval]` will
// turn this into `(#[cfg(all())] 1)`, which is a parenthesized expression, not a tuple
// expression. `#[cfg]` is not supported inside parenthesized expressions, so this will
// produce an error when attribute collection runs.
let _ = #[cfg_eval] #[print_attr] #[cfg_attr(not(FALSE), rustc_dummy)]
(#[cfg(FALSE)] 0, #[cfg(all(/*true*/))] 1,);
}
Loading

0 comments on commit a836d9b

Please sign in to comment.