@@ -46,7 +46,7 @@ use crate::ThinVec;
4646use crate :: tokenstream:: { self , DelimSpan , TokenTree , TokenStream , TreeAndJoint } ;
4747use crate :: symbol:: { Symbol , keywords} ;
4848
49- use errors:: { Applicability , DiagnosticBuilder , DiagnosticId } ;
49+ use errors:: { Applicability , DiagnosticBuilder , DiagnosticId , FatalError } ;
5050use rustc_target:: spec:: abi:: { self , Abi } ;
5151use syntax_pos:: { Span , MultiSpan , BytePos , FileName } ;
5252use log:: { debug, trace} ;
@@ -256,8 +256,15 @@ pub struct Parser<'a> {
256256 /// it gets removed from here. Every entry left at the end gets emitted as an independent
257257 /// error.
258258 crate unclosed_delims : Vec < UnmatchedBrace > ,
259+ last_unexpected_token_span : Option < Span > ,
259260}
260261
262+ impl < ' a > Drop for Parser < ' a > {
263+ fn drop ( & mut self ) {
264+ let diag = self . diagnostic ( ) ;
265+ emit_unclosed_delims ( & mut self . unclosed_delims , diag) ;
266+ }
267+ }
261268
262269#[ derive( Clone ) ]
263270struct TokenCursor {
@@ -582,6 +589,7 @@ impl<'a> Parser<'a> {
582589 unmatched_angle_bracket_count : 0 ,
583590 max_angle_bracket_count : 0 ,
584591 unclosed_delims : Vec :: new ( ) ,
592+ last_unexpected_token_span : None ,
585593 } ;
586594
587595 let tok = parser. next_tok ( ) ;
@@ -775,6 +783,8 @@ impl<'a> Parser<'a> {
775783 } else if inedible. contains ( & self . token ) {
776784 // leave it in the input
777785 Ok ( false )
786+ } else if self . last_unexpected_token_span == Some ( self . span ) {
787+ FatalError . raise ( ) ;
778788 } else {
779789 let mut expected = edible. iter ( )
780790 . map ( |x| TokenType :: Token ( x. clone ( ) ) )
@@ -802,6 +812,7 @@ impl<'a> Parser<'a> {
802812 ( self . sess . source_map ( ) . next_point ( self . prev_span ) ,
803813 format ! ( "expected {} here" , expect) ) )
804814 } ;
815+ self . last_unexpected_token_span = Some ( self . span ) ;
805816 let mut err = self . fatal ( & msg_exp) ;
806817 if self . token . is_ident_named ( "and" ) {
807818 err. span_suggestion_short (
@@ -1497,9 +1508,13 @@ impl<'a> Parser<'a> {
14971508 pub fn parse_trait_item ( & mut self , at_end : & mut bool ) -> PResult < ' a , TraitItem > {
14981509 maybe_whole ! ( self , NtTraitItem , |x| x) ;
14991510 let attrs = self . parse_outer_attributes ( ) ?;
1511+ let mut unclosed_delims = vec ! [ ] ;
15001512 let ( mut item, tokens) = self . collect_tokens ( |this| {
1501- this. parse_trait_item_ ( at_end, attrs)
1513+ let item = this. parse_trait_item_ ( at_end, attrs) ;
1514+ unclosed_delims. append ( & mut this. unclosed_delims ) ;
1515+ item
15021516 } ) ?;
1517+ self . unclosed_delims . append ( & mut unclosed_delims) ;
15031518 // See `parse_item` for why this clause is here.
15041519 if !item. attrs . iter ( ) . any ( |attr| attr. style == AttrStyle :: Inner ) {
15051520 item. tokens = Some ( tokens) ;
@@ -6333,7 +6348,10 @@ impl<'a> Parser<'a> {
63336348 fn_inputs. append ( & mut input) ;
63346349 ( fn_inputs, recovered)
63356350 } else {
6336- return self . unexpected ( ) ;
6351+ match self . expect_one_of ( & [ ] , & [ ] ) {
6352+ Err ( err) => return Err ( err) ,
6353+ Ok ( recovered) => ( vec ! [ self_arg] , recovered) ,
6354+ }
63376355 }
63386356 } else {
63396357 self . parse_seq_to_before_end ( & token:: CloseDelim ( token:: Paren ) , sep, parse_arg_fn) ?
@@ -6459,9 +6477,13 @@ impl<'a> Parser<'a> {
64596477 pub fn parse_impl_item ( & mut self , at_end : & mut bool ) -> PResult < ' a , ImplItem > {
64606478 maybe_whole ! ( self , NtImplItem , |x| x) ;
64616479 let attrs = self . parse_outer_attributes ( ) ?;
6480+ let mut unclosed_delims = vec ! [ ] ;
64626481 let ( mut item, tokens) = self . collect_tokens ( |this| {
6463- this. parse_impl_item_ ( at_end, attrs)
6482+ let item = this. parse_impl_item_ ( at_end, attrs) ;
6483+ unclosed_delims. append ( & mut this. unclosed_delims ) ;
6484+ item
64646485 } ) ?;
6486+ self . unclosed_delims . append ( & mut unclosed_delims) ;
64656487
64666488 // See `parse_item` for why this clause is here.
64676489 if !item. attrs . iter ( ) . any ( |attr| attr. style == AttrStyle :: Inner ) {
@@ -7781,9 +7803,13 @@ impl<'a> Parser<'a> {
77817803 macros_allowed : bool ,
77827804 attributes_allowed : bool ,
77837805 ) -> PResult < ' a , Option < P < Item > > > {
7806+ let mut unclosed_delims = vec ! [ ] ;
77847807 let ( ret, tokens) = self . collect_tokens ( |this| {
7785- this. parse_item_implementation ( attrs, macros_allowed, attributes_allowed)
7808+ let item = this. parse_item_implementation ( attrs, macros_allowed, attributes_allowed) ;
7809+ unclosed_delims. append ( & mut this. unclosed_delims ) ;
7810+ item
77867811 } ) ?;
7812+ self . unclosed_delims . append ( & mut unclosed_delims) ;
77877813
77887814 // Once we've parsed an item and recorded the tokens we got while
77897815 // parsing we may want to store `tokens` into the item we're about to
@@ -8539,8 +8565,6 @@ impl<'a> Parser<'a> {
85398565 module : self . parse_mod_items ( & token:: Eof , lo) ?,
85408566 span : lo. to ( self . span ) ,
85418567 } ) ;
8542- emit_unclosed_delims ( & self . unclosed_delims , self . diagnostic ( ) ) ;
8543- self . unclosed_delims . clear ( ) ;
85448568 krate
85458569 }
85468570
@@ -8571,8 +8595,8 @@ impl<'a> Parser<'a> {
85718595 }
85728596}
85738597
8574- pub fn emit_unclosed_delims ( unclosed_delims : & [ UnmatchedBrace ] , handler : & errors:: Handler ) {
8575- for unmatched in unclosed_delims {
8598+ pub fn emit_unclosed_delims ( unclosed_delims : & mut Vec < UnmatchedBrace > , handler : & errors:: Handler ) {
8599+ for unmatched in unclosed_delims. iter ( ) {
85768600 let mut err = handler. struct_span_err ( unmatched. found_span , & format ! (
85778601 "incorrect close delimiter: `{}`" ,
85788602 pprust:: token_to_string( & token:: Token :: CloseDelim ( unmatched. found_delim) ) ,
@@ -8586,4 +8610,5 @@ pub fn emit_unclosed_delims(unclosed_delims: &[UnmatchedBrace], handler: &errors
85868610 }
85878611 err. emit ( ) ;
85888612 }
8613+ unclosed_delims. clear ( ) ;
85898614}
0 commit comments