diff options
| author | bors <bors@rust-lang.org> | 2018-06-11 10:43:20 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2018-06-11 10:43:20 +0000 |
| commit | 13f8d073fe5c87638d9ebe849c5fdd80e87da95a (patch) | |
| tree | 0d0471bcb122e2d35c2ad951a2555c6f08e3da20 | |
| parent | 18a00bd9855421a74f40a29fcb4dd2e0c8bea59f (diff) | |
| parent | 987020846ce40ce479df3d62adc47ca4609a5317 (diff) | |
| download | rust-13f8d073fe5c87638d9ebe849c5fdd80e87da95a.tar.gz rust-13f8d073fe5c87638d9ebe849c5fdd80e87da95a.zip | |
Auto merge of #51480 - dtolnay:lifetime, r=kennytm
Enable fall through past $:lifetime matcher
```rust
macro_rules! is_lifetime {
($lifetime:lifetime) => { true };
($other:tt) => { false };
}
fn main() {
println!("{}", is_lifetime!('lifetime));
println!("{}", is_lifetime!(@));
}
```
Before this fix, the `is_lifetime!` invocation would fail to compile with:
```
error: expected a lifetime, found `@`
--> src/main.rs:8:33
|
8 | println!("{}", is_lifetime!(@));
| ^
```
Fixes #50903.
Fixes #51477.
r? @kennytm
| -rw-r--r-- | src/libsyntax/ext/tt/macro_parser.rs | 8 | ||||
| -rw-r--r-- | src/test/compile-fail/macro-non-lifetime.rs | 2 | ||||
| -rw-r--r-- | src/test/run-pass/macro-first-set.rs | 43 |
3 files changed, 52 insertions, 1 deletions
diff --git a/src/libsyntax/ext/tt/macro_parser.rs b/src/libsyntax/ext/tt/macro_parser.rs index b53d94db27e..fe458fa9977 100644 --- a/src/libsyntax/ext/tt/macro_parser.rs +++ b/src/libsyntax/ext/tt/macro_parser.rs @@ -827,6 +827,14 @@ fn may_begin_with(name: &str, token: &Token) -> bool { Token::Interpolated(ref nt) => may_be_ident(&nt.0), _ => false, }, + "lifetime" => match *token { + Token::Lifetime(_) => true, + Token::Interpolated(ref nt) => match nt.0 { + token::NtLifetime(_) | token::NtTT(_) => true, + _ => false, + }, + _ => false, + }, _ => match *token { token::CloseDelim(_) => false, _ => true, diff --git a/src/test/compile-fail/macro-non-lifetime.rs b/src/test/compile-fail/macro-non-lifetime.rs index a2706e83229..647d7aaa759 100644 --- a/src/test/compile-fail/macro-non-lifetime.rs +++ b/src/test/compile-fail/macro-non-lifetime.rs @@ -16,5 +16,5 @@ macro_rules! m { ($x:lifetime) => { } } fn main() { m!(a); - //~^ ERROR expected a lifetime, found `a` + //~^ ERROR no rules expected the token `a` } diff --git a/src/test/run-pass/macro-first-set.rs b/src/test/run-pass/macro-first-set.rs index 99e5d22fb47..c371a33257f 100644 --- a/src/test/run-pass/macro-first-set.rs +++ b/src/test/run-pass/macro-first-set.rs @@ -199,6 +199,40 @@ fn test_24189() { //}}} +//{{{ issue 50903 ============================================================== + +macro_rules! foo_50903 { + ($($lif:lifetime ,)* #) => {}; +} + +foo_50903!('a, 'b, #); +foo_50903!('a, #); +foo_50903!(#); + +//}}} + +//{{{ issue 51477 ============================================================== + +macro_rules! foo_51477 { + ($lifetime:lifetime) => { + "last token is lifetime" + }; + ($other:tt) => { + "last token is other" + }; + ($first:tt $($rest:tt)*) => { + foo_51477!($($rest)*) + }; +} + +fn test_51477() { + assert_eq!("last token is lifetime", foo_51477!('a)); + assert_eq!("last token is other", foo_51477!(@)); + assert_eq!("last token is lifetime", foo_51477!(@ {} 'a)); +} + +//}}} + //{{{ some more tests ========================================================== macro_rules! test_block { @@ -234,6 +268,14 @@ macro_rules! test_meta_block { test_meta_block!(windows {}); +macro_rules! test_lifetime { + (1. $($l:lifetime)* $($b:block)*) => {}; + (2. $($b:block)* $($l:lifetime)*) => {}; +} + +test_lifetime!(1. 'a 'b {} {}); +test_lifetime!(2. {} {} 'a 'b); + //}}} fn main() { @@ -241,5 +283,6 @@ fn main() { test_40569(); test_35650(); test_24189(); + test_51477(); } |
