Skip to content

Commit bab812d

Browse files
committed
Rollup merge of #50416 - rleungx:non-lifetime, r=estebank
check if the token is a lifetime before parsing Fixes #50381.
2 parents 4cc4a67 + 390c3ce commit bab812d

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

src/libsyntax/ext/tt/macro_parser.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,13 @@ fn parse_nt<'a>(p: &mut Parser<'a>, sp: Span, name: &str) -> Nonterminal {
835835
"path" => token::NtPath(panictry!(p.parse_path_common(PathStyle::Type, false))),
836836
"meta" => token::NtMeta(panictry!(p.parse_meta_item())),
837837
"vis" => token::NtVis(panictry!(p.parse_visibility(true))),
838-
"lifetime" => token::NtLifetime(p.expect_lifetime().ident),
838+
"lifetime" => if p.check_lifetime() {
839+
token::NtLifetime(p.expect_lifetime().ident)
840+
} else {
841+
let token_str = pprust::token_to_string(&p.token);
842+
p.fatal(&format!("expected a lifetime, found `{}`", &token_str)).emit();
843+
FatalError.raise();
844+
}
839845
// this is not supposed to happen, since it has been checked
840846
// when compiling the macro.
841847
_ => p.span_bug(sp, "invalid fragment specifier"),

src/libsyntax/parse/parser.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2042,7 +2042,7 @@ impl<'a> Parser<'a> {
20422042
})
20432043
}
20442044

2045-
fn check_lifetime(&mut self) -> bool {
2045+
pub fn check_lifetime(&mut self) -> bool {
20462046
self.expected_tokens.push(TokenType::Lifetime);
20472047
self.token.is_lifetime()
20482048
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Test for issue #50381: non-lifetime passed to :lifetime.
12+
13+
#![feature(macro_lifetime_matcher)]
14+
15+
macro_rules! m { ($x:lifetime) => { } }
16+
17+
fn main() {
18+
m!(a);
19+
//~^ ERROR expected a lifetime, found `a`
20+
}

0 commit comments

Comments
 (0)