Skip to content

Commit 2e5f30c

Browse files
committed
Save parser state lazily
1 parent 8959c99 commit 2e5f30c

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/libsyntax/parse/parser.rs

+14-9
Original file line numberDiff line numberDiff line change
@@ -2053,13 +2053,18 @@ impl<'a> Parser<'a> {
20532053
is_args_start(&this.token, include_paren)
20542054
};
20552055

2056-
let expr_without_disambig = style == PathStyle::Expr && check_args_start(self, false);
2057-
let parser_snapshot_before_generics = self.clone();
2058-
2059-
Ok(if style == PathStyle::Type && check_args_start(self, true) ||
2060-
style != PathStyle::Mod && self.check(&token::ModSep)
2061-
&& self.look_ahead(1, |t| is_args_start(t, true))
2062-
|| expr_without_disambig {
2056+
let mut parser_snapshot_before_generics = None;
2057+
2058+
Ok(if style == PathStyle::Type && check_args_start(self, true)
2059+
|| style != PathStyle::Mod && self.check(&token::ModSep)
2060+
&& self.look_ahead(1, |t| is_args_start(t, true))
2061+
|| style == PathStyle::Expr && check_args_start(self, false) && {
2062+
// Check for generic arguments in an expression without a disambiguating `::`.
2063+
// We have to save a snapshot, because it could end up being an expression
2064+
// instead.
2065+
parser_snapshot_before_generics = Some(self.clone());
2066+
true
2067+
} {
20632068
// Generic arguments are found - `<`, `(`, `::<` or `::(`.
20642069
let lo = self.span;
20652070
if self.eat(&token::ModSep) && style == PathStyle::Type && enable_warning {
@@ -2078,9 +2083,9 @@ impl<'a> Parser<'a> {
20782083

20792084
match args {
20802085
Err(mut err) => {
2081-
if expr_without_disambig {
2086+
if let Some(snapshot) = parser_snapshot_before_generics {
20822087
err.cancel();
2083-
mem::replace(self, parser_snapshot_before_generics);
2088+
mem::replace(self, snapshot);
20842089
return Ok(PathSegment::from_ident(ident));
20852090
}
20862091
return Err(err);

0 commit comments

Comments
 (0)