@@ -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+
838849fn 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,
0 commit comments