Skip to content

Commit bfc7502

Browse files
committed
Use Token::uninterpolate in couple more places matching on (Nt)Ident
1 parent caab03a commit bfc7502

File tree

7 files changed

+28
-38
lines changed

7 files changed

+28
-38
lines changed

Diff for: src/librustc_ast/attr/mod.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ impl MetaItem {
441441
I: Iterator<Item = TokenTree>,
442442
{
443443
// FIXME: Share code with `parse_path`.
444-
let path = match tokens.next() {
444+
let path = match tokens.next().map(TokenTree::uninterpolate) {
445445
Some(TokenTree::Token(Token { kind: kind @ token::Ident(..), span }))
446446
| Some(TokenTree::Token(Token { kind: kind @ token::ModSep, span })) => 'arm: {
447447
let mut segments = if let token::Ident(name, _) = kind {
@@ -457,7 +457,7 @@ impl MetaItem {
457457
};
458458
loop {
459459
if let Some(TokenTree::Token(Token { kind: token::Ident(name, _), span })) =
460-
tokens.next()
460+
tokens.next().map(TokenTree::uninterpolate)
461461
{
462462
segments.push(PathSegment::from_ident(Ident::new(name, span)));
463463
} else {
@@ -474,7 +474,6 @@ impl MetaItem {
474474
Path { span, segments }
475475
}
476476
Some(TokenTree::Token(Token { kind: token::Interpolated(nt), .. })) => match *nt {
477-
token::Nonterminal::NtIdent(ident, _) => Path::from_ident(ident),
478477
token::Nonterminal::NtMeta(ref item) => return item.meta(item.path.span),
479478
token::Nonterminal::NtPath(ref path) => path.clone(),
480479
_ => return None,

Diff for: src/librustc_ast/token.rs

+11-21
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ impl Token {
357357

358358
/// Returns `true` if the token can appear at the start of an expression.
359359
pub fn can_begin_expr(&self) -> bool {
360-
match self.kind {
360+
match self.uninterpolate().kind {
361361
Ident(name, is_raw) =>
362362
ident_can_begin_expr(name, self.span, is_raw), // value name or keyword
363363
OpenDelim(..) | // tuple, array or block
@@ -375,12 +375,10 @@ impl Token {
375375
Lifetime(..) | // labeled loop
376376
Pound => true, // expression attributes
377377
Interpolated(ref nt) => match **nt {
378-
NtIdent(ident, is_raw) => ident_can_begin_expr(ident.name, ident.span, is_raw),
379378
NtLiteral(..) |
380379
NtExpr(..) |
381380
NtBlock(..) |
382-
NtPath(..) |
383-
NtLifetime(..) => true,
381+
NtPath(..) => true,
384382
_ => false,
385383
},
386384
_ => false,
@@ -389,7 +387,7 @@ impl Token {
389387

390388
/// Returns `true` if the token can appear at the start of a type.
391389
pub fn can_begin_type(&self) -> bool {
392-
match self.kind {
390+
match self.uninterpolate().kind {
393391
Ident(name, is_raw) =>
394392
ident_can_begin_type(name, self.span, is_raw), // type name or keyword
395393
OpenDelim(Paren) | // tuple
@@ -403,8 +401,7 @@ impl Token {
403401
Lt | BinOp(Shl) | // associated path
404402
ModSep => true, // global path
405403
Interpolated(ref nt) => match **nt {
406-
NtIdent(ident, is_raw) => ident_can_begin_type(ident.name, ident.span, is_raw),
407-
NtTy(..) | NtPath(..) | NtLifetime(..) => true,
404+
NtTy(..) | NtPath(..) => true,
408405
_ => false,
409406
},
410407
_ => false,
@@ -445,11 +442,10 @@ impl Token {
445442
///
446443
/// Keep this in sync with `Lit::from_token`.
447444
pub fn can_begin_literal_or_bool(&self) -> bool {
448-
match self.kind {
445+
match self.uninterpolate().kind {
449446
Literal(..) | BinOp(Minus) => true,
450447
Ident(name, false) if name.is_bool_lit() => true,
451448
Interpolated(ref nt) => match &**nt {
452-
NtIdent(ident, false) if ident.name.is_bool_lit() => true,
453449
NtExpr(e) | NtLiteral(e) => matches!(e.kind, ast::ExprKind::Lit(_)),
454450
_ => false,
455451
},
@@ -475,24 +471,18 @@ impl Token {
475471

476472
/// Returns an identifier if this token is an identifier.
477473
pub fn ident(&self) -> Option<(ast::Ident, /* is_raw */ bool)> {
478-
match self.kind {
479-
Ident(name, is_raw) => Some((ast::Ident::new(name, self.span), is_raw)),
480-
Interpolated(ref nt) => match **nt {
481-
NtIdent(ident, is_raw) => Some((ident, is_raw)),
482-
_ => None,
483-
},
474+
let token = self.uninterpolate();
475+
match token.kind {
476+
Ident(name, is_raw) => Some((ast::Ident::new(name, token.span), is_raw)),
484477
_ => None,
485478
}
486479
}
487480

488481
/// Returns a lifetime identifier if this token is a lifetime.
489482
pub fn lifetime(&self) -> Option<ast::Ident> {
490-
match self.kind {
491-
Lifetime(name) => Some(ast::Ident::new(name, self.span)),
492-
Interpolated(ref nt) => match **nt {
493-
NtLifetime(ident) => Some(ident),
494-
_ => None,
495-
},
483+
let token = self.uninterpolate();
484+
match token.kind {
485+
Lifetime(name) => Some(ast::Ident::new(name, token.span)),
496486
_ => None,
497487
}
498488
}

Diff for: src/librustc_ast/tokenstream.rs

+7
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,13 @@ impl TokenTree {
116116
pub fn close_tt(span: DelimSpan, delim: DelimToken) -> TokenTree {
117117
TokenTree::token(token::CloseDelim(delim), span.close)
118118
}
119+
120+
pub fn uninterpolate(self) -> TokenTree {
121+
match self {
122+
TokenTree::Token(token) => TokenTree::Token(token.uninterpolate().into_owned()),
123+
tt => tt,
124+
}
125+
}
119126
}
120127

121128
impl<CTX> HashStable<CTX> for TokenStream

Diff for: src/librustc_ast/util/literal.rs

+4-11
Original file line numberDiff line numberDiff line change
@@ -191,23 +191,16 @@ impl Lit {
191191
///
192192
/// Keep this in sync with `Token::can_begin_literal_or_bool`.
193193
pub fn from_token(token: &Token) -> Result<Lit, LitError> {
194-
let lit = match token.kind {
194+
let lit = match token.uninterpolate().kind {
195195
token::Ident(name, false) if name.is_bool_lit() => {
196196
token::Lit::new(token::Bool, name, None)
197197
}
198198
token::Literal(lit) => lit,
199199
token::Interpolated(ref nt) => {
200-
match &**nt {
201-
token::NtIdent(ident, false) if ident.name.is_bool_lit() => {
202-
let lit = token::Lit::new(token::Bool, ident.name, None);
203-
return Lit::from_lit_token(lit, ident.span);
200+
if let token::NtExpr(expr) | token::NtLiteral(expr) = &**nt {
201+
if let ast::ExprKind::Lit(lit) = &expr.kind {
202+
return Ok(lit.clone());
204203
}
205-
token::NtExpr(expr) | token::NtLiteral(expr) => {
206-
if let ast::ExprKind::Lit(lit) = &expr.kind {
207-
return Ok(lit.clone());
208-
}
209-
}
210-
_ => {}
211204
}
212205
return Err(LitError::NotLiteral);
213206
}

Diff for: src/librustc_parse/parser/expr.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ macro_rules! maybe_whole_expr {
5050
AttrVec::new(),
5151
));
5252
}
53-
// N.B., `NtIdent(ident)` is normalized to `Ident` in `fn bump`.
5453
_ => {}
5554
};
5655
}
@@ -482,7 +481,7 @@ impl<'a> Parser<'a> {
482481
}
483482

484483
fn is_mistaken_not_ident_negation(&self) -> bool {
485-
let token_cannot_continue_expr = |t: &Token| match t.kind {
484+
let token_cannot_continue_expr = |t: &Token| match t.uninterpolate().kind {
486485
// These tokens can start an expression after `!`, but
487486
// can't continue an expression after an ident
488487
token::Ident(name, is_raw) => token::ident_can_begin_expr(name, t.span, is_raw),

Diff for: src/librustc_parse/parser/item.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1544,6 +1544,8 @@ impl<'a> Parser<'a> {
15441544

15451545
let is_name_required = match self.token.kind {
15461546
token::DotDotDot => false,
1547+
// FIXME: Consider using interpolated token for this edition check,
1548+
// it should match the intent of edition hygiene better.
15471549
_ => req_name(self.token.uninterpolate().span.edition()),
15481550
};
15491551
let (pat, ty) = if is_name_required || self.is_named_param() {

Diff for: src/librustc_parse/parser/pat.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ impl<'a> Parser<'a> {
151151
/// Note that there are more tokens such as `@` for which we know that the `|`
152152
/// is an illegal parse. However, the user's intent is less clear in that case.
153153
fn recover_trailing_vert(&mut self, lo: Option<Span>) -> bool {
154-
let is_end_ahead = self.look_ahead(1, |token| match &token.kind {
154+
let is_end_ahead = self.look_ahead(1, |token| match &token.uninterpolate().kind {
155155
token::FatArrow // e.g. `a | => 0,`.
156156
| token::Ident(kw::If, false) // e.g. `a | if expr`.
157157
| token::Eq // e.g. `let a | = 0`.

0 commit comments

Comments
 (0)