@@ -854,14 +854,22 @@ fn assoc_const(
854
854
ty : & clean:: Type ,
855
855
value : AssocConstValue < ' _ > ,
856
856
link : AssocItemLink < ' _ > ,
857
- indent : usize ,
857
+ parent : Option < ItemType > ,
858
858
cx : & Context < ' _ > ,
859
859
) {
860
860
let tcx = cx. tcx ( ) ;
861
+ let ( indent, indent_str, end_newline) = if parent == Some ( ItemType :: Trait ) {
862
+ let indent_str = " " ;
863
+ write ! ( w, "{}" , render_attributes_in_pre( it, indent_str, cx) ) ;
864
+ ( 4 , indent_str, Ending :: NoNewline )
865
+ } else {
866
+ render_attributes_in_code ( w, it, cx) ;
867
+ ( 0 , "" , Ending :: Newline )
868
+ } ;
861
869
write ! (
862
870
w,
863
871
"{indent}{vis}const <a{href} class=\" constant\" >{name}</a>{generics}: {ty}" ,
864
- indent = " " . repeat ( indent ) ,
872
+ indent = indent_str ,
865
873
vis = visibility_print_with_space( it, cx) ,
866
874
href = assoc_href_attr( it, link, cx) ,
867
875
name = it. name. as_ref( ) . unwrap( ) ,
@@ -883,7 +891,7 @@ fn assoc_const(
883
891
write ! ( w, " = {}" , Escape ( & repr) ) ;
884
892
}
885
893
}
886
- write ! ( w, "{}" , print_where_clause( generics, cx, indent, Ending :: NoNewline ) ) ;
894
+ write ! ( w, "{}" , print_where_clause( generics, cx, indent, end_newline ) ) ;
887
895
}
888
896
889
897
fn assoc_type (
@@ -893,13 +901,21 @@ fn assoc_type(
893
901
bounds : & [ clean:: GenericBound ] ,
894
902
default : Option < & clean:: Type > ,
895
903
link : AssocItemLink < ' _ > ,
896
- indent : usize ,
904
+ parent : Option < ItemType > ,
897
905
cx : & Context < ' _ > ,
898
906
) {
907
+ let ( indent, indent_str, end_newline) = if parent == Some ( ItemType :: Trait ) {
908
+ let indent_str = " " ;
909
+ write ! ( w, "{}" , render_attributes_in_pre( it, indent_str, cx) ) ;
910
+ ( 4 , indent_str, Ending :: NoNewline )
911
+ } else {
912
+ render_attributes_in_code ( w, it, cx) ;
913
+ ( 0 , "" , Ending :: Newline )
914
+ } ;
899
915
write ! (
900
916
w,
901
917
"{indent}{vis}type <a{href} class=\" associatedtype\" >{name}</a>{generics}" ,
902
- indent = " " . repeat ( indent ) ,
918
+ indent = indent_str ,
903
919
vis = visibility_print_with_space( it, cx) ,
904
920
href = assoc_href_attr( it, link, cx) ,
905
921
name = it. name. as_ref( ) . unwrap( ) ,
@@ -912,7 +928,7 @@ fn assoc_type(
912
928
if let Some ( default) = default {
913
929
write ! ( w, " = {}" , default . print( cx) )
914
930
}
915
- write ! ( w, "{}" , print_where_clause( generics, cx, indent, Ending :: NoNewline ) ) ;
931
+ write ! ( w, "{}" , print_where_clause( generics, cx, indent, end_newline ) ) ;
916
932
}
917
933
918
934
fn assoc_method (
@@ -1098,24 +1114,17 @@ fn render_assoc_item(
1098
1114
clean:: MethodItem ( m, _) => {
1099
1115
assoc_method ( w, item, & m. generics , & m. decl , link, parent, cx, render_mode)
1100
1116
}
1101
- clean:: RequiredAssocConstItem ( generics, ty) => assoc_const (
1102
- w,
1103
- item,
1104
- generics,
1105
- ty,
1106
- AssocConstValue :: None ,
1107
- link,
1108
- if parent == ItemType :: Trait { 4 } else { 0 } ,
1109
- cx,
1110
- ) ,
1117
+ clean:: RequiredAssocConstItem ( generics, ty) => {
1118
+ assoc_const ( w, item, generics, ty, AssocConstValue :: None , link, Some ( parent) , cx)
1119
+ }
1111
1120
clean:: ProvidedAssocConstItem ( ci) => assoc_const (
1112
1121
w,
1113
1122
item,
1114
1123
& ci. generics ,
1115
1124
& ci. type_ ,
1116
1125
AssocConstValue :: TraitDefault ( & ci. kind ) ,
1117
1126
link,
1118
- if parent == ItemType :: Trait { 4 } else { 0 } ,
1127
+ Some ( parent) ,
1119
1128
cx,
1120
1129
) ,
1121
1130
clean:: ImplAssocConstItem ( ci) => assoc_const (
@@ -1125,27 +1134,20 @@ fn render_assoc_item(
1125
1134
& ci. type_ ,
1126
1135
AssocConstValue :: Impl ( & ci. kind ) ,
1127
1136
link,
1128
- if parent == ItemType :: Trait { 4 } else { 0 } ,
1129
- cx,
1130
- ) ,
1131
- clean:: RequiredAssocTypeItem ( ref generics, ref bounds) => assoc_type (
1132
- w,
1133
- item,
1134
- generics,
1135
- bounds,
1136
- None ,
1137
- link,
1138
- if parent == ItemType :: Trait { 4 } else { 0 } ,
1137
+ Some ( parent) ,
1139
1138
cx,
1140
1139
) ,
1140
+ clean:: RequiredAssocTypeItem ( ref generics, ref bounds) => {
1141
+ assoc_type ( w, item, generics, bounds, None , link, Some ( parent) , cx)
1142
+ }
1141
1143
clean:: AssocTypeItem ( ref ty, ref bounds) => assoc_type (
1142
1144
w,
1143
1145
item,
1144
1146
& ty. generics ,
1145
1147
bounds,
1146
1148
Some ( ty. item_type . as_ref ( ) . unwrap_or ( & ty. type_ ) ) ,
1147
1149
link,
1148
- if parent == ItemType :: Trait { 4 } else { 0 } ,
1150
+ Some ( parent) ,
1149
1151
cx,
1150
1152
) ,
1151
1153
_ => panic ! ( "render_assoc_item called on non-associated-item" ) ,
@@ -1167,6 +1169,20 @@ fn render_attributes_in_pre<'a, 'tcx: 'a>(
1167
1169
} )
1168
1170
}
1169
1171
1172
+ // When an attribute is rendered inside a `<pre>` tag, it is formatted using
1173
+ // a whitespace suffix.
1174
+ fn render_attributes_in_pre_same_line < ' a , ' tcx : ' a > (
1175
+ it : & ' a clean:: Item ,
1176
+ cx : & ' a Context < ' tcx > ,
1177
+ ) -> impl fmt:: Display + Captures < ' a > + Captures < ' tcx > {
1178
+ crate :: html:: format:: display_fn ( move |f| {
1179
+ for a in it. attributes ( cx. tcx ( ) , cx. cache ( ) , false ) {
1180
+ write ! ( f, "{a} " ) ?;
1181
+ }
1182
+ Ok ( ( ) )
1183
+ } )
1184
+ }
1185
+
1170
1186
// When an attribute is rendered inside a <code> tag, it is formatted using
1171
1187
// a div to produce a newline after it.
1172
1188
fn render_attributes_in_code ( w : & mut impl fmt:: Write , it : & clean:: Item , cx : & Context < ' _ > ) {
@@ -1175,6 +1191,17 @@ fn render_attributes_in_code(w: &mut impl fmt::Write, it: &clean::Item, cx: &Con
1175
1191
}
1176
1192
}
1177
1193
1194
+ // Same as `render_attributes_in_code()`, but on the same line.
1195
+ fn render_attributes_in_code_same_line (
1196
+ w : & mut impl fmt:: Write ,
1197
+ it : & clean:: Item ,
1198
+ cx : & Context < ' _ > ,
1199
+ ) {
1200
+ for attr in it. attributes ( cx. tcx ( ) , cx. cache ( ) , false ) {
1201
+ write ! ( w, "<span class=\" code-attribute\" >{attr} </span>" ) . unwrap ( ) ;
1202
+ }
1203
+ }
1204
+
1178
1205
#[ derive( Copy , Clone ) ]
1179
1206
enum AssocItemLink < ' a > {
1180
1207
Anchor ( Option < & ' a str > ) ,
@@ -1529,7 +1556,7 @@ fn notable_traits_decl(ty: &clean::Type, cx: &Context<'_>) -> (String, String) {
1529
1556
& [ ] , // intentionally leaving out bounds
1530
1557
Some ( & tydef. type_ ) ,
1531
1558
src_link,
1532
- 0 ,
1559
+ None ,
1533
1560
cx,
1534
1561
) ;
1535
1562
out. push_str ( ";</div>" ) ;
@@ -1733,7 +1760,7 @@ fn render_impl(
1733
1760
ty,
1734
1761
AssocConstValue :: None ,
1735
1762
link. anchor ( if trait_. is_some ( ) { & source_id } else { & id } ) ,
1736
- 0 ,
1763
+ None ,
1737
1764
cx,
1738
1765
) ;
1739
1766
w. write_str ( "</h4></section>" ) ;
@@ -1759,7 +1786,7 @@ fn render_impl(
1759
1786
_ => unreachable ! ( ) ,
1760
1787
} ,
1761
1788
link. anchor ( if trait_. is_some ( ) { & source_id } else { & id } ) ,
1762
- 0 ,
1789
+ None ,
1763
1790
cx,
1764
1791
) ;
1765
1792
w. write_str ( "</h4></section>" ) ;
@@ -1781,7 +1808,7 @@ fn render_impl(
1781
1808
bounds,
1782
1809
None ,
1783
1810
link. anchor ( if trait_. is_some ( ) { & source_id } else { & id } ) ,
1784
- 0 ,
1811
+ None ,
1785
1812
cx,
1786
1813
) ;
1787
1814
w. write_str ( "</h4></section>" ) ;
@@ -1803,7 +1830,7 @@ fn render_impl(
1803
1830
& [ ] , // intentionally leaving out bounds
1804
1831
Some ( tydef. item_type . as_ref ( ) . unwrap_or ( & tydef. type_ ) ) ,
1805
1832
link. anchor ( if trait_. is_some ( ) { & source_id } else { & id } ) ,
1806
- 0 ,
1833
+ None ,
1807
1834
cx,
1808
1835
) ;
1809
1836
w. write_str ( "</h4></section>" ) ;
@@ -2100,7 +2127,7 @@ pub(crate) fn render_impl_summary(
2100
2127
& [ ] , // intentionally leaving out bounds
2101
2128
Some ( & tydef. type_ ) ,
2102
2129
AssocItemLink :: Anchor ( None ) ,
2103
- 0 ,
2130
+ None ,
2104
2131
cx,
2105
2132
) ;
2106
2133
w. write_str ( ";</div>" ) ;
0 commit comments