Skip to content

Commit 9e590de

Browse files
committed
Introduce helper Token::non_raw_ident
1 parent 025737e commit 9e590de

7 files changed

Lines changed: 39 additions & 47 deletions

File tree

compiler/rustc_ast/src/token.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -920,43 +920,43 @@ impl Token {
920920

921921
/// Returns `true` if the token is a given keyword, `kw`.
922922
pub fn is_keyword(&self, kw: Symbol) -> bool {
923-
self.is_non_raw_ident_where(|id| id.name == kw)
923+
self.non_raw_ident().is_some_and(|id| id.name == kw)
924924
}
925925

926926
/// Returns `true` if the token is a given keyword, `kw` or if `case` is `Insensitive` and this
927927
/// token is an identifier equal to `kw` ignoring the case.
928928
pub fn is_keyword_case(&self, kw: Symbol, case: Case) -> bool {
929929
self.is_keyword(kw)
930930
|| (case == Case::Insensitive
931-
&& self.is_non_raw_ident_where(|id| {
931+
&& self.non_raw_ident().is_some_and(|id| {
932932
// Do an ASCII case-insensitive match, because all keywords are ASCII.
933933
id.name.as_str().eq_ignore_ascii_case(kw.as_str())
934934
}))
935935
}
936936

937937
pub fn is_path_segment_keyword(&self) -> bool {
938-
self.is_non_raw_ident_where(sp::Ident::is_path_segment_keyword)
938+
self.non_raw_ident().is_some_and(sp::Ident::is_path_segment_keyword)
939939
}
940940

941941
/// Returns true for reserved identifiers used internally for elided lifetimes,
942942
/// unnamed method parameters, crate root module, error recovery etc.
943943
pub fn is_special_ident(&self) -> bool {
944-
self.is_non_raw_ident_where(sp::Ident::is_special)
944+
self.non_raw_ident().is_some_and(sp::Ident::is_special)
945945
}
946946

947947
/// Returns `true` if the token is a keyword used in the language.
948948
pub fn is_used_keyword(&self) -> bool {
949-
self.is_non_raw_ident_where(sp::Ident::is_used_keyword)
949+
self.non_raw_ident().is_some_and(sp::Ident::is_used_keyword)
950950
}
951951

952952
/// Returns `true` if the token is a keyword reserved for possible future use.
953953
pub fn is_unused_keyword(&self) -> bool {
954-
self.is_non_raw_ident_where(sp::Ident::is_unused_keyword)
954+
self.non_raw_ident().is_some_and(sp::Ident::is_unused_keyword)
955955
}
956956

957957
/// Returns `true` if the token is either a special identifier or a keyword.
958958
pub fn is_reserved_ident(&self) -> bool {
959-
self.is_non_raw_ident_where(sp::Ident::is_reserved)
959+
self.non_raw_ident().is_some_and(sp::Ident::is_reserved)
960960
}
961961

962962
pub fn is_non_reserved_ident(&self) -> bool {
@@ -975,7 +975,7 @@ impl Token {
975975

976976
/// Returns `true` if the token is the identifier `true` or `false`.
977977
pub fn is_bool_lit(&self) -> bool {
978-
self.is_non_raw_ident_where(|id| id.name.is_bool_lit())
978+
self.non_raw_ident().is_some_and(|id| id.name.is_bool_lit())
979979
}
980980

981981
pub fn is_numeric_lit(&self) -> bool {
@@ -990,11 +990,11 @@ impl Token {
990990
matches!(self.kind, Literal(Lit { kind: LitKind::Integer, .. }))
991991
}
992992

993-
/// Returns `true` if the token is a non-raw identifier for which `pred` holds.
994-
pub fn is_non_raw_ident_where(&self, pred: impl FnOnce(sp::Ident) -> bool) -> bool {
993+
/// Returns an identifier if this token is a non-raw identifier.
994+
pub fn non_raw_ident(&self) -> Option<sp::Ident> {
995995
match self.ident() {
996-
Some((id, IdentKind::Normal | IdentKind::ForcedKeyword)) => pred(id),
997-
_ => false,
996+
Some((id, IdentKind::Normal | IdentKind::ForcedKeyword)) => Some(id),
997+
_ => None,
998998
}
999999
}
10001000

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ impl<'a> Parser<'a> {
377377
// Recover from `and` and `or` which are mistaken for `&&` and `||` respectively.
378378
if self.may_recover()
379379
&& op.is_none()
380-
&& let Some((ident, IdentKind::Normal | IdentKind::ForcedKeyword)) = self.token.ident()
380+
&& let Some(ident) = self.token.non_raw_ident()
381381
{
382382
let (op, sub): (_, fn(_) -> _) = match ident.name {
383383
sym::and => (BinOpKind::And, diagnostics::InvalidLogicalOperatorSub::Conjunction),
@@ -685,9 +685,8 @@ impl<'a> Parser<'a> {
685685
// Check for typo of `'a: loop { break 'a }` with a missing `'`.
686686
if let ExprKind::Path(None, ast::Path { segments, .. }) = &lhs.kind
687687
&& let [segment] = segments.as_slice()
688-
&& self.token.is_non_raw_ident_where(|id| {
689-
matches!(id.name, kw::For | kw::Loop | kw::While)
690-
})
688+
&& let Some(ident) = self.token.non_raw_ident()
689+
&& let kw::For | kw::Loop | kw::While = ident.name
691690
{
692691
let snapshot = self.create_snapshot_for_diagnostic();
693692
let label = Label {

compiler/rustc_parse/src/parser/function.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use ast::token::IdentKind;
21
use rustc_ast as ast;
32
use rustc_ast::ast::*;
43
use rustc_ast::token::{self, InvisibleOrigin, MetaVarKind, TokenKind};
@@ -330,13 +329,13 @@ impl<'a> Parser<'a> {
330329
// Two qualifiers `$qual $qual` is enough, e.g. `async unsafe`.
331330
|| (
332331
(
333-
t.is_non_raw_ident_where(|i|
332+
t.non_raw_ident().is_some_and(|i|
334333
quals.iter().any(|exp| exp.kw == i.name)
335334
// Rule out 2015 `const async: T = val`.
336335
&& i.is_reserved()
337336
)
338337
|| case == Case::Insensitive
339-
&& t.is_non_raw_ident_where(|i| quals.iter().any(|exp| {
338+
&& t.non_raw_ident().is_some_and(|i| quals.iter().any(|exp| {
340339
exp.kw.as_str() == i.name.as_str().to_lowercase()
341340
}))
342341
)
@@ -837,12 +836,10 @@ impl<'a> Parser<'a> {
837836
/// Returns the parsed optional self parameter and whether a self shortcut was used.
838837
fn parse_self_param(&mut self) -> PResult<'a, Option<Param>> {
839838
// Extract an identifier *after* having confirmed that the token is one.
840-
let expect_self_ident = |this: &mut Self| match this.token.ident() {
841-
Some((ident, IdentKind::Normal | IdentKind::ForcedKeyword)) => {
842-
this.bump();
843-
ident
844-
}
845-
_ => unreachable!(),
839+
let expect_self_ident = |this: &mut Self| {
840+
let ident = this.token.non_raw_ident().unwrap();
841+
this.bump();
842+
ident
846843
};
847844
// is lifetime `n` tokens ahead?
848845
let is_lifetime = |this: &Self, n| this.look_ahead(n, |t| t.is_lifetime());

compiler/rustc_parse/src/parser/item.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,7 +1072,7 @@ impl<'a> Parser<'a> {
10721072
// However, we must avoid keywords that occur as binary operators.
10731073
// Currently, the only applicable keyword is `as` (`default as Ty`).
10741074
if self.check_keyword(exp!(Default))
1075-
&& self.look_ahead(1, |t| t.is_non_raw_ident_where(|i| i.name != kw::As))
1075+
&& self.look_ahead(1, |t| t.non_raw_ident().is_some_and(|i| i.name != kw::As))
10761076
{
10771077
self.psess.gated_spans.gate(sym::specialization, self.token.span);
10781078
self.bump(); // `default`
@@ -1473,15 +1473,13 @@ impl<'a> Parser<'a> {
14731473
}
14741474

14751475
fn parse_ident_or_underscore(&mut self) -> PResult<'a, Ident> {
1476-
match self.token.ident() {
1477-
Some((
1478-
ident @ Ident { name: kw::Underscore, .. },
1479-
IdentKind::Normal | IdentKind::ForcedKeyword,
1480-
)) => {
1481-
self.bump();
1482-
Ok(ident)
1483-
}
1484-
_ => self.parse_ident(),
1476+
if let Some(ident) = self.token.non_raw_ident()
1477+
&& let kw::Underscore = ident.name
1478+
{
1479+
self.bump();
1480+
Ok(ident)
1481+
} else {
1482+
self.parse_ident()
14851483
}
14861484
}
14871485

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ impl<'a> Parser<'a> {
558558
if self.check_keyword(exp) {
559559
true
560560
} else if case == Case::Insensitive
561-
&& let Some((ident, IdentKind::Normal | IdentKind::ForcedKeyword)) = self.token.ident()
561+
&& let Some(ident) = self.token.non_raw_ident()
562562
// Do an ASCII case-insensitive match, because all keywords are ASCII.
563563
&& ident.as_str().eq_ignore_ascii_case(exp.kw.as_str())
564564
{
@@ -590,7 +590,7 @@ impl<'a> Parser<'a> {
590590
if self.eat_keyword(exp) {
591591
true
592592
} else if case == Case::Insensitive
593-
&& let Some((ident, IdentKind::Normal | IdentKind::ForcedKeyword)) = self.token.ident()
593+
&& let Some(ident) = self.token.non_raw_ident()
594594
// Do an ASCII case-insensitive match, because all keywords are ASCII.
595595
&& ident.as_str().eq_ignore_ascii_case(exp.kw.as_str())
596596
{

compiler/rustc_parse/src/parser/path.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::mem;
22

3-
use ast::token::IdentKind;
43
use rustc_ast::token::{self, MetaVarKind, Token, TokenKind};
54
use rustc_ast::{
65
self as ast, AngleBracketedArg, AngleBracketedArgs, AnonConst, AssocItemConstraint,
@@ -458,14 +457,13 @@ impl<'a> Parser<'a> {
458457
}
459458

460459
pub(super) fn parse_path_segment_ident(&mut self) -> PResult<'a, Ident> {
461-
match self.token.ident() {
462-
Some((ident, IdentKind::Normal | IdentKind::ForcedKeyword))
463-
if ident.is_path_segment_keyword() =>
464-
{
465-
self.bump();
466-
Ok(ident)
467-
}
468-
_ => self.parse_ident(),
460+
if let Some(ident) = self.token.non_raw_ident()
461+
&& ident.is_path_segment_keyword()
462+
{
463+
self.bump();
464+
Ok(ident)
465+
} else {
466+
self.parse_ident()
469467
}
470468
}
471469

compiler/rustc_parse/src/parser/stmt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,7 @@ impl<'a> Parser<'a> {
10221022
&& self.look_ahead(1, |token| {
10231023
token.is_metavar_block()
10241024
|| token.kind == token::OpenBrace
1025-
|| token.is_non_raw_ident_where(|id| {
1025+
|| token.non_raw_ident().is_some_and(|id| {
10261026
matches!(
10271027
id.name,
10281028
kw::For | kw::Loop | kw::While

0 commit comments

Comments
 (0)