Skip to content

Commit d03698f

Browse files
committed
wip
1 parent 54cb4a6 commit d03698f

6 files changed

Lines changed: 32 additions & 55 deletions

File tree

src/librustdoc/clean/utils.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -442,19 +442,16 @@ fn print_const_with_custom_print_scalar<'tcx>(
442442
}
443443

444444
pub(crate) fn is_literal_expr(tcx: TyCtxt<'_>, hir_id: hir::HirId) -> bool {
445-
if let hir::Node::Expr(expr) = tcx.hir_node(hir_id) {
446-
if let hir::ExprKind::Lit(_) = &expr.kind {
447-
return true;
448-
}
449-
450-
if let hir::ExprKind::Unary(hir::UnOp::Neg, expr) = &expr.kind
451-
&& let hir::ExprKind::Lit(_) = &expr.kind
452-
{
453-
return true;
454-
}
455-
}
456-
457-
false
445+
use ExprKind::{Lit, Path, Unary};
446+
use hir::{Expr, ExprKind, Node, UnOp};
447+
448+
matches!(
449+
tcx.hir_node(hir_id),
450+
Node::Expr(Expr {
451+
kind: Lit(_) | Path(_) | Unary(UnOp::Neg, Expr { kind: Lit(_) | Path(_), .. }),
452+
..
453+
})
454+
)
458455
}
459456

460457
/// Given a type Path, resolve it to a Type using the TyCtxt

src/librustdoc/html/render/print_item.rs

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1920,9 +1920,19 @@ fn item_constant(
19201920
let tcx = cx.tcx();
19211921
render_attributes_in_code(w, it, "", cx)?;
19221922

1923+
let val = fmt::from_fn(|f| {
1924+
let is_literal = c.is_literal(tcx);
1925+
let expr = c.expr(tcx);
1926+
if is_literal {
1927+
write!(f, " = {expr}", expr = Escape(&expr))
1928+
} else {
1929+
f.write_str(" = _")
1930+
}
1931+
});
1932+
19231933
write!(
19241934
w,
1925-
"{vis}const {name}{generics}: {typ}{where_clause}",
1935+
"{vis}const {name}{generics}: {typ}{val}{where_clause};",
19261936
vis = visibility_print_with_space(it, cx),
19271937
name = it.name.unwrap(),
19281938
generics = print_generics(generics, cx),
@@ -1931,34 +1941,6 @@ fn item_constant(
19311941
print_where_clause(generics, cx, 0, Ending::NoNewline).maybe_display(),
19321942
)?;
19331943

1934-
// FIXME: The code below now prints
1935-
// ` = _; // 100i32`
1936-
// if the expression is
1937-
// `50 + 50`
1938-
// which looks just wrong.
1939-
// Should we print
1940-
// ` = 100i32;`
1941-
// instead?
1942-
1943-
let value = c.value(tcx);
1944-
let is_literal = c.is_literal(tcx);
1945-
let expr = c.expr(tcx);
1946-
if value.is_some() || is_literal {
1947-
write!(w, " = {expr};", expr = Escape(&expr))?;
1948-
} else {
1949-
w.write_str(";")?;
1950-
}
1951-
1952-
if !is_literal && let Some(value) = &value {
1953-
let value_lowercase = value.to_lowercase();
1954-
let expr_lowercase = expr.to_lowercase();
1955-
1956-
if value_lowercase != expr_lowercase
1957-
&& value_lowercase.trim_end_matches("i32") != expr_lowercase
1958-
{
1959-
write!(w, " // {value}", value = Escape(value))?;
1960-
}
1961-
}
19621944
Ok::<(), fmt::Error>(())
19631945
})?;
19641946

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#![crate_name = "foo"]
22

33
//@ has 'foo/constant.HOUR_IN_SECONDS.html'
4-
//@ has - '//*[@class="rust item-decl"]//code' 'pub const HOUR_IN_SECONDS: u64 = _; // 3_600u64'
4+
//@ has - '//*[@class="rust item-decl"]//code' 'pub const HOUR_IN_SECONDS: u64 = _;'
55
pub const HOUR_IN_SECONDS: u64 = 60 * 60;
66

