@@ -746,32 +746,38 @@ impl<'a> Parser<'a> {
746
746
// Parsing e.g. `X..`.
747
747
if let RangeEnd :: Included ( _) = re. node {
748
748
// FIXME(Centril): Consider semantic errors instead in `ast_validation`.
749
- self . inclusive_range_with_incorrect_end ( re . span ) ;
749
+ self . inclusive_range_with_incorrect_end ( ) ;
750
750
}
751
751
None
752
752
} ;
753
753
Ok ( PatKind :: Range ( Some ( begin) , end, re) )
754
754
}
755
755
756
- pub ( super ) fn inclusive_range_with_incorrect_end ( & mut self , span : Span ) {
756
+ pub ( super ) fn inclusive_range_with_incorrect_end ( & mut self ) {
757
757
let tok = & self . token ;
758
-
758
+ let span = self . prev_token . span ;
759
759
// If the user typed "..==" instead of "..=", we want to give them
760
760
// a specific error message telling them to use "..=".
761
+ // If they typed "..=>", suggest they use ".. =>".
761
762
// Otherwise, we assume that they meant to type a half open exclusive
762
763
// range and give them an error telling them to do that instead.
763
- if matches ! ( tok. kind, token:: Eq ) && tok. span . lo ( ) == span. hi ( ) {
764
- let span_with_eq = span. to ( tok. span ) ;
764
+ let no_space = tok. span . lo ( ) == span. hi ( ) ;
765
+ match tok. kind {
766
+ token:: Eq if no_space => {
767
+ let span_with_eq = span. to ( tok. span ) ;
765
768
766
- // Ensure the user doesn't receive unhelpful unexpected token errors
767
- self . bump ( ) ;
768
- if self . is_pat_range_end_start ( 0 ) {
769
- let _ = self . parse_pat_range_end ( ) . map_err ( |e| e. cancel ( ) ) ;
770
- }
769
+ // Ensure the user doesn't receive unhelpful unexpected token errors
770
+ self . bump ( ) ;
771
+ if self . is_pat_range_end_start ( 0 ) {
772
+ let _ = self . parse_pat_range_end ( ) . map_err ( |e| e. cancel ( ) ) ;
773
+ }
771
774
772
- self . error_inclusive_range_with_extra_equals ( span_with_eq) ;
773
- } else {
774
- self . error_inclusive_range_with_no_end ( span) ;
775
+ self . error_inclusive_range_with_extra_equals ( span_with_eq) ;
776
+ }
777
+ token:: Gt if no_space => {
778
+ self . error_inclusive_range_match_arrow ( span) ;
779
+ }
780
+ _ => self . error_inclusive_range_with_no_end ( span) ,
775
781
}
776
782
}
777
783
@@ -782,6 +788,18 @@ impl<'a> Parser<'a> {
782
788
. emit ( ) ;
783
789
}
784
790
791
+ fn error_inclusive_range_match_arrow ( & self , span : Span ) {
792
+ let without_eq = span. with_hi ( span. hi ( ) - rustc_span:: BytePos ( 1 ) ) ;
793
+ self . struct_span_err ( span, "unexpected `=>` after open range" )
794
+ . span_suggestion_verbose (
795
+ without_eq. shrink_to_hi ( ) ,
796
+ "add a space between the pattern and `=>`" ,
797
+ " " ,
798
+ Applicability :: MachineApplicable ,
799
+ )
800
+ . emit ( ) ;
801
+ }
802
+
785
803
fn error_inclusive_range_with_no_end ( & self , span : Span ) {
786
804
struct_span_err ! ( self . sess. span_diagnostic, span, E0586 , "inclusive range with no end" )
787
805
. span_suggestion_short ( span, "use `..` instead" , ".." , Applicability :: MachineApplicable )
0 commit comments