@@ -19,7 +19,6 @@ use crate::errors::{
19
19
} ;
20
20
21
21
use crate :: fluent_generated as fluent;
22
- use crate :: lexer:: UnmatchedDelim ;
23
22
use crate :: parser;
24
23
use rustc_ast as ast;
25
24
use rustc_ast:: ptr:: P ;
@@ -220,7 +219,6 @@ impl MultiSugg {
220
219
/// is dropped.
221
220
pub struct SnapshotParser < ' a > {
222
221
parser : Parser < ' a > ,
223
- unclosed_delims : Vec < UnmatchedDelim > ,
224
222
}
225
223
226
224
impl < ' a > Deref for SnapshotParser < ' a > {
@@ -255,27 +253,15 @@ impl<'a> Parser<'a> {
255
253
& self . sess . span_diagnostic
256
254
}
257
255
258
- /// Replace `self` with `snapshot.parser` and extend `unclosed_delims` with `snapshot.unclosed_delims`.
259
- /// This is to avoid losing unclosed delims errors `create_snapshot_for_diagnostic` clears.
256
+ /// Replace `self` with `snapshot.parser`.
260
257
pub ( super ) fn restore_snapshot ( & mut self , snapshot : SnapshotParser < ' a > ) {
261
258
* self = snapshot. parser ;
262
- self . unclosed_delims . extend ( snapshot. unclosed_delims ) ;
263
- }
264
-
265
- pub fn unclosed_delims ( & self ) -> & [ UnmatchedDelim ] {
266
- & self . unclosed_delims
267
259
}
268
260
269
261
/// Create a snapshot of the `Parser`.
270
262
pub fn create_snapshot_for_diagnostic ( & self ) -> SnapshotParser < ' a > {
271
- let mut snapshot = self . clone ( ) ;
272
- let unclosed_delims = self . unclosed_delims . clone ( ) ;
273
- // Clear `unclosed_delims` in snapshot to avoid
274
- // duplicate errors being emitted when the `Parser`
275
- // is dropped (which may or may not happen, depending
276
- // if the parsing the snapshot is created for is successful)
277
- snapshot. unclosed_delims . clear ( ) ;
278
- SnapshotParser { parser : snapshot, unclosed_delims }
263
+ let snapshot = self . clone ( ) ;
264
+ SnapshotParser { parser : snapshot }
279
265
}
280
266
281
267
pub ( super ) fn span_to_snippet ( & self , span : Span ) -> Result < String , SpanSnippetError > {
@@ -579,21 +565,6 @@ impl<'a> Parser<'a> {
579
565
} else {
580
566
label_sp
581
567
} ;
582
- match self . recover_closing_delimiter (
583
- & expected
584
- . iter ( )
585
- . filter_map ( |tt| match tt {
586
- TokenType :: Token ( t) => Some ( t. clone ( ) ) ,
587
- _ => None ,
588
- } )
589
- . collect :: < Vec < _ > > ( ) ,
590
- err,
591
- ) {
592
- Err ( e) => err = e,
593
- Ok ( recovered) => {
594
- return Ok ( recovered) ;
595
- }
596
- }
597
568
598
569
if self . check_too_many_raw_str_terminators ( & mut err) {
599
570
if expected. contains ( & TokenType :: Token ( token:: Semi ) ) && self . eat ( & token:: Semi ) {
@@ -1573,12 +1544,6 @@ impl<'a> Parser<'a> {
1573
1544
) ;
1574
1545
let mut err = self . struct_span_err ( sp, & msg) ;
1575
1546
let label_exp = format ! ( "expected `{token_str}`" ) ;
1576
- match self . recover_closing_delimiter ( & [ t. clone ( ) ] , err) {
1577
- Err ( e) => err = e,
1578
- Ok ( recovered) => {
1579
- return Ok ( recovered) ;
1580
- }
1581
- }
1582
1547
let sm = self . sess . source_map ( ) ;
1583
1548
if !sm. is_multiline ( prev_sp. until ( sp) ) {
1584
1549
// When the spans are in the same line, it means that the only content
@@ -1795,81 +1760,6 @@ impl<'a> Parser<'a> {
1795
1760
}
1796
1761
}
1797
1762
1798
- pub ( super ) fn recover_closing_delimiter (
1799
- & mut self ,
1800
- tokens : & [ TokenKind ] ,
1801
- mut err : DiagnosticBuilder < ' a , ErrorGuaranteed > ,
1802
- ) -> PResult < ' a , bool > {
1803
- let mut pos = None ;
1804
- // We want to use the last closing delim that would apply.
1805
- for ( i, unmatched) in self . unclosed_delims . iter ( ) . enumerate ( ) . rev ( ) {
1806
- if tokens. contains ( & token:: CloseDelim ( unmatched. expected_delim ) )
1807
- && Some ( self . token . span ) > unmatched. unclosed_span
1808
- {
1809
- pos = Some ( i) ;
1810
- }
1811
- }
1812
- match pos {
1813
- Some ( pos) => {
1814
- // Recover and assume that the detected unclosed delimiter was meant for
1815
- // this location. Emit the diagnostic and act as if the delimiter was
1816
- // present for the parser's sake.
1817
-
1818
- // Don't attempt to recover from this unclosed delimiter more than once.
1819
- let unmatched = self . unclosed_delims . remove ( pos) ;
1820
- let delim = TokenType :: Token ( token:: CloseDelim ( unmatched. expected_delim ) ) ;
1821
- if unmatched. found_delim . is_none ( ) {
1822
- // We encountered `Eof`, set this fact here to avoid complaining about missing
1823
- // `fn main()` when we found place to suggest the closing brace.
1824
- * self . sess . reached_eof . borrow_mut ( ) = true ;
1825
- }
1826
-
1827
- // We want to suggest the inclusion of the closing delimiter where it makes
1828
- // the most sense, which is immediately after the last token:
1829
- //
1830
- // {foo(bar {}}
1831
- // ^ ^
1832
- // | |
1833
- // | help: `)` may belong here
1834
- // |
1835
- // unclosed delimiter
1836
- if let Some ( sp) = unmatched. unclosed_span {
1837
- let mut primary_span: Vec < Span > =
1838
- err. span . primary_spans ( ) . iter ( ) . cloned ( ) . collect ( ) ;
1839
- primary_span. push ( sp) ;
1840
- let mut primary_span: MultiSpan = primary_span. into ( ) ;
1841
- for span_label in err. span . span_labels ( ) {
1842
- if let Some ( label) = span_label. label {
1843
- primary_span. push_span_label ( span_label. span , label) ;
1844
- }
1845
- }
1846
- err. set_span ( primary_span) ;
1847
- err. span_label ( sp, "unclosed delimiter" ) ;
1848
- }
1849
- // Backticks should be removed to apply suggestions.
1850
- let mut delim = delim. to_string ( ) ;
1851
- delim. retain ( |c| c != '`' ) ;
1852
- err. span_suggestion_short (
1853
- self . prev_token . span . shrink_to_hi ( ) ,
1854
- & format ! ( "`{delim}` may belong here" ) ,
1855
- delim,
1856
- Applicability :: MaybeIncorrect ,
1857
- ) ;
1858
- if unmatched. found_delim . is_none ( ) {
1859
- // Encountered `Eof` when lexing blocks. Do not recover here to avoid knockdown
1860
- // errors which would be emitted elsewhere in the parser and let other error
1861
- // recovery consume the rest of the file.
1862
- Err ( err)
1863
- } else {
1864
- err. emit ( ) ;
1865
- self . expected_tokens . clear ( ) ; // Reduce the number of errors.
1866
- Ok ( true )
1867
- }
1868
- }
1869
- _ => Err ( err) ,
1870
- }
1871
- }
1872
-
1873
1763
/// Eats tokens until we can be relatively sure we reached the end of the
1874
1764
/// statement. This is something of a best-effort heuristic.
1875
1765
///
0 commit comments