@@ -156,6 +156,9 @@ impl AttemptLocalParseRecovery {
156156 }
157157}
158158
159+ // SnapshotParser is used to create a snapshot of the parser
160+ // without causing duplicate errors being emitted when the `Parser`
161+ // is dropped.
159162pub ( super ) struct SnapshotParser < ' a > {
160163 parser : Parser < ' a > ,
161164 unclosed_delims : Vec < UnmatchedBrace > ,
@@ -200,15 +203,21 @@ impl<'a> Parser<'a> {
200203 & self . sess . span_diagnostic
201204 }
202205
203- pub ( super ) fn restore ( & mut self , snapshot : SnapshotParser < ' a > ) {
206+ /// Relace `self` with `snapshot.parser` and extend `unclosed_delims` with `snapshot.unclosed_delims`.
207+ /// This is to avoid losing unclosed delims errors `create_snapshot_for_diagnostic` clears.
208+ pub ( super ) fn restore_snapshot ( & mut self , snapshot : SnapshotParser < ' a > ) {
204209 * self = snapshot. parser ;
205210 self . unclosed_delims . extend ( snapshot. unclosed_delims . clone ( ) ) ;
206211 }
207212
208- pub ( super ) fn diagnostic_snapshot ( & self ) -> SnapshotParser < ' a > {
213+ /// Create a snapshot of the `Parser`.
214+ pub ( super ) fn create_snapshot_for_diagnostic ( & self ) -> SnapshotParser < ' a > {
209215 let mut snapshot = self . clone ( ) ;
210216 let unclosed_delims = self . unclosed_delims . clone ( ) ;
211- // initialize unclosed_delims to avoid duplicate errors.
217+ // Clear `unclosed_delims` in snapshot to avoid
218+ // duplicate errors being emitted when the `Parser`
219+ // is dropped (which may or may not happen, depending
220+ // if the parsing the snapshot is created for is successful)
212221 snapshot. unclosed_delims . clear ( ) ;
213222 SnapshotParser { parser : snapshot, unclosed_delims }
214223 }
0 commit comments