Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Feb 22, 2021
1 parent a15f484 commit c02d210
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/test/ui/proc-macro/auxiliary/test-macros.rs
Expand Up @@ -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.

Expand Down Expand Up @@ -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();
Expand All @@ -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
}
Expand All @@ -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]
Expand Down
13 changes: 13 additions & 0 deletions 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() {}
28 changes: 28 additions & 0 deletions 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 <https://github.com/rust-lang/rust/issues/79202>

error: aborting due to 2 previous errors; 1 warning emitted

23 changes: 23 additions & 0 deletions 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() {}
7 changes: 7 additions & 0 deletions 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 ;

0 comments on commit c02d210

Please sign in to comment.