@@ -19,7 +19,6 @@ use crate::errors::{
1919} ;
2020
2121use crate :: fluent_generated as fluent;
22- use crate :: lexer:: UnmatchedDelim ;
2322use crate :: parser;
2423use rustc_ast as ast;
2524use rustc_ast:: ptr:: P ;
@@ -220,7 +219,6 @@ impl MultiSugg {
220219/// is dropped.
221220pub struct SnapshotParser < ' a > {
222221 parser : Parser < ' a > ,
223- unclosed_delims : Vec < UnmatchedDelim > ,
224222}
225223
226224impl < ' a > Deref for SnapshotParser < ' a > {
@@ -255,27 +253,15 @@ impl<'a> Parser<'a> {
255253 & self . sess . span_diagnostic
256254 }
257255
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`.
260257 pub ( super ) fn restore_snapshot ( & mut self , snapshot : SnapshotParser < ' a > ) {
261258 * self = snapshot. parser ;
262- self . unclosed_delims . extend ( snapshot. unclosed_delims ) ;
263- }
264-
265- pub fn unclosed_delims ( & self ) -> & [ UnmatchedDelim ] {
266- & self . unclosed_delims
267259 }
268260
269261 /// Create a snapshot of the `Parser`.
270262 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 }
279265 }
280266
281267 pub ( super ) fn span_to_snippet ( & self , span : Span ) -> Result < String , SpanSnippetError > {
@@ -579,21 +565,6 @@ impl<'a> Parser<'a> {
579565 } else {
580566 label_sp
581567 } ;
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- }
597568
598569 if self . check_too_many_raw_str_terminators ( & mut err) {
599570 if expected. contains ( & TokenType :: Token ( token:: Semi ) ) && self . eat ( & token:: Semi ) {
@@ -1573,12 +1544,6 @@ impl<'a> Parser<'a> {
15731544 ) ;
15741545 let mut err = self . struct_span_err ( sp, & msg) ;
15751546 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- }
15821547 let sm = self . sess . source_map ( ) ;
15831548 if !sm. is_multiline ( prev_sp. until ( sp) ) {
15841549 // When the spans are in the same line, it means that the only content
@@ -1795,81 +1760,6 @@ impl<'a> Parser<'a> {
17951760 }
17961761 }
17971762
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-
18731763 /// Eats tokens until we can be relatively sure we reached the end of the
18741764 /// statement. This is something of a best-effort heuristic.
18751765 ///
0 commit comments