Skip to content

Commit 5062d47

Browse files
committed
Don't show a computed value in the docs of a literal const
1 parent a8714e6 commit 5062d47

File tree

5 files changed

+40
-3
lines changed

5 files changed

+40
-3
lines changed

src/libcore/num/f32.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub const DIGITS: u32 = 6;
3030
///
3131
/// [Machine epsilon]: https://en.wikipedia.org/wiki/Machine_epsilon
3232
#[stable(feature = "rust1", since = "1.0.0")]
33-
pub const EPSILON: f32 = 1.19209290e-07_f32;
33+
pub const EPSILON: f32 = 1.1920929e-07_f32;
3434

3535
/// Smallest finite `f32` value.
3636
#[stable(feature = "rust1", since = "1.0.0")]

src/librustdoc/clean/inline.rs

+3
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,9 @@ fn build_const(cx: &DocContext<'_>, did: DefId) -> clean::Constant {
467467
type_: cx.tcx.type_of(did).clean(cx),
468468
expr: print_inlined_const(cx, did),
469469
value: clean::print_evaluated_const(cx, did),
470+
is_literal: cx.tcx.hir().as_local_hir_id(did).map(
471+
|hir_id| clean::is_literal_expr(cx, hir_id)
472+
).unwrap_or(false),
470473
}
471474
}
472475

src/librustdoc/clean/mod.rs

+22
Original file line numberDiff line numberDiff line change
@@ -1301,6 +1301,7 @@ impl Clean<Constant> for hir::ConstArg {
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),
13031303
value: None,
1304+
is_literal: is_literal_expr(cx, self.value.body.hir_id),
13041305
}
13051306
}
13061307
}
@@ -3276,6 +3277,7 @@ impl<'tcx> Clean<Constant> for ty::Const<'tcx> {
32763277
type_: self.ty.clean(cx),
32773278
expr: format!("{}", self),
32783279
value: None,
3280+
is_literal: false,
32793281
}
32803282
}
32813283
}
@@ -3834,11 +3836,13 @@ pub struct Constant {
38343836
pub type_: Type,
38353837
pub expr: String,
38363838
pub value: Option<String>,
3839+
pub is_literal: bool,
38373840
}
38383841

38393842
impl Clean<Item> for doctree::Constant<'_> {
38403843
fn clean(&self, cx: &DocContext<'_>) -> Item {
38413844
let def_id = cx.tcx.hir().local_def_id(self.id);
3845+
38423846
Item {
38433847
name: Some(self.name.clean(cx)),
38443848
attrs: self.attrs.clean(cx),
@@ -3851,6 +3855,7 @@ impl Clean<Item> for doctree::Constant<'_> {
38513855
type_: self.type_.clean(cx),
38523856
expr: print_const_expr(cx, self.expr),
38533857
value: print_evaluated_const(cx, def_id),
3858+
is_literal: is_literal_expr( cx, self.expr.hir_id),
38543859
}),
38553860
}
38563861
}
@@ -4248,6 +4253,7 @@ pub fn print_evaluated_const(cx: &DocContext<'_>, def_id: DefId) -> Option<Strin
42484253
let value = cx.tcx.const_eval(param_env.and(cid)).ok().and_then(|value| {
42494254
match (value.val, &value.ty.kind) {
42504255
(_, ty::Ref(..)) => None,
4256+
(ty::ConstKind::Value(ConstValue::Scalar(_)), ty::Adt(_, _)) => None,
42514257
(ty::ConstKind::Value(ConstValue::Scalar(_)), _) =>
42524258
Some(print_const_with_custom_print_scalar(cx, value)),
42534259
_ => None,
@@ -4310,6 +4316,22 @@ fn print_const_expr(cx: &DocContext<'_>, body: hir::BodyId) -> String {
43104316
cx.tcx.hir().hir_to_pretty_string(body.hir_id)
43114317
}
43124318

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+
43134335
/// Given a type Path, resolve it to a Type using the TyCtxt
43144336
fn resolve_type(cx: &DocContext<'_>,
43154337
path: Path,

src/librustdoc/html/render.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2297,7 +2297,7 @@ fn item_constant(w: &mut Buffer, cx: &Context, it: &clean::Item, c: &clean::Cons
22972297
);
22982298

22992299
if let Some(value) = &c.value {
2300-
if value != &c.expr {
2300+
if value.to_lowercase() != c.expr.to_lowercase() && !c.is_literal {
23012301
write!(w, " /* {value} */", value = value);
23022302
}
23032303
}

src/test/rustdoc/show-const-contents.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@
44
// @has show_const_contents/constant.CONST_S.html 'show this'
55
pub const CONST_S: &'static str = "show this";
66

7-
// @has show_const_contents/constant.CONST_I32.html '= 42; /* 42i32 */'
7+
// @has show_const_contents/constant.CONST_I32.html '= 42;'
8+
// @!has show_const_contents/constant.CONST_I32.html '; /*'
89
pub const CONST_I32: i32 = 42;
910

11+
// @has show_const_contents/constant.CONST_NEG_I32.html '= -42;'
12+
// @!has show_const_contents/constant.CONST_NEG_I32.html '; /*'
13+
pub const CONST_NEG_I32: i32 = -42;
14+
1015
// @has show_const_contents/constant.CONST_EQ_TO_VALUE_I32.html '= 42i32;'
1116
// @!has show_const_contents/constant.CONST_EQ_TO_VALUE_I32.html '/* 42i32 */'
1217
pub const CONST_EQ_TO_VALUE_I32: i32 = 42i32;
@@ -26,4 +31,11 @@ pub const UNIT: () = ();
2631
pub struct MyType(i32);
2732

2833
// @has show_const_contents/constant.MY_TYPE.html '= MyType(42);'
34+
// @!has show_const_contents/constant.MY_TYPE.html '; /*'
2935
pub const MY_TYPE: MyType = MyType(42);
36+
37+
pub struct MyTypeWithStr(&'static str);
38+
39+
// @has show_const_contents/constant.MY_TYPE_WITH_STR.html '= MyTypeWithStr("show this");'
40+
// @!has show_const_contents/constant.MY_TYPE_WITH_STR.html '; /*'
41+
pub const MY_TYPE_WITH_STR: MyTypeWithStr = MyTypeWithStr("show this");

0 commit comments

Comments
 (0)