Skip to content

Commit c02d210

Browse files
committed
Add tests
1 parent a15f484 commit c02d210

5 files changed

+87
-6
lines changed

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

+16-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#![crate_type = "proc-macro"]
88

99
extern crate proc_macro;
10-
use proc_macro::TokenStream;
10+
use proc_macro::{TokenStream, TokenTree};
1111

1212
// Macro that return empty token stream.
1313

@@ -80,6 +80,10 @@ pub fn recollect_derive(input: TokenStream) -> TokenStream {
8080
// Macros that print their input in the original and re-collected forms (if they differ).
8181

8282
fn print_helper(input: TokenStream, kind: &str) -> TokenStream {
83+
print_helper_ext(input, kind, true)
84+
}
85+
86+
fn print_helper_ext(input: TokenStream, kind: &str, debug: bool) -> TokenStream {
8387
let input_display = format!("{}", input);
8488
let input_debug = format!("{:#?}", input);
8589
let recollected = input.into_iter().collect();
@@ -89,9 +93,11 @@ fn print_helper(input: TokenStream, kind: &str) -> TokenStream {
8993
if recollected_display != input_display {
9094
println!("PRINT-{} RE-COLLECTED (DISPLAY): {}", kind, recollected_display);
9195
}
92-
println!("PRINT-{} INPUT (DEBUG): {}", kind, input_debug);
93-
if recollected_debug != input_debug {
94-
println!("PRINT-{} RE-COLLECTED (DEBUG): {}", kind, recollected_debug);
96+
if debug {
97+
println!("PRINT-{} INPUT (DEBUG): {}", kind, input_debug);
98+
if recollected_debug != input_debug {
99+
println!("PRINT-{} RE-COLLECTED (DEBUG): {}", kind, recollected_debug);
100+
}
95101
}
96102
recollected
97103
}
@@ -108,8 +114,12 @@ pub fn print_bang_consume(input: TokenStream) -> TokenStream {
108114
}
109115

110116
#[proc_macro_attribute]
111-
pub fn print_attr(_: TokenStream, input: TokenStream) -> TokenStream {
112-
print_helper(input, "ATTR")
117+
pub fn print_attr(args: TokenStream, input: TokenStream) -> TokenStream {
118+
let debug = match &args.into_iter().collect::<Vec<_>>()[..] {
119+
[TokenTree::Ident(ident)] if ident.to_string() == "nodebug" => false,
120+
_ => true,
121+
};
122+
print_helper_ext(input, "ATTR", debug)
113123
}
114124

115125
#[proc_macro_attribute]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// aux-build:test-macros.rs
2+
3+
#![dummy] //~ ERROR cannot find attribute `dummy` in this scope
4+
5+
#[macro_use]
6+
extern crate test_macros;
7+
8+
#[derive(Empty)] //~ ERROR cannot determine resolution for the attribute macro `derive`
9+
#[empty_helper] //~ WARN derive helper attribute is used before it is introduced
10+
//~| WARN this was previously accepted
11+
struct Foo {}
12+
13+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
error: cannot find attribute `dummy` in this scope
2+
--> $DIR/derive-helper-legacy-spurious.rs:3:4
3+
|
4+
LL | #![dummy]
5+
| ^^^^^
6+
7+
error: cannot determine resolution for the attribute macro `derive`
8+
--> $DIR/derive-helper-legacy-spurious.rs:8:3
9+
|
10+
LL | #[derive(Empty)]
11+
| ^^^^^^
12+
|
13+
= note: import resolution is stuck, try simplifying macro imports
14+
15+
warning: derive helper attribute is used before it is introduced
16+
--> $DIR/derive-helper-legacy-spurious.rs:9:3
17+
|
18+
LL | #[derive(Empty)]
19+
| ----- the attribute is introduced here
20+
LL | #[empty_helper]
21+
| ^^^^^^^^^^^^
22+
|
23+
= note: `#[warn(legacy_derive_helpers)]` on by default
24+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
25+
= note: for more information, see issue #79202 <https://github.com/rust-lang/rust/issues/79202>
26+
27+
error: aborting due to 2 previous errors; 1 warning emitted
28+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Order of inert attributes, both built-in and custom is preserved during expansion.
2+
3+
// check-pass
4+
// compile-flags: -Z span-debug
5+
// aux-build:test-macros.rs
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+
/// 1
14+
#[rustfmt::attr2]
15+
#[doc = "3"]
16+
#[print_attr(nodebug)]
17+
#[doc = "4"]
18+
#[rustfmt::attr5]
19+
/// 6
20+
#[print_attr(nodebug)]
21+
struct S;
22+
23+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
PRINT-ATTR INPUT (DISPLAY): /// 1
2+
#[doc = "3"] #[doc = "4"] #[rustfmt :: attr5] /// 6
3+
#[print_attr(nodebug)] #[rustfmt :: attr2] struct S ;
4+
PRINT-ATTR RE-COLLECTED (DISPLAY): #[doc = " 1"] #[doc = "3"] #[doc = "4"] #[rustfmt :: attr5] #[doc = " 6"]
5+
#[print_attr(nodebug)] #[rustfmt :: attr2] struct S ;
6+
PRINT-ATTR INPUT (DISPLAY): #[doc = " 1"] #[doc = "3"] #[doc = "4"] #[doc = " 6"] #[rustfmt :: attr2]
7+
#[rustfmt :: attr5] struct S ;

0 commit comments

Comments
 (0)