Skip to content

Commit 4e6952e

Browse files
committed
Auto merge of rust-lang#133793 - nnethercote:speed-up-expected_tokens, r=<try>
Speed up `Parser::expected_tokens` r? `@ghost`
2 parents efdd9e8 + 0133601 commit 4e6952e

File tree

18 files changed

+1270
-785
lines changed

18 files changed

+1270
-785
lines changed

compiler/rustc_builtin_macros/src/asm.rs

+54-52
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
88
use rustc_errors::PResult;
99
use rustc_expand::base::*;
1010
use rustc_index::bit_set::GrowableBitSet;
11-
use rustc_parse::parser::Parser;
11+
use rustc_parse::exp;
12+
use rustc_parse::parser::{Parser, TokenType};
1213
use rustc_session::lint;
13-
use rustc_span::symbol::{Ident, Symbol, kw, sym};
14+
use rustc_span::symbol::{Ident, Symbol, kw};
1415
use rustc_span::{ErrorGuaranteed, InnerSpan, Span};
1516
use rustc_target::asm::InlineAsmArch;
1617
use smallvec::smallvec;
@@ -39,11 +40,11 @@ pub struct AsmArgs {
3940
/// - `Err(_)` if the current token matches the keyword, but was not expected
4041
fn eat_operand_keyword<'a>(
4142
p: &mut Parser<'a>,
42-
symbol: Symbol,
43+
(symbol, token_type): (Symbol, TokenType),
4344
asm_macro: AsmMacro,
4445
) -> PResult<'a, bool> {
4546
if matches!(asm_macro, AsmMacro::Asm) {
46-
Ok(p.eat_keyword(symbol))
47+
Ok(p.eat_keyword((symbol, token_type)))
4748
} else {
4849
let span = p.token.span;
4950
if p.eat_keyword_noexpect(symbol) {
@@ -96,28 +97,28 @@ pub fn parse_asm_args<'a>(
9697

9798
let mut allow_templates = true;
9899
while p.token != token::Eof {
99-
if !p.eat(&token::Comma) {
100+
if !p.eat(exp!(Comma)) {
100101
if allow_templates {
101102
// After a template string, we always expect *only* a comma...
102103
return Err(dcx.create_err(errors::AsmExpectedComma { span: p.token.span }));
103104
} else {
104105
// ...after that delegate to `expect` to also include the other expected tokens.
105-
return Err(p.expect(&token::Comma).err().unwrap());
106+
return Err(p.expect(exp!(Comma)).err().unwrap());
106107
}
107108
}
108109
if p.token == token::Eof {
109110
break;
110111
} // accept trailing commas
111112

112113
// Parse clobber_abi
113-
if p.eat_keyword(sym::clobber_abi) {
114+
if p.eat_keyword(exp!(ClobberAbi)) {
114115
parse_clobber_abi(p, &mut args)?;
115116
allow_templates = false;
116117
continue;
117118
}
118119

119120
// Parse options
120-
if p.eat_keyword(sym::options) {
121+
if p.eat_keyword(exp!(Options)) {
121122
parse_options(p, &mut args, asm_macro)?;
122123
allow_templates = false;
123124
continue;
@@ -129,65 +130,65 @@ pub fn parse_asm_args<'a>(
129130
let name = if p.token.is_ident() && p.look_ahead(1, |t| *t == token::Eq) {
130131
let (ident, _) = p.token.ident().unwrap();
131132
p.bump();
132-
p.expect(&token::Eq)?;
133+
p.expect(exp!(Eq))?;
133134
allow_templates = false;
134135
Some(ident.name)
135136
} else {
136137
None
137138
};
138139

139140
let mut explicit_reg = false;
140-
let op = if eat_operand_keyword(p, kw::In, asm_macro)? {
141+
let op = if eat_operand_keyword(p, exp!(In), asm_macro)? {
141142
let reg = parse_reg(p, &mut explicit_reg)?;
142-
if p.eat_keyword(kw::Underscore) {
143+
if p.eat_keyword(exp!(Underscore)) {
143144
let err = dcx.create_err(errors::AsmUnderscoreInput { span: p.token.span });
144145
return Err(err);
145146
}
146147
let expr = p.parse_expr()?;
147148
ast::InlineAsmOperand::In { reg, expr }
148-
} else if eat_operand_keyword(p, sym::out, asm_macro)? {
149+
} else if eat_operand_keyword(p, exp!(Out), asm_macro)? {
149150
let reg = parse_reg(p, &mut explicit_reg)?;
150-
let expr = if p.eat_keyword(kw::Underscore) { None } else { Some(p.parse_expr()?) };
151+
let expr = if p.eat_keyword(exp!(Underscore)) { None } else { Some(p.parse_expr()?) };
151152
ast::InlineAsmOperand::Out { reg, expr, late: false }
152-
} else if eat_operand_keyword(p, sym::lateout, asm_macro)? {
153+
} else if eat_operand_keyword(p, exp!(Lateout), asm_macro)? {
153154
let reg = parse_reg(p, &mut explicit_reg)?;
154-
let expr = if p.eat_keyword(kw::Underscore) { None } else { Some(p.parse_expr()?) };
155+
let expr = if p.eat_keyword(exp!(Underscore)) { None } else { Some(p.parse_expr()?) };
155156
ast::InlineAsmOperand::Out { reg, expr, late: true }
156-
} else if eat_operand_keyword(p, sym::inout, asm_macro)? {
157+
} else if eat_operand_keyword(p, exp!(Inout), asm_macro)? {
157158
let reg = parse_reg(p, &mut explicit_reg)?;
158-
if p.eat_keyword(kw::Underscore) {
159+
if p.eat_keyword(exp!(Underscore)) {
159160
let err = dcx.create_err(errors::AsmUnderscoreInput { span: p.token.span });
160161
return Err(err);
161162
}
162163
let expr = p.parse_expr()?;
163-
if p.eat(&token::FatArrow) {
164+
if p.eat(exp!(FatArrow)) {
164165
let out_expr =
165-
if p.eat_keyword(kw::Underscore) { None } else { Some(p.parse_expr()?) };
166+
if p.eat_keyword(exp!(Underscore)) { None } else { Some(p.parse_expr()?) };
166167
ast::InlineAsmOperand::SplitInOut { reg, in_expr: expr, out_expr, late: false }
167168
} else {
168169
ast::InlineAsmOperand::InOut { reg, expr, late: false }
169170
}
170-
} else if eat_operand_keyword(p, sym::inlateout, asm_macro)? {
171+
} else if eat_operand_keyword(p, exp!(Inlateout), asm_macro)? {
171172
let reg = parse_reg(p, &mut explicit_reg)?;
172-
if p.eat_keyword(kw::Underscore) {
173+
if p.eat_keyword(exp!(Underscore)) {
173174
let err = dcx.create_err(errors::AsmUnderscoreInput { span: p.token.span });
174175
return Err(err);
175176
}
176177
let expr = p.parse_expr()?;
177-
if p.eat(&token::FatArrow) {
178+
if p.eat(exp!(FatArrow)) {
178179
let out_expr =
179-
if p.eat_keyword(kw::Underscore) { None } else { Some(p.parse_expr()?) };
180+
if p.eat_keyword(exp!(Underscore)) { None } else { Some(p.parse_expr()?) };
180181
ast::InlineAsmOperand::SplitInOut { reg, in_expr: expr, out_expr, late: true }
181182
} else {
182183
ast::InlineAsmOperand::InOut { reg, expr, late: true }
183184
}
184-
} else if eat_operand_keyword(p, sym::label, asm_macro)? {
185+
} else if eat_operand_keyword(p, exp!(Label), asm_macro)? {
185186
let block = p.parse_block()?;
186187
ast::InlineAsmOperand::Label { block }
187-
} else if p.eat_keyword(kw::Const) {
188+
} else if p.eat_keyword(exp!(Const)) {
188189
let anon_const = p.parse_expr_anon_const()?;
189190
ast::InlineAsmOperand::Const { anon_const }
190-
} else if p.eat_keyword(sym::sym) {
191+
} else if p.eat_keyword(exp!(Sym)) {
191192
let expr = p.parse_expr()?;
192193
let ast::ExprKind::Path(qself, path) = &expr.kind else {
193194
let err = dcx.create_err(errors::AsmSymNoPath { span: expr.span });
@@ -390,31 +391,32 @@ fn parse_options<'a>(
390391
) -> PResult<'a, ()> {
391392
let span_start = p.prev_token.span;
392393

393-
p.expect(&token::OpenDelim(Delimiter::Parenthesis))?;
394-
395-
while !p.eat(&token::CloseDelim(Delimiter::Parenthesis)) {
396-
const OPTIONS: [(Symbol, ast::InlineAsmOptions); ast::InlineAsmOptions::COUNT] = [
397-
(sym::pure, ast::InlineAsmOptions::PURE),
398-
(sym::nomem, ast::InlineAsmOptions::NOMEM),
399-
(sym::readonly, ast::InlineAsmOptions::READONLY),
400-
(sym::preserves_flags, ast::InlineAsmOptions::PRESERVES_FLAGS),
401-
(sym::noreturn, ast::InlineAsmOptions::NORETURN),
402-
(sym::nostack, ast::InlineAsmOptions::NOSTACK),
403-
(sym::may_unwind, ast::InlineAsmOptions::MAY_UNWIND),
404-
(sym::att_syntax, ast::InlineAsmOptions::ATT_SYNTAX),
405-
(kw::Raw, ast::InlineAsmOptions::RAW),
394+
p.expect(exp!(OpenParen))?;
395+
396+
while !p.eat(exp!(CloseParen)) {
397+
const OPTIONS: [((Symbol, TokenType), ast::InlineAsmOptions);
398+
ast::InlineAsmOptions::COUNT] = [
399+
(exp!(Pure), ast::InlineAsmOptions::PURE),
400+
(exp!(Nomem), ast::InlineAsmOptions::NOMEM),
401+
(exp!(Readonly), ast::InlineAsmOptions::READONLY),
402+
(exp!(PreservesFlags), ast::InlineAsmOptions::PRESERVES_FLAGS),
403+
(exp!(Noreturn), ast::InlineAsmOptions::NORETURN),
404+
(exp!(Nostack), ast::InlineAsmOptions::NOSTACK),
405+
(exp!(MayUnwind), ast::InlineAsmOptions::MAY_UNWIND),
406+
(exp!(AttSyntax), ast::InlineAsmOptions::ATT_SYNTAX),
407+
(exp!(Raw), ast::InlineAsmOptions::RAW),
406408
];
407409

408410
'blk: {
409-
for (symbol, option) in OPTIONS {
411+
for ((sym, token_type), option) in OPTIONS {
410412
let kw_matched = if asm_macro.is_supported_option(option) {
411-
p.eat_keyword(symbol)
413+
p.eat_keyword((sym, token_type))
412414
} else {
413-
p.eat_keyword_noexpect(symbol)
415+
p.eat_keyword_noexpect(sym)
414416
};
415417

416418
if kw_matched {
417-
try_set_option(p, args, asm_macro, symbol, option);
419+
try_set_option(p, args, asm_macro, sym, option);
418420
break 'blk;
419421
}
420422
}
@@ -423,10 +425,10 @@ fn parse_options<'a>(
423425
}
424426

425427
// Allow trailing commas
426-
if p.eat(&token::CloseDelim(Delimiter::Parenthesis)) {
428+
if p.eat(exp!(CloseParen)) {
427429
break;
428430
}
429-
p.expect(&token::Comma)?;
431+
p.expect(exp!(Comma))?;
430432
}
431433

432434
let new_span = span_start.to(p.prev_token.span);
@@ -438,14 +440,14 @@ fn parse_options<'a>(
438440
fn parse_clobber_abi<'a>(p: &mut Parser<'a>, args: &mut AsmArgs) -> PResult<'a, ()> {
439441
let span_start = p.prev_token.span;
440442

441-
p.expect(&token::OpenDelim(Delimiter::Parenthesis))?;
443+
p.expect(exp!(OpenParen))?;
442444

443-
if p.eat(&token::CloseDelim(Delimiter::Parenthesis)) {
445+
if p.eat(exp!(CloseParen)) {
444446
return Err(p.dcx().create_err(errors::NonABI { span: p.token.span }));
445447
}
446448

447449
let mut new_abis = Vec::new();
448-
while !p.eat(&token::CloseDelim(Delimiter::Parenthesis)) {
450+
while !p.eat(exp!(CloseParen)) {
449451
match p.parse_str_lit() {
450452
Ok(str_lit) => {
451453
new_abis.push((str_lit.symbol_unescaped, str_lit.span));
@@ -457,10 +459,10 @@ fn parse_clobber_abi<'a>(p: &mut Parser<'a>, args: &mut AsmArgs) -> PResult<'a,
457459
};
458460

459461
// Allow trailing commas
460-
if p.eat(&token::CloseDelim(Delimiter::Parenthesis)) {
462+
if p.eat(exp!(CloseParen)) {
461463
break;
462464
}
463-
p.expect(&token::Comma)?;
465+
p.expect(exp!(Comma))?;
464466
}
465467

466468
let full_span = span_start.to(p.prev_token.span);
@@ -483,7 +485,7 @@ fn parse_reg<'a>(
483485
p: &mut Parser<'a>,
484486
explicit_reg: &mut bool,
485487
) -> PResult<'a, ast::InlineAsmRegOrRegClass> {
486-
p.expect(&token::OpenDelim(Delimiter::Parenthesis))?;
488+
p.expect(exp!(OpenParen))?;
487489
let result = match p.token.uninterpolate().kind {
488490
token::Ident(name, IdentIsRaw::No) => ast::InlineAsmRegOrRegClass::RegClass(name),
489491
token::Literal(token::Lit { kind: token::LitKind::Str, symbol, suffix: _ }) => {
@@ -497,7 +499,7 @@ fn parse_reg<'a>(
497499
}
498500
};
499501
p.bump();
500-
p.expect(&token::CloseDelim(Delimiter::Parenthesis))?;
502+
p.expect(exp!(CloseParen))?;
501503
Ok(result)
502504
}
503505

compiler/rustc_builtin_macros/src/assert.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc_ast::{DelimArgs, Expr, ExprKind, MacCall, Path, PathSegment, UnOp, tok
77
use rustc_ast_pretty::pprust;
88
use rustc_errors::PResult;
99
use rustc_expand::base::{DummyResult, ExpandResult, ExtCtxt, MacEager, MacroExpanderResult};
10+
use rustc_parse::exp;
1011
use rustc_parse::parser::Parser;
1112
use rustc_span::symbol::{Ident, Symbol, sym};
1213
use rustc_span::{DUMMY_SP, Span};
@@ -144,7 +145,7 @@ fn parse_assert<'a>(cx: &ExtCtxt<'a>, sp: Span, stream: TokenStream) -> PResult<
144145
cx.dcx().emit_err(errors::AssertMissingComma { span: parser.token.span, comma });
145146

146147
parse_custom_message(&mut parser)
147-
} else if parser.eat(&token::Comma) {
148+
} else if parser.eat(exp!(Comma)) {
148149
parse_custom_message(&mut parser)
149150
} else {
150151
None

compiler/rustc_builtin_macros/src/cfg.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc_ast::token;
66
use rustc_ast::tokenstream::TokenStream;
77
use rustc_errors::PResult;
88
use rustc_expand::base::{DummyResult, ExpandResult, ExtCtxt, MacEager, MacroExpanderResult};
9+
use rustc_parse::exp;
910
use rustc_span::Span;
1011
use {rustc_ast as ast, rustc_attr as attr};
1112

@@ -48,9 +49,9 @@ fn parse_cfg<'a>(
4849

4950
let cfg = p.parse_meta_item_inner()?;
5051

51-
let _ = p.eat(&token::Comma);
52+
let _ = p.eat(exp!(Comma));
5253

53-
if !p.eat(&token::Eof) {
54+
if !p.eat(exp!(Eof)) {
5455
return Err(cx.dcx().create_err(errors::OneCfgPattern { span }));
5556
}
5657

compiler/rustc_builtin_macros/src/format.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use rustc_errors::{Applicability, Diag, MultiSpan, PResult, SingleLabelManySpans
1212
use rustc_expand::base::*;
1313
use rustc_lint_defs::builtin::NAMED_ARGUMENTS_USED_POSITIONALLY;
1414
use rustc_lint_defs::{BufferedEarlyLint, BuiltinLintDiag, LintId};
15+
use rustc_parse::exp;
1516
use rustc_parse_format as parse;
1617
use rustc_span::symbol::{Ident, Symbol};
1718
use rustc_span::{BytePos, ErrorGuaranteed, InnerSpan, Span};
@@ -94,12 +95,12 @@ fn parse_args<'a>(ecx: &ExtCtxt<'a>, sp: Span, tts: TokenStream) -> PResult<'a,
9495
let mut first = true;
9596

9697
while p.token != token::Eof {
97-
if !p.eat(&token::Comma) {
98+
if !p.eat(exp!(Comma)) {
9899
if first {
99-
p.clear_expected_tokens();
100+
p.clear_expected_token_types();
100101
}
101102

102-
match p.expect(&token::Comma) {
103+
match p.expect(exp!(Comma)) {
103104
Err(err) => {
104105
match token::TokenKind::Comma.similar_tokens() {
105106
Some(tks) if tks.contains(&p.token.kind) => {
@@ -123,7 +124,7 @@ fn parse_args<'a>(ecx: &ExtCtxt<'a>, sp: Span, tts: TokenStream) -> PResult<'a,
123124
match p.token.ident() {
124125
Some((ident, _)) if p.look_ahead(1, |t| *t == token::Eq) => {
125126
p.bump();
126-
p.expect(&token::Eq)?;
127+
p.expect(exp!(Eq))?;
127128
let expr = p.parse_expr()?;
128129
if let Some((_, prev)) = args.by_name(ident.name) {
129130
ecx.dcx().emit_err(errors::FormatDuplicateArg {

compiler/rustc_builtin_macros/src/pattern_type.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ use rustc_ast::tokenstream::TokenStream;
33
use rustc_ast::{Pat, Ty, ast};
44
use rustc_errors::PResult;
55
use rustc_expand::base::{self, DummyResult, ExpandResult, ExtCtxt, MacroExpanderResult};
6-
use rustc_span::{Span, sym};
6+
use rustc_parse::exp;
7+
use rustc_span::Span;
78

89
pub(crate) fn expand<'cx>(
910
cx: &'cx mut ExtCtxt<'_>,
@@ -24,7 +25,7 @@ fn parse_pat_ty<'a>(cx: &mut ExtCtxt<'a>, stream: TokenStream) -> PResult<'a, (P
2425
let mut parser = cx.new_parser_from_tts(stream);
2526

2627
let ty = parser.parse_ty()?;
27-
parser.expect_keyword(sym::is)?;
28+
parser.expect_keyword(exp!(Is))?;
2829
let pat = parser.parse_pat_no_top_alt(None, None)?;
2930

3031
Ok((ty, pat))

compiler/rustc_builtin_macros/src/util.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_expand::expand::AstFragment;
77
use rustc_feature::AttributeTemplate;
88
use rustc_lint_defs::BuiltinLintDiag;
99
use rustc_lint_defs::builtin::DUPLICATE_MACRO_ATTRIBUTES;
10-
use rustc_parse::{parser, validate_attr};
10+
use rustc_parse::{exp, parser, validate_attr};
1111
use rustc_session::errors::report_lit_error;
1212
use rustc_span::{BytePos, Span, Symbol};
1313

@@ -204,7 +204,7 @@ pub(crate) fn get_single_expr_from_tts(
204204
Ok(ret) => ret,
205205
Err(guar) => return ExpandResult::Ready(Err(guar)),
206206
};
207-
let _ = p.eat(&token::Comma);
207+
let _ = p.eat(exp!(Comma));
208208

209209
if p.token != token::Eof {
210210
cx.dcx().emit_err(errors::OnlyOneArgument { span, name });
@@ -237,7 +237,7 @@ pub(crate) fn get_exprs_from_tts(
237237
let expr = cx.expander().fully_expand_fragment(AstFragment::Expr(expr)).make_expr();
238238

239239
es.push(expr);
240-
if p.eat(&token::Comma) {
240+
if p.eat(exp!(Comma)) {
241241
continue;
242242
}
243243
if p.token != token::Eof {

compiler/rustc_expand/src/module.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::path::{self, Path, PathBuf};
44
use rustc_ast::ptr::P;
55
use rustc_ast::{AttrVec, Attribute, Inline, Item, ModSpans, token};
66
use rustc_errors::{Diag, ErrorGuaranteed};
7-
use rustc_parse::{new_parser_from_file, unwrap_or_emit_fatal, validate_attr};
7+
use rustc_parse::{exp, new_parser_from_file, unwrap_or_emit_fatal, validate_attr};
88
use rustc_session::Session;
99
use rustc_session::parse::ParseSess;
1010
use rustc_span::Span;
@@ -70,7 +70,7 @@ pub(crate) fn parse_external_mod(
7070
let mut parser =
7171
unwrap_or_emit_fatal(new_parser_from_file(&sess.psess, &mp.file_path, Some(span)));
7272
let (inner_attrs, items, inner_span) =
73-
parser.parse_mod(&token::Eof).map_err(|err| ModError::ParserError(err))?;
73+
parser.parse_mod(exp!(Eof)).map_err(|err| ModError::ParserError(err))?;
7474
attrs.extend(inner_attrs);
7575
(items, inner_span, mp.file_path)
7676
};

0 commit comments

Comments
 (0)