Skip to content

Commit 3ded252

Browse files
committed
implement SnapshotParser struct
1 parent 192acb4 commit 3ded252

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

compiler/rustc_parse/src/parser/diagnostics.rs

+30-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use super::{
55
SemiColonMode, SeqSep, TokenExpectType, TokenType,
66
};
77

8+
use crate::lexer::UnmatchedBrace;
89
use rustc_ast as ast;
910
use rustc_ast::ptr::P;
1011
use rustc_ast::token::{self, Lit, LitKind, TokenKind};
@@ -21,6 +22,7 @@ use rustc_errors::{Applicability, DiagnosticBuilder, Handler, PResult};
2122
use rustc_span::source_map::Spanned;
2223
use rustc_span::symbol::{kw, Ident};
2324
use rustc_span::{MultiSpan, Span, SpanSnippetError, DUMMY_SP};
25+
use std::ops::{Deref, DerefMut};
2426

2527
use std::mem::take;
2628

@@ -154,6 +156,25 @@ impl AttemptLocalParseRecovery {
154156
}
155157
}
156158

159+
pub(super) struct SnapshotParser<'a> {
160+
parser: Parser<'a>,
161+
unclosed_delims: Vec<UnmatchedBrace>,
162+
}
163+
164+
impl<'a> Deref for SnapshotParser<'a> {
165+
type Target = Parser<'a>;
166+
167+
fn deref(&self) -> &Self::Target {
168+
&self.parser
169+
}
170+
}
171+
172+
impl<'a> DerefMut for SnapshotParser<'a> {
173+
fn deref_mut(&mut self) -> &mut Self::Target {
174+
&mut self.parser
175+
}
176+
}
177+
157178
impl<'a> Parser<'a> {
158179
pub(super) fn span_err<S: Into<MultiSpan>>(
159180
&self,
@@ -179,11 +200,17 @@ impl<'a> Parser<'a> {
179200
&self.sess.span_diagnostic
180201
}
181202

182-
pub(super) fn diagnostic_snapshot(&self) -> Self {
203+
pub(super) fn restore(&mut self, snapshot: SnapshotParser<'a>) {
204+
*self = snapshot.parser;
205+
self.unclosed_delims.extend(snapshot.unclosed_delims.clone());
206+
}
207+
208+
pub(super) fn diagnostic_snapshot(&self) -> SnapshotParser<'a> {
183209
let mut snapshot = self.clone();
210+
let unclosed_delims = self.unclosed_delims.clone();
184211
// initialize unclosed_delims to avoid duplicate errors.
185-
snapshot.unclosed_delims = vec![];
186-
snapshot
212+
snapshot.unclosed_delims.clear();
213+
SnapshotParser { parser: snapshot, unclosed_delims }
187214
}
188215

189216
pub(super) fn span_to_snippet(&self, span: Span) -> Result<String, SpanSnippetError> {

compiler/rustc_parse/src/parser/path.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -630,9 +630,9 @@ impl<'a> Parser<'a> {
630630
Ok(ty) => GenericArg::Type(ty),
631631
Err(err) => {
632632
if is_const_fn {
633-
if let Ok(expr) = snapshot.parse_expr_res(Restrictions::CONST_EXPR, None) {
634-
snapshot.unclosed_delims.extend(self.unclosed_delims.clone());
635-
*self = snapshot;
633+
if let Ok(expr) = (*snapshot).parse_expr_res(Restrictions::CONST_EXPR, None)
634+
{
635+
self.restore(snapshot);
636636
return Ok(Some(self.dummy_const_arg_needs_braces(err, expr.span)));
637637
}
638638
}

0 commit comments

Comments
 (0)