Skip to content

Commit

Permalink
Auto merge of #61612 - nnethercote:improve-parse_bottom_expr, r=petro…
Browse files Browse the repository at this point in the history
…chenkov

Special-case literals in `parse_bottom_expr`.

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`.

r? @petrochenkov
  • Loading branch information
bors committed Jun 12, 2019
2 parents 24ddd16 + 35b5f43 commit 55cee44
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions src/libsyntax/parse/parser.rs
Expand Up @@ -1994,8 +1994,29 @@ impl<'a> Parser<'a> {

let ex: ExprKind;

macro_rules! parse_lit {
() => {
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());
}
}
}
}

// Note: when adding new syntax here, don't forget to adjust TokenKind::can_begin_expr().
match self.token.kind {
// 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(_) => {
parse_lit!()
}
token::OpenDelim(token::Paren) => {
self.bump();

Expand Down Expand Up @@ -2241,16 +2262,7 @@ impl<'a> Parser<'a> {
self.bump();
return Ok(self.mk_expr(self.token.span, ExprKind::Err, ThinVec::new()));
}
match self.parse_literal_maybe_minus() {
Ok(expr) => {
hi = expr.span;
ex = expr.node.clone();
}
Err(mut err) => {
self.cancel(&mut err);
return Err(self.expected_expression_found());
}
}
parse_lit!()
}
}
}
Expand Down

0 comments on commit 55cee44

Please sign in to comment.