Skip to content

Commit

Permalink
implement SnapshotParser struct
Browse files Browse the repository at this point in the history
  • Loading branch information
TaKO8Ki committed Mar 9, 2022
1 parent 192acb4 commit 3ded252
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
33 changes: 30 additions & 3 deletions compiler/rustc_parse/src/parser/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use super::{
SemiColonMode, SeqSep, TokenExpectType, TokenType,
};

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

use std::mem::take;

Expand Down Expand Up @@ -154,6 +156,25 @@ impl AttemptLocalParseRecovery {
}
}

pub(super) struct SnapshotParser<'a> {
parser: Parser<'a>,
unclosed_delims: Vec<UnmatchedBrace>,
}

impl<'a> Deref for SnapshotParser<'a> {
type Target = Parser<'a>;

fn deref(&self) -> &Self::Target {
&self.parser
}
}

impl<'a> DerefMut for SnapshotParser<'a> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.parser
}
}

impl<'a> Parser<'a> {
pub(super) fn span_err<S: Into<MultiSpan>>(
&self,
Expand All @@ -179,11 +200,17 @@ impl<'a> Parser<'a> {
&self.sess.span_diagnostic
}

pub(super) fn diagnostic_snapshot(&self) -> Self {
pub(super) fn restore(&mut self, snapshot: SnapshotParser<'a>) {
*self = snapshot.parser;
self.unclosed_delims.extend(snapshot.unclosed_delims.clone());
}

pub(super) fn diagnostic_snapshot(&self) -> SnapshotParser<'a> {
let mut snapshot = self.clone();
let unclosed_delims = self.unclosed_delims.clone();
// initialize unclosed_delims to avoid duplicate errors.
snapshot.unclosed_delims = vec![];
snapshot
snapshot.unclosed_delims.clear();
SnapshotParser { parser: snapshot, unclosed_delims }
}

pub(super) fn span_to_snippet(&self, span: Span) -> Result<String, SpanSnippetError> {
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_parse/src/parser/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,9 +630,9 @@ impl<'a> Parser<'a> {
Ok(ty) => GenericArg::Type(ty),
Err(err) => {
if is_const_fn {
if let Ok(expr) = snapshot.parse_expr_res(Restrictions::CONST_EXPR, None) {
snapshot.unclosed_delims.extend(self.unclosed_delims.clone());
*self = snapshot;
if let Ok(expr) = (*snapshot).parse_expr_res(Restrictions::CONST_EXPR, None)
{
self.restore(snapshot);
return Ok(Some(self.dummy_const_arg_needs_braces(err, expr.span)));
}
}
Expand Down

0 comments on commit 3ded252

Please sign in to comment.