Skip to content

Commit 7a30bb1

Browse files
committed
Address review comments
1 parent 9be233c commit 7a30bb1

File tree

4 files changed

+22
-17
lines changed

4 files changed

+22
-17
lines changed

src/librustc_ast/token.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,15 @@ pub enum TokenKind {
225225
/* Literals */
226226
Literal(Lit),
227227

228-
/* Name components */
228+
/// Identifier token.
229+
/// Do not forget about `NtIdent` when you want to match on identifiers.
230+
/// It's recommended to use `Token::(ident,uninterpolate,uninterpolated_span)` to
231+
/// treat regular and interpolated identifiers in the same way.
229232
Ident(ast::Name, /* is_raw */ bool),
233+
/// Lifetime identifier token.
234+
/// Do not forget about `NtLifetime` when you want to match on lifetime identifiers.
235+
/// It's recommended to use `Token::(lifetime,uninterpolate,uninterpolated_span)` to
236+
/// treat regular and interpolated lifetime identifiers in the same way.
230237
Lifetime(ast::Name),
231238

232239
Interpolated(Lrc<Nonterminal>),
@@ -328,11 +335,12 @@ impl Token {
328335
mem::replace(self, Token::dummy())
329336
}
330337

331-
/// For interpolated tokens returns a span of the fragment to which the interpolated
332-
/// token refers, for all other tokens this is just a regular span.
338+
/// For interpolated tokens, returns a span of the fragment to which the interpolated
339+
/// token refers. For all other tokens this is just a regular span.
333340
/// It is particularly important to use this for identifiers and lifetimes
334-
/// for which spans affect name resolution. This also includes edition checks
335-
/// for edition-specific keyword identifiers.
341+
/// for which spans affect name resolution and edition checks.
342+
/// Note that keywords are also identifiers, so they should use this
343+
/// if they keep spans or perform edition checks.
336344
pub fn uninterpolated_span(&self) -> Span {
337345
match &self.kind {
338346
Interpolated(nt) => nt.span(),
@@ -453,6 +461,7 @@ impl Token {
453461
}
454462
}
455463

464+
// A convenience function for matching on identifiers during parsing.
456465
// Turns interpolated identifier (`$i: ident`) or lifetime (`$l: lifetime`) token
457466
// into the regular identifier or lifetime token it refers to,
458467
// otherwise returns the original token.

src/librustc_expand/mbe/macro_parser.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -751,10 +751,7 @@ pub(super) fn parse_tt(parser: &mut Cow<'_, Parser<'_>>, ms: &[TokenTree]) -> Na
751751
/// The token is an identifier, but not `_`.
752752
/// We prohibit passing `_` to macros expecting `ident` for now.
753753
fn get_macro_ident(token: &Token) -> Option<(Ident, bool)> {
754-
match token.ident() {
755-
Some((ident, is_raw)) if ident.name != kw::Underscore => Some((ident, is_raw)),
756-
_ => None,
757-
}
754+
token.ident().filter(|(ident, _)| ident.name != kw::Underscore)
758755
}
759756

760757
/// Checks whether a non-terminal may begin with a particular token.

src/librustc_parse/parser/expr.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,8 @@ impl<'a> Parser<'a> {
9797
match self.parse_expr() {
9898
Ok(expr) => Ok(expr),
9999
Err(mut err) => match self.token.ident() {
100-
Some((ident, false))
101-
if ident.name == kw::Underscore
102-
&& self.look_ahead(1, |t| t == &token::Comma) =>
100+
Some((Ident { name: kw::Underscore, .. }, false))
101+
if self.look_ahead(1, |t| t == &token::Comma) =>
103102
{
104103
// Special-case handling of `foo(_, _, _)`
105104
err.emit();
@@ -333,13 +332,13 @@ impl<'a> Parser<'a> {
333332
fn check_assoc_op(&self) -> Option<Spanned<AssocOp>> {
334333
let (op, span) = match (AssocOp::from_token(&self.token), self.token.ident()) {
335334
(Some(op), _) => (op, self.token.span),
336-
(None, Some((ident, false))) if ident.name == sym::and => {
335+
(None, Some((Ident { name: sym::and, span }, false))) => {
337336
self.error_bad_logical_op("and", "&&", "conjunction");
338-
(AssocOp::LAnd, ident.span)
337+
(AssocOp::LAnd, span)
339338
}
340-
(None, Some((ident, false))) if ident.name == sym::or => {
339+
(None, Some((Ident { name: sym::or, span }, false))) => {
341340
self.error_bad_logical_op("or", "||", "disjunction");
342-
(AssocOp::LOr, ident.span)
341+
(AssocOp::LOr, span)
343342
}
344343
_ => return None,
345344
};

src/librustc_parse/parser/item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ impl<'a> Parser<'a> {
751751

752752
fn parse_ident_or_underscore(&mut self) -> PResult<'a, ast::Ident> {
753753
match self.token.ident() {
754-
Some((ident, false)) if ident.name == kw::Underscore => {
754+
Some((ident @ Ident { name: kw::Underscore, .. }, false)) => {
755755
self.bump();
756756
Ok(ident)
757757
}

0 commit comments

Comments
 (0)