Rollup merge of #72795 - petrochenkov:identgroup, r=nikomatsakis

Add a test for `$:ident` in proc macro input

cc https://github.com/rust-lang/rust/issues/72545#issuecomment-636388019
This commit is contained in:
Yuki Okushi 2020-06-02 13:07:13 +09:00 committed by GitHub
commit db0c346395
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 99 additions and 33 deletions

View File

@ -108,5 +108,6 @@ pub fn print_attr(_: TokenStream, input: TokenStream) -> TokenStream {
#[proc_macro_derive(Print, attributes(print_helper))]
pub fn print_derive(input: TokenStream) -> TokenStream {
print_helper(input, "DERIVE")
print_helper(input, "DERIVE");
TokenStream::new()
}

View File

@ -1,3 +1,4 @@
// check-pass
// edition:2018
// aux-build:test-macros.rs
// aux-build:dollar-crate-external.rs
@ -23,7 +24,7 @@ mod local {
struct A($crate::S);
#[derive(Print)]
struct D($crate::S); //~ ERROR the name `D` is defined multiple times
struct D($crate::S);
};
}
@ -33,7 +34,7 @@ mod local {
mod external {
use crate::dollar_crate_external;
dollar_crate_external::external!(); //~ ERROR the name `D` is defined multiple times
dollar_crate_external::external!();
}
fn main() {}

View File

@ -1,30 +0,0 @@
error[E0428]: the name `D` is defined multiple times
--> $DIR/dollar-crate.rs:26:13
|
LL | struct D($crate::S);
| ^^^^^^^^^^^^^^^^^^^^
| |
| `D` redefined here
| previous definition of the type `D` here
...
LL | local!();
| --------- in this macro invocation
|
= note: `D` must be defined only once in the type namespace of this module
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0428]: the name `D` is defined multiple times
--> $DIR/dollar-crate.rs:36:5
|
LL | dollar_crate_external::external!();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| `D` redefined here
| previous definition of the type `D` here
|
= note: `D` must be defined only once in the type namespace of this module
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0428`.

View File

@ -0,0 +1,25 @@
// Check what token streams proc macros see when interpolated tokens are passed to them as input.
// check-pass
// aux-build:test-macros.rs
#[macro_use]
extern crate test_macros;
macro_rules! pass_ident {
($i:ident) => {
fn f() {
print_bang!($i);
}
#[print_attr]
const $i: u8 = 0;
#[derive(Print)]
struct $i {}
};
}
pass_ident!(A);
fn main() {}

View File

@ -0,0 +1,69 @@
PRINT-BANG INPUT (DISPLAY): A
PRINT-BANG RE-COLLECTED (DISPLAY): A
PRINT-BANG INPUT (DEBUG): TokenStream [
Group {
delimiter: None,
stream: TokenStream [
Ident {
ident: "A",
span: #0 bytes(402..403),
},
],
span: #3 bytes(269..271),
},
]
PRINT-ATTR INPUT (DISPLAY): const A: u8 = 0;
PRINT-ATTR RE-COLLECTED (DISPLAY): const A : u8 = 0 ;
PRINT-ATTR INPUT (DEBUG): TokenStream [
Ident {
ident: "const",
span: #0 bytes(0..0),
},
Ident {
ident: "A",
span: #0 bytes(0..0),
},
Punct {
ch: ':',
spacing: Alone,
span: #0 bytes(0..0),
},
Ident {
ident: "u8",
span: #0 bytes(0..0),
},
Punct {
ch: '=',
spacing: Alone,
span: #0 bytes(0..0),
},
Literal {
kind: Integer,
symbol: "0",
suffix: None,
span: #0 bytes(0..0),
},
Punct {
ch: ';',
spacing: Alone,
span: #0 bytes(0..0),
},
]
PRINT-DERIVE INPUT (DISPLAY): struct A {
}
PRINT-DERIVE RE-COLLECTED (DISPLAY): struct A { }
PRINT-DERIVE INPUT (DEBUG): TokenStream [
Ident {
ident: "struct",
span: #0 bytes(0..0),
},
Ident {
ident: "A",
span: #0 bytes(0..0),
},
Group {
delimiter: Brace,
stream: TokenStream [],
span: #0 bytes(0..0),
},
]