Skip to content

Commit 6bdfd12

Browse files
committed
Suppress = _ on associated constants in impls
1 parent da89d10 commit 6bdfd12

File tree

3 files changed

+47
-14
lines changed

3 files changed

+47
-14
lines changed

src/librustdoc/html/render/mod.rs

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -835,12 +835,23 @@ fn assoc_href_attr(it: &clean::Item, link: AssocItemLink<'_>, cx: &Context<'_>)
835835
href.map(|href| format!(" href=\"{href}\"")).unwrap_or_default()
836836
}
837837

838+
#[derive(Debug)]
839+
enum AssocConstValue<'a> {
840+
// In trait definitions, it is relevant for the public API whether an
841+
// associated constant comes with a default value, so even if we cannot
842+
// render its value, the presence of a value must be shown using `= _`.
843+
TraitDefault(&'a clean::ConstantKind),
844+
// In impls, there is no need to show `= _`.
845+
Impl(&'a clean::ConstantKind),
846+
None,
847+
}
848+
838849
fn assoc_const(
839850
w: &mut Buffer,
840851
it: &clean::Item,
841852
generics: &clean::Generics,
842853
ty: &clean::Type,
843-
default: Option<&clean::ConstantKind>,
854+
value: AssocConstValue<'_>,
844855
link: AssocItemLink<'_>,
845856
indent: usize,
846857
cx: &Context<'_>,
@@ -856,15 +867,20 @@ fn assoc_const(
856867
generics = generics.print(cx),
857868
ty = ty.print(cx),
858869
);
859-
if let Some(default) = default {
860-
w.write_str(" = ");
861-
870+
if let AssocConstValue::TraitDefault(konst) | AssocConstValue::Impl(konst) = value {
862871
// FIXME: `.value()` uses `clean::utils::format_integer_with_underscore_sep` under the
863872
// hood which adds noisy underscores and a type suffix to number literals.
864873
// This hurts readability in this context especially when more complex expressions
865874
// are involved and it doesn't add much of value.
866875
// Find a way to print constants here without all that jazz.
867-
write!(w, "{}", Escape(&default.value(tcx).unwrap_or_else(|| default.expr(tcx))));
876+
let repr = konst.value(tcx).unwrap_or_else(|| konst.expr(tcx));
877+
if match value {
878+
AssocConstValue::TraitDefault(_) => true, // always show
879+
AssocConstValue::Impl(_) => repr != "_", // show if there is a meaningful value to show
880+
AssocConstValue::None => unreachable!(),
881+
} {
882+
write!(w, " = {}", Escape(&repr));
883+
}
868884
}
869885
write!(w, "{}", print_where_clause(generics, cx, indent, Ending::NoNewline));
870886
}
@@ -1086,17 +1102,27 @@ fn render_assoc_item(
10861102
item,
10871103
generics,
10881104
ty,
1089-
None,
1105+
AssocConstValue::None,
1106+
link,
1107+
if parent == ItemType::Trait { 4 } else { 0 },
1108+
cx,
1109+
),
1110+
clean::ProvidedAssocConstItem(ci) => assoc_const(
1111+
w,
1112+
item,
1113+
&ci.generics,
1114+
&ci.type_,
1115+
AssocConstValue::TraitDefault(&ci.kind),
10901116
link,
10911117
if parent == ItemType::Trait { 4 } else { 0 },
10921118
cx,
10931119
),
1094-
clean::ProvidedAssocConstItem(ci) | clean::ImplAssocConstItem(ci) => assoc_const(
1120+
clean::ImplAssocConstItem(ci) => assoc_const(
10951121
w,
10961122
item,
10971123
&ci.generics,
10981124
&ci.type_,
1099-
Some(&ci.kind),
1125+
AssocConstValue::Impl(&ci.kind),
11001126
link,
11011127
if parent == ItemType::Trait { 4 } else { 0 },
11021128
cx,
@@ -1704,7 +1730,7 @@ fn render_impl(
17041730
item,
17051731
generics,
17061732
ty,
1707-
None,
1733+
AssocConstValue::None,
17081734
link.anchor(if trait_.is_some() { &source_id } else { &id }),
17091735
0,
17101736
cx,
@@ -1726,7 +1752,11 @@ fn render_impl(
17261752
item,
17271753
&ci.generics,
17281754
&ci.type_,
1729-
Some(&ci.kind),
1755+
match item.kind {
1756+
clean::ProvidedAssocConstItem(_) => AssocConstValue::TraitDefault(&ci.kind),
1757+
clean::ImplAssocConstItem(_) => AssocConstValue::Impl(&ci.kind),
1758+
_ => unreachable!(),
1759+
},
17301760
link.anchor(if trait_.is_some() { &source_id } else { &id }),
17311761
0,
17321762
cx,

tests/rustdoc/assoc-consts-underscore.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,17 @@ pub trait Trait {
1414
}
1515

1616
impl Trait for Struct {
17-
//@ has assoc_consts_underscore/struct.Struct.html '//*[@id="associatedconstant.REQUIRED"]' \
17+
//@ !has assoc_consts_underscore/struct.Struct.html '//*[@id="associatedconstant.REQUIRED"]' \
1818
// 'const REQUIRED: Struct = _'
19+
//@ has - '//*[@id="associatedconstant.REQUIRED"]' 'const REQUIRED: Struct'
1920
const REQUIRED: Struct = Struct { _private: () };
20-
//@ has - '//*[@id="associatedconstant.OPTIONAL"]' 'const OPTIONAL: Struct = _'
21+
//@ !has - '//*[@id="associatedconstant.OPTIONAL"]' 'const OPTIONAL: Struct = _'
22+
//@ has - '//*[@id="associatedconstant.OPTIONAL"]' 'const OPTIONAL: Struct'
2123
const OPTIONAL: Struct = Struct { _private: () };
2224
}
2325

2426
impl Struct {
25-
//@ has - '//*[@id="associatedconstant.INHERENT"]' 'const INHERENT: Struct = _'
27+
//@ !has - '//*[@id="associatedconstant.INHERENT"]' 'const INHERENT: Struct = _'
28+
//@ has - '//*[@id="associatedconstant.INHERENT"]' 'const INHERENT: Struct'
2629
pub const INHERENT: Struct = Struct { _private: () };
2730
}

tests/rustdoc/bold-tag-101743.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ impl<const B: Word> Repr<B> {
1414
// If we change back to rendering the value of consts, check this doesn't add
1515
// a <b> tag, but escapes correctly
1616

17-
//@ has foo/struct.Repr.html '//section[@id="associatedconstant.BASE"]/h4' '= _'
17+
//@ !has foo/struct.Repr.html '//section[@id="associatedconstant.BASE"]/h4' '='
1818
pub const BASE: IBig = base_as_ibig::<B>();
1919
}

0 commit comments

Comments
 (0)