77
//@ has 'foo/constant.NEGATIVE.html'
8-
//@ has - '//*[@class="rust item-decl"]//code' 'pub const NEGATIVE: i64 = _; // -3_600i64'
8+
//@ has - '//*[@class="rust item-decl"]//code' 'pub const NEGATIVE: i64 = _;'
99
pub const NEGATIVE: i64 = -60 * 60;

tests/rustdoc/constant/generic-const-items.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
//@ has 'generic_const_items/constant.K.html'
55
//@ has - '//*[@class="rust item-decl"]//code' \
6-
// "pub const K<'a, T: 'a + Copy, const N: usize>: Option<[T; N]> \
6+
// "pub const K<'a, T: 'a + Copy, const N: usize>: Option<[T; N]> = None \
77
// where \
88
// String: From<T>;"
99
pub const K<'a, T: 'a + Copy, const N: usize>: Option<[T; N]> = None

tests/rustdoc/constant/show-const-contents.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ pub const CONST_NEG_I32: i32 = -42;
2121
//@ !hasraw show_const_contents/constant.CONST_EQ_TO_VALUE_I32.html '// 42i32'
2222
pub const CONST_EQ_TO_VALUE_I32: i32 = 42i32;
2323

24-
//@ hasraw show_const_contents/constant.CONST_CALC_I32.html '= _; // 43i32'
24+
//@ hasraw show_const_contents/constant.CONST_CALC_I32.html '= _;'
2525
pub const CONST_CALC_I32: i32 = 42 + 1;
2626

2727
//@ !hasraw show_const_contents/constant.CONST_REF_I32.html '= &42;'
2828
//@ !hasraw show_const_contents/constant.CONST_REF_I32.html '; //'
2929
pub const CONST_REF_I32: &'static i32 = &42;
3030

31-
//@ hasraw show_const_contents/constant.CONST_I32_MAX.html '= i32::MAX; // 2_147_483_647i32'
31+
//@ hasraw show_const_contents/constant.CONST_I32_MAX.html '= i32::MAX;'
3232
pub const CONST_I32_MAX: i32 = i32::MAX;
3333

3434
//@ !hasraw show_const_contents/constant.UNIT.html '= ();'
@@ -47,21 +47,19 @@ pub struct MyTypeWithStr(&'static str);
4747
//@ !hasraw show_const_contents/constant.MY_TYPE_WITH_STR.html '; //'
4848
pub const MY_TYPE_WITH_STR: MyTypeWithStr = MyTypeWithStr("show this");
4949

50-
//@ hasraw show_const_contents/constant.PI.html '= 3.14159265358979323846264338327950288_f32;'
51-
//@ hasraw show_const_contents/constant.PI.html '; // 3.14159274f32'
50+
//@ hasraw show_const_contents/constant.PI.html '= _;'
5251
pub use std::f32::consts::PI;
53-
54-
//@ hasraw show_const_contents/constant.MAX.html '= i32::MAX; // 2_147_483_647i32'
52+
//@ hasraw show_const_contents/constant.MAX.html '= i32::MAX;'
5553
#[allow(deprecated, deprecated_in_future)]
5654
pub use std::i32::MAX;
5755

5856
macro_rules! int_module {
59-
($T:ident) => (
57+
($T:ident) => {
6058
pub const MIN: $T = $T::MIN;
61-
)
59+
};
6260
}
6361

64-
//@ hasraw show_const_contents/constant.MIN.html '= i16::MIN; // -32_768i16'
62+
//@ hasraw show_const_contents/constant.MIN.html '= i16::MIN;'
6563
int_module!(i16);
6664

6765
//@ has show_const_contents/constant.ESCAPE.html //pre '= r#"<script>alert("ESCAPE");</script>"#;'

tests/rustdoc/inline_cross/generic-const-items.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
//@ has 'user/constant.K.html'
77
//@ has - '//*[@class="rust item-decl"]//code' \
8-
// "pub const K<'a, T: 'a + Copy, const N: usize>: Option<[T; N]> \
8+
// "pub const K<'a, T: 'a + Copy, const N: usize>: Option<[T; N]> = _ \
99
// where \
1010
// String: From<T>;"
1111
pub use generic_const_items::K;

0 commit comments

Comments
 (0)