Add tests

This commit is contained in:
Vadim Petrochenkov 2021-02-23 00:07:36 +03:00
parent a15f484b91
commit c02d21033d
5 changed files with 87 additions and 6 deletions

View File

@ -7,7 +7,7 @@
#![crate_type = "proc-macro"]
extern crate proc_macro;
use proc_macro::TokenStream;
use proc_macro::{TokenStream, TokenTree};
// Macro that return empty token stream.
@ -80,6 +80,10 @@ pub fn recollect_derive(input: TokenStream) -> TokenStream {
// Macros that print their input in the original and re-collected forms (if they differ).
fn print_helper(input: TokenStream, kind: &str) -> TokenStream {
print_helper_ext(input, kind, true)
}
fn print_helper_ext(input: TokenStream, kind: &str, debug: bool) -> TokenStream {
let input_display = format!("{}", input);
let input_debug = format!("{:#?}", input);
let recollected = input.into_iter().collect();
@ -89,9 +93,11 @@ fn print_helper(input: TokenStream, kind: &str) -> TokenStream {
if recollected_display != input_display {
println!("PRINT-{} RE-COLLECTED (DISPLAY): {}", kind, recollected_display);
}
println!("PRINT-{} INPUT (DEBUG): {}", kind, input_debug);
if recollected_debug != input_debug {
println!("PRINT-{} RE-COLLECTED (DEBUG): {}", kind, recollected_debug);
if debug {
println!("PRINT-{} INPUT (DEBUG): {}", kind, input_debug);
if recollected_debug != input_debug {
println!("PRINT-{} RE-COLLECTED (DEBUG): {}", kind, recollected_debug);
}
}
recollected
}
@ -108,8 +114,12 @@ pub fn print_bang_consume(input: TokenStream) -> TokenStream {
}
#[proc_macro_attribute]
pub fn print_attr(_: TokenStream, input: TokenStream) -> TokenStream {
print_helper(input, "ATTR")
pub fn print_attr(args: TokenStream, input: TokenStream) -> TokenStream {
let debug = match &args.into_iter().collect::<Vec<_>>()[..] {
[TokenTree::Ident(ident)] if ident.to_string() == "nodebug" => false,
_ => true,
};
print_helper_ext(input, "ATTR", debug)
}
#[proc_macro_attribute]

View File

@ -0,0 +1,13 @@
// aux-build:test-macros.rs
#![dummy] //~ ERROR cannot find attribute `dummy` in this scope
#[macro_use]
extern crate test_macros;
#[derive(Empty)] //~ ERROR cannot determine resolution for the attribute macro `derive`
#[empty_helper] //~ WARN derive helper attribute is used before it is introduced
//~| WARN this was previously accepted
struct Foo {}
fn main() {}

View File

@ -0,0 +1,28 @@
error: cannot find attribute `dummy` in this scope
--> $DIR/derive-helper-legacy-spurious.rs:3:4
|
LL | #![dummy]
| ^^^^^
error: cannot determine resolution for the attribute macro `derive`
--> $DIR/derive-helper-legacy-spurious.rs:8:3
|
LL | #[derive(Empty)]
| ^^^^^^
|
= note: import resolution is stuck, try simplifying macro imports
warning: derive helper attribute is used before it is introduced
--> $DIR/derive-helper-legacy-spurious.rs:9:3
|
LL | #[derive(Empty)]
| ----- the attribute is introduced here
LL | #[empty_helper]
| ^^^^^^^^^^^^
|
= note: `#[warn(legacy_derive_helpers)]` on by default
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #79202 <https://github.com/rust-lang/rust/issues/79202>
error: aborting due to 2 previous errors; 1 warning emitted

View File

@ -0,0 +1,23 @@
// Order of inert attributes, both built-in and custom is preserved during expansion.
// check-pass
// compile-flags: -Z span-debug
// aux-build:test-macros.rs
#![no_std] // Don't load unnecessary hygiene information from std
extern crate std;
#[macro_use]
extern crate test_macros;
/// 1
#[rustfmt::attr2]
#[doc = "3"]
#[print_attr(nodebug)]
#[doc = "4"]
#[rustfmt::attr5]
/// 6
#[print_attr(nodebug)]
struct S;
fn main() {}

View File

@ -0,0 +1,7 @@
PRINT-ATTR INPUT (DISPLAY): /// 1
#[doc = "3"] #[doc = "4"] #[rustfmt :: attr5] /// 6
#[print_attr(nodebug)] #[rustfmt :: attr2] struct S ;
PRINT-ATTR RE-COLLECTED (DISPLAY): #[doc = " 1"] #[doc = "3"] #[doc = "4"] #[rustfmt :: attr5] #[doc = " 6"]
#[print_attr(nodebug)] #[rustfmt :: attr2] struct S ;
PRINT-ATTR INPUT (DISPLAY): #[doc = " 1"] #[doc = "3"] #[doc = "4"] #[doc = " 6"] #[rustfmt :: attr2]
#[rustfmt :: attr5] struct S ;