@@ -835,12 +835,23 @@ fn assoc_href_attr(it: &clean::Item, link: AssocItemLink<'_>, cx: &Context<'_>)
835
835
href. map ( |href| format ! ( " href=\" {href}\" " ) ) . unwrap_or_default ( )
836
836
}
837
837
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
+
838
849
fn assoc_const (
839
850
w : & mut Buffer ,
840
851
it : & clean:: Item ,
841
852
generics : & clean:: Generics ,
842
853
ty : & clean:: Type ,
843
- default : Option < & clean :: ConstantKind > ,
854
+ value : AssocConstValue < ' _ > ,
844
855
link : AssocItemLink < ' _ > ,
845
856
indent : usize ,
846
857
cx : & Context < ' _ > ,
@@ -856,15 +867,20 @@ fn assoc_const(
856
867
generics = generics. print( cx) ,
857
868
ty = ty. print( cx) ,
858
869
) ;
859
- if let Some ( default) = default {
860
- w. write_str ( " = " ) ;
861
-
870
+ if let AssocConstValue :: TraitDefault ( konst) | AssocConstValue :: Impl ( konst) = value {
862
871
// FIXME: `.value()` uses `clean::utils::format_integer_with_underscore_sep` under the
863
872
// hood which adds noisy underscores and a type suffix to number literals.
864
873
// This hurts readability in this context especially when more complex expressions
865
874
// are involved and it doesn't add much of value.
866
875
// 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
+ }
868
884
}
869
885
write ! ( w, "{}" , print_where_clause( generics, cx, indent, Ending :: NoNewline ) ) ;
870
886
}
@@ -1086,17 +1102,27 @@ fn render_assoc_item(
1086
1102
item,
1087
1103
generics,
1088
1104
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 ) ,
1090
1116
link,
1091
1117
if parent == ItemType :: Trait { 4 } else { 0 } ,
1092
1118
cx,
1093
1119
) ,
1094
- clean:: ProvidedAssocConstItem ( ci ) | clean :: ImplAssocConstItem ( ci) => assoc_const (
1120
+ clean:: ImplAssocConstItem ( ci) => assoc_const (
1095
1121
w,
1096
1122
item,
1097
1123
& ci. generics ,
1098
1124
& ci. type_ ,
1099
- Some ( & ci. kind ) ,
1125
+ AssocConstValue :: Impl ( & ci. kind ) ,
1100
1126
link,
1101
1127
if parent == ItemType :: Trait { 4 } else { 0 } ,
1102
1128
cx,
@@ -1704,7 +1730,7 @@ fn render_impl(
1704
1730
item,
1705
1731
generics,
1706
1732
ty,
1707
- None ,
1733
+ AssocConstValue :: None ,
1708
1734
link. anchor ( if trait_. is_some ( ) { & source_id } else { & id } ) ,
1709
1735
0 ,
1710
1736
cx,
@@ -1726,7 +1752,11 @@ fn render_impl(
1726
1752
item,
1727
1753
& ci. generics ,
1728
1754
& 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
+ } ,
1730
1760
link. anchor ( if trait_. is_some ( ) { & source_id } else { & id } ) ,
1731
1761
0 ,
1732
1762
cx,
0 commit comments