11use std:: assert_matches:: debug_assert_matches;
22use std:: fmt:: { self , Display , Write as _} ;
3- use std:: mem;
43use std:: sync:: LazyLock as Lazy ;
4+ use std:: { ascii, mem} ;
55
66use rustc_ast:: tokenstream:: TokenTree ;
77use rustc_hir:: def:: { DefKind , Res } ;
@@ -24,7 +24,7 @@ use crate::clean::{
2424 clean_middle_ty, inline,
2525} ;
2626use crate :: core:: DocContext ;
27- use crate :: display:: Joined as _;
27+ use crate :: display:: { Joined as _, MaybeDisplay as _ } ;
2828
2929#[ cfg( test) ]
3030mod tests;
@@ -254,14 +254,7 @@ pub(crate) fn qpath_to_string(p: &hir::QPath<'_>) -> String {
254254 fmt:: from_fn ( |f| {
255255 segments
256256 . iter ( )
257- . map ( |seg| {
258- fmt:: from_fn ( |f| {
259- if seg. ident . name != kw:: PathRoot {
260- write ! ( f, "{}" , seg. ident) ?;
261- }
262- Ok ( ( ) )
263- } )
264- } )
257+ . map ( |seg| ( seg. ident . name != kw:: PathRoot ) . then_some ( seg. ident ) . maybe_display ( ) )
265258 . joined ( "::" , f)
266259 } )
267260 . to_string ( )
@@ -391,30 +384,12 @@ pub(crate) fn print_evaluated_const(
391384 } )
392385}
393386
394- fn format_integer_with_underscore_sep ( num : & str ) -> String {
395- let num_chars: Vec < _ > = num. chars ( ) . collect ( ) ;
396- let mut num_start_index = if num_chars. first ( ) == Some ( & '-' ) { 1 } else { 0 } ;
397- let chunk_size = match & num. as_bytes ( ) [ num_start_index..] {
398- [ b'0' , b'b' | b'x' , ..] => {
399- num_start_index += 2 ;
400- 4
401- }
402- [ b'0' , b'o' , ..] => {
403- num_start_index += 2 ;
404- let remaining_chars = num_chars. len ( ) - num_start_index;
405- if remaining_chars <= 6 {
406- // don't add underscores to Unix permissions like 0755 or 100755
407- return num. to_string ( ) ;
408- }
409- 3
410- }
411- _ => 3 ,
412- } ;
413-
414- num_chars[ ..num_start_index]
415- . iter ( )
416- . chain ( num_chars[ num_start_index..] . rchunks ( chunk_size) . rev ( ) . intersperse ( & [ '_' ] ) . flatten ( ) )
417- . collect ( )
387+ fn format_integer_with_underscore_sep ( num : u128 , is_negative : bool ) -> String {
388+ let num = num. to_string ( ) ;
389+ let chars = num. as_ascii ( ) . unwrap ( ) ;
390+ let mut result = if is_negative { "-" . to_string ( ) } else { String :: new ( ) } ;
391+ result. extend ( chars. rchunks ( 3 ) . rev ( ) . intersperse ( & [ ascii:: Char :: LowLine ] ) . flatten ( ) ) ;
392+ result
418393}
419394
420395fn print_const_with_custom_print_scalar < ' tcx > (
@@ -428,7 +403,10 @@ fn print_const_with_custom_print_scalar<'tcx>(
428403 match ( ct, ct. ty ( ) . kind ( ) ) {
429404 ( mir:: Const :: Val ( mir:: ConstValue :: Scalar ( int) , _) , ty:: Uint ( ui) ) => {
430405 let mut output = if with_underscores {
431- format_integer_with_underscore_sep ( & int. to_string ( ) )
406+ format_integer_with_underscore_sep (
407+ int. assert_scalar_int ( ) . to_bits_unchecked ( ) ,
408+ false ,
409+ )
432410 } else {
433411 int. to_string ( )
434412 } ;
@@ -445,7 +423,10 @@ fn print_const_with_custom_print_scalar<'tcx>(
445423 . size ;
446424 let sign_extended_data = int. assert_scalar_int ( ) . to_int ( size) ;
447425 let mut output = if with_underscores {
448- format_integer_with_underscore_sep ( & sign_extended_data. to_string ( ) )
426+ format_integer_with_underscore_sep (
427+ sign_extended_data. unsigned_abs ( ) ,
428+ sign_extended_data. is_negative ( ) ,
429+ )
449430 } else {
450431 sign_extended_data. to_string ( )
451432 } ;
0 commit comments