@@ -16,7 +16,7 @@ use rustc::infer::region_constraints::{RegionConstraintData, Constraint};
16
16
use rustc:: middle:: resolve_lifetime as rl;
17
17
use rustc:: middle:: lang_items;
18
18
use rustc:: middle:: stability;
19
- use rustc:: mir:: interpret:: GlobalId ;
19
+ use rustc:: mir:: interpret:: { GlobalId , ConstValue , Scalar , sign_extend } ;
20
20
use rustc:: hir;
21
21
use rustc:: hir:: def:: { CtorKind , DefKind , Res } ;
22
22
use rustc:: hir:: def_id:: { CrateNum , DefId , CRATE_DEF_INDEX , LOCAL_CRATE } ;
@@ -1300,6 +1300,7 @@ impl Clean<Constant> for hir::ConstArg {
1300
1300
Constant {
1301
1301
type_ : cx. tcx . type_of ( cx. tcx . hir ( ) . body_owner_def_id ( self . value . body ) ) . clean ( cx) ,
1302
1302
expr : print_const_expr ( cx, self . value . body ) ,
1303
+ value : None ,
1303
1304
}
1304
1305
}
1305
1306
}
@@ -3274,6 +3275,7 @@ impl<'tcx> Clean<Constant> for ty::Const<'tcx> {
3274
3275
Constant {
3275
3276
type_ : self . ty . clean ( cx) ,
3276
3277
expr : format ! ( "{}" , self ) ,
3278
+ value : None ,
3277
3279
}
3278
3280
}
3279
3281
}
@@ -3831,21 +3833,24 @@ impl Clean<Item> for doctree::Static<'_> {
3831
3833
pub struct Constant {
3832
3834
pub type_ : Type ,
3833
3835
pub expr : String ,
3836
+ pub value : Option < String > ,
3834
3837
}
3835
3838
3836
3839
impl Clean < Item > for doctree:: Constant < ' _ > {
3837
3840
fn clean ( & self , cx : & DocContext < ' _ > ) -> Item {
3841
+ let def_id = cx. tcx . hir ( ) . local_def_id ( self . id ) ;
3838
3842
Item {
3839
3843
name : Some ( self . name . clean ( cx) ) ,
3840
3844
attrs : self . attrs . clean ( cx) ,
3841
3845
source : self . whence . clean ( cx) ,
3842
- def_id : cx . tcx . hir ( ) . local_def_id ( self . id ) ,
3846
+ def_id : def_id ,
3843
3847
visibility : self . vis . clean ( cx) ,
3844
3848
stability : cx. stability ( self . id ) . clean ( cx) ,
3845
3849
deprecation : cx. deprecation ( self . id ) . clean ( cx) ,
3846
3850
inner : ConstantItem ( Constant {
3847
3851
type_ : self . type_ . clean ( cx) ,
3848
3852
expr : print_const_expr ( cx, self . expr ) ,
3853
+ value : print_evaluated_const ( cx, def_id) ,
3849
3854
} ) ,
3850
3855
}
3851
3856
}
@@ -4232,6 +4237,50 @@ fn name_from_pat(p: &hir::Pat) -> String {
4232
4237
}
4233
4238
}
4234
4239
4240
+ pub fn print_evaluated_const ( cx : & DocContext < ' _ > , def_id : DefId ) -> Option < String > {
4241
+ let param_env = cx. tcx . param_env ( def_id) ;
4242
+ let substs = InternalSubsts :: identity_for_item ( cx. tcx , def_id) ;
4243
+ let cid = GlobalId {
4244
+ instance : ty:: Instance :: new ( def_id, substs) ,
4245
+ promoted : None
4246
+ } ;
4247
+
4248
+ let value = cx. tcx . const_eval ( param_env. and ( cid) ) . ok ( ) . and_then ( |value| {
4249
+ match ( value. val , & value. ty . kind ) {
4250
+ ( _, ty:: Ref ( ..) ) => None ,
4251
+ ( ty:: ConstKind :: Value ( ConstValue :: Scalar ( _) ) , _) =>
4252
+ Some ( print_const_with_custom_print_scalar ( cx, value) ) ,
4253
+ _ => None ,
4254
+ }
4255
+ } ) ;
4256
+
4257
+ value
4258
+ }
4259
+
4260
+ fn print_const_with_custom_print_scalar ( cx : & DocContext < ' _ > , ct : & ' tcx ty:: Const < ' tcx > ) -> String {
4261
+ // Use a slightly different format for integer types which always shows the actual value.
4262
+ // For all other types, fallback to the original `pretty_print_const`.
4263
+ match ( ct. val , & ct. ty . kind ) {
4264
+ ( ty:: ConstKind :: Value ( ConstValue :: Scalar ( Scalar :: Raw { data, .. } ) ) , ty:: Uint ( ui) ) => {
4265
+ let ui_str = ui. name_str ( ) ;
4266
+ format ! ( "{}{}" , data, ui_str)
4267
+ } ,
4268
+ ( ty:: ConstKind :: Value ( ConstValue :: Scalar ( Scalar :: Raw { data, .. } ) ) , ty:: Int ( i) ) => {
4269
+ let ty = cx. tcx . lift ( & ct. ty ) . unwrap ( ) ;
4270
+ let size = cx. tcx . layout_of ( ty:: ParamEnv :: empty ( ) . and ( ty) )
4271
+ . unwrap ( )
4272
+ . size ;
4273
+ let i_str = i. name_str ( ) ;
4274
+ let sign_extended_data = sign_extend ( data, size) as i128 ;
4275
+
4276
+ format ! ( "{}{}" , sign_extended_data, i_str)
4277
+ } ,
4278
+ _ => {
4279
+ ct. to_string ( )
4280
+ }
4281
+ }
4282
+ }
4283
+
4235
4284
fn print_const ( cx : & DocContext < ' _ > , n : & ty:: Const < ' _ > ) -> String {
4236
4285
match n. val {
4237
4286
ty:: ConstKind :: Unevaluated ( def_id, _) => {
0 commit comments