Skip to content

Commit

Permalink
Special-case literals in parse_bottom_expr.
Browse files Browse the repository at this point in the history
This makes parsing faster, particularly for code with large constants,
for two reasons:
- it skips all the keyword comparisons for literals;
- it skips the allocation done by the `mk_expr` call in
  `parse_literal_maybe_minus`.
  • Loading branch information
nnethercote committed Jun 7, 2019
1 parent 7cdaffd commit a4952ba
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1994,6 +1994,21 @@ impl<'a> Parser<'a> {

// Note: when adding new syntax here, don't forget to adjust Token::can_begin_expr().
match self.token {
// This match arm is a special-case of the `_` match arm below and
// could be removed without changing functionality, but it's faster
// to have it here, especially for programs with large constants.
token::Literal(_) => {
match self.parse_lit() {
Ok(literal) => {
hi = self.prev_span;
ex = ExprKind::Lit(literal);
}
Err(mut err) => {
self.cancel(&mut err);
return Err(self.expected_expression_found());
}
}
}
token::OpenDelim(token::Paren) => {
self.bump();

Expand Down

0 comments on commit a4952ba

Please sign in to comment.