Skip to content

Commit

Permalink
Adjust non-empty tuple struct span to start before fields
Browse files Browse the repository at this point in the history
Resolves 5011

Tuple structs with visibility modifiers and comments before the first
field were incorrectly formatted. Comments would duplicate part of the
visibility modifier and struct name.

When trying to parse the tuple fields the ``items::Context`` searches
for the opening '(', but because the visibility modifier introduces
another '(' -- for example ``pub(crate)`` -- the parsing gets messed up.

Now the span is adjusted to start after the struct identifier, or after
any generics. Adjusting the span in this way ensures that the
``items::Contex`` will correctly find the tuple fields.
  • Loading branch information
ytmimi authored and calebcartwright committed Oct 13, 2021
1 parent d418057 commit f7c4a44
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1469,12 +1469,17 @@ fn format_tuple_struct(
format_empty_struct_or_tuple(context, inner_span, offset, &mut result, "(", ")");
} else {
let shape = Shape::indented(offset, context.config).sub_width(1)?;
let lo = if let Some(generics) = struct_parts.generics {
generics.span.hi()
} else {
struct_parts.ident.span.hi()
};
result = overflow::rewrite_with_parens(
context,
&result,
fields.iter(),
shape,
span,
mk_sp(lo, span.hi()),
context.config.fn_call_width(),
None,
)?;
Expand Down
12 changes: 12 additions & 0 deletions tests/source/issue-5011.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
pub(crate) struct ASlash(
// hello
i32
);

pub(crate) struct AStar(
/* hello */
i32
);

pub(crate) struct BStar(/* hello */ i32);

8 changes: 8 additions & 0 deletions tests/target/issue-5011.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
pub(crate) struct ASlash(
// hello
i32,
);

pub(crate) struct AStar(/* hello */ i32);

pub(crate) struct BStar(/* hello */ i32);

0 comments on commit f7c4a44

Please sign in to comment.