11use super :: { ForceCollect , Parser , PathStyle , TrailingToken } ;
2- use crate :: errors:: RemoveLet ;
2+ use crate :: errors:: {
3+ InclusiveRangeExtraEquals , InclusiveRangeMatchArrow , InclusiveRangeNoEnd , RemoveLet ,
4+ } ;
35use crate :: { maybe_recover_from_interpolated_ty_qpath, maybe_whole} ;
46use rustc_ast:: mut_visit:: { noop_visit_pat, MutVisitor } ;
57use rustc_ast:: ptr:: P ;
@@ -9,7 +11,7 @@ use rustc_ast::{
911 PatField , PatKind , Path , QSelf , RangeEnd , RangeSyntax ,
1012} ;
1113use rustc_ast_pretty:: pprust;
12- use rustc_errors:: { struct_span_err , Applicability , DiagnosticBuilder , ErrorGuaranteed , PResult } ;
14+ use rustc_errors:: { Applicability , DiagnosticBuilder , ErrorGuaranteed , PResult } ;
1315use rustc_session:: errors:: ExprParenthesesNeeded ;
1416use rustc_span:: source_map:: { respan, Span , Spanned } ;
1517use rustc_span:: symbol:: { kw, sym, Ident } ;
@@ -746,47 +748,52 @@ impl<'a> Parser<'a> {
746748 // Parsing e.g. `X..`.
747749 if let RangeEnd :: Included ( _) = re. node {
748750 // FIXME(Centril): Consider semantic errors instead in `ast_validation`.
749- self . inclusive_range_with_incorrect_end ( re . span ) ;
751+ self . inclusive_range_with_incorrect_end ( ) ;
750752 }
751753 None
752754 } ;
753755 Ok ( PatKind :: Range ( Some ( begin) , end, re) )
754756 }
755757
756- pub ( super ) fn inclusive_range_with_incorrect_end ( & mut self , span : Span ) {
758+ pub ( super ) fn inclusive_range_with_incorrect_end ( & mut self ) {
757759 let tok = & self . token ;
758-
760+ let span = self . prev_token . span ;
759761 // If the user typed "..==" instead of "..=", we want to give them
760762 // a specific error message telling them to use "..=".
763+ // If they typed "..=>", suggest they use ".. =>".
761764 // Otherwise, we assume that they meant to type a half open exclusive
762765 // 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 ) ;
766+ let no_space = tok. span . lo ( ) == span. hi ( ) ;
767+ match tok. kind {
768+ token:: Eq if no_space => {
769+ let span_with_eq = span. to ( tok. span ) ;
765770
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- }
771+ // Ensure the user doesn't receive unhelpful unexpected token errors
772+ self . bump ( ) ;
773+ if self . is_pat_range_end_start ( 0 ) {
774+ let _ = self . parse_pat_range_end ( ) . map_err ( |e| e. cancel ( ) ) ;
775+ }
771776
772- self . error_inclusive_range_with_extra_equals ( span_with_eq) ;
773- } else {
774- self . error_inclusive_range_with_no_end ( span) ;
777+ self . error_inclusive_range_with_extra_equals ( span_with_eq) ;
778+ }
779+ token:: Gt if no_space => {
780+ self . error_inclusive_range_match_arrow ( span) ;
781+ }
782+ _ => self . error_inclusive_range_with_no_end ( span) ,
775783 }
776784 }
777785
778786 fn error_inclusive_range_with_extra_equals ( & self , span : Span ) {
779- self . struct_span_err ( span, "unexpected `=` after inclusive range" )
780- . span_suggestion_short ( span, "use `..=` instead" , "..=" , Applicability :: MaybeIncorrect )
781- . note ( "inclusive ranges end with a single equals sign (`..=`)" )
782- . emit ( ) ;
787+ self . sess . emit_err ( InclusiveRangeExtraEquals { span } ) ;
788+ }
789+
790+ fn error_inclusive_range_match_arrow ( & self , span : Span ) {
791+ let after_pat = span. with_hi ( span. hi ( ) - rustc_span:: BytePos ( 1 ) ) . shrink_to_hi ( ) ;
792+ self . sess . emit_err ( InclusiveRangeMatchArrow { span, after_pat } ) ;
783793 }
784794
785795 fn error_inclusive_range_with_no_end ( & self , span : Span ) {
786- struct_span_err ! ( self . sess. span_diagnostic, span, E0586 , "inclusive range with no end" )
787- . span_suggestion_short ( span, "use `..` instead" , ".." , Applicability :: MachineApplicable )
788- . note ( "inclusive ranges must be bounded at the end (`..=b` or `a..=b`)" )
789- . emit ( ) ;
796+ self . sess . emit_err ( InclusiveRangeNoEnd { span } ) ;
790797 }
791798
792799 /// Parse a range-to pattern, `..X` or `..=X` where `X` remains to be parsed.
0 commit comments