Skip to content

Commit

Permalink
Add more proc-macro attribute tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron1011 committed Mar 1, 2021
1 parent 573a697 commit 8aed5b4
Show file tree
Hide file tree
Showing 16 changed files with 2,249 additions and 371 deletions.
26 changes: 26 additions & 0 deletions src/test/ui/proc-macro/attr-complex-fn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// check-pass
// compile-flags: -Z span-debug --error-format human
// aux-build:test-macros.rs

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

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

#[macro_use]
extern crate test_macros;

trait MyTrait<T> {}
struct MyStruct<const N: bool>;

#[print_attr]
fn foo<T: MyTrait<MyStruct<{ true }>>>() {}

impl<T> MyTrait<T> for MyStruct<{true}> {
#![print_attr]
#![rustc_dummy]
}

fn main() {}
171 changes: 171 additions & 0 deletions src/test/ui/proc-macro/attr-complex-fn.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
PRINT-ATTR INPUT (DISPLAY): fn foo < T : MyTrait < MyStruct < { true } >> > () { }
PRINT-ATTR INPUT (DEBUG): TokenStream [
Ident {
ident: "fn",
span: $DIR/attr-complex-fn.rs:19:1: 19:3 (#0),
},
Ident {
ident: "foo",
span: $DIR/attr-complex-fn.rs:19:4: 19:7 (#0),
},
Punct {
ch: '<',
spacing: Alone,
span: $DIR/attr-complex-fn.rs:19:7: 19:8 (#0),
},
Ident {
ident: "T",
span: $DIR/attr-complex-fn.rs:19:8: 19:9 (#0),
},
Punct {
ch: ':',
spacing: Alone,
span: $DIR/attr-complex-fn.rs:19:9: 19:10 (#0),
},
Ident {
ident: "MyTrait",
span: $DIR/attr-complex-fn.rs:19:11: 19:18 (#0),
},
Punct {
ch: '<',
spacing: Alone,
span: $DIR/attr-complex-fn.rs:19:18: 19:19 (#0),
},
Ident {
ident: "MyStruct",
span: $DIR/attr-complex-fn.rs:19:19: 19:27 (#0),
},
Punct {
ch: '<',
spacing: Alone,
span: $DIR/attr-complex-fn.rs:19:27: 19:28 (#0),
},
Group {
delimiter: Brace,
stream: TokenStream [
Ident {
ident: "true",
span: $DIR/attr-complex-fn.rs:19:30: 19:34 (#0),
},
],
span: $DIR/attr-complex-fn.rs:19:28: 19:36 (#0),
},
Punct {
ch: '>',
spacing: Joint,
span: $DIR/attr-complex-fn.rs:19:36: 19:38 (#0),
},
Punct {
ch: '>',
spacing: Joint,
span: $DIR/attr-complex-fn.rs:19:36: 19:38 (#0),
},
Punct {
ch: '>',
spacing: Alone,
span: $DIR/attr-complex-fn.rs:19:38: 19:39 (#0),
},
Group {
delimiter: Parenthesis,
stream: TokenStream [],
span: $DIR/attr-complex-fn.rs:19:39: 19:41 (#0),
},
Group {
delimiter: Brace,
stream: TokenStream [],
span: $DIR/attr-complex-fn.rs:19:42: 19:44 (#0),
},
]
PRINT-ATTR INPUT (DISPLAY): impl < T > MyTrait < T > for MyStruct < { true } > { # ! [rustc_dummy] }
PRINT-ATTR INPUT (DEBUG): TokenStream [
Ident {
ident: "impl",
span: $DIR/attr-complex-fn.rs:21:1: 24:2 (#0),
},
Punct {
ch: '<',
spacing: Alone,
span: $DIR/attr-complex-fn.rs:21:1: 24:2 (#0),
},
Ident {
ident: "T",
span: $DIR/attr-complex-fn.rs:21:1: 24:2 (#0),
},
Punct {
ch: '>',
spacing: Alone,
span: $DIR/attr-complex-fn.rs:21:1: 24:2 (#0),
},
Ident {
ident: "MyTrait",
span: $DIR/attr-complex-fn.rs:21:1: 24:2 (#0),
},
Punct {
ch: '<',
spacing: Alone,
span: $DIR/attr-complex-fn.rs:21:1: 24:2 (#0),
},
Ident {
ident: "T",
span: $DIR/attr-complex-fn.rs:21:1: 24:2 (#0),
},
Punct {
ch: '>',
spacing: Alone,
span: $DIR/attr-complex-fn.rs:21:1: 24:2 (#0),
},
Ident {
ident: "for",
span: $DIR/attr-complex-fn.rs:21:1: 24:2 (#0),
},
Ident {
ident: "MyStruct",
span: $DIR/attr-complex-fn.rs:21:1: 24:2 (#0),
},
Punct {
ch: '<',
spacing: Alone,
span: $DIR/attr-complex-fn.rs:21:1: 24:2 (#0),
},
Group {
delimiter: Brace,
stream: TokenStream [
Ident {
ident: "true",
span: $DIR/attr-complex-fn.rs:21:1: 24:2 (#0),
},
],
span: $DIR/attr-complex-fn.rs:21:1: 24:2 (#0),
},
Punct {
ch: '>',
spacing: Alone,
span: $DIR/attr-complex-fn.rs:21:1: 24:2 (#0),
},
Group {
delimiter: Brace,
stream: TokenStream [
Punct {
ch: '#',
spacing: Joint,
span: $DIR/attr-complex-fn.rs:21:1: 24:2 (#0),
},
Punct {
ch: '!',
spacing: Alone,
span: $DIR/attr-complex-fn.rs:21:1: 24:2 (#0),
},
Group {
delimiter: Bracket,
stream: TokenStream [
Ident {
ident: "rustc_dummy",
span: $DIR/attr-complex-fn.rs:21:1: 24:2 (#0),
},
],
span: $DIR/attr-complex-fn.rs:21:1: 24:2 (#0),
},
],
span: $DIR/attr-complex-fn.rs:21:1: 24:2 (#0),
},
]
14 changes: 14 additions & 0 deletions src/test/ui/proc-macro/auxiliary/test-macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,20 @@ pub fn print_attr_args(args: TokenStream, input: TokenStream) -> TokenStream {
input
}

#[proc_macro_attribute]
pub fn print_target_and_args(args: TokenStream, input: TokenStream) -> TokenStream {
print_helper(args, "ATTR_ARGS");
print_helper(input.clone(), "ATTR");
input
}

#[proc_macro_attribute]
pub fn print_target_and_args_consume(args: TokenStream, input: TokenStream) -> TokenStream {
print_helper(args, "ATTR_ARGS");
print_helper(input.clone(), "ATTR");
TokenStream::new()
}

#[proc_macro_derive(Print, attributes(print_helper))]
pub fn print_derive(input: TokenStream) -> TokenStream {
print_helper(input, "DERIVE");
Expand Down
34 changes: 34 additions & 0 deletions src/test/ui/proc-macro/expand-to-derive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// check-pass
// compile-flags: -Z span-debug --error-format human
// aux-build:test-macros.rs

#![feature(rustc_attrs)]

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

#[macro_use]
extern crate test_macros;

macro_rules! expand_to_derive {
($item:item) => {
#[derive(Print)]
struct Foo {
#[cfg(FALSE)] removed: bool,
field: [bool; {
$item
0
}]
}
};
}

expand_to_derive! {
#[cfg_attr(not(FALSE), rustc_dummy)]
struct Inner {
#[cfg(FALSE)] removed_inner_field: bool,
other_inner_field: u8,
}
}

fn main() {}
109 changes: 109 additions & 0 deletions src/test/ui/proc-macro/expand-to-derive.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
PRINT-DERIVE INPUT (DISPLAY): struct Foo
{
field :
[bool ; { #[rustc_dummy] struct Inner { other_inner_field : u8, } 0 }],
}
PRINT-DERIVE INPUT (DEBUG): TokenStream [
Ident {
ident: "struct",
span: $DIR/expand-to-derive.rs:16:9: 22:10 (#4),
},
Ident {
ident: "Foo",
span: $DIR/expand-to-derive.rs:16:9: 22:10 (#4),
},
Group {
delimiter: Brace,
stream: TokenStream [
Ident {
ident: "field",
span: $DIR/expand-to-derive.rs:16:9: 22:10 (#4),
},
Punct {
ch: ':',
spacing: Alone,
span: $DIR/expand-to-derive.rs:16:9: 22:10 (#4),
},
Group {
delimiter: Bracket,
stream: TokenStream [
Ident {
ident: "bool",
span: $DIR/expand-to-derive.rs:16:9: 22:10 (#4),
},
Punct {
ch: ';',
spacing: Alone,
span: $DIR/expand-to-derive.rs:16:9: 22:10 (#4),
},
Group {
delimiter: Brace,
stream: TokenStream [
Punct {
ch: '#',
spacing: Alone,
span: $DIR/expand-to-derive.rs:16:9: 22:10 (#4),
},
Group {
delimiter: Bracket,
stream: TokenStream [
Ident {
ident: "rustc_dummy",
span: $DIR/expand-to-derive.rs:16:9: 22:10 (#4),
},
],
span: $DIR/expand-to-derive.rs:16:9: 22:10 (#4),
},
Ident {
ident: "struct",
span: $DIR/expand-to-derive.rs:16:9: 22:10 (#4),
},
Ident {
ident: "Inner",
span: $DIR/expand-to-derive.rs:16:9: 22:10 (#4),
},
Group {
delimiter: Brace,
stream: TokenStream [
Ident {
ident: "other_inner_field",
span: $DIR/expand-to-derive.rs:16:9: 22:10 (#4),
},
Punct {
ch: ':',
spacing: Alone,
span: $DIR/expand-to-derive.rs:16:9: 22:10 (#4),
},
Ident {
ident: "u8",
span: $DIR/expand-to-derive.rs:16:9: 22:10 (#4),
},
Punct {
ch: ',',
spacing: Alone,
span: $DIR/expand-to-derive.rs:16:9: 22:10 (#4),
},
],
span: $DIR/expand-to-derive.rs:16:9: 22:10 (#4),
},
Literal {
kind: Integer,
symbol: "0",
suffix: None,
span: $DIR/expand-to-derive.rs:16:9: 22:10 (#4),
},
],
span: $DIR/expand-to-derive.rs:16:9: 22:10 (#4),
},
],
span: $DIR/expand-to-derive.rs:16:9: 22:10 (#4),
},
Punct {
ch: ',',
spacing: Alone,
span: $DIR/expand-to-derive.rs:16:9: 22:10 (#4),
},
],
span: $DIR/expand-to-derive.rs:16:9: 22:10 (#4),
},
]
Loading

0 comments on commit 8aed5b4

Please sign in to comment.