@@ -261,15 +261,19 @@ pub(crate) fn print_const(cx: &DocContext<'_>, n: ty::Const<'_>) -> String {
261261 }
262262}
263263
264- pub ( crate ) fn print_evaluated_const ( tcx : TyCtxt < ' _ > , def_id : DefId ) -> Option < String > {
264+ pub ( crate ) fn print_evaluated_const (
265+ tcx : TyCtxt < ' _ > ,
266+ def_id : DefId ,
267+ underscores_and_type : bool ,
268+ ) -> Option < String > {
265269 tcx. const_eval_poly ( def_id) . ok ( ) . and_then ( |val| {
266270 let ty = tcx. type_of ( def_id) ;
267271 match ( val, ty. kind ( ) ) {
268272 ( _, & ty:: Ref ( ..) ) => None ,
269273 ( ConstValue :: Scalar ( _) , & ty:: Adt ( _, _) ) => None ,
270274 ( ConstValue :: Scalar ( _) , _) => {
271275 let const_ = mir:: ConstantKind :: from_value ( val, ty) ;
272- Some ( print_const_with_custom_print_scalar ( tcx, const_) )
276+ Some ( print_const_with_custom_print_scalar ( tcx, const_, underscores_and_type ) )
273277 }
274278 _ => None ,
275279 }
@@ -302,23 +306,35 @@ fn format_integer_with_underscore_sep(num: &str) -> String {
302306 . collect ( )
303307}
304308
305- fn print_const_with_custom_print_scalar ( tcx : TyCtxt < ' _ > , ct : mir:: ConstantKind < ' _ > ) -> String {
309+ fn print_const_with_custom_print_scalar (
310+ tcx : TyCtxt < ' _ > ,
311+ ct : mir:: ConstantKind < ' _ > ,
312+ underscores_and_type : bool ,
313+ ) -> String {
306314 // Use a slightly different format for integer types which always shows the actual value.
307315 // For all other types, fallback to the original `pretty_print_const`.
308316 match ( ct, ct. ty ( ) . kind ( ) ) {
309317 ( mir:: ConstantKind :: Val ( ConstValue :: Scalar ( int) , _) , ty:: Uint ( ui) ) => {
310- format ! ( "{}{}" , format_integer_with_underscore_sep( & int. to_string( ) ) , ui. name_str( ) )
318+ if underscores_and_type {
319+ format ! ( "{}{}" , format_integer_with_underscore_sep( & int. to_string( ) ) , ui. name_str( ) )
320+ } else {
321+ int. to_string ( )
322+ }
311323 }
312324 ( mir:: ConstantKind :: Val ( ConstValue :: Scalar ( int) , _) , ty:: Int ( i) ) => {
313325 let ty = tcx. lift ( ct. ty ( ) ) . unwrap ( ) ;
314326 let size = tcx. layout_of ( ty:: ParamEnv :: empty ( ) . and ( ty) ) . unwrap ( ) . size ;
315327 let data = int. assert_bits ( size) ;
316328 let sign_extended_data = size. sign_extend ( data) as i128 ;
317- format ! (
318- "{}{}" ,
319- format_integer_with_underscore_sep( & sign_extended_data. to_string( ) ) ,
320- i. name_str( )
321- )
329+ if underscores_and_type {
330+ format ! (
331+ "{}{}" ,
332+ format_integer_with_underscore_sep( & sign_extended_data. to_string( ) ) ,
333+ i. name_str( )
334+ )
335+ } else {
336+ sign_extended_data. to_string ( )
337+ }
322338 }
323339 _ => ct. to_string ( ) ,
324340 }
0 commit comments