@@ -156,6 +156,9 @@ impl AttemptLocalParseRecovery {
156
156
}
157
157
}
158
158
159
+ // SnapshotParser is used to create a snapshot of the parser
160
+ // without causing duplicate errors being emitted when the `Parser`
161
+ // is dropped.
159
162
pub ( super ) struct SnapshotParser < ' a > {
160
163
parser : Parser < ' a > ,
161
164
unclosed_delims : Vec < UnmatchedBrace > ,
@@ -200,15 +203,21 @@ impl<'a> Parser<'a> {
200
203
& self . sess . span_diagnostic
201
204
}
202
205
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 > ) {
204
209
* self = snapshot. parser ;
205
210
self . unclosed_delims . extend ( snapshot. unclosed_delims . clone ( ) ) ;
206
211
}
207
212
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 > {
209
215
let mut snapshot = self . clone ( ) ;
210
216
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)
212
221
snapshot. unclosed_delims . clear ( ) ;
213
222
SnapshotParser { parser : snapshot, unclosed_delims }
214
223
}
0 commit comments