Skip to content

Commit

Permalink
Add some missing support for NtIdent
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Feb 24, 2020
1 parent 79cd224 commit 59261f0
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 36 deletions.
6 changes: 6 additions & 0 deletions src/librustc_expand/mbe/macro_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,12 @@ pub(super) fn parse_tt(parser: &mut Cow<'_, Parser<'_>>, ms: &[TokenTree]) -> Na
fn get_macro_name(token: &Token) -> Option<(Name, bool)> {
match token.kind {
token::Ident(name, is_raw) if name != kw::Underscore => Some((name, is_raw)),
token::Interpolated(ref nt) => match **nt {
token::NtIdent(ident, is_raw) if ident.name != kw::Underscore => {
Some((ident.name, is_raw))
}
_ => None,
},
_ => None,
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/librustc_parse/parser/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use syntax::ast::{
};
use syntax::ast::{AttrVec, ItemKind, Mutability, Pat, PatKind, PathSegment, QSelf, Ty, TyKind};
use syntax::ptr::P;
use syntax::token::{self, token_can_begin_expr, TokenKind};
use syntax::token::{self, TokenKind};
use syntax::util::parser::AssocOp;

use log::{debug, trace};
Expand Down Expand Up @@ -900,8 +900,7 @@ impl<'a> Parser<'a> {
} else if !sm.is_multiline(self.prev_span.until(self.token.span)) {
// The current token is in the same line as the prior token, not recoverable.
} else if self.look_ahead(1, |t| {
t == &token::CloseDelim(token::Brace)
|| token_can_begin_expr(t) && t.kind != token::Colon
t == &token::CloseDelim(token::Brace) || t.can_begin_expr() && t.kind != token::Colon
}) && [token::Comma, token::Colon].contains(&self.token.kind)
{
// Likely typo: `,` → `;` or `:` → `;`. This is triggered if the current token is
Expand All @@ -919,7 +918,7 @@ impl<'a> Parser<'a> {
} else if self.look_ahead(0, |t| {
t == &token::CloseDelim(token::Brace)
|| (
token_can_begin_expr(t) && t != &token::Semi && t != &token::Pound
t.can_begin_expr() && t != &token::Semi && t != &token::Pound
// Avoid triggering with too many trailing `#` in raw string.
)
}) {
Expand Down
54 changes: 25 additions & 29 deletions src/libsyntax/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,36 +147,30 @@ impl Lit {

pub fn ident_can_begin_expr(name: ast::Name, span: Span, is_raw: bool) -> bool {
let ident_token = Token::new(Ident(name, is_raw), span);
token_can_begin_expr(&ident_token)
}

pub fn token_can_begin_expr(ident_token: &Token) -> bool {
!ident_token.is_reserved_ident()
|| ident_token.is_path_segment_keyword()
|| match ident_token.kind {
TokenKind::Ident(ident, _) => [
kw::Async,
kw::Do,
kw::Box,
kw::Break,
kw::Continue,
kw::False,
kw::For,
kw::If,
kw::Let,
kw::Loop,
kw::Match,
kw::Move,
kw::Return,
kw::True,
kw::Unsafe,
kw::While,
kw::Yield,
kw::Static,
]
.contains(&ident),
_ => false,
}
|| [
kw::Async,
kw::Do,
kw::Box,
kw::Break,
kw::Continue,
kw::False,
kw::For,
kw::If,
kw::Let,
kw::Loop,
kw::Match,
kw::Move,
kw::Return,
kw::True,
kw::Unsafe,
kw::While,
kw::Yield,
kw::Static,
]
.contains(&name)
}

fn ident_can_begin_type(name: ast::Name, span: Span, is_raw: bool) -> bool {
Expand Down Expand Up @@ -369,8 +363,8 @@ impl Token {
Lifetime(..) | // labeled loop
Pound => true, // expression attributes
Interpolated(ref nt) => match **nt {
NtIdent(ident, is_raw) => ident_can_begin_expr(ident.name, ident.span, is_raw),
NtLiteral(..) |
NtIdent(..) |
NtExpr(..) |
NtBlock(..) |
NtPath(..) |
Expand All @@ -397,7 +391,8 @@ impl Token {
Lt | BinOp(Shl) | // associated path
ModSep => true, // global path
Interpolated(ref nt) => match **nt {
NtIdent(..) | NtTy(..) | NtPath(..) | NtLifetime(..) => true,
NtIdent(ident, is_raw) => ident_can_begin_type(ident.name, ident.span, is_raw),
NtTy(..) | NtPath(..) | NtLifetime(..) => true,
_ => false,
},
_ => false,
Expand Down Expand Up @@ -442,6 +437,7 @@ impl Token {
Literal(..) | BinOp(Minus) => true,
Ident(name, false) if name.is_bool_lit() => true,
Interpolated(ref nt) => match &**nt {
NtIdent(ident, false) if ident.name.is_bool_lit() => true,
NtExpr(e) | NtLiteral(e) => matches!(e.kind, ast::ExprKind::Lit(_)),
_ => false,
},
Expand Down
13 changes: 10 additions & 3 deletions src/libsyntax/util/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,17 @@ impl Lit {
}
token::Literal(lit) => lit,
token::Interpolated(ref nt) => {
if let token::NtExpr(expr) | token::NtLiteral(expr) = &**nt {
if let ast::ExprKind::Lit(lit) = &expr.kind {
return Ok(lit.clone());
match &**nt {
token::NtIdent(ident, false) if ident.name.is_bool_lit() => {
let lit = token::Lit::new(token::Bool, ident.name, None);
return Lit::from_lit_token(lit, ident.span);
}
token::NtExpr(expr) | token::NtLiteral(expr) => {
if let ast::ExprKind::Lit(lit) = &expr.kind {
return Ok(lit.clone());
}
}
_ => {}
}
return Err(LitError::NotLiteral);
}
Expand Down

0 comments on commit 59261f0

Please sign in to comment.