File tree Expand file tree Collapse file tree 1 file changed +20
-1
lines changed Expand file tree Collapse file tree 1 file changed +20
-1
lines changed Original file line number Diff line number Diff line change @@ -637,7 +637,26 @@ impl<'a> Parser<'a> {
637
637
let mut err = self . fatal ( & format ! ( "expected `{}`, found `{}`" ,
638
638
token_str,
639
639
this_token_str) ) ;
640
- err. span_label ( self . span , format ! ( "expected `{}`" , token_str) ) ;
640
+
641
+ let sp = if self . token == token:: Token :: Eof {
642
+ // EOF, don't want to point at the following char, but rather the last token
643
+ self . prev_span
644
+ } else {
645
+ self . sess . codemap ( ) . next_point ( self . prev_span )
646
+ } ;
647
+ let label_exp = format ! ( "expected `{}`" , token_str) ;
648
+ let cm = self . sess . codemap ( ) ;
649
+ match ( cm. lookup_line ( self . span . lo ( ) ) , cm. lookup_line ( sp. lo ( ) ) ) {
650
+ ( Ok ( ref a) , Ok ( ref b) ) if a. line == b. line => {
651
+ // When the spans are in the same line, it means that the only content between
652
+ // them is whitespace, point only at the found token.
653
+ err. span_label ( self . span , label_exp) ;
654
+ }
655
+ _ => {
656
+ err. span_label ( sp, label_exp) ;
657
+ err. span_label ( self . span , "unexpected token" ) ;
658
+ }
659
+ }
641
660
Err ( err)
642
661
}
643
662
} else {
You can’t perform that action at this time.
0 commit comments