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