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

Update comment search in ControlFlow::rewrite_pat_expr for for loops #5016

Merged
merged 1 commit into from
Oct 14, 2021

Conversation

ytmimi
Copy link
Contributor

@ytmimi ytmimi commented Oct 6, 2021

Resolves #5009

For loops represented by a ControlFlow object use " in" as their connector.
In order to find the span where comments might be located, rustfmt
searches for the first uncommented occurrence of the word "in" within the
current span and adjusts it's starting point to look for comments right after that.
visually this looks like this:

    ruftfmt starts looking for comments here
            |
            V
    for x in /* ... */ 0..1 {}

This works well in most cases, however when the pattern also contains
the word "in", this leads to issues.

    ruftfmt starts looking for comments here
          |
          V
    for in_here in /* ... */ 0..1 {}
        -------
        pattern

In order to correctly identify the connector, the new approach first
updates the span to start after the pattern and then searches for the
first uncommented occurrence of "in".

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.

Thanks again! Another instance where you've got the right idea, though I think we can simplify the implementation a bit by taking advantage of existing spans directly from the AST.

src/expr.rs Outdated Show resolved Hide resolved
Resolves 5009

For loops represented by a ControlFlow object use " in" as their connector.
rustfmt searches for the first uncommented occurrence of the word "in" within the
current span and adjusts it's starting point to look for comments right after that.
visually this looks like this:

    rustfmt starts looking for comments here
            |
            V
    for x in /* ... */ 0..1 {}

This works well in most cases, however when the pattern also contains
the word "in", this leads to issues.

    rustfmt starts looking for comments here
          |
          V
    for in_here in /* ... */ 0..1 {}
        -------
        pattern

In order to correctly identify the connector, the new approach first
updates the span to start after the pattern and then searches for the
first uncommented occurrence of "in".
@calebcartwright
Copy link
Member

Thanks!

@calebcartwright calebcartwright merged commit f2fb3c9 into rust-lang:master Oct 14, 2021
@ytmimi ytmimi deleted the issue_5009 branch October 14, 2021 17:56
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.

Invalid syntax produced if "in" is a subset of a variable name between for ... in.
2 participants