@@ -261,15 +261,19 @@ pub(crate) fn print_const(cx: &DocContext<'_>, n: ty::Const<'_>) -> String {
261
261
}
262
262
}
263
263
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 > {
265
269
tcx. const_eval_poly ( def_id) . ok ( ) . and_then ( |val| {
266
270
let ty = tcx. type_of ( def_id) ;
267
271
match ( val, ty. kind ( ) ) {
268
272
( _, & ty:: Ref ( ..) ) => None ,
269
273
( ConstValue :: Scalar ( _) , & ty:: Adt ( _, _) ) => None ,
270
274
( ConstValue :: Scalar ( _) , _) => {
271
275
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 ) )
273
277
}
274
278
_ => None ,
275
279
}
@@ -302,23 +306,35 @@ fn format_integer_with_underscore_sep(num: &str) -> String {
302
306
. collect ( )
303
307
}
304
308
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 {
306
314
// Use a slightly different format for integer types which always shows the actual value.
307
315
// For all other types, fallback to the original `pretty_print_const`.
308
316
match ( ct, ct. ty ( ) . kind ( ) ) {
309
317
( 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
+ }
311
323
}
312
324
( mir:: ConstantKind :: Val ( ConstValue :: Scalar ( int) , _) , ty:: Int ( i) ) => {
313
325
let ty = tcx. lift ( ct. ty ( ) ) . unwrap ( ) ;
314
326
let size = tcx. layout_of ( ty:: ParamEnv :: empty ( ) . and ( ty) ) . unwrap ( ) . size ;
315
327
let data = int. assert_bits ( size) ;
316
328
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
+ }
322
338
}
323
339
_ => ct. to_string ( ) ,
324
340
}
0 commit comments