@@ -5,6 +5,7 @@ use super::{
5
5
SemiColonMode , SeqSep , TokenExpectType , TokenType ,
6
6
} ;
7
7
8
+ use crate :: lexer:: UnmatchedBrace ;
8
9
use rustc_ast as ast;
9
10
use rustc_ast:: ptr:: P ;
10
11
use rustc_ast:: token:: { self , Lit , LitKind , TokenKind } ;
@@ -21,6 +22,7 @@ use rustc_errors::{Applicability, DiagnosticBuilder, Handler, PResult};
21
22
use rustc_span:: source_map:: Spanned ;
22
23
use rustc_span:: symbol:: { kw, Ident } ;
23
24
use rustc_span:: { MultiSpan , Span , SpanSnippetError , DUMMY_SP } ;
25
+ use std:: ops:: { Deref , DerefMut } ;
24
26
25
27
use std:: mem:: take;
26
28
@@ -154,6 +156,25 @@ impl AttemptLocalParseRecovery {
154
156
}
155
157
}
156
158
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
+
157
178
impl < ' a > Parser < ' a > {
158
179
pub ( super ) fn span_err < S : Into < MultiSpan > > (
159
180
& self ,
@@ -179,11 +200,17 @@ impl<'a> Parser<'a> {
179
200
& self . sess . span_diagnostic
180
201
}
181
202
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 > {
183
209
let mut snapshot = self . clone ( ) ;
210
+ let unclosed_delims = self . unclosed_delims . clone ( ) ;
184
211
// 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 }
187
214
}
188
215
189
216
pub ( super ) fn span_to_snippet ( & self , span : Span ) -> Result < String , SpanSnippetError > {
0 commit comments