Skip to content

Commit 2db8236

Browse files
committed
add doc comments
1 parent 3ded252 commit 2db8236

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

compiler/rustc_parse/src/parser/diagnostics.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
159162
pub(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
}

compiler/rustc_parse/src/parser/path.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -625,14 +625,14 @@ impl<'a> Parser<'a> {
625625
} else if self.check_type() {
626626
// Parse type argument.
627627
let is_const_fn = self.look_ahead(1, |t| t.kind == token::OpenDelim(token::Paren));
628-
let mut snapshot = self.diagnostic_snapshot();
628+
let mut snapshot = self.create_snapshot_for_diagnostic();
629629
match self.parse_ty() {
630630
Ok(ty) => GenericArg::Type(ty),
631631
Err(err) => {
632632
if is_const_fn {
633633
if let Ok(expr) = (*snapshot).parse_expr_res(Restrictions::CONST_EXPR, None)
634634
{
635-
self.restore(snapshot);
635+
self.restore_snapshot(snapshot);
636636
return Ok(Some(self.dummy_const_arg_needs_braces(err, expr.span)));
637637
}
638638
}

0 commit comments

Comments
 (0)