@@ -17,6 +17,7 @@ use rustc_ast::token::{self, Delimiter, Token, TokenKind};
17
17
use rustc_ast:: tokenstream:: Spacing ;
18
18
use rustc_ast:: util:: case:: Case ;
19
19
use rustc_ast:: util:: classify;
20
+ use rustc_ast:: util:: literal:: LitError ;
20
21
use rustc_ast:: util:: parser:: { prec_let_scrutinee_needs_par, AssocOp , Fixity } ;
21
22
use rustc_ast:: visit:: Visitor ;
22
23
use rustc_ast:: { self as ast, AttrStyle , AttrVec , CaptureBy , ExprField , UnOp , DUMMY_NODE_ID } ;
@@ -30,9 +31,10 @@ use rustc_errors::{
30
31
PResult , StashKey ,
31
32
} ;
32
33
use rustc_macros:: Subdiagnostic ;
33
- use rustc_session:: errors:: { report_lit_error , ExprParenthesesNeeded } ;
34
+ use rustc_session:: errors:: ExprParenthesesNeeded ;
34
35
use rustc_session:: lint:: builtin:: BREAK_WITH_LABEL_AND_LOOP ;
35
36
use rustc_session:: lint:: BuiltinLintDiagnostics ;
37
+ use rustc_session:: parse:: ParseSess ;
36
38
use rustc_span:: source_map:: { self , Spanned } ;
37
39
use rustc_span:: symbol:: kw:: PathRoot ;
38
40
use rustc_span:: symbol:: { kw, sym, Ident , Symbol } ;
@@ -3665,6 +3667,90 @@ impl<'a> Parser<'a> {
3665
3667
}
3666
3668
}
3667
3669
3670
+ pub fn report_lit_error ( sess : & ParseSess , err : LitError , lit : token:: Lit , span : Span ) {
3671
+ // Checks if `s` looks like i32 or u1234 etc.
3672
+ fn looks_like_width_suffix ( first_chars : & [ char ] , s : & str ) -> bool {
3673
+ s. len ( ) > 1 && s. starts_with ( first_chars) && s[ 1 ..] . chars ( ) . all ( |c| c. is_ascii_digit ( ) )
3674
+ }
3675
+
3676
+ // Try to lowercase the prefix if the prefix and suffix are valid.
3677
+ fn fix_base_capitalisation ( prefix : & str , suffix : & str ) -> Option < String > {
3678
+ let mut chars = suffix. chars ( ) ;
3679
+
3680
+ let base_char = chars. next ( ) . unwrap ( ) ;
3681
+ let base = match base_char {
3682
+ 'B' => 2 ,
3683
+ 'O' => 8 ,
3684
+ 'X' => 16 ,
3685
+ _ => return None ,
3686
+ } ;
3687
+
3688
+ // check that the suffix contains only base-appropriate characters
3689
+ let valid = prefix == "0"
3690
+ && chars
3691
+ . filter ( |c| * c != '_' )
3692
+ . take_while ( |c| * c != 'i' && * c != 'u' )
3693
+ . all ( |c| c. to_digit ( base) . is_some ( ) ) ;
3694
+
3695
+ valid. then ( || format ! ( "0{}{}" , base_char. to_ascii_lowercase( ) , & suffix[ 1 ..] ) )
3696
+ }
3697
+
3698
+ let token:: Lit { kind, symbol, suffix, .. } = lit;
3699
+ match err {
3700
+ // `LexerError` is an error, but it was already reported
3701
+ // by lexer, so here we don't report it the second time.
3702
+ LitError :: LexerError => { }
3703
+ LitError :: InvalidSuffix => {
3704
+ if let Some ( suffix) = suffix {
3705
+ sess. emit_err ( errors:: InvalidLiteralSuffix { span, kind : kind. descr ( ) , suffix } ) ;
3706
+ }
3707
+ }
3708
+ LitError :: InvalidIntSuffix => {
3709
+ let suf = suffix. expect ( "suffix error with no suffix" ) ;
3710
+ let suf = suf. as_str ( ) ;
3711
+ if looks_like_width_suffix ( & [ 'i' , 'u' ] , suf) {
3712
+ // If it looks like a width, try to be helpful.
3713
+ sess. emit_err ( errors:: InvalidIntLiteralWidth { span, width : suf[ 1 ..] . into ( ) } ) ;
3714
+ } else if let Some ( fixed) = fix_base_capitalisation ( symbol. as_str ( ) , suf) {
3715
+ sess. emit_err ( errors:: InvalidNumLiteralBasePrefix { span, fixed } ) ;
3716
+ } else {
3717
+ sess. emit_err ( errors:: InvalidNumLiteralSuffix { span, suffix : suf. to_string ( ) } ) ;
3718
+ }
3719
+ }
3720
+ LitError :: InvalidFloatSuffix => {
3721
+ let suf = suffix. expect ( "suffix error with no suffix" ) ;
3722
+ let suf = suf. as_str ( ) ;
3723
+ if looks_like_width_suffix ( & [ 'f' ] , suf) {
3724
+ // If it looks like a width, try to be helpful.
3725
+ sess. emit_err ( errors:: InvalidFloatLiteralWidth {
3726
+ span,
3727
+ width : suf[ 1 ..] . to_string ( ) ,
3728
+ } ) ;
3729
+ } else {
3730
+ sess. emit_err ( errors:: InvalidFloatLiteralSuffix { span, suffix : suf. to_string ( ) } ) ;
3731
+ }
3732
+ }
3733
+ LitError :: NonDecimalFloat ( base) => {
3734
+ match base {
3735
+ 16 => sess. emit_err ( errors:: HexadecimalFloatLiteralNotSupported { span } ) ,
3736
+ 8 => sess. emit_err ( errors:: OctalFloatLiteralNotSupported { span } ) ,
3737
+ 2 => sess. emit_err ( errors:: BinaryFloatLiteralNotSupported { span } ) ,
3738
+ _ => unreachable ! ( ) ,
3739
+ } ;
3740
+ }
3741
+ LitError :: IntTooLarge ( base) => {
3742
+ let max = u128:: MAX ;
3743
+ let limit = match base {
3744
+ 2 => format ! ( "{max:#b}" ) ,
3745
+ 8 => format ! ( "{max:#o}" ) ,
3746
+ 16 => format ! ( "{max:#x}" ) ,
3747
+ _ => format ! ( "{max}" ) ,
3748
+ } ;
3749
+ sess. emit_err ( errors:: IntLiteralTooLarge { span, limit } ) ;
3750
+ }
3751
+ }
3752
+ }
3753
+
3668
3754
/// Used to forbid `let` expressions in certain syntactic locations.
3669
3755
#[ derive( Clone , Copy , Subdiagnostic ) ]
3670
3756
pub ( crate ) enum ForbiddenLetReason {
0 commit comments