Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust non-empty tuple struct span to start before fields #5015

Merged
merged 1 commit into from
Oct 13, 2021

Conversation

ytmimi
Copy link
Contributor

@ytmimi ytmimi commented Oct 5, 2021

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 updated to start right before the tuple struct's opening
'(' so that the items::Contex will always find the tuple fields.

Copy link
Member

@calebcartwright calebcartwright left a comment

Choose a reason for hiding this comment

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

Thank you for another PR! You definitely found the correct source of the bug, and have the right idea about fixing it via an adjusted span. However, we'll want to tweak the precise span we're using just a bit, inline comments left below with details

src/items.rs Outdated
Comment on lines 1472 to 1475
let span_start_before_tuple_fields = mk_sp(
context.snippet_provider.span_before_last(span, "("),
span.hi(),
);
Copy link
Member

Choose a reason for hiding this comment

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

We don't want to try to search for the last occurrence, as the presence of fields and various generics could produce different specious span derivations (there could be other opening parens that come after the one we want in this context).

What we really need to do is make sure we're passing the correct subset of the span with the lo position being advanced far enough. To be safe, we should probably check for the presence of generics and use the hi position of that span if present, otherwise using the high of the ident (the identifier/name, e.g. AStar and BStar in our tests) should be fine:

Suggested change
let span_start_before_tuple_fields = mk_sp(
context.snippet_provider.span_before_last(span, "("),
span.hi(),
);
let lo = if let Some(generics) = struct_parts.generics {
generics.span.hi()
} else {
struct_parts.ident.span.hi()
};

src/items.rs Outdated
result = overflow::rewrite_with_parens(
context,
&result,
fields.iter(),
shape,
span,
span_start_before_tuple_fields,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
span_start_before_tuple_fields,
mk_sp(lo, span.hi()),

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for pointing this out. I didn't even realize that struct_parts had all the information I needed to adjust the span correctly. I definitely feel like I'm learning more about the project with each PR!

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.
@ytmimi
Copy link
Contributor Author

ytmimi commented Oct 12, 2021

Just wanted to let you know that I made the changes you suggested!

@calebcartwright
Copy link
Member

Excellent, thank you!

@calebcartwright calebcartwright merged commit f7c4a44 into rust-lang:master Oct 13, 2021
@ytmimi ytmimi deleted the issue_5011 branch October 13, 2021 03:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Various issues with pub(crate) tuple structs containing comments
2 participants