about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFrank King <frankking1729@gmail.com>2022-02-07 10:57:39 +0800
committerGitHub <noreply@github.com>2022-02-06 20:57:39 -0600
commit5df8c8f7e554e036fcb14a2d93d145c92d56bc2e (patch)
treedf42d71743e48fd9778d6c8d80d7dd8e153288b6
parentfd6e11cc57da5728a9a52c0a515fd80e811a43d5 (diff)
downloadrust-5df8c8f7e554e036fcb14a2d93d145c92d56bc2e.tar.gz
rust-5df8c8f7e554e036fcb14a2d93d145c92d56bc2e.zip
Fix doc of generic items formmating error (#5124)
* Fix doc of generic items formmating error

* Remove tracked `attrs_end_with_doc_comment` flag in `RewriteContext`

* Fix duplicated doc comments of const generic params

* Fix `<ast::GenericParam as Spanned>::span()`

* Remove duplicated source file of `doc-of-generic-item.rs`
-rw-r--r--src/spanned.rs15
-rw-r--r--src/types.rs11
-rw-r--r--tests/target/doc-of-generic-item.rs14
3 files changed, 28 insertions, 12 deletions
diff --git a/src/spanned.rs b/src/spanned.rs
index 8e6c75a3744..2136cfeae1a 100644
--- a/src/spanned.rs
+++ b/src/spanned.rs
@@ -113,17 +113,10 @@ impl Spanned for ast::Param {
 
 impl Spanned for ast::GenericParam {
     fn span(&self) -> Span {
-        let lo = if let ast::GenericParamKind::Const {
-            ty: _,
-            kw_span,
-            default: _,
-        } = self.kind
-        {
-            kw_span.lo()
-        } else if self.attrs.is_empty() {
-            self.ident.span.lo()
-        } else {
-            self.attrs[0].span.lo()
+        let lo = match self.kind {
+            _ if !self.attrs.is_empty() => self.attrs[0].span.lo(),
+            ast::GenericParamKind::Const { kw_span, .. } => kw_span.lo(),
+            _ => self.ident.span.lo(),
         };
         let hi = if self.bounds.is_empty() {
             self.ident.span.hi()
diff --git a/src/types.rs b/src/types.rs
index 5de30129266..b13e75a93b3 100644
--- a/src/types.rs
+++ b/src/types.rs
@@ -575,7 +575,16 @@ impl Rewrite for ast::GenericParam {
         let mut result = String::with_capacity(128);
         // FIXME: If there are more than one attributes, this will force multiline.
         match self.attrs.rewrite(context, shape) {
-            Some(ref rw) if !rw.is_empty() => result.push_str(&format!("{} ", rw)),
+            Some(ref rw) if !rw.is_empty() => {
+                result.push_str(rw);
+                // When rewriting generic params, an extra newline should be put
+                // if the attributes end with a doc comment
+                if let Some(true) = self.attrs.last().map(|a| a.is_doc_comment()) {
+                    result.push_str(&shape.indent.to_string_with_newline(context.config));
+                } else {
+                    result.push(' ');
+                }
+            }
             _ => (),
         }
 
diff --git a/tests/target/doc-of-generic-item.rs b/tests/target/doc-of-generic-item.rs
new file mode 100644
index 00000000000..2efc5e09a3d
--- /dev/null
+++ b/tests/target/doc-of-generic-item.rs
@@ -0,0 +1,14 @@
+// Non-doc pre-comment of Foo
+/// doc of Foo
+// Non-doc post-comment of Foo
+struct Foo<
+    // Non-doc pre-comment of 'a
+    /// doc of 'a
+    'a,
+    // Non-doc pre-comment of T
+    /// doc of T
+    T,
+    // Non-doc pre-comment of N
+    /// doc of N
+    const N: item,
+>;