Skip to content

Commit 965e75a

Browse files
authored
Rollup merge of rust-lang#61612 - nnethercote:improve-parse_bottom_expr, r=petrochenkov
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
2 parents 78c326f + 35b5f43 commit 965e75a

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1986,8 +1986,29 @@ impl<'a> Parser<'a> {
19861986

19871987
let ex: ExprKind;
19881988

1989+
macro_rules! parse_lit {
1990+
() => {
1991+
match self.parse_lit() {
1992+
Ok(literal) => {
1993+
hi = self.prev_span;
1994+
ex = ExprKind::Lit(literal);
1995+
}
1996+
Err(mut err) => {
1997+
self.cancel(&mut err);
1998+
return Err(self.expected_expression_found());
1999+
}
2000+
}
2001+
}
2002+
}
2003+
19892004
// Note: when adding new syntax here, don't forget to adjust TokenKind::can_begin_expr().
19902005
match self.token.kind {
2006+
// This match arm is a special-case of the `_` match arm below and
2007+
// could be removed without changing functionality, but it's faster
2008+
// to have it here, especially for programs with large constants.
2009+
token::Literal(_) => {
2010+
parse_lit!()
2011+
}
19912012
token::OpenDelim(token::Paren) => {
19922013
self.bump();
19932014

@@ -2233,16 +2254,7 @@ impl<'a> Parser<'a> {
22332254
self.bump();
22342255
return Ok(self.mk_expr(self.token.span, ExprKind::Err, ThinVec::new()));
22352256
}
2236-
match self.parse_literal_maybe_minus() {
2237-
Ok(expr) => {
2238-
hi = expr.span;
2239-
ex = expr.node.clone();
2240-
}
2241-
Err(mut err) => {
2242-
self.cancel(&mut err);
2243-
return Err(self.expected_expression_found());
2244-
}
2245-
}
2257+
parse_lit!()
22462258
}
22472259
}
22482260
}

0 commit comments

Comments
 (0)