about summary refs log tree commit diff
diff options
context:
space:
mode:
authorTakayuki Maeda <takoyaki0316@gmail.com>2022-03-10 19:34:42 +0900
committerTakayuki Maeda <takoyaki0316@gmail.com>2022-03-10 19:34:42 +0900
commit2db82368574b7b751203418300345a4eb75f1147 (patch)
tree47cc31689e643c9d1f2ed65f725409c22afc422a
parent3ded25204b9b91cf4f14977cae81215a60409558 (diff)
downloadrust-2db82368574b7b751203418300345a4eb75f1147.tar.gz
rust-2db82368574b7b751203418300345a4eb75f1147.zip
add doc comments
-rw-r--r--compiler/rustc_parse/src/parser/diagnostics.rs15
-rw-r--r--compiler/rustc_parse/src/parser/path.rs4
2 files changed, 14 insertions, 5 deletions
diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs
index 4360399d447..e042df98edf 100644
--- a/compiler/rustc_parse/src/parser/diagnostics.rs
+++ b/compiler/rustc_parse/src/parser/diagnostics.rs
@@ -156,6 +156,9 @@ impl AttemptLocalParseRecovery {
     }
 }
 
+// SnapshotParser is used to create a snapshot of the parser
+// without causing duplicate errors being emitted when the `Parser`
+// is dropped.
 pub(super) struct SnapshotParser<'a> {
     parser: Parser<'a>,
     unclosed_delims: Vec<UnmatchedBrace>,
@@ -200,15 +203,21 @@ impl<'a> Parser<'a> {
         &self.sess.span_diagnostic
     }
 
-    pub(super) fn restore(&mut self, snapshot: SnapshotParser<'a>) {
+    /// Relace `self` with `snapshot.parser` and extend `unclosed_delims` with `snapshot.unclosed_delims`.
+    /// This is to avoid losing unclosed delims errors `create_snapshot_for_diagnostic` clears.
+    pub(super) fn restore_snapshot(&mut self, snapshot: SnapshotParser<'a>) {
         *self = snapshot.parser;
         self.unclosed_delims.extend(snapshot.unclosed_delims.clone());
     }
 
-    pub(super) fn diagnostic_snapshot(&self) -> SnapshotParser<'a> {
+    /// Create a snapshot of the `Parser`.
+    pub(super) fn create_snapshot_for_diagnostic(&self) -> SnapshotParser<'a> {
         let mut snapshot = self.clone();
         let unclosed_delims = self.unclosed_delims.clone();
-        // initialize unclosed_delims to avoid duplicate errors.
+        // Clear `unclosed_delims` in snapshot to avoid
+        // duplicate errors being emitted when the `Parser`
+        // is dropped (which may or may not happen, depending
+        // if the parsing the snapshot is created for is successful)
         snapshot.unclosed_delims.clear();
         SnapshotParser { parser: snapshot, unclosed_delims }
     }
diff --git a/compiler/rustc_parse/src/parser/path.rs b/compiler/rustc_parse/src/parser/path.rs
index 12d50fdba10..5d41d808336 100644
--- a/compiler/rustc_parse/src/parser/path.rs
+++ b/compiler/rustc_parse/src/parser/path.rs
@@ -625,14 +625,14 @@ impl<'a> Parser<'a> {
         } else if self.check_type() {
             // Parse type argument.
             let is_const_fn = self.look_ahead(1, |t| t.kind == token::OpenDelim(token::Paren));
-            let mut snapshot = self.diagnostic_snapshot();
+            let mut snapshot = self.create_snapshot_for_diagnostic();
             match self.parse_ty() {
                 Ok(ty) => GenericArg::Type(ty),
                 Err(err) => {
                     if is_const_fn {
                         if let Ok(expr) = (*snapshot).parse_expr_res(Restrictions::CONST_EXPR, None)
                         {
-                            self.restore(snapshot);
+                            self.restore_snapshot(snapshot);
                             return Ok(Some(self.dummy_const_arg_needs_braces(err, expr.span)));
                         }
                     }