@@ -1301,6 +1301,7 @@ impl Clean<Constant> for hir::ConstArg {
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
1303
value : None ,
1304
+ is_literal : is_literal_expr ( cx, self . value . body . hir_id ) ,
1304
1305
}
1305
1306
}
1306
1307
}
@@ -3276,6 +3277,7 @@ impl<'tcx> Clean<Constant> for ty::Const<'tcx> {
3276
3277
type_ : self . ty . clean ( cx) ,
3277
3278
expr : format ! ( "{}" , self ) ,
3278
3279
value : None ,
3280
+ is_literal : false ,
3279
3281
}
3280
3282
}
3281
3283
}
@@ -3834,11 +3836,13 @@ pub struct Constant {
3834
3836
pub type_ : Type ,
3835
3837
pub expr : String ,
3836
3838
pub value : Option < String > ,
3839
+ pub is_literal : bool ,
3837
3840
}
3838
3841
3839
3842
impl Clean < Item > for doctree:: Constant < ' _ > {
3840
3843
fn clean ( & self , cx : & DocContext < ' _ > ) -> Item {
3841
3844
let def_id = cx. tcx . hir ( ) . local_def_id ( self . id ) ;
3845
+
3842
3846
Item {
3843
3847
name : Some ( self . name . clean ( cx) ) ,
3844
3848
attrs : self . attrs . clean ( cx) ,
@@ -3851,6 +3855,7 @@ impl Clean<Item> for doctree::Constant<'_> {
3851
3855
type_ : self . type_ . clean ( cx) ,
3852
3856
expr : print_const_expr ( cx, self . expr ) ,
3853
3857
value : print_evaluated_const ( cx, def_id) ,
3858
+ is_literal : is_literal_expr ( cx, self . expr . hir_id ) ,
3854
3859
} ) ,
3855
3860
}
3856
3861
}
@@ -4248,6 +4253,7 @@ pub fn print_evaluated_const(cx: &DocContext<'_>, def_id: DefId) -> Option<Strin
4248
4253
let value = cx. tcx . const_eval ( param_env. and ( cid) ) . ok ( ) . and_then ( |value| {
4249
4254
match ( value. val , & value. ty . kind ) {
4250
4255
( _, ty:: Ref ( ..) ) => None ,
4256
+ ( ty:: ConstKind :: Value ( ConstValue :: Scalar ( _) ) , ty:: Adt ( _, _) ) => None ,
4251
4257
( ty:: ConstKind :: Value ( ConstValue :: Scalar ( _) ) , _) =>
4252
4258
Some ( print_const_with_custom_print_scalar ( cx, value) ) ,
4253
4259
_ => None ,
@@ -4310,6 +4316,22 @@ fn print_const_expr(cx: &DocContext<'_>, body: hir::BodyId) -> String {
4310
4316
cx. tcx . hir ( ) . hir_to_pretty_string ( body. hir_id )
4311
4317
}
4312
4318
4319
+ fn is_literal_expr ( cx : & DocContext < ' _ > , hir_id : hir:: HirId ) -> bool {
4320
+ if let hir:: Node :: Expr ( expr) = cx. tcx . hir ( ) . get ( hir_id) {
4321
+ if let hir:: ExprKind :: Lit ( _) = & expr. kind {
4322
+ return true ;
4323
+ }
4324
+
4325
+ if let hir:: ExprKind :: Unary ( hir:: UnOp :: UnNeg , expr) = & expr. kind {
4326
+ if let hir:: ExprKind :: Lit ( _) = & expr. kind {
4327
+ return true ;
4328
+ }
4329
+ }
4330
+ }
4331
+
4332
+ false
4333
+ }
4334
+
4313
4335
/// Given a type Path, resolve it to a Type using the TyCtxt
4314
4336
fn resolve_type ( cx : & DocContext < ' _ > ,
4315
4337
path : Path ,
0 commit comments