Skip to content

Commit 8aed5b4

Browse files
committed
Add more proc-macro attribute tests
1 parent 573a697 commit 8aed5b4

16 files changed

+2249
-371
lines changed
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// check-pass
2+
// compile-flags: -Z span-debug --error-format human
3+
// aux-build:test-macros.rs
4+
5+
#![feature(stmt_expr_attributes)]
6+
#![feature(custom_inner_attributes)]
7+
#![feature(rustc_attrs)]
8+
9+
#![no_std] // Don't load unnecessary hygiene information from std
10+
extern crate std;
11+
12+
#[macro_use]
13+
extern crate test_macros;
14+
15+
trait MyTrait<T> {}
16+
struct MyStruct<const N: bool>;
17+
18+
#[print_attr]
19+
fn foo<T: MyTrait<MyStruct<{ true }>>>() {}
20+
21+
impl<T> MyTrait<T> for MyStruct<{true}> {
22+
#![print_attr]
23+
#![rustc_dummy]
24+
}
25+
26+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
PRINT-ATTR INPUT (DISPLAY): fn foo < T : MyTrait < MyStruct < { true } >> > () { }
2+
PRINT-ATTR INPUT (DEBUG): TokenStream [
3+
Ident {
4+
ident: "fn",
5+
span: $DIR/attr-complex-fn.rs:19:1: 19:3 (#0),
6+
},
7+
Ident {
8+
ident: "foo",
9+
span: $DIR/attr-complex-fn.rs:19:4: 19:7 (#0),
10+
},
11+
Punct {
12+
ch: '<',
13+
spacing: Alone,
14+
span: $DIR/attr-complex-fn.rs:19:7: 19:8 (#0),
15+
},
16+
Ident {
17+
ident: "T",
18+
span: $DIR/attr-complex-fn.rs:19:8: 19:9 (#0),
19+
},
20+
Punct {
21+
ch: ':',
22+
spacing: Alone,
23+
span: $DIR/attr-complex-fn.rs:19:9: 19:10 (#0),
24+
},
25+
Ident {
26+
ident: "MyTrait",
27+
span: $DIR/attr-complex-fn.rs:19:11: 19:18 (#0),
28+
},
29+
Punct {
30+
ch: '<',
31+
spacing: Alone,
32+
span: $DIR/attr-complex-fn.rs:19:18: 19:19 (#0),
33+
},
34+
Ident {
35+
ident: "MyStruct",
36+
span: $DIR/attr-complex-fn.rs:19:19: 19:27 (#0),
37+
},
38+
Punct {
39+
ch: '<',
40+
spacing: Alone,
41+
span: $DIR/attr-complex-fn.rs:19:27: 19:28 (#0),
42+
},
43+
Group {
44+
delimiter: Brace,
45+
stream: TokenStream [
46+
Ident {
47+
ident: "true",
48+
span: $DIR/attr-complex-fn.rs:19:30: 19:34 (#0),
49+
},
50+
],
51+
span: $DIR/attr-complex-fn.rs:19:28: 19:36 (#0),
52+
},
53+
Punct {
54+
ch: '>',
55+
spacing: Joint,
56+
span: $DIR/attr-complex-fn.rs:19:36: 19:38 (#0),
57+
},
58+
Punct {
59+
ch: '>',
60+
spacing: Joint,
61+
span: $DIR/attr-complex-fn.rs:19:36: 19:38 (#0),
62+
},
63+
Punct {
64+
ch: '>',
65+
spacing: Alone,
66+
span: $DIR/attr-complex-fn.rs:19:38: 19:39 (#0),
67+
},
68+
Group {
69+
delimiter: Parenthesis,
70+
stream: TokenStream [],
71+
span: $DIR/attr-complex-fn.rs:19:39: 19:41 (#0),
72+
},
73+
Group {
74+
delimiter: Brace,
75+
stream: TokenStream [],
76+
span: $DIR/attr-complex-fn.rs:19:42: 19:44 (#0),
77+
},
78+
]
79+
PRINT-ATTR INPUT (DISPLAY): impl < T > MyTrait < T > for MyStruct < { true } > { # ! [rustc_dummy] }
80+
PRINT-ATTR INPUT (DEBUG): TokenStream [
81+
Ident {
82+
ident: "impl",
83+
span: $DIR/attr-complex-fn.rs:21:1: 24:2 (#0),
84+
},
85+
Punct {
86+
ch: '<',
87+
spacing: Alone,
88+
span: $DIR/attr-complex-fn.rs:21:1: 24:2 (#0),
89+
},
90+
Ident {
91+
ident: "T",
92+
span: $DIR/attr-complex-fn.rs:21:1: 24:2 (#0),
93+
},
94+
Punct {
95+
ch: '>',
96+
spacing: Alone,
97+
span: $DIR/attr-complex-fn.rs:21:1: 24:2 (#0),
98+
},
99+
Ident {
100+
ident: "MyTrait",
101+
span: $DIR/attr-complex-fn.rs:21:1: 24:2 (#0),
102+
},
103+
Punct {
104+
ch: '<',
105+
spacing: Alone,
106+
span: $DIR/attr-complex-fn.rs:21:1: 24:2 (#0),
107+
},
108+
Ident {
109+
ident: "T",
110+
span: $DIR/attr-complex-fn.rs:21:1: 24:2 (#0),
111+
},
112+
Punct {
113+
ch: '>',
114+
spacing: Alone,
115+
span: $DIR/attr-complex-fn.rs:21:1: 24:2 (#0),
116+
},
117+
Ident {
118+
ident: "for",
119+
span: $DIR/attr-complex-fn.rs:21:1: 24:2 (#0),
120+
},
121+
Ident {
122+
ident: "MyStruct",
123+
span: $DIR/attr-complex-fn.rs:21:1: 24:2 (#0),
124+
},
125+
Punct {
126+
ch: '<',
127+
spacing: Alone,
128+
span: $DIR/attr-complex-fn.rs:21:1: 24:2 (#0),
129+
},
130+
Group {
131+
delimiter: Brace,
132+
stream: TokenStream [
133+
Ident {
134+
ident: "true",
135+
span: $DIR/attr-complex-fn.rs:21:1: 24:2 (#0),
136+
},
137+
],
138+
span: $DIR/attr-complex-fn.rs:21:1: 24:2 (#0),
139+
},
140+
Punct {
141+
ch: '>',
142+
spacing: Alone,
143+
span: $DIR/attr-complex-fn.rs:21:1: 24:2 (#0),
144+
},
145+
Group {
146+
delimiter: Brace,
147+
stream: TokenStream [
148+
Punct {
149+
ch: '#',
150+
spacing: Joint,
151+
span: $DIR/attr-complex-fn.rs:21:1: 24:2 (#0),
152+
},
153+
Punct {
154+
ch: '!',
155+
spacing: Alone,
156+
span: $DIR/attr-complex-fn.rs:21:1: 24:2 (#0),
157+
},
158+
Group {
159+
delimiter: Bracket,
160+
stream: TokenStream [
161+
Ident {
162+
ident: "rustc_dummy",
163+
span: $DIR/attr-complex-fn.rs:21:1: 24:2 (#0),
164+
},
165+
],
166+
span: $DIR/attr-complex-fn.rs:21:1: 24:2 (#0),
167+
},
168+
],
169+
span: $DIR/attr-complex-fn.rs:21:1: 24:2 (#0),
170+
},
171+
]

src/test/ui/proc-macro/auxiliary/test-macros.rs

+14
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,20 @@ pub fn print_attr_args(args: TokenStream, input: TokenStream) -> TokenStream {
128128
input
129129
}
130130

131+
#[proc_macro_attribute]
132+
pub fn print_target_and_args(args: TokenStream, input: TokenStream) -> TokenStream {
133+
print_helper(args, "ATTR_ARGS");
134+
print_helper(input.clone(), "ATTR");
135+
input
136+
}
137+
138+
#[proc_macro_attribute]
139+
pub fn print_target_and_args_consume(args: TokenStream, input: TokenStream) -> TokenStream {
140+
print_helper(args, "ATTR_ARGS");
141+
print_helper(input.clone(), "ATTR");
142+
TokenStream::new()
143+
}
144+
131145
#[proc_macro_derive(Print, attributes(print_helper))]
132146
pub fn print_derive(input: TokenStream) -> TokenStream {
133147
print_helper(input, "DERIVE");
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// check-pass
2+
// compile-flags: -Z span-debug --error-format human
3+
// aux-build:test-macros.rs
4+
5+
#![feature(rustc_attrs)]
6+
7+
#![no_std] // Don't load unnecessary hygiene information from std
8+
extern crate std;
9+
10+
#[macro_use]
11+
extern crate test_macros;
12+
13+
macro_rules! expand_to_derive {
14+
($item:item) => {
15+
#[derive(Print)]
16+
struct Foo {
17+
#[cfg(FALSE)] removed: bool,
18+
field: [bool; {
19+
$item
20+
0
21+
}]
22+
}
23+
};
24+
}
25+
26+
expand_to_derive! {
27+
#[cfg_attr(not(FALSE), rustc_dummy)]
28+
struct Inner {
29+
#[cfg(FALSE)] removed_inner_field: bool,
30+
other_inner_field: u8,
31+
}
32+
}
33+
34+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
PRINT-DERIVE INPUT (DISPLAY): struct Foo
2+
{
3+
field :
4+
[bool ; { #[rustc_dummy] struct Inner { other_inner_field : u8, } 0 }],
5+
}
6+
PRINT-DERIVE INPUT (DEBUG): TokenStream [
7+
Ident {
8+
ident: "struct",
9+
span: $DIR/expand-to-derive.rs:16:9: 22:10 (#4),
10+
},
11+
Ident {
12+
ident: "Foo",
13+
span: $DIR/expand-to-derive.rs:16:9: 22:10 (#4),
14+
},
15+
Group {
16+
delimiter: Brace,
17+
stream: TokenStream [
18+
Ident {
19+
ident: "field",
20+
span: $DIR/expand-to-derive.rs:16:9: 22:10 (#4),
21+
},
22+
Punct {
23+
ch: ':',
24+
spacing: Alone,
25+
span: $DIR/expand-to-derive.rs:16:9: 22:10 (#4),
26+
},
27+
Group {
28+
delimiter: Bracket,
29+
stream: TokenStream [
30+
Ident {
31+
ident: "bool",
32+
span: $DIR/expand-to-derive.rs:16:9: 22:10 (#4),
33+
},
34+
Punct {
35+
ch: ';',
36+
spacing: Alone,
37+
span: $DIR/expand-to-derive.rs:16:9: 22:10 (#4),
38+
},
39+
Group {
40+
delimiter: Brace,
41+
stream: TokenStream [
42+
Punct {
43+
ch: '#',
44+
spacing: Alone,
45+
span: $DIR/expand-to-derive.rs:16:9: 22:10 (#4),
46+
},
47+
Group {
48+
delimiter: Bracket,
49+
stream: TokenStream [
50+
Ident {
51+
ident: "rustc_dummy",
52+
span: $DIR/expand-to-derive.rs:16:9: 22:10 (#4),
53+
},
54+
],
55+
span: $DIR/expand-to-derive.rs:16:9: 22:10 (#4),
56+
},
57+
Ident {
58+
ident: "struct",
59+
span: $DIR/expand-to-derive.rs:16:9: 22:10 (#4),
60+
},
61+
Ident {
62+
ident: "Inner",
63+
span: $DIR/expand-to-derive.rs:16:9: 22:10 (#4),
64+
},
65+
Group {
66+
delimiter: Brace,
67+
stream: TokenStream [
68+
Ident {
69+
ident: "other_inner_field",
70+
span: $DIR/expand-to-derive.rs:16:9: 22:10 (#4),
71+
},
72+
Punct {
73+
ch: ':',
74+
spacing: Alone,
75+
span: $DIR/expand-to-derive.rs:16:9: 22:10 (#4),
76+
},
77+
Ident {
78+
ident: "u8",
79+
span: $DIR/expand-to-derive.rs:16:9: 22:10 (#4),
80+
},
81+
Punct {
82+
ch: ',',
83+
spacing: Alone,
84+
span: $DIR/expand-to-derive.rs:16:9: 22:10 (#4),
85+
},
86+
],
87+
span: $DIR/expand-to-derive.rs:16:9: 22:10 (#4),
88+
},
89+
Literal {
90+
kind: Integer,
91+
symbol: "0",
92+
suffix: None,
93+
span: $DIR/expand-to-derive.rs:16:9: 22:10 (#4),
94+
},
95+
],
96+
span: $DIR/expand-to-derive.rs:16:9: 22:10 (#4),
97+
},
98+
],
99+
span: $DIR/expand-to-derive.rs:16:9: 22:10 (#4),
100+
},
101+
Punct {
102+
ch: ',',
103+
spacing: Alone,
104+
span: $DIR/expand-to-derive.rs:16:9: 22:10 (#4),
105+
},
106+
],
107+
span: $DIR/expand-to-derive.rs:16:9: 22:10 (#4),
108+
},
109+
]

0 commit comments

Comments
 (0)