about summary refs log tree commit diff
path: root/compiler/rustc_parse/src
diff options
context:
space:
mode:
authorTakayuki Maeda <takoyaki0316@gmail.com>2022-03-09 23:13:04 +0900
committerTakayuki Maeda <takoyaki0316@gmail.com>2022-03-09 23:13:04 +0900
commit3ded25204b9b91cf4f14977cae81215a60409558 (patch)
tree312f1406768f11a1d478b1eb9f2a8f1017a9a1e5 /compiler/rustc_parse/src
parent192acb4b98bb027cb09eaeb8906dbc94a76b9b66 (diff)
downloadrust-3ded25204b9b91cf4f14977cae81215a60409558.tar.gz
rust-3ded25204b9b91cf4f14977cae81215a60409558.zip
implement `SnapshotParser` struct
Diffstat (limited to 'compiler/rustc_parse/src')
-rw-r--r--compiler/rustc_parse/src/parser/diagnostics.rs33
-rw-r--r--compiler/rustc_parse/src/parser/path.rs6
2 files changed, 33 insertions, 6 deletions
diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs
index 94805f4eb8b..4360399d447 100644
--- a/compiler/rustc_parse/src/parser/diagnostics.rs
+++ b/compiler/rustc_parse/src/parser/diagnostics.rs
@@ -5,6 +5,7 @@ use super::{
     SemiColonMode, SeqSep, TokenExpectType, TokenType,
 };
 
+use crate::lexer::UnmatchedBrace;
 use rustc_ast as ast;
 use rustc_ast::ptr::P;
 use rustc_ast::token::{self, Lit, LitKind, TokenKind};
@@ -21,6 +22,7 @@ use rustc_errors::{Applicability, DiagnosticBuilder, Handler, PResult};
 use rustc_span::source_map::Spanned;
 use rustc_span::symbol::{kw, Ident};
 use rustc_span::{MultiSpan, Span, SpanSnippetError, DUMMY_SP};
+use std::ops::{Deref, DerefMut};
 
 use std::mem::take;
 
@@ -154,6 +156,25 @@ impl AttemptLocalParseRecovery {
     }
 }
 
+pub(super) struct SnapshotParser<'a> {
+    parser: Parser<'a>,
+    unclosed_delims: Vec<UnmatchedBrace>,
+}
+
+impl<'a> Deref for SnapshotParser<'a> {
+    type Target = Parser<'a>;
+
+    fn deref(&self) -> &Self::Target {
+        &self.parser
+    }
+}
+
+impl<'a> DerefMut for SnapshotParser<'a> {
+    fn deref_mut(&mut self) -> &mut Self::Target {
+        &mut self.parser
+    }
+}
+
 impl<'a> Parser<'a> {
     pub(super) fn span_err<S: Into<MultiSpan>>(
         &self,
@@ -179,11 +200,17 @@ impl<'a> Parser<'a> {
         &self.sess.span_diagnostic
     }
 
-    pub(super) fn diagnostic_snapshot(&self) -> Self {
+    pub(super) fn restore(&mut self, snapshot: SnapshotParser<'a>) {
+        *self = snapshot.parser;
+        self.unclosed_delims.extend(snapshot.unclosed_delims.clone());
+    }
+
+    pub(super) fn diagnostic_snapshot(&self) -> SnapshotParser<'a> {
         let mut snapshot = self.clone();
+        let unclosed_delims = self.unclosed_delims.clone();
         // initialize unclosed_delims to avoid duplicate errors.
-        snapshot.unclosed_delims = vec![];
-        snapshot
+        snapshot.unclosed_delims.clear();
+        SnapshotParser { parser: snapshot, unclosed_delims }
     }
 
     pub(super) fn span_to_snippet(&self, span: Span) -> Result<String, SpanSnippetError> {
diff --git a/compiler/rustc_parse/src/parser/path.rs b/compiler/rustc_parse/src/parser/path.rs
index 264174b0ac7..12d50fdba10 100644
--- a/compiler/rustc_parse/src/parser/path.rs
+++ b/compiler/rustc_parse/src/parser/path.rs
@@ -630,9 +630,9 @@ impl<'a> Parser<'a> {
                 Ok(ty) => GenericArg::Type(ty),
                 Err(err) => {
                     if is_const_fn {
-                        if let Ok(expr) = snapshot.parse_expr_res(Restrictions::CONST_EXPR, None) {
-                            snapshot.unclosed_delims.extend(self.unclosed_delims.clone());
-                            *self = snapshot;
+                        if let Ok(expr) = (*snapshot).parse_expr_res(Restrictions::CONST_EXPR, None)
+                        {
+                            self.restore(snapshot);
                             return Ok(Some(self.dummy_const_arg_needs_braces(err, expr.span)));
                         }
                     }