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

Fix for regression in zig fmt's handling of 'for-if' #8127

Merged
merged 2 commits into from
Mar 15, 2021

Conversation

LewisGaul
Copy link
Contributor

Fixes #8088

  • Added testcase to reproduce the issue
  • Factored out common code into renderWhilePayload()
  • Determine whether newline is needed before the 'if'
  • Fix indentation of the 'then' expression when no newline is inserted before the 'if'

lib/std/zig/render.zig Outdated Show resolved Hide resolved
Copy link
Member

@ifreund ifreund left a comment

Choose a reason for hiding this comment

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

This is already an improvement over the status quo and fixes the regression. There's still some inconsistency here that I think would be good to iron out if you're game for it:

// zig fmt accepts this:
if (a)
    if (b)
        f(x);

// but changes this
if (a)
    for (b) |c|
        foo(x);
// to this:
if (a) for (b) |c|
    foo(x);

The same thing happens with while in the place of for

@LewisGaul
Copy link
Contributor Author

This is already an improvement over the status quo and fixes the regression. There's still some inconsistency here that I think would be good to iron out if you're game for it:

Nice catch, I can certainly have a go at addressing that. Thanks again for taking a look.

legaul-cisco and others added 2 commits March 15, 2021 14:48
Add failing testcase to reproduce issue 8088

Tidy up renderWhile(), factoring out renderWhilePayload()

Ensure correct newline is used before 'then' token in while/for/if

Handle indents for 'if' inside 'for' or 'while'

Stop special-casing 'if' compared to 'for' and 'while'
The main realization here was that getting rid of the early returns
in renderWhile() and rewriting the logic into a mostly unified execution
path took things from ~200 lines to ~100 lines and improved consistency
by deduplicating code.

Also add several test cases and fix a few issues along the way:

Fixes ziglang#6114
Fixes ziglang#8022
Copy link
Member

@ifreund ifreund left a comment

Choose a reason for hiding this comment

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

Thanks for getting the ball rolling on this! I took your work as a starting point and iterated a bit further to end in what I think is a much better place than the status quo :)

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.

Zig fmt introduces newline on for-around-if where it formerly didn't
3 participants