Skip to content

Commit 43b27df

Browse files
committed
rustc_ast: Introduce Token::uninterpolate
1 parent f4a03c4 commit 43b27df

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

src/librustc_ast/token.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use rustc_macros::HashStable_Generic;
1414
use rustc_span::symbol::kw;
1515
use rustc_span::symbol::Symbol;
1616
use rustc_span::{self, Span, DUMMY_SP};
17-
use std::fmt;
18-
use std::mem;
17+
use std::borrow::Cow;
18+
use std::{fmt, mem};
1919

2020
#[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
2121
#[derive(HashStable_Generic)]
@@ -457,6 +457,22 @@ impl Token {
457457
}
458458
}
459459

460+
// Turns interpolated identifier (`$i: ident`) or lifetime (`$l: lifetime`) token
461+
// into the regular identifier or lifetime token it refers to,
462+
// otherwise returns the original token.
463+
pub fn uninterpolate(&self) -> Cow<'_, Token> {
464+
match &self.kind {
465+
Interpolated(nt) => match **nt {
466+
NtIdent(ident, is_raw) => {
467+
Cow::Owned(Token::new(Ident(ident.name, is_raw), ident.span))
468+
}
469+
NtLifetime(ident) => Cow::Owned(Token::new(Lifetime(ident.name), ident.span)),
470+
_ => Cow::Borrowed(self),
471+
},
472+
_ => Cow::Borrowed(self),
473+
}
474+
}
475+
460476
/// Returns an identifier if this token is an identifier.
461477
pub fn ident(&self) -> Option<(ast::Ident, /* is_raw */ bool)> {
462478
match self.kind {

src/librustc_parse/parser/expr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ impl<'a> Parser<'a> {
435435
let attrs = self.parse_or_use_outer_attributes(attrs)?;
436436
let lo = self.token.span;
437437
// Note: when adding new unary operators, don't forget to adjust TokenKind::can_begin_expr()
438-
let (hi, ex) = match self.normalized_token.kind {
438+
let (hi, ex) = match self.token.uninterpolate().kind {
439439
token::Not => self.parse_unary_expr(lo, UnOp::Not), // `!expr`
440440
token::Tilde => self.recover_tilde_expr(lo), // `~expr`
441441
token::BinOp(token::Minus) => self.parse_unary_expr(lo, UnOp::Neg), // `-expr`
@@ -755,7 +755,7 @@ impl<'a> Parser<'a> {
755755
}
756756

757757
fn parse_dot_suffix_expr(&mut self, lo: Span, base: P<Expr>) -> PResult<'a, P<Expr>> {
758-
match self.normalized_token.kind {
758+
match self.token.uninterpolate().kind {
759759
token::Ident(..) => self.parse_dot_suffix(base, lo),
760760
token::Literal(token::Lit { kind: token::Integer, symbol, suffix }) => {
761761
Ok(self.parse_tuple_field_access_expr(lo, base, symbol, suffix))

src/librustc_parse/parser/item.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,7 +1544,7 @@ impl<'a> Parser<'a> {
15441544

15451545
let is_name_required = match self.token.kind {
15461546
token::DotDotDot => false,
1547-
_ => req_name(self.normalized_token.span.edition()),
1547+
_ => req_name(self.token.uninterpolate().span.edition()),
15481548
};
15491549
let (pat, ty) = if is_name_required || self.is_named_param() {
15501550
debug!("parse_param_general parse_pat (is_name_required:{})", is_name_required);
@@ -1648,7 +1648,7 @@ impl<'a> Parser<'a> {
16481648
// Only a limited set of initial token sequences is considered `self` parameters; anything
16491649
// else is parsed as a normal function parameter list, so some lookahead is required.
16501650
let eself_lo = self.token.span;
1651-
let (eself, eself_ident, eself_hi) = match self.normalized_token.kind {
1651+
let (eself, eself_ident, eself_hi) = match self.token.uninterpolate().kind {
16521652
token::BinOp(token::And) => {
16531653
let eself = if is_isolated_self(self, 1) {
16541654
// `&self`

0 commit comments

Comments
 (0)