Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions src/formatting/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1596,6 +1596,7 @@ fn rewrite_type<R: Rewrite>(
generics: &ast::Generics,
generic_bounds_opt: Option<&ast::GenericBounds>,
rhs: Option<&R>,
span: Span,
) -> Option<String> {
let mut result = String::with_capacity(128);
result.push_str(&format!("{}type ", format_visibility(context, vis)));
Expand Down Expand Up @@ -1642,12 +1643,40 @@ fn rewrite_type<R: Rewrite>(
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,
)?
Comment on lines +1669 to +1676
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will want to use the comment_shape here for comments, but in cases where there's a where clause we don't want to for the assignment operator and the rhs. That's because in cases where the item has a where clause followed by a block or assignment the block/assignment is not supposed to be indented.

https://github.com/rust-dev-tools/fmt-rfcs/blob/master/guide/items.md#where-clauses

}
_ => format!("{}=", result),
};

// 1 = `;`
let shape = Shape::indented(indent, context.config).sub_width(1)?;
Expand All @@ -1664,6 +1693,7 @@ pub(crate) fn rewrite_opaque_type(
generic_bounds: &ast::GenericBounds,
generics: &ast::Generics,
vis: &ast::Visibility,
span: Span,
) -> Option<String> {
let opaque_type_bounds = OpaqueTypeBounds { generic_bounds };
rewrite_type(
Expand All @@ -1674,6 +1704,7 @@ pub(crate) fn rewrite_opaque_type(
generics,
Some(generic_bounds),
Some(&opaque_type_bounds),
span,
)
}

Expand Down Expand Up @@ -1931,6 +1962,7 @@ pub(crate) fn rewrite_type_alias(
context: &RewriteContext<'_>,
indent: Indent,
vis: &ast::Visibility,
span: Span,
) -> Option<String> {
rewrite_type(
context,
Expand All @@ -1940,6 +1972,7 @@ pub(crate) fn rewrite_type_alias(
generics,
generic_bounds_opt,
ty_opt,
span,
)
}

Expand Down Expand Up @@ -1989,8 +2022,9 @@ pub(crate) fn rewrite_associated_impl_type(
generics: &ast::Generics,
context: &RewriteContext<'_>,
indent: Indent,
span: Span,
) -> Option<String> {
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)),
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions src/formatting/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -617,6 +618,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
generic_bounds,
generics,
&item.vis,
item.span,
);
self.push_rewrite(item.span, rewrite);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -734,6 +737,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
generics,
&self.get_context(),
self.block_indent,
ii.span,
)
};
let rewrite = match ty {
Expand Down
16 changes: 16 additions & 0 deletions tests/source/issue-4244.rs
Original file line number Diff line number Diff line change
@@ -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 <T> {
type E /* Comment E */ = SS;
}

type F<'a: 'static, T: Ord + 'static>: Eq + PartialEq where T: 'static + Copy /* x */ = Vec<u8>;
20 changes: 20 additions & 0 deletions tests/target/issue-4244.rs
Original file line number Diff line number Diff line change
@@ -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<T> {
type E /* Comment E */ = SS;
}

type F<'a: 'static, T: Ord + 'static>: Eq + PartialEq
where
T: 'static + Copy, /* x */
= Vec<u8>;