From 55d55f55e642bc8c79b0163b9748637903fe3be4 Mon Sep 17 00:00:00 2001 From: Frank King Date: Sat, 4 Dec 2021 16:25:43 +0800 Subject: [PATCH 1/5] Fix doc of generic items formmating error --- src/attr.rs | 3 +++ src/rewrite.rs | 3 +++ src/types.rs | 9 ++++++++- src/visitor.rs | 1 + tests/source/doc-of-generic-item.rs | 8 ++++++++ tests/target/doc-of-generic-item.rs | 8 ++++++++ 6 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 tests/source/doc-of-generic-item.rs create mode 100644 tests/target/doc-of-generic-item.rs diff --git a/src/attr.rs b/src/attr.rs index 76b66e9da80..0634ca9d90f 100644 --- a/src/attr.rs +++ b/src/attr.rs @@ -426,6 +426,9 @@ impl Rewrite for [ast::Attribute] { } attrs = &attrs[doc_comment_len..]; + if attrs.is_empty() { + context.attrs_end_with_doc_comment.set(true); + } continue; } diff --git a/src/rewrite.rs b/src/rewrite.rs index c8abe70141b..ad364f0c70a 100644 --- a/src/rewrite.rs +++ b/src/rewrite.rs @@ -31,6 +31,9 @@ pub(crate) struct RewriteContext<'a> { pub(crate) inside_macro: Rc>, // Force block indent style even if we are using visual indent style. pub(crate) use_block: Cell, + // When rewriting generic params, an extra newline should be put + // if the attributes end with a doc comment + pub(crate) attrs_end_with_doc_comment: Cell, // When `is_if_else_block` is true, unindent the comment on top // of the `else` or `else if`. pub(crate) is_if_else_block: Cell, diff --git a/src/types.rs b/src/types.rs index 88f5dc43245..a293b4856c1 100644 --- a/src/types.rs +++ b/src/types.rs @@ -572,7 +572,14 @@ 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); + if context.attrs_end_with_doc_comment.get() { + result.push_str(&shape.indent.to_string_with_newline(context.config)); + } else { + result.push(' '); + } + } _ => (), } diff --git a/src/visitor.rs b/src/visitor.rs index e4a7be742ab..f9ce6eb8b76 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -988,6 +988,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { config: self.config, inside_macro: Rc::new(Cell::new(false)), use_block: Cell::new(false), + attrs_end_with_doc_comment: Cell::new(false), is_if_else_block: Cell::new(false), force_one_line_chain: Cell::new(false), snippet_provider: self.snippet_provider, diff --git a/tests/source/doc-of-generic-item.rs b/tests/source/doc-of-generic-item.rs new file mode 100644 index 00000000000..6052e95a625 --- /dev/null +++ b/tests/source/doc-of-generic-item.rs @@ -0,0 +1,8 @@ +struct Foo< + /// doc of 'a + 'a, + /// doc of T + T, + /// doc of N + const N: item +>; \ No newline at end of file diff --git a/tests/target/doc-of-generic-item.rs b/tests/target/doc-of-generic-item.rs new file mode 100644 index 00000000000..60de117517d --- /dev/null +++ b/tests/target/doc-of-generic-item.rs @@ -0,0 +1,8 @@ +struct Foo< + /// doc of 'a + 'a, + /// doc of T + T, + /// doc of N + const N: item, +>; \ No newline at end of file From 0f634d82eeafe052ca7f1521735c57209d31adfb Mon Sep 17 00:00:00 2001 From: Frank King Date: Sun, 5 Dec 2021 11:02:54 +0800 Subject: [PATCH 2/5] Remove tracked `attrs_end_with_doc_comment` flag in `RewriteContext` --- src/attr.rs | 3 --- src/rewrite.rs | 3 --- src/types.rs | 4 +++- src/visitor.rs | 1 - 4 files changed, 3 insertions(+), 8 deletions(-) diff --git a/src/attr.rs b/src/attr.rs index 0634ca9d90f..76b66e9da80 100644 --- a/src/attr.rs +++ b/src/attr.rs @@ -426,9 +426,6 @@ impl Rewrite for [ast::Attribute] { } attrs = &attrs[doc_comment_len..]; - if attrs.is_empty() { - context.attrs_end_with_doc_comment.set(true); - } continue; } diff --git a/src/rewrite.rs b/src/rewrite.rs index ad364f0c70a..c8abe70141b 100644 --- a/src/rewrite.rs +++ b/src/rewrite.rs @@ -31,9 +31,6 @@ pub(crate) struct RewriteContext<'a> { pub(crate) inside_macro: Rc>, // Force block indent style even if we are using visual indent style. pub(crate) use_block: Cell, - // When rewriting generic params, an extra newline should be put - // if the attributes end with a doc comment - pub(crate) attrs_end_with_doc_comment: Cell, // When `is_if_else_block` is true, unindent the comment on top // of the `else` or `else if`. pub(crate) is_if_else_block: Cell, diff --git a/src/types.rs b/src/types.rs index a293b4856c1..025c095da65 100644 --- a/src/types.rs +++ b/src/types.rs @@ -574,7 +574,9 @@ impl Rewrite for ast::GenericParam { match self.attrs.rewrite(context, shape) { Some(ref rw) if !rw.is_empty() => { result.push_str(rw); - if context.attrs_end_with_doc_comment.get() { + // 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/src/visitor.rs b/src/visitor.rs index f9ce6eb8b76..e4a7be742ab 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -988,7 +988,6 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { config: self.config, inside_macro: Rc::new(Cell::new(false)), use_block: Cell::new(false), - attrs_end_with_doc_comment: Cell::new(false), is_if_else_block: Cell::new(false), force_one_line_chain: Cell::new(false), snippet_provider: self.snippet_provider, From 6ed6fe0e4713425c3b303eda00cab02a3b653f91 Mon Sep 17 00:00:00 2001 From: Frank King Date: Sun, 5 Dec 2021 11:46:02 +0800 Subject: [PATCH 3/5] Fix duplicated doc comments of const generic params --- src/lists.rs | 14 ++++++++++++-- tests/source/doc-of-generic-item.rs | 4 ++-- tests/target/doc-of-generic-item.rs | 2 +- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/lists.rs b/src/lists.rs index 3515dd17251..fa751d6d1db 100644 --- a/src/lists.rs +++ b/src/lists.rs @@ -5,7 +5,9 @@ use std::iter::Peekable; use rustc_span::BytePos; -use crate::comment::{find_comment_end, rewrite_comment, FindUncommented}; +use crate::comment::{ + comment_style, find_comment_end, rewrite_comment, CommentStyle, FindUncommented, +}; use crate::config::lists::*; use crate::config::{Config, IndentStyle}; use crate::rewrite::RewriteContext; @@ -358,7 +360,15 @@ where } // Pre-comments - if let Some(ref comment) = item.pre_comment { + let pre_comment = item.pre_comment.as_ref().and_then(|comment| { + match comment_style(comment, false) { + // FIXME: the span of a const generic param should not include + // its document comments. + CommentStyle::DoubleBullet | CommentStyle::TripleSlash | CommentStyle::Doc => None, + _ => Some(comment), + } + }); + if let Some(ref comment) = pre_comment { // Block style in non-vertical mode. let block_mode = tactic == DefinitiveListTactic::Horizontal; // Width restriction is only relevant in vertical mode. diff --git a/tests/source/doc-of-generic-item.rs b/tests/source/doc-of-generic-item.rs index 6052e95a625..b602cd4cade 100644 --- a/tests/source/doc-of-generic-item.rs +++ b/tests/source/doc-of-generic-item.rs @@ -4,5 +4,5 @@ struct Foo< /// doc of T T, /// doc of N - const N: item ->; \ No newline at end of file + const N: item, +>; diff --git a/tests/target/doc-of-generic-item.rs b/tests/target/doc-of-generic-item.rs index 60de117517d..b602cd4cade 100644 --- a/tests/target/doc-of-generic-item.rs +++ b/tests/target/doc-of-generic-item.rs @@ -5,4 +5,4 @@ struct Foo< T, /// doc of N const N: item, ->; \ No newline at end of file +>; From dcae958af8012ae4ae25813138a8bf42c012dc20 Mon Sep 17 00:00:00 2001 From: Frank King Date: Sun, 6 Feb 2022 17:19:35 +0800 Subject: [PATCH 4/5] Fix `::span()` --- src/lists.rs | 14 ++------------ src/spanned.rs | 15 ++++----------- 2 files changed, 6 insertions(+), 23 deletions(-) diff --git a/src/lists.rs b/src/lists.rs index fa751d6d1db..3515dd17251 100644 --- a/src/lists.rs +++ b/src/lists.rs @@ -5,9 +5,7 @@ use std::iter::Peekable; use rustc_span::BytePos; -use crate::comment::{ - comment_style, find_comment_end, rewrite_comment, CommentStyle, FindUncommented, -}; +use crate::comment::{find_comment_end, rewrite_comment, FindUncommented}; use crate::config::lists::*; use crate::config::{Config, IndentStyle}; use crate::rewrite::RewriteContext; @@ -360,15 +358,7 @@ where } // Pre-comments - let pre_comment = item.pre_comment.as_ref().and_then(|comment| { - match comment_style(comment, false) { - // FIXME: the span of a const generic param should not include - // its document comments. - CommentStyle::DoubleBullet | CommentStyle::TripleSlash | CommentStyle::Doc => None, - _ => Some(comment), - } - }); - if let Some(ref comment) = pre_comment { + if let Some(ref comment) = item.pre_comment { // Block style in non-vertical mode. let block_mode = tactic == DefinitiveListTactic::Horizontal; // Width restriction is only relevant in vertical mode. 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() From 850009b21019cc12592304667eb958fc7ef92005 Mon Sep 17 00:00:00 2001 From: Frank King Date: Sun, 6 Feb 2022 18:39:46 +0800 Subject: [PATCH 5/5] Remove duplicated source file of `doc-of-generic-item.rs` --- tests/source/doc-of-generic-item.rs | 8 -------- tests/target/doc-of-generic-item.rs | 6 ++++++ 2 files changed, 6 insertions(+), 8 deletions(-) delete mode 100644 tests/source/doc-of-generic-item.rs diff --git a/tests/source/doc-of-generic-item.rs b/tests/source/doc-of-generic-item.rs deleted file mode 100644 index b602cd4cade..00000000000 --- a/tests/source/doc-of-generic-item.rs +++ /dev/null @@ -1,8 +0,0 @@ -struct Foo< - /// doc of 'a - 'a, - /// doc of T - T, - /// doc of N - const N: item, ->; diff --git a/tests/target/doc-of-generic-item.rs b/tests/target/doc-of-generic-item.rs index b602cd4cade..2efc5e09a3d 100644 --- a/tests/target/doc-of-generic-item.rs +++ b/tests/target/doc-of-generic-item.rs @@ -1,8 +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, >;