@@ -736,15 +736,48 @@ impl<'a> Parser<'a> {
736
736
// Parsing e.g. `X..`.
737
737
if let RangeEnd :: Included ( _) = re. node {
738
738
// FIXME(Centril): Consider semantic errors instead in `ast_validation`.
739
- // Possibly also do this for `X..=` in *expression* contexts.
740
- self . error_inclusive_range_with_no_end ( re. span ) ;
739
+ self . inclusive_range_with_incorrect_end ( re. span ) ;
741
740
}
742
741
None
743
742
} ;
744
743
Ok ( PatKind :: Range ( Some ( begin) , end, re) )
745
744
}
746
745
747
- pub ( super ) fn error_inclusive_range_with_no_end ( & self , span : Span ) {
746
+ pub ( super ) fn inclusive_range_with_incorrect_end ( & mut self , span : Span ) {
747
+ let tok = & self . token ;
748
+
749
+ // If the user typed "..==" instead of "..=", we want to give them
750
+ // a specific error message telling them to use "..=".
751
+ // Otherwise, we assume that they meant to type a half open exclusive
752
+ // range and give them an error telling them to do that instead.
753
+ if matches ! ( tok. kind, token:: Eq ) && tok. span . lo ( ) == span. hi ( ) {
754
+ let span_with_eq = span. to ( tok. span ) ;
755
+
756
+ // Ensure the user doesn't receive unhelpful unexpected token errors
757
+ self . bump ( ) ;
758
+ if self . is_pat_range_end_start ( 0 ) {
759
+ let _ = self . parse_pat_range_end ( ) ;
760
+ }
761
+
762
+ self . error_inclusive_range_with_extra_equals ( span_with_eq) ;
763
+ } else {
764
+ self . error_inclusive_range_with_no_end ( span) ;
765
+ }
766
+ }
767
+
768
+ fn error_inclusive_range_with_extra_equals ( & self , span : Span ) {
769
+ self . struct_span_err ( span, "unexpected `=` after inclusive range" )
770
+ . span_suggestion_short (
771
+ span,
772
+ "use `..=` instead" ,
773
+ "..=" . to_string ( ) ,
774
+ Applicability :: MaybeIncorrect ,
775
+ )
776
+ . note ( "inclusive ranges end with a single equals sign (`..=`)" )
777
+ . emit ( ) ;
778
+ }
779
+
780
+ fn error_inclusive_range_with_no_end ( & self , span : Span ) {
748
781
struct_span_err ! ( self . sess. span_diagnostic, span, E0586 , "inclusive range with no end" )
749
782
. span_suggestion_short (
750
783
span,
0 commit comments