Skip to content

Commit 8b4bdc2

Browse files
committed
refactor lifetime out of is_lifetime
1 parent f552425 commit 8b4bdc2

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

src/libsyntax/parse/parser.rs

+6-13
Original file line numberDiff line numberDiff line change
@@ -2036,19 +2036,12 @@ impl<'a> Parser<'a> {
20362036

20372037
/// Parse single lifetime 'a or panic.
20382038
pub fn expect_lifetime(&mut self) -> Lifetime {
2039-
let lifetime = match self.token {
2040-
token::Lifetime(ident) =>
2041-
Lifetime { ident: ident, span: self.span, id: ast::DUMMY_NODE_ID },
2042-
token::Interpolated(ref nt) => match nt.0 {
2043-
token::NtLifetime(lifetime) =>
2044-
lifetime,
2045-
_ => self.span_bug(self.span, "not a lifetime")
2046-
}
2047-
_ => self.span_bug(self.span, "not a lifetime")
2048-
};
2049-
2050-
self.bump();
2051-
lifetime
2039+
if let Some(lifetime) = self.token.lifetime(self.span) {
2040+
self.bump();
2041+
lifetime
2042+
} else {
2043+
self.span_bug(self.span, "not a lifetime")
2044+
}
20522045
}
20532046

20542047
/// Parse mutability (`mut` or nothing).

src/libsyntax/parse/token.rs

+14-6
Original file line numberDiff line numberDiff line change
@@ -314,18 +314,26 @@ impl Token {
314314
false
315315
}
316316

317-
/// Returns `true` if the token is a lifetime.
318-
pub fn is_lifetime(&self) -> bool {
317+
/// Returns a lifetime with the span and a dummy id if it is a lifetime,
318+
/// or the original lifetime if it is an interpolated lifetime, ignoring
319+
/// the span.
320+
pub fn lifetime(&self, span: Span) -> Option<ast::Lifetime> {
319321
match *self {
320-
Lifetime(..) => true,
322+
Lifetime(ident) =>
323+
Some(ast::Lifetime { ident: ident, span: span, id: ast::DUMMY_NODE_ID }),
321324
Interpolated(ref nt) => match nt.0 {
322-
NtLifetime(..) => true,
323-
_ => false,
325+
NtLifetime(lifetime) => Some(lifetime),
326+
_ => None,
324327
},
325-
_ => false,
328+
_ => None,
326329
}
327330
}
328331

332+
/// Returns `true` if the token is a lifetime.
333+
pub fn is_lifetime(&self) -> bool {
334+
self.lifetime(syntax_pos::DUMMY_SP).is_some()
335+
}
336+
329337
/// Returns `true` if the token is either the `mut` or `const` keyword.
330338
pub fn is_mutability(&self) -> bool {
331339
self.is_keyword(keywords::Mut) ||

0 commit comments

Comments
 (0)