Skip to content

Commit 55cee44

Browse files
committed
Auto merge of #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 24ddd16 + 35b5f43 commit 55cee44

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
@@ -1994,8 +1994,29 @@ impl<'a> Parser<'a> {
19941994

19951995
let ex: ExprKind;
19961996

1997+
macro_rules! parse_lit {
1998+
() => {
1999+
match self.parse_lit() {
2000+
Ok(literal) => {
2001+
hi = self.prev_span;
2002+
ex = ExprKind::Lit(literal);
2003+
}
2004+
Err(mut err) => {
2005+
self.cancel(&mut err);
2006+
return Err(self.expected_expression_found());
2007+
}
2008+
}
2009+
}
2010+
}
2011+
19972012
// Note: when adding new syntax here, don't forget to adjust TokenKind::can_begin_expr().
19982013
match self.token.kind {
2014+
// This match arm is a special-case of the `_` match arm below and
2015+
// could be removed without changing functionality, but it's faster
2016+
// to have it here, especially for programs with large constants.
2017+
token::Literal(_) => {
2018+
parse_lit!()
2019+
}
19992020
token::OpenDelim(token::Paren) => {
20002021
self.bump();
20012022

@@ -2241,16 +2262,7 @@ impl<'a> Parser<'a> {
22412262
self.bump();
22422263
return Ok(self.mk_expr(self.token.span, ExprKind::Err, ThinVec::new()));
22432264
}
2244-
match self.parse_literal_maybe_minus() {
2245-
Ok(expr) => {
2246-
hi = expr.span;
2247-
ex = expr.node.clone();
2248-
}
2249-
Err(mut err) => {
2250-
self.cancel(&mut err);
2251-
return Err(self.expected_expression_found());
2252-
}
2253-
}
2265+
parse_lit!()
22542266
}
22552267
}
22562268
}

0 commit comments

Comments
 (0)