Skip to content

Commit 13f8d07

Browse files
committed
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
2 parents 18a00bd + 9870208 commit 13f8d07

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

Diff for: src/libsyntax/ext/tt/macro_parser.rs

+8
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,14 @@ fn may_begin_with(name: &str, token: &Token) -> bool {
827827
Token::Interpolated(ref nt) => may_be_ident(&nt.0),
828828
_ => false,
829829
},
830+
"lifetime" => match *token {
831+
Token::Lifetime(_) => true,
832+
Token::Interpolated(ref nt) => match nt.0 {
833+
token::NtLifetime(_) | token::NtTT(_) => true,
834+
_ => false,
835+
},
836+
_ => false,
837+
},
830838
_ => match *token {
831839
token::CloseDelim(_) => false,
832840
_ => true,

Diff for: src/test/compile-fail/macro-non-lifetime.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ macro_rules! m { ($x:lifetime) => { } }
1616

1717
fn main() {
1818
m!(a);
19-
//~^ ERROR expected a lifetime, found `a`
19+
//~^ ERROR no rules expected the token `a`
2020
}

Diff for: src/test/run-pass/macro-first-set.rs

+43
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,40 @@ fn test_24189() {
199199

200200
//}}}
201201

202+
//{{{ issue 50903 ==============================================================
203+
204+
macro_rules! foo_50903 {
205+
($($lif:lifetime ,)* #) => {};
206+
}
207+
208+
foo_50903!('a, 'b, #);
209+
foo_50903!('a, #);
210+
foo_50903!(#);
211+
212+
//}}}
213+
214+
//{{{ issue 51477 ==============================================================
215+
216+
macro_rules! foo_51477 {
217+
($lifetime:lifetime) => {
218+
"last token is lifetime"
219+
};
220+
($other:tt) => {
221+
"last token is other"
222+
};
223+
($first:tt $($rest:tt)*) => {
224+
foo_51477!($($rest)*)
225+
};
226+
}
227+
228+
fn test_51477() {
229+
assert_eq!("last token is lifetime", foo_51477!('a));
230+
assert_eq!("last token is other", foo_51477!(@));
231+
assert_eq!("last token is lifetime", foo_51477!(@ {} 'a));
232+
}
233+
234+
//}}}
235+
202236
//{{{ some more tests ==========================================================
203237

204238
macro_rules! test_block {
@@ -234,12 +268,21 @@ macro_rules! test_meta_block {
234268

235269
test_meta_block!(windows {});
236270

271+
macro_rules! test_lifetime {
272+
(1. $($l:lifetime)* $($b:block)*) => {};
273+
(2. $($b:block)* $($l:lifetime)*) => {};
274+
}
275+
276+
test_lifetime!(1. 'a 'b {} {});
277+
test_lifetime!(2. {} {} 'a 'b);
278+
237279
//}}}
238280

239281
fn main() {
240282
test_26444();
241283
test_40569();
242284
test_35650();
243285
test_24189();
286+
test_51477();
244287
}
245288

0 commit comments

Comments
 (0)