@@ -914,7 +914,6 @@ crate struct DocFragment {
914914 crate parent_module : Option < DefId > ,
915915 crate doc : Symbol ,
916916 crate kind : DocFragmentKind ,
917- crate need_backline : bool ,
918917 crate indent : usize ,
919918}
920919
@@ -930,20 +929,16 @@ crate enum DocFragmentKind {
930929 RawDoc ,
931930}
932931
933- // The goal of this function is to apply the `DocFragment` transformations that are required when
934- // transforming into the final markdown. So the transformations in here are:
935- //
936- // * Applying the computed indent to each lines in each doc fragment (a `DocFragment` can contain
937- // multiple lines in case of `#[doc = ""]`).
938- // * Adding backlines between `DocFragment`s and adding an extra one if required (stored in the
939- // `need_backline` field).
932+ /// The goal of this function is to apply the `DocFragment` transformation that is required when
933+ /// transforming into the final Markdown, which is applying the computed indent to each line in
934+ /// each doc fragment (a `DocFragment` can contain multiple lines in case of `#[doc = ""]`).
935+ ///
936+ /// Note: remove the trailing newline where appropriate
940937fn add_doc_fragment ( out : & mut String , frag : & DocFragment ) {
941938 let s = frag. doc . as_str ( ) ;
942939 let mut iter = s. lines ( ) ;
943940 if s == "" {
944- if frag. need_backline {
945- out. push ( '\n' ) ;
946- }
941+ out. push ( '\n' ) ;
947942 return ;
948943 }
949944 while let Some ( line) = iter. next ( ) {
@@ -955,9 +950,6 @@ fn add_doc_fragment(out: &mut String, frag: &DocFragment) {
955950 }
956951 out. push ( '\n' ) ;
957952 }
958- if !frag. need_backline {
959- out. pop ( ) ;
960- }
961953}
962954
963955/// Collapse a collection of [`DocFragment`]s into one string,
@@ -967,6 +959,7 @@ crate fn collapse_doc_fragments(doc_strings: &[DocFragment]) -> String {
967959 for frag in doc_strings {
968960 add_doc_fragment ( & mut acc, frag) ;
969961 }
962+ acc. pop ( ) ;
970963 acc
971964}
972965
@@ -1032,7 +1025,6 @@ impl Attributes {
10321025 additional_attrs : Option < ( & [ ast:: Attribute ] , DefId ) > ,
10331026 ) -> Attributes {
10341027 let mut doc_strings: Vec < DocFragment > = vec ! [ ] ;
1035-
10361028 let clean_attr = |( attr, parent_module) : ( & ast:: Attribute , Option < DefId > ) | {
10371029 if let Some ( value) = attr. doc_str ( ) {
10381030 trace ! ( "got doc_str={:?}" , value) ;
@@ -1043,18 +1035,8 @@ impl Attributes {
10431035 DocFragmentKind :: RawDoc
10441036 } ;
10451037
1046- let frag = DocFragment {
1047- span : attr. span ,
1048- doc : value,
1049- kind,
1050- parent_module,
1051- need_backline : false ,
1052- indent : 0 ,
1053- } ;
1054-
1055- if let Some ( prev) = doc_strings. last_mut ( ) {
1056- prev. need_backline = true ;
1057- }
1038+ let frag =
1039+ DocFragment { span : attr. span , doc : value, kind, parent_module, indent : 0 } ;
10581040
10591041 doc_strings. push ( frag) ;
10601042
@@ -1090,6 +1072,7 @@ impl Attributes {
10901072 }
10911073 add_doc_fragment ( & mut out, new_frag) ;
10921074 }
1075+ out. pop ( ) ;
10931076 if out. is_empty ( ) { None } else { Some ( out) }
10941077 }
10951078
@@ -1098,10 +1081,17 @@ impl Attributes {
10981081 /// The module can be different if this is a re-export with added documentation.
10991082 crate fn collapsed_doc_value_by_module_level ( & self ) -> FxHashMap < Option < DefId > , String > {
11001083 let mut ret = FxHashMap :: default ( ) ;
1084+ if self . doc_strings . len ( ) == 0 {
1085+ return ret;
1086+ }
1087+ let last_index = self . doc_strings . len ( ) - 1 ;
11011088
1102- for new_frag in self . doc_strings . iter ( ) {
1089+ for ( i , new_frag) in self . doc_strings . iter ( ) . enumerate ( ) {
11031090 let out = ret. entry ( new_frag. parent_module ) . or_default ( ) ;
11041091 add_doc_fragment ( out, new_frag) ;
1092+ if i == last_index {
1093+ out. pop ( ) ;
1094+ }
11051095 }
11061096 ret
11071097 }
0 commit comments