@@ -51,7 +51,7 @@ use crate::symbol::{kw, sym, Symbol};
5151use errors:: { Applicability , DiagnosticBuilder , DiagnosticId , FatalError } ;
5252use rustc_target:: spec:: abi:: { self , Abi } ;
5353use syntax_pos:: {
54- Span , MultiSpan , BytePos , FileName ,
54+ BytePos , DUMMY_SP , FileName , MultiSpan , Span ,
5555 hygiene:: CompilerDesugaringKind ,
5656} ;
5757use log:: { debug, trace} ;
@@ -233,6 +233,8 @@ pub struct Parser<'a> {
233233 /// error.
234234 crate unclosed_delims : Vec < UnmatchedBrace > ,
235235 last_unexpected_token_span : Option < Span > ,
236+ /// If `true`, this `Parser` is not parsing Rust code but rather a macro call.
237+ is_subparser : Option < & ' static str > ,
236238}
237239
238240impl < ' a > Drop for Parser < ' a > {
@@ -309,7 +311,7 @@ impl TokenCursor {
309311 self . frame = frame;
310312 continue
311313 } else {
312- return TokenAndSpan { tok : token:: Eof , sp : syntax_pos :: DUMMY_SP }
314+ return TokenAndSpan { tok : token:: Eof , sp : DUMMY_SP }
313315 } ;
314316
315317 match self . frame . last_token {
@@ -533,17 +535,19 @@ enum TokenExpectType {
533535}
534536
535537impl < ' a > Parser < ' a > {
536- pub fn new ( sess : & ' a ParseSess ,
537- tokens : TokenStream ,
538- directory : Option < Directory < ' a > > ,
539- recurse_into_file_modules : bool ,
540- desugar_doc_comments : bool )
541- -> Self {
538+ pub fn new (
539+ sess : & ' a ParseSess ,
540+ tokens : TokenStream ,
541+ directory : Option < Directory < ' a > > ,
542+ recurse_into_file_modules : bool ,
543+ desugar_doc_comments : bool ,
544+ is_subparser : Option < & ' static str > ,
545+ ) -> Self {
542546 let mut parser = Parser {
543547 sess,
544548 token : token:: Whitespace ,
545- span : syntax_pos :: DUMMY_SP ,
546- prev_span : syntax_pos :: DUMMY_SP ,
549+ span : DUMMY_SP ,
550+ prev_span : DUMMY_SP ,
547551 meta_var_span : None ,
548552 prev_token_kind : PrevTokenKind :: Other ,
549553 restrictions : Restrictions :: empty ( ) ,
@@ -568,6 +572,7 @@ impl<'a> Parser<'a> {
568572 max_angle_bracket_count : 0 ,
569573 unclosed_delims : Vec :: new ( ) ,
570574 last_unexpected_token_span : None ,
575+ is_subparser,
571576 } ;
572577
573578 let tok = parser. next_tok ( ) ;
@@ -639,16 +644,28 @@ impl<'a> Parser<'a> {
639644 } else {
640645 let token_str = pprust:: token_to_string ( t) ;
641646 let this_token_str = self . this_token_descr ( ) ;
642- let mut err = self . fatal ( & format ! ( "expected `{}`, found {}" ,
643- token_str,
644- this_token_str) ) ;
645-
646- let sp = if self . token == token:: Token :: Eof {
647- // EOF, don't want to point at the following char, but rather the last token
648- self . prev_span
649- } else {
650- self . sess . source_map ( ) . next_point ( self . prev_span )
647+ let ( prev_sp, sp) = match ( & self . token , self . is_subparser ) {
648+ // Point at the end of the macro call when reaching end of macro arguments.
649+ ( token:: Token :: Eof , Some ( _) ) => {
650+ let sp = self . sess . source_map ( ) . next_point ( self . span ) ;
651+ ( sp, sp)
652+ }
653+ // We don't want to point at the following span after DUMMY_SP.
654+ // This happens when the parser finds an empty TokenStream.
655+ _ if self . prev_span == DUMMY_SP => ( self . span , self . span ) ,
656+ // EOF, don't want to point at the following char, but rather the last token.
657+ ( token:: Token :: Eof , None ) => ( self . prev_span , self . span ) ,
658+ _ => ( self . sess . source_map ( ) . next_point ( self . prev_span ) , self . span ) ,
651659 } ;
660+ let msg = format ! (
661+ "expected `{}`, found {}" ,
662+ token_str,
663+ match ( & self . token, self . is_subparser) {
664+ ( token:: Token :: Eof , Some ( origin) ) => format!( "end of {}" , origin) ,
665+ _ => this_token_str,
666+ } ,
667+ ) ;
668+ let mut err = self . struct_span_err ( sp, & msg) ;
652669 let label_exp = format ! ( "expected `{}`" , token_str) ;
653670 match self . recover_closing_delimiter ( & [ t. clone ( ) ] , err) {
654671 Err ( e) => err = e,
@@ -657,15 +674,15 @@ impl<'a> Parser<'a> {
657674 }
658675 }
659676 let cm = self . sess . source_map ( ) ;
660- match ( cm. lookup_line ( self . span . lo ( ) ) , cm. lookup_line ( sp. lo ( ) ) ) {
677+ match ( cm. lookup_line ( prev_sp . lo ( ) ) , cm. lookup_line ( sp. lo ( ) ) ) {
661678 ( Ok ( ref a) , Ok ( ref b) ) if a. line == b. line => {
662679 // When the spans are in the same line, it means that the only content
663680 // between them is whitespace, point only at the found token.
664- err. span_label ( self . span , label_exp) ;
681+ err. span_label ( sp , label_exp) ;
665682 }
666683 _ => {
667- err. span_label ( sp , label_exp) ;
668- err. span_label ( self . span , "unexpected token" ) ;
684+ err. span_label ( prev_sp , label_exp) ;
685+ err. span_label ( sp , "unexpected token" ) ;
669686 }
670687 }
671688 Err ( err)
@@ -812,7 +829,7 @@ impl<'a> Parser<'a> {
812829 // | expected one of 8 possible tokens here
813830 err. span_label ( self . span , label_exp) ;
814831 }
815- _ if self . prev_span == syntax_pos :: DUMMY_SP => {
832+ _ if self . prev_span == DUMMY_SP => {
816833 // Account for macro context where the previous span might not be
817834 // available to avoid incorrect output (#54841).
818835 err. span_label ( self . span , "unexpected token" ) ;
@@ -2041,7 +2058,7 @@ impl<'a> Parser<'a> {
20412058 path = self . parse_path ( PathStyle :: Type ) ?;
20422059 path_span = path_lo. to ( self . prev_span ) ;
20432060 } else {
2044- path = ast:: Path { segments : Vec :: new ( ) , span : syntax_pos :: DUMMY_SP } ;
2061+ path = ast:: Path { segments : Vec :: new ( ) , span : DUMMY_SP } ;
20452062 path_span = self . span . to ( self . span ) ;
20462063 }
20472064
@@ -2627,16 +2644,24 @@ impl<'a> Parser<'a> {
26272644 }
26282645 Err ( mut err) => {
26292646 self . cancel ( & mut err) ;
2630- let msg = format ! ( "expected expression, found {}" ,
2631- self . this_token_descr( ) ) ;
2632- let mut err = self . fatal ( & msg) ;
2647+ let ( span, msg) = match ( & self . token , self . is_subparser ) {
2648+ ( & token:: Token :: Eof , Some ( origin) ) => {
2649+ let sp = self . sess . source_map ( ) . next_point ( self . span ) ;
2650+ ( sp, format ! ( "expected expression, found end of {}" , origin) )
2651+ }
2652+ _ => ( self . span , format ! (
2653+ "expected expression, found {}" ,
2654+ self . this_token_descr( ) ,
2655+ ) ) ,
2656+ } ;
2657+ let mut err = self . struct_span_err ( span, & msg) ;
26332658 let sp = self . sess . source_map ( ) . start_point ( self . span ) ;
26342659 if let Some ( sp) = self . sess . ambiguous_block_expr_parse . borrow ( )
26352660 . get ( & sp)
26362661 {
26372662 self . sess . expr_parentheses_needed ( & mut err, * sp, None ) ;
26382663 }
2639- err. span_label ( self . span , "expected expression" ) ;
2664+ err. span_label ( span, "expected expression" ) ;
26402665 return Err ( err) ;
26412666 }
26422667 }
@@ -5592,7 +5617,7 @@ impl<'a> Parser<'a> {
55925617 where_clause : WhereClause {
55935618 id : ast:: DUMMY_NODE_ID ,
55945619 predicates : Vec :: new ( ) ,
5595- span : syntax_pos :: DUMMY_SP ,
5620+ span : DUMMY_SP ,
55965621 } ,
55975622 span : span_lo. to ( self . prev_span ) ,
55985623 } )
@@ -5838,7 +5863,7 @@ impl<'a> Parser<'a> {
58385863 let mut where_clause = WhereClause {
58395864 id : ast:: DUMMY_NODE_ID ,
58405865 predicates : Vec :: new ( ) ,
5841- span : syntax_pos :: DUMMY_SP ,
5866+ span : DUMMY_SP ,
58425867 } ;
58435868
58445869 if !self . eat_keyword ( kw:: Where ) {
@@ -7005,15 +7030,15 @@ impl<'a> Parser<'a> {
70057030 Ident :: with_empty_ctxt ( sym:: warn_directory_ownership) ) ,
70067031 tokens : TokenStream :: empty ( ) ,
70077032 is_sugared_doc : false ,
7008- span : syntax_pos :: DUMMY_SP ,
7033+ span : DUMMY_SP ,
70097034 } ;
70107035 attr:: mark_known ( & attr) ;
70117036 attrs. push ( attr) ;
70127037 }
70137038 Ok ( ( id, ItemKind :: Mod ( module) , Some ( attrs) ) )
70147039 } else {
70157040 let placeholder = ast:: Mod {
7016- inner : syntax_pos :: DUMMY_SP ,
7041+ inner : DUMMY_SP ,
70177042 items : Vec :: new ( ) ,
70187043 inline : false
70197044 } ;
0 commit comments