diff --git a/src/formatting/items.rs b/src/formatting/items.rs index 615526f92ff..b2d5c84561a 100644 --- a/src/formatting/items.rs +++ b/src/formatting/items.rs @@ -1596,6 +1596,7 @@ fn rewrite_type( generics: &ast::Generics, generic_bounds_opt: Option<&ast::GenericBounds>, rhs: Option<&R>, + span: Span, ) -> Option { let mut result = String::with_capacity(128); result.push_str(&format!("{}type ", format_visibility(context, vis))); @@ -1642,12 +1643,40 @@ fn rewrite_type( if let Some(ty) = rhs { // If there's a where clause, add a newline before the assignment. Otherwise just add a // space. - if !generics.where_clause.predicates.is_empty() { + let has_where = !generics.where_clause.predicates.is_empty(); + if has_where { result.push_str(&indent.to_string_with_newline(context.config)); } else { result.push(' '); } - let lhs = format!("{}=", result); + + let comment_span = context + .snippet_provider + .opt_span_before(span, "=") + .map(|op_lo| mk_sp(generics.where_clause.span.hi(), op_lo)); + + let lhs = match comment_span { + Some(comment_span) + if contains_comment(context.snippet_provider.span_to_snippet(comment_span)?) => + { + let comment_shape = if has_where { + Shape::indented(indent, context.config) + } else { + Shape::indented(indent, context.config) + .block_left(context.config.tab_spaces())? + }; + + combine_strs_with_missing_comments( + context, + result.trim_end(), + "=", + comment_span, + comment_shape, + true, + )? + } + _ => format!("{}=", result), + }; // 1 = `;` let shape = Shape::indented(indent, context.config).sub_width(1)?; @@ -1664,6 +1693,7 @@ pub(crate) fn rewrite_opaque_type( generic_bounds: &ast::GenericBounds, generics: &ast::Generics, vis: &ast::Visibility, + span: Span, ) -> Option { let opaque_type_bounds = OpaqueTypeBounds { generic_bounds }; rewrite_type( @@ -1674,6 +1704,7 @@ pub(crate) fn rewrite_opaque_type( generics, Some(generic_bounds), Some(&opaque_type_bounds), + span, ) } @@ -1931,6 +1962,7 @@ pub(crate) fn rewrite_type_alias( context: &RewriteContext<'_>, indent: Indent, vis: &ast::Visibility, + span: Span, ) -> Option { rewrite_type( context, @@ -1940,6 +1972,7 @@ pub(crate) fn rewrite_type_alias( generics, generic_bounds_opt, ty_opt, + span, ) } @@ -1989,8 +2022,9 @@ pub(crate) fn rewrite_associated_impl_type( generics: &ast::Generics, context: &RewriteContext<'_>, indent: Indent, + span: Span, ) -> Option { - let result = rewrite_type_alias(ident, ty_opt, generics, None, context, indent, vis)?; + let result = rewrite_type_alias(ident, ty_opt, generics, None, context, indent, vis, span)?; match defaultness { ast::Defaultness::Default(..) => Some(format!("default {}", result)), @@ -3227,6 +3261,7 @@ impl Rewrite for ast::ForeignItem { &context, shape.indent, &self.vis, + self.span, ), ast::ForeignItemKind::MacCall(ref mac) => { rewrite_macro(mac, None, context, shape, MacroPosition::Item) diff --git a/src/formatting/visitor.rs b/src/formatting/visitor.rs index 7b142e0d480..dea632f703a 100644 --- a/src/formatting/visitor.rs +++ b/src/formatting/visitor.rs @@ -606,6 +606,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { &self.get_context(), self.block_indent, &item.vis, + item.span, ); self.push_rewrite(item.span, rewrite); } @@ -617,6 +618,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { generic_bounds, generics, &item.vis, + item.span, ); self.push_rewrite(item.span, rewrite); } @@ -685,6 +687,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { &self.get_context(), self.block_indent, &ti.vis, + ti.span, ); self.push_rewrite(ti.span, rewrite); } @@ -734,6 +737,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { generics, &self.get_context(), self.block_indent, + ii.span, ) }; let rewrite = match ty { diff --git a/tests/source/issue-4244.rs b/tests/source/issue-4244.rs new file mode 100644 index 00000000000..34b51085e13 --- /dev/null +++ b/tests/source/issue-4244.rs @@ -0,0 +1,16 @@ +pub struct SS {} + +pub type A /* A Comment */ = SS; + +pub type B // Comment +// B += SS; + +pub type C + /* Comment C */ = SS; + +pub trait D { + type E /* Comment E */ = SS; +} + +type F<'a: 'static, T: Ord + 'static>: Eq + PartialEq where T: 'static + Copy /* x */ = Vec; diff --git a/tests/target/issue-4244.rs b/tests/target/issue-4244.rs new file mode 100644 index 00000000000..8958ba99e80 --- /dev/null +++ b/tests/target/issue-4244.rs @@ -0,0 +1,20 @@ +pub struct SS {} + +pub type A /* A Comment */ = SS; + +pub type B // Comment + // B + = SS; + +pub type C + /* Comment C */ + = SS; + +pub trait D { + type E /* Comment E */ = SS; +} + +type F<'a: 'static, T: Ord + 'static>: Eq + PartialEq +where + T: 'static + Copy, /* x */ += Vec;