From 7a30bb1676690596f73659d18959877d993510ae Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Mon, 9 Mar 2020 12:42:33 +0300 Subject: [PATCH] Address review comments --- src/librustc_ast/token.rs | 19 ++++++++++++++----- src/librustc_expand/mbe/macro_parser.rs | 5 +---- src/librustc_parse/parser/expr.rs | 13 ++++++------- src/librustc_parse/parser/item.rs | 2 +- 4 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/librustc_ast/token.rs b/src/librustc_ast/token.rs index b022d969deccb..b67b7d346f756 100644 --- a/src/librustc_ast/token.rs +++ b/src/librustc_ast/token.rs @@ -225,8 +225,15 @@ pub enum TokenKind { /* Literals */ Literal(Lit), - /* Name components */ + /// Identifier token. + /// Do not forget about `NtIdent` when you want to match on identifiers. + /// It's recommended to use `Token::(ident,uninterpolate,uninterpolated_span)` to + /// treat regular and interpolated identifiers in the same way. Ident(ast::Name, /* is_raw */ bool), + /// Lifetime identifier token. + /// Do not forget about `NtLifetime` when you want to match on lifetime identifiers. + /// It's recommended to use `Token::(lifetime,uninterpolate,uninterpolated_span)` to + /// treat regular and interpolated lifetime identifiers in the same way. Lifetime(ast::Name), Interpolated(Lrc), @@ -328,11 +335,12 @@ impl Token { mem::replace(self, Token::dummy()) } - /// For interpolated tokens returns a span of the fragment to which the interpolated - /// token refers, for all other tokens this is just a regular span. + /// For interpolated tokens, returns a span of the fragment to which the interpolated + /// token refers. For all other tokens this is just a regular span. /// It is particularly important to use this for identifiers and lifetimes - /// for which spans affect name resolution. This also includes edition checks - /// for edition-specific keyword identifiers. + /// for which spans affect name resolution and edition checks. + /// Note that keywords are also identifiers, so they should use this + /// if they keep spans or perform edition checks. pub fn uninterpolated_span(&self) -> Span { match &self.kind { Interpolated(nt) => nt.span(), @@ -453,6 +461,7 @@ impl Token { } } + // A convenience function for matching on identifiers during parsing. // Turns interpolated identifier (`$i: ident`) or lifetime (`$l: lifetime`) token // into the regular identifier or lifetime token it refers to, // otherwise returns the original token. diff --git a/src/librustc_expand/mbe/macro_parser.rs b/src/librustc_expand/mbe/macro_parser.rs index efba3a8ccb1e6..6d4d7f5b4f394 100644 --- a/src/librustc_expand/mbe/macro_parser.rs +++ b/src/librustc_expand/mbe/macro_parser.rs @@ -751,10 +751,7 @@ pub(super) fn parse_tt(parser: &mut Cow<'_, Parser<'_>>, ms: &[TokenTree]) -> Na /// The token is an identifier, but not `_`. /// We prohibit passing `_` to macros expecting `ident` for now. fn get_macro_ident(token: &Token) -> Option<(Ident, bool)> { - match token.ident() { - Some((ident, is_raw)) if ident.name != kw::Underscore => Some((ident, is_raw)), - _ => None, - } + token.ident().filter(|(ident, _)| ident.name != kw::Underscore) } /// Checks whether a non-terminal may begin with a particular token. diff --git a/src/librustc_parse/parser/expr.rs b/src/librustc_parse/parser/expr.rs index d28b9cd968218..bfca9f07f05d5 100644 --- a/src/librustc_parse/parser/expr.rs +++ b/src/librustc_parse/parser/expr.rs @@ -97,9 +97,8 @@ impl<'a> Parser<'a> { match self.parse_expr() { Ok(expr) => Ok(expr), Err(mut err) => match self.token.ident() { - Some((ident, false)) - if ident.name == kw::Underscore - && self.look_ahead(1, |t| t == &token::Comma) => + Some((Ident { name: kw::Underscore, .. }, false)) + if self.look_ahead(1, |t| t == &token::Comma) => { // Special-case handling of `foo(_, _, _)` err.emit(); @@ -333,13 +332,13 @@ impl<'a> Parser<'a> { fn check_assoc_op(&self) -> Option> { let (op, span) = match (AssocOp::from_token(&self.token), self.token.ident()) { (Some(op), _) => (op, self.token.span), - (None, Some((ident, false))) if ident.name == sym::and => { + (None, Some((Ident { name: sym::and, span }, false))) => { self.error_bad_logical_op("and", "&&", "conjunction"); - (AssocOp::LAnd, ident.span) + (AssocOp::LAnd, span) } - (None, Some((ident, false))) if ident.name == sym::or => { + (None, Some((Ident { name: sym::or, span }, false))) => { self.error_bad_logical_op("or", "||", "disjunction"); - (AssocOp::LOr, ident.span) + (AssocOp::LOr, span) } _ => return None, }; diff --git a/src/librustc_parse/parser/item.rs b/src/librustc_parse/parser/item.rs index bf612bfc0e4a1..126686c8defbf 100644 --- a/src/librustc_parse/parser/item.rs +++ b/src/librustc_parse/parser/item.rs @@ -751,7 +751,7 @@ impl<'a> Parser<'a> { fn parse_ident_or_underscore(&mut self) -> PResult<'a, ast::Ident> { match self.token.ident() { - Some((ident, false)) if ident.name == kw::Underscore => { + Some((ident @ Ident { name: kw::Underscore, .. }, false)) => { self.bump(); Ok(ident) }