@@ -17,6 +17,7 @@ use rustc_ast::token::{self, Delimiter, Token, TokenKind};
1717use rustc_ast:: tokenstream:: Spacing ;
1818use rustc_ast:: util:: case:: Case ;
1919use rustc_ast:: util:: classify;
20+ use rustc_ast:: util:: literal:: LitError ;
2021use rustc_ast:: util:: parser:: { prec_let_scrutinee_needs_par, AssocOp , Fixity } ;
2122use rustc_ast:: visit:: Visitor ;
2223use rustc_ast:: { self as ast, AttrStyle , AttrVec , CaptureBy , ExprField , UnOp , DUMMY_NODE_ID } ;
@@ -30,9 +31,10 @@ use rustc_errors::{
3031 PResult , StashKey ,
3132} ;
3233use rustc_macros:: Subdiagnostic ;
33- use rustc_session:: errors:: { report_lit_error , ExprParenthesesNeeded } ;
34+ use rustc_session:: errors:: ExprParenthesesNeeded ;
3435use rustc_session:: lint:: builtin:: BREAK_WITH_LABEL_AND_LOOP ;
3536use rustc_session:: lint:: BuiltinLintDiagnostics ;
37+ use rustc_session:: parse:: ParseSess ;
3638use rustc_span:: source_map:: { self , Spanned } ;
3739use rustc_span:: symbol:: kw:: PathRoot ;
3840use rustc_span:: symbol:: { kw, sym, Ident , Symbol } ;
@@ -3616,6 +3618,90 @@ impl<'a> Parser<'a> {
36163618 }
36173619}
36183620
3621+ pub fn report_lit_error ( sess : & ParseSess , err : LitError , lit : token:: Lit , span : Span ) {
3622+ // Checks if `s` looks like i32 or u1234 etc.
3623+ fn looks_like_width_suffix ( first_chars : & [ char ] , s : & str ) -> bool {
3624+ s. len ( ) > 1 && s. starts_with ( first_chars) && s[ 1 ..] . chars ( ) . all ( |c| c. is_ascii_digit ( ) )
3625+ }
3626+
3627+ // Try to lowercase the prefix if the prefix and suffix are valid.
3628+ fn fix_base_capitalisation ( prefix : & str , suffix : & str ) -> Option < String > {
3629+ let mut chars = suffix. chars ( ) ;
3630+
3631+ let base_char = chars. next ( ) . unwrap ( ) ;
3632+ let base = match base_char {
3633+ 'B' => 2 ,
3634+ 'O' => 8 ,
3635+ 'X' => 16 ,
3636+ _ => return None ,
3637+ } ;
3638+
3639+ // check that the suffix contains only base-appropriate characters
3640+ let valid = prefix == "0"
3641+ && chars
3642+ . filter ( |c| * c != '_' )
3643+ . take_while ( |c| * c != 'i' && * c != 'u' )
3644+ . all ( |c| c. to_digit ( base) . is_some ( ) ) ;
3645+
3646+ valid. then ( || format ! ( "0{}{}" , base_char. to_ascii_lowercase( ) , & suffix[ 1 ..] ) )
3647+ }
3648+
3649+ let token:: Lit { kind, symbol, suffix, .. } = lit;
3650+ match err {
3651+ // `LexerError` is an error, but it was already reported
3652+ // by lexer, so here we don't report it the second time.
3653+ LitError :: LexerError => { }
3654+ LitError :: InvalidSuffix => {
3655+ if let Some ( suffix) = suffix {
3656+ sess. emit_err ( errors:: InvalidLiteralSuffix { span, kind : kind. descr ( ) , suffix } ) ;
3657+ }
3658+ }
3659+ LitError :: InvalidIntSuffix => {
3660+ let suf = suffix. expect ( "suffix error with no suffix" ) ;
3661+ let suf = suf. as_str ( ) ;
3662+ if looks_like_width_suffix ( & [ 'i' , 'u' ] , suf) {
3663+ // If it looks like a width, try to be helpful.
3664+ sess. emit_err ( errors:: InvalidIntLiteralWidth { span, width : suf[ 1 ..] . into ( ) } ) ;
3665+ } else if let Some ( fixed) = fix_base_capitalisation ( symbol. as_str ( ) , suf) {
3666+ sess. emit_err ( errors:: InvalidNumLiteralBasePrefix { span, fixed } ) ;
3667+ } else {
3668+ sess. emit_err ( errors:: InvalidNumLiteralSuffix { span, suffix : suf. to_string ( ) } ) ;
3669+ }
3670+ }
3671+ LitError :: InvalidFloatSuffix => {
3672+ let suf = suffix. expect ( "suffix error with no suffix" ) ;
3673+ let suf = suf. as_str ( ) ;
3674+ if looks_like_width_suffix ( & [ 'f' ] , suf) {
3675+ // If it looks like a width, try to be helpful.
3676+ sess. emit_err ( errors:: InvalidFloatLiteralWidth {
3677+ span,
3678+ width : suf[ 1 ..] . to_string ( ) ,
3679+ } ) ;
3680+ } else {
3681+ sess. emit_err ( errors:: InvalidFloatLiteralSuffix { span, suffix : suf. to_string ( ) } ) ;
3682+ }
3683+ }
3684+ LitError :: NonDecimalFloat ( base) => {
3685+ match base {
3686+ 16 => sess. emit_err ( errors:: HexadecimalFloatLiteralNotSupported { span } ) ,
3687+ 8 => sess. emit_err ( errors:: OctalFloatLiteralNotSupported { span } ) ,
3688+ 2 => sess. emit_err ( errors:: BinaryFloatLiteralNotSupported { span } ) ,
3689+ _ => unreachable ! ( ) ,
3690+ } ;
3691+ }
3692+ LitError :: IntTooLarge ( base) => {
3693+ let max = u128:: MAX ;
3694+ let limit = match base {
3695+ 2 => format ! ( "{max:#b}" ) ,
3696+ 8 => format ! ( "{max:#o}" ) ,
3697+ 16 => format ! ( "{max:#x}" ) ,
3698+ _ => format ! ( "{max}" ) ,
3699+ } ;
3700+ sess. emit_err ( errors:: IntLiteralTooLarge { span, limit } ) ;
3701+ }
3702+ }
3703+ }
3704+
36193705/// Used to forbid `let` expressions in certain syntactic locations.
36203706#[ derive( Clone , Copy , Subdiagnostic ) ]
36213707pub ( crate ) enum ForbiddenLetReason {
0 commit comments