@@ -16,7 +16,7 @@ use rustc::infer::region_constraints::{RegionConstraintData, Constraint};
1616use rustc:: middle:: resolve_lifetime as rl;
1717use rustc:: middle:: lang_items;
1818use rustc:: middle:: stability;
19- use rustc:: mir:: interpret:: GlobalId ;
19+ use rustc:: mir:: interpret:: { GlobalId , ConstValue , Scalar , sign_extend } ;
2020use rustc:: hir;
2121use rustc:: hir:: def:: { CtorKind , DefKind , Res } ;
2222use rustc:: hir:: def_id:: { CrateNum , DefId , CRATE_DEF_INDEX , LOCAL_CRATE } ;
@@ -1300,6 +1300,7 @@ impl Clean<Constant> for hir::ConstArg {
13001300 Constant {
13011301 type_ : cx. tcx . type_of ( cx. tcx . hir ( ) . body_owner_def_id ( self . value . body ) ) . clean ( cx) ,
13021302 expr : print_const_expr ( cx, self . value . body ) ,
1303+ value : None ,
13031304 }
13041305 }
13051306}
@@ -3274,6 +3275,7 @@ impl<'tcx> Clean<Constant> for ty::Const<'tcx> {
32743275 Constant {
32753276 type_ : self . ty . clean ( cx) ,
32763277 expr : format ! ( "{}" , self ) ,
3278+ value : None ,
32773279 }
32783280 }
32793281}
@@ -3831,21 +3833,24 @@ impl Clean<Item> for doctree::Static<'_> {
38313833pub struct Constant {
38323834 pub type_ : Type ,
38333835 pub expr : String ,
3836+ pub value : Option < String > ,
38343837}
38353838
38363839impl Clean < Item > for doctree:: Constant < ' _ > {
38373840 fn clean ( & self , cx : & DocContext < ' _ > ) -> Item {
3841+ let def_id = cx. tcx . hir ( ) . local_def_id ( self . id ) ;
38383842 Item {
38393843 name : Some ( self . name . clean ( cx) ) ,
38403844 attrs : self . attrs . clean ( cx) ,
38413845 source : self . whence . clean ( cx) ,
3842- def_id : cx . tcx . hir ( ) . local_def_id ( self . id ) ,
3846+ def_id : def_id ,
38433847 visibility : self . vis . clean ( cx) ,
38443848 stability : cx. stability ( self . id ) . clean ( cx) ,
38453849 deprecation : cx. deprecation ( self . id ) . clean ( cx) ,
38463850 inner : ConstantItem ( Constant {
38473851 type_ : self . type_ . clean ( cx) ,
38483852 expr : print_const_expr ( cx, self . expr ) ,
3853+ value : print_evaluated_const ( cx, def_id) ,
38493854 } ) ,
38503855 }
38513856 }
@@ -4232,6 +4237,50 @@ fn name_from_pat(p: &hir::Pat) -> String {
42324237 }
42334238}
42344239
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+
42354284fn print_const ( cx : & DocContext < ' _ > , n : & ty:: Const < ' _ > ) -> String {
42364285 match n. val {
42374286 ty:: ConstKind :: Unevaluated ( def_id, _) => {
0 commit comments