Skip to content

Commit f8ab126

Browse files
committed
rustc_parse: Use Token::ident where possible
1 parent f943349 commit f8ab126

File tree

7 files changed

+86
-92
lines changed

7 files changed

+86
-92
lines changed

src/librustc_builtin_macros/format.rs

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -156,44 +156,43 @@ fn parse_args<'a>(
156156
if p.token == token::Eof {
157157
break;
158158
} // accept trailing commas
159-
if p.token.is_ident() && p.look_ahead(1, |t| *t == token::Eq) {
160-
named = true;
161-
let name = if let token::Ident(name, _) = p.normalized_token.kind {
159+
match p.token.ident() {
160+
Some((ident, _)) if p.look_ahead(1, |t| *t == token::Eq) => {
161+
named = true;
162162
p.bump();
163-
name
164-
} else {
165-
unreachable!();
166-
};
163+
p.expect(&token::Eq)?;
164+
let e = p.parse_expr()?;
165+
if let Some(prev) = names.get(&ident.name) {
166+
ecx.struct_span_err(e.span, &format!("duplicate argument named `{}`", ident))
167+
.span_label(args[*prev].span, "previously here")
168+
.span_label(e.span, "duplicate argument")
169+
.emit();
170+
continue;
171+
}
167172

168-
p.expect(&token::Eq)?;
169-
let e = p.parse_expr()?;
170-
if let Some(prev) = names.get(&name) {
171-
ecx.struct_span_err(e.span, &format!("duplicate argument named `{}`", name))
172-
.span_label(args[*prev].span, "previously here")
173-
.span_label(e.span, "duplicate argument")
174-
.emit();
175-
continue;
173+
// Resolve names into slots early.
174+
// Since all the positional args are already seen at this point
175+
// if the input is valid, we can simply append to the positional
176+
// args. And remember the names.
177+
let slot = args.len();
178+
names.insert(ident.name, slot);
179+
args.push(e);
176180
}
177-
178-
// Resolve names into slots early.
179-
// Since all the positional args are already seen at this point
180-
// if the input is valid, we can simply append to the positional
181-
// args. And remember the names.
182-
let slot = args.len();
183-
names.insert(name, slot);
184-
args.push(e);
185-
} else {
186-
let e = p.parse_expr()?;
187-
if named {
188-
let mut err = ecx
189-
.struct_span_err(e.span, "positional arguments cannot follow named arguments");
190-
err.span_label(e.span, "positional arguments must be before named arguments");
191-
for pos in names.values() {
192-
err.span_label(args[*pos].span, "named argument");
181+
_ => {
182+
let e = p.parse_expr()?;
183+
if named {
184+
let mut err = ecx.struct_span_err(
185+
e.span,
186+
"positional arguments cannot follow named arguments",
187+
);
188+
err.span_label(e.span, "positional arguments must be before named arguments");
189+
for pos in names.values() {
190+
err.span_label(args[*pos].span, "named argument");
191+
}
192+
err.emit();
193193
}
194-
err.emit();
194+
args.push(e);
195195
}
196-
args.push(e);
197196
}
198197
}
199198
Ok((fmtstr, args, names))

src/librustc_expand/mbe/macro_parser.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -750,15 +750,9 @@ pub(super) fn parse_tt(parser: &mut Cow<'_, Parser<'_>>, ms: &[TokenTree]) -> Na
750750

751751
/// The token is an identifier, but not `_`.
752752
/// We prohibit passing `_` to macros expecting `ident` for now.
753-
fn get_macro_name(token: &Token) -> Option<(Name, bool)> {
754-
match token.kind {
755-
token::Ident(name, is_raw) if name != kw::Underscore => Some((name, is_raw)),
756-
token::Interpolated(ref nt) => match **nt {
757-
token::NtIdent(ident, is_raw) if ident.name != kw::Underscore => {
758-
Some((ident.name, is_raw))
759-
}
760-
_ => None,
761-
},
753+
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)),
762756
_ => None,
763757
}
764758
}
@@ -783,7 +777,7 @@ fn may_begin_with(token: &Token, name: Name) -> bool {
783777
&& !token.is_keyword(kw::Let)
784778
}
785779
sym::ty => token.can_begin_type(),
786-
sym::ident => get_macro_name(token).is_some(),
780+
sym::ident => get_macro_ident(token).is_some(),
787781
sym::literal => token.can_begin_literal_or_bool(),
788782
sym::vis => match token.kind {
789783
// The follow-set of :vis + "priv" keyword + interpolated
@@ -888,9 +882,9 @@ fn parse_nt_inner<'a>(p: &mut Parser<'a>, sp: Span, name: Symbol) -> PResult<'a,
888882
sym::ty => token::NtTy(p.parse_ty()?),
889883
// this could be handled like a token, since it is one
890884
sym::ident => {
891-
if let Some((name, is_raw)) = get_macro_name(&p.token) {
885+
if let Some((ident, is_raw)) = get_macro_ident(&p.token) {
892886
p.bump();
893-
token::NtIdent(Ident::new(name, p.normalized_prev_token.span), is_raw)
887+
token::NtIdent(ident, is_raw)
894888
} else {
895889
let token_str = pprust::token_to_string(&p.token);
896890
let msg = &format!("expected ident, found {}", &token_str);

src/librustc_parse/parser/diagnostics.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,17 +192,19 @@ impl<'a> Parser<'a> {
192192
TokenKind::CloseDelim(token::DelimToken::Brace),
193193
TokenKind::CloseDelim(token::DelimToken::Paren),
194194
];
195-
if let token::Ident(name, false) = self.normalized_token.kind {
196-
if Ident::new(name, self.normalized_token.span).is_raw_guess()
197-
&& self.look_ahead(1, |t| valid_follow.contains(&t.kind))
195+
match self.token.ident() {
196+
Some((ident, false))
197+
if ident.is_raw_guess()
198+
&& self.look_ahead(1, |t| valid_follow.contains(&t.kind)) =>
198199
{
199200
err.span_suggestion(
200-
self.normalized_token.span,
201+
ident.span,
201202
"you can escape reserved keywords to use them as identifiers",
202-
format!("r#{}", name),
203+
format!("r#{}", ident.name),
203204
Applicability::MaybeIncorrect,
204205
);
205206
}
207+
_ => {}
206208
}
207209
if let Some(token_descr) = super::token_descr_opt(&self.token) {
208210
err.span_label(self.token.span, format!("expected identifier, found {}", token_descr));

src/librustc_parse/parser/expr.rs

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,10 @@ impl<'a> Parser<'a> {
9797
fn parse_expr_catch_underscore(&mut self) -> PResult<'a, P<Expr>> {
9898
match self.parse_expr() {
9999
Ok(expr) => Ok(expr),
100-
Err(mut err) => match self.normalized_token.kind {
101-
token::Ident(name, false)
102-
if name == kw::Underscore && self.look_ahead(1, |t| t == &token::Comma) =>
100+
Err(mut err) => match self.token.ident() {
101+
Some((ident, false))
102+
if ident.name == kw::Underscore
103+
&& self.look_ahead(1, |t| t == &token::Comma) =>
103104
{
104105
// Special-case handling of `foo(_, _, _)`
105106
err.emit();
@@ -331,21 +332,19 @@ impl<'a> Parser<'a> {
331332
///
332333
/// Also performs recovery for `and` / `or` which are mistaken for `&&` and `||` respectively.
333334
fn check_assoc_op(&self) -> Option<Spanned<AssocOp>> {
334-
Some(Spanned {
335-
node: match (AssocOp::from_token(&self.token), &self.normalized_token.kind) {
336-
(Some(op), _) => op,
337-
(None, token::Ident(sym::and, false)) => {
338-
self.error_bad_logical_op("and", "&&", "conjunction");
339-
AssocOp::LAnd
340-
}
341-
(None, token::Ident(sym::or, false)) => {
342-
self.error_bad_logical_op("or", "||", "disjunction");
343-
AssocOp::LOr
344-
}
345-
_ => return None,
346-
},
347-
span: self.normalized_token.span,
348-
})
335+
let (op, span) = match (AssocOp::from_token(&self.token), self.token.ident()) {
336+
(Some(op), _) => (op, self.token.span),
337+
(None, Some((ident, false))) if ident.name == sym::and => {
338+
self.error_bad_logical_op("and", "&&", "conjunction");
339+
(AssocOp::LAnd, ident.span)
340+
}
341+
(None, Some((ident, false))) if ident.name == sym::or => {
342+
self.error_bad_logical_op("or", "||", "disjunction");
343+
(AssocOp::LOr, ident.span)
344+
}
345+
_ => return None,
346+
};
347+
Some(source_map::respan(span, op))
349348
}
350349

351350
/// Error on `and` and `or` suggesting `&&` and `||` respectively.
@@ -1907,20 +1906,23 @@ impl<'a> Parser<'a> {
19071906

19081907
/// Use in case of error after field-looking code: `S { foo: () with a }`.
19091908
fn find_struct_error_after_field_looking_code(&self) -> Option<Field> {
1910-
if let token::Ident(name, _) = self.normalized_token.kind {
1911-
if !self.token.is_reserved_ident() && self.look_ahead(1, |t| *t == token::Colon) {
1912-
return Some(ast::Field {
1913-
ident: Ident::new(name, self.normalized_token.span),
1909+
match self.token.ident() {
1910+
Some((ident, is_raw))
1911+
if (is_raw || !ident.is_reserved())
1912+
&& self.look_ahead(1, |t| *t == token::Colon) =>
1913+
{
1914+
Some(ast::Field {
1915+
ident,
19141916
span: self.token.span,
19151917
expr: self.mk_expr_err(self.token.span),
19161918
is_shorthand: false,
19171919
attrs: AttrVec::new(),
19181920
id: DUMMY_NODE_ID,
19191921
is_placeholder: false,
1920-
});
1922+
})
19211923
}
1924+
_ => None,
19221925
}
1923-
None
19241926
}
19251927

19261928
fn recover_struct_comma_after_dotdot(&mut self, span: Span) {

src/librustc_parse/parser/item.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -750,10 +750,10 @@ impl<'a> Parser<'a> {
750750
}
751751

752752
fn parse_ident_or_underscore(&mut self) -> PResult<'a, ast::Ident> {
753-
match self.normalized_token.kind {
754-
token::Ident(name @ kw::Underscore, false) => {
753+
match self.token.ident() {
754+
Some((ident, false)) if ident.name == kw::Underscore => {
755755
self.bump();
756-
Ok(Ident::new(name, self.normalized_prev_token.span))
756+
Ok(ident)
757757
}
758758
_ => self.parse_ident(),
759759
}
@@ -1609,15 +1609,12 @@ impl<'a> Parser<'a> {
16091609
/// Returns the parsed optional self parameter and whether a self shortcut was used.
16101610
fn parse_self_param(&mut self) -> PResult<'a, Option<Param>> {
16111611
// Extract an identifier *after* having confirmed that the token is one.
1612-
let expect_self_ident = |this: &mut Self| {
1613-
match this.normalized_token.kind {
1614-
// Preserve hygienic context.
1615-
token::Ident(name, _) => {
1616-
this.bump();
1617-
Ident::new(name, this.normalized_prev_token.span)
1618-
}
1619-
_ => unreachable!(),
1612+
let expect_self_ident = |this: &mut Self| match this.token.ident() {
1613+
Some((ident, false)) => {
1614+
this.bump();
1615+
ident
16201616
}
1617+
_ => unreachable!(),
16211618
};
16221619
// Is `self` `n` tokens ahead?
16231620
let is_isolated_self = |this: &Self, n| {

src/librustc_parse/parser/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -480,9 +480,9 @@ impl<'a> Parser<'a> {
480480
}
481481

482482
fn parse_ident_common(&mut self, recover: bool) -> PResult<'a, ast::Ident> {
483-
match self.normalized_token.kind {
484-
token::Ident(name, _) => {
485-
if self.token.is_reserved_ident() {
483+
match self.token.ident() {
484+
Some((ident, is_raw)) => {
485+
if !is_raw && ident.is_reserved() {
486486
let mut err = self.expected_ident_found();
487487
if recover {
488488
err.emit();
@@ -491,7 +491,7 @@ impl<'a> Parser<'a> {
491491
}
492492
}
493493
self.bump();
494-
Ok(Ident::new(name, self.normalized_prev_token.span))
494+
Ok(ident)
495495
}
496496
_ => Err(match self.prev_token.kind {
497497
TokenKind::DocComment(..) => {

src/librustc_parse/parser/path.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,10 @@ impl<'a> Parser<'a> {
240240
}
241241

242242
pub(super) fn parse_path_segment_ident(&mut self) -> PResult<'a, Ident> {
243-
match self.normalized_token.kind {
244-
token::Ident(name, _) if name.is_path_segment_keyword() => {
243+
match self.token.ident() {
244+
Some((ident, false)) if ident.is_path_segment_keyword() => {
245245
self.bump();
246-
Ok(Ident::new(name, self.normalized_prev_token.span))
246+
Ok(ident)
247247
}
248248
_ => self.parse_ident(),
249249
}

0 commit comments

Comments
 (0)