From c02d21033d3603f1dac3bfd6062c37f69a1681cd Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Tue, 23 Feb 2021 00:07:36 +0300 Subject: [PATCH] Add tests --- .../ui/proc-macro/auxiliary/test-macros.rs | 22 +++++++++++---- .../derive-helper-legacy-spurious.rs | 13 +++++++++ .../derive-helper-legacy-spurious.stderr | 28 +++++++++++++++++++ .../ui/proc-macro/inert-attribute-order.rs | 23 +++++++++++++++ .../proc-macro/inert-attribute-order.stdout | 7 +++++ 5 files changed, 87 insertions(+), 6 deletions(-) create mode 100644 src/test/ui/proc-macro/derive-helper-legacy-spurious.rs create mode 100644 src/test/ui/proc-macro/derive-helper-legacy-spurious.stderr create mode 100644 src/test/ui/proc-macro/inert-attribute-order.rs create mode 100644 src/test/ui/proc-macro/inert-attribute-order.stdout diff --git a/src/test/ui/proc-macro/auxiliary/test-macros.rs b/src/test/ui/proc-macro/auxiliary/test-macros.rs index 57a7ffa39ef00..a7ed4bc88250d 100644 --- a/src/test/ui/proc-macro/auxiliary/test-macros.rs +++ b/src/test/ui/proc-macro/auxiliary/test-macros.rs @@ -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::>()[..] { + [TokenTree::Ident(ident)] if ident.to_string() == "nodebug" => false, + _ => true, + }; + print_helper_ext(input, "ATTR", debug) } #[proc_macro_attribute] diff --git a/src/test/ui/proc-macro/derive-helper-legacy-spurious.rs b/src/test/ui/proc-macro/derive-helper-legacy-spurious.rs new file mode 100644 index 0000000000000..8180aab0caa13 --- /dev/null +++ b/src/test/ui/proc-macro/derive-helper-legacy-spurious.rs @@ -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() {} diff --git a/src/test/ui/proc-macro/derive-helper-legacy-spurious.stderr b/src/test/ui/proc-macro/derive-helper-legacy-spurious.stderr new file mode 100644 index 0000000000000..96754fed9933c --- /dev/null +++ b/src/test/ui/proc-macro/derive-helper-legacy-spurious.stderr @@ -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 + +error: aborting due to 2 previous errors; 1 warning emitted + diff --git a/src/test/ui/proc-macro/inert-attribute-order.rs b/src/test/ui/proc-macro/inert-attribute-order.rs new file mode 100644 index 0000000000000..f807967564116 --- /dev/null +++ b/src/test/ui/proc-macro/inert-attribute-order.rs @@ -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() {} diff --git a/src/test/ui/proc-macro/inert-attribute-order.stdout b/src/test/ui/proc-macro/inert-attribute-order.stdout new file mode 100644 index 0000000000000..7c0620b50b30f --- /dev/null +++ b/src/test/ui/proc-macro/inert-attribute-order.stdout @@ -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 ;