Skip to content

Commit

Permalink
Save parser state lazily
Browse files Browse the repository at this point in the history
  • Loading branch information
varkor committed Sep 11, 2018
1 parent 8959c99 commit 2e5f30c
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2053,13 +2053,18 @@ impl<'a> Parser<'a> {
is_args_start(&this.token, include_paren)
};

let expr_without_disambig = style == PathStyle::Expr && check_args_start(self, false);
let parser_snapshot_before_generics = self.clone();

Ok(if style == PathStyle::Type && check_args_start(self, true) ||
style != PathStyle::Mod && self.check(&token::ModSep)
&& self.look_ahead(1, |t| is_args_start(t, true))
|| expr_without_disambig {
let mut parser_snapshot_before_generics = None;

Ok(if style == PathStyle::Type && check_args_start(self, true)
|| style != PathStyle::Mod && self.check(&token::ModSep)
&& self.look_ahead(1, |t| is_args_start(t, true))
|| style == PathStyle::Expr && check_args_start(self, false) && {
// Check for generic arguments in an expression without a disambiguating `::`.
// We have to save a snapshot, because it could end up being an expression
// instead.
parser_snapshot_before_generics = Some(self.clone());
true
} {
// Generic arguments are found - `<`, `(`, `::<` or `::(`.
let lo = self.span;
if self.eat(&token::ModSep) && style == PathStyle::Type && enable_warning {
Expand All @@ -2078,9 +2083,9 @@ impl<'a> Parser<'a> {

match args {
Err(mut err) => {
if expr_without_disambig {
if let Some(snapshot) = parser_snapshot_before_generics {
err.cancel();
mem::replace(self, parser_snapshot_before_generics);
mem::replace(self, snapshot);
return Ok(PathSegment::from_ident(ident));
}
return Err(err);
Expand Down

0 comments on commit 2e5f30c

Please sign in to comment.