1
1
use super :: { ForceCollect , Parser , PathStyle , TrailingToken } ;
2
- use crate :: errors:: RemoveLet ;
2
+ use crate :: errors:: {
3
+ InclusiveRangeExtraEquals , InclusiveRangeMatchArrow , InclusiveRangeNoEnd , RemoveLet ,
4
+ } ;
3
5
use crate :: { maybe_recover_from_interpolated_ty_qpath, maybe_whole} ;
4
6
use rustc_ast:: mut_visit:: { noop_visit_pat, MutVisitor } ;
5
7
use rustc_ast:: ptr:: P ;
@@ -9,7 +11,7 @@ use rustc_ast::{
9
11
PatField , PatKind , Path , QSelf , RangeEnd , RangeSyntax ,
10
12
} ;
11
13
use rustc_ast_pretty:: pprust;
12
- use rustc_errors:: { struct_span_err , Applicability , DiagnosticBuilder , ErrorGuaranteed , PResult } ;
14
+ use rustc_errors:: { Applicability , DiagnosticBuilder , ErrorGuaranteed , PResult } ;
13
15
use rustc_session:: errors:: ExprParenthesesNeeded ;
14
16
use rustc_span:: source_map:: { respan, Span , Spanned } ;
15
17
use rustc_span:: symbol:: { kw, sym, Ident } ;
@@ -746,47 +748,52 @@ impl<'a> Parser<'a> {
746
748
// Parsing e.g. `X..`.
747
749
if let RangeEnd :: Included ( _) = re. node {
748
750
// 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 ( ) ;
750
752
}
751
753
None
752
754
} ;
753
755
Ok ( PatKind :: Range ( Some ( begin) , end, re) )
754
756
}
755
757
756
- pub ( super ) fn inclusive_range_with_incorrect_end ( & mut self , span : Span ) {
758
+ pub ( super ) fn inclusive_range_with_incorrect_end ( & mut self ) {
757
759
let tok = & self . token ;
758
-
760
+ let span = self . prev_token . span ;
759
761
// If the user typed "..==" instead of "..=", we want to give them
760
762
// a specific error message telling them to use "..=".
763
+ // If they typed "..=>", suggest they use ".. =>".
761
764
// Otherwise, we assume that they meant to type a half open exclusive
762
765
// 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 ) ;
765
770
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
+ }
771
776
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) ,
775
783
}
776
784
}
777
785
778
786
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 } ) ;
783
793
}
784
794
785
795
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 } ) ;
790
797
}
791
798
792
799
/// Parse a range-to pattern, `..X` or `..=X` where `X` remains to be parsed.
0 commit comments