@@ -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 } ;
@@ -3657,6 +3659,90 @@ impl<'a> Parser<'a> {
36573659 }
36583660}
36593661
3662+ pub fn report_lit_error ( sess : & ParseSess , err : LitError , lit : token:: Lit , span : Span ) {
3663+ // Checks if `s` looks like i32 or u1234 etc.
3664+ fn looks_like_width_suffix ( first_chars : & [ char ] , s : & str ) -> bool {
3665+ s. len ( ) > 1 && s. starts_with ( first_chars) && s[ 1 ..] . chars ( ) . all ( |c| c. is_ascii_digit ( ) )
3666+ }
3667+
3668+ // Try to lowercase the prefix if the prefix and suffix are valid.
3669+ fn fix_base_capitalisation ( prefix : & str , suffix : & str ) -> Option < String > {
3670+ let mut chars = suffix. chars ( ) ;
3671+
3672+ let base_char = chars. next ( ) . unwrap ( ) ;
3673+ let base = match base_char {
3674+ 'B' => 2 ,
3675+ 'O' => 8 ,
3676+ 'X' => 16 ,
3677+ _ => return None ,
3678+ } ;
3679+
3680+ // check that the suffix contains only base-appropriate characters
3681+ let valid = prefix == "0"
3682+ && chars
3683+ . filter ( |c| * c != '_' )
3684+ . take_while ( |c| * c != 'i' && * c != 'u' )
3685+ . all ( |c| c. to_digit ( base) . is_some ( ) ) ;
3686+
3687+ valid. then ( || format ! ( "0{}{}" , base_char. to_ascii_lowercase( ) , & suffix[ 1 ..] ) )
3688+ }
3689+
3690+ let token:: Lit { kind, symbol, suffix, .. } = lit;
3691+ match err {
3692+ // `LexerError` is an error, but it was already reported
3693+ // by lexer, so here we don't report it the second time.
3694+ LitError :: LexerError => { }
3695+ LitError :: InvalidSuffix => {
3696+ if let Some ( suffix) = suffix {
3697+ sess. emit_err ( errors:: InvalidLiteralSuffix { span, kind : kind. descr ( ) , suffix } ) ;
3698+ }
3699+ }
3700+ LitError :: InvalidIntSuffix => {
3701+ let suf = suffix. expect ( "suffix error with no suffix" ) ;
3702+ let suf = suf. as_str ( ) ;
3703+ if looks_like_width_suffix ( & [ 'i' , 'u' ] , suf) {
3704+ // If it looks like a width, try to be helpful.
3705+ sess. emit_err ( errors:: InvalidIntLiteralWidth { span, width : suf[ 1 ..] . into ( ) } ) ;
3706+ } else if let Some ( fixed) = fix_base_capitalisation ( symbol. as_str ( ) , suf) {
3707+ sess. emit_err ( errors:: InvalidNumLiteralBasePrefix { span, fixed } ) ;
3708+ } else {
3709+ sess. emit_err ( errors:: InvalidNumLiteralSuffix { span, suffix : suf. to_string ( ) } ) ;
3710+ }
3711+ }
3712+ LitError :: InvalidFloatSuffix => {
3713+ let suf = suffix. expect ( "suffix error with no suffix" ) ;
3714+ let suf = suf. as_str ( ) ;
3715+ if looks_like_width_suffix ( & [ 'f' ] , suf) {
3716+ // If it looks like a width, try to be helpful.
3717+ sess. emit_err ( errors:: InvalidFloatLiteralWidth {
3718+ span,
3719+ width : suf[ 1 ..] . to_string ( ) ,
3720+ } ) ;
3721+ } else {
3722+ sess. emit_err ( errors:: InvalidFloatLiteralSuffix { span, suffix : suf. to_string ( ) } ) ;
3723+ }
3724+ }
3725+ LitError :: NonDecimalFloat ( base) => {
3726+ match base {
3727+ 16 => sess. emit_err ( errors:: HexadecimalFloatLiteralNotSupported { span } ) ,
3728+ 8 => sess. emit_err ( errors:: OctalFloatLiteralNotSupported { span } ) ,
3729+ 2 => sess. emit_err ( errors:: BinaryFloatLiteralNotSupported { span } ) ,
3730+ _ => unreachable ! ( ) ,
3731+ } ;
3732+ }
3733+ LitError :: IntTooLarge ( base) => {
3734+ let max = u128:: MAX ;
3735+ let limit = match base {
3736+ 2 => format ! ( "{max:#b}" ) ,
3737+ 8 => format ! ( "{max:#o}" ) ,
3738+ 16 => format ! ( "{max:#x}" ) ,
3739+ _ => format ! ( "{max}" ) ,
3740+ } ;
3741+ sess. emit_err ( errors:: IntLiteralTooLarge { span, limit } ) ;
3742+ }
3743+ }
3744+ }
3745+
36603746/// Used to forbid `let` expressions in certain syntactic locations.
36613747#[ derive( Clone , Copy , Subdiagnostic ) ]
36623748pub ( crate ) enum ForbiddenLetReason {
0 commit comments