Skip to content

Commit 35b5f43

Browse files
committed
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 replaces the unnecessary `parse_literal_maybe_minus` call with `parse_lit`, avoiding an unnecessary allocation via `mk_expr`.
1 parent d132f54 commit 35b5f43

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

src/libsyntax/parse/parser.rs

+22-10
Original file line numberDiff line numberDiff line change
@@ -2002,8 +2002,29 @@ impl<'a> Parser<'a> {
20022002

20032003
let ex: ExprKind;
20042004

2005+
macro_rules! parse_lit {
2006+
() => {
2007+
match self.parse_lit() {
2008+
Ok(literal) => {
2009+
hi = self.prev_span;
2010+
ex = ExprKind::Lit(literal);
2011+
}
2012+
Err(mut err) => {
2013+
self.cancel(&mut err);
2014+
return Err(self.expected_expression_found());
2015+
}
2016+
}
2017+
}
2018+
}
2019+
20052020
// Note: when adding new syntax here, don't forget to adjust TokenKind::can_begin_expr().
20062021
match self.token.kind {
2022+
// This match arm is a special-case of the `_` match arm below and
2023+
// could be removed without changing functionality, but it's faster
2024+
// to have it here, especially for programs with large constants.
2025+
token::Literal(_) => {
2026+
parse_lit!()
2027+
}
20072028
token::OpenDelim(token::Paren) => {
20082029
self.bump();
20092030

@@ -2249,16 +2270,7 @@ impl<'a> Parser<'a> {
22492270
self.bump();
22502271
return Ok(self.mk_expr(self.span, ExprKind::Err, ThinVec::new()));
22512272
}
2252-
match self.parse_literal_maybe_minus() {
2253-
Ok(expr) => {
2254-
hi = expr.span;
2255-
ex = expr.node.clone();
2256-
}
2257-
Err(mut err) => {
2258-
self.cancel(&mut err);
2259-
return Err(self.expected_expression_found());
2260-
}
2261-
}
2273+
parse_lit!()
22622274
}
22632275
}
22642276
}

0 commit comments

Comments
 (0)