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

nargo fmt: inconsistent formatting with arrays #3945

Closed
LHerskind opened this issue Jan 4, 2024 · 5 comments · Fixed by #6300
Closed

nargo fmt: inconsistent formatting with arrays #3945

LHerskind opened this issue Jan 4, 2024 · 5 comments · Fixed by #6300
Assignees
Labels
bug Something isn't working

Comments

@LHerskind
Copy link
Contributor

Aim

Run fmt on

dep::std::hash::pedersen_hash_with_separator([
    contract_address.to_field(),
    storage_slot
], GENERATOR_INDEX__PUBLIC_LEAF_INDEX)

Expected Behavior

dep::std::hash::pedersen_hash_with_separator([
    contract_address.to_field(),
    storage_slot
], GENERATOR_INDEX__PUBLIC_LEAF_INDEX)

Or

dep::std::hash::pedersen_hash_with_separator(
    [
        contract_address.to_field(),
        storage_slot
    ], 
    GENERATOR_INDEX__PUBLIC_LEAF_INDEX
)

Bug

The formatted code looks like

dep::std::hash::pedersen_hash_with_separator(
    [
    contract_address.to_field(),
    storage_slot
],
    GENERATOR_INDEX__PUBLIC_LEAF_INDEX
)

To Reproduce

  1. Run nargo fmt

Installation Method

None

Nargo Version

No response

Additional Context

Saw the issue as part of https://github.com/AztecProtocol/aztec-packages/pull/3803/files/c6f4b7b9b9efe9a118e57aec0f28219523812563

Would you like to submit a PR for this Issue?

No

Support Needs

No response

@benesjan
Copy link
Contributor

benesjan commented Jan 4, 2024

I tried to use the master branch of noir (version 219423e) instead of the aztec one to see if the issues pertain and unfortunately they do.

@benesjan
Copy link
Contributor

benesjan commented Jan 4, 2024

2 more issue I've stumbled upon.

  1. When I put 2 spaces after "fn" the formatter leaves it as it is:

fn main(input: PrivateKernelInputsInner) -> distinct pub KernelCircuitPublicInputs { input.native_private_kernel_circuit_inner() }

  1. It doesn't format spaces in dependencies:

use dep:: protocol_types::{ address::AztecAddress, abis::function_selector::FunctionSelector, };

@benesjan
Copy link
Contributor

benesjan commented Jan 5, 2024

The formatter ignores spaces in struct functions:
image

@kevaundray
Copy link
Contributor

@f01dab1e can you take this on?

@ghost
Copy link

ghost commented Jan 5, 2024

I'll have a look on Monday.

github-merge-queue bot pushed a commit that referenced this issue Feb 8, 2024
# Description

chore: implement import formatting

## Additional Context

#3945 (comment)

## Documentation

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[Exceptional Case]** Documentation to be submitted in a separate
PR.

# PR Checklist

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.

Co-authored-by: kevaundray <[email protected]>
Co-authored-by: jfecher <[email protected]>
@michaeljklein michaeljklein self-assigned this Mar 5, 2024
@asterite asterite mentioned this issue Oct 18, 2024
5 tasks
github-merge-queue bot pushed a commit that referenced this issue Oct 22, 2024
# Description

## Problem

Resolves #5281
Resolves #4768
Resolves #4767
Resolves #4766
Resolves #4558
Resolves #4462
Resolves #3945
Resolves #3312

## Summary

I thought about trying to extend the existing formatter to format more
code, but I couldn't understand it very well: it partially implemented
things, and it lacked comments and some explanation of how it works. I
think some chunks might have been copied from the Rust formatter. I also
took a look into it but though it might be too complex.

[I wrote a formatter in the past for
Crystal](https://github.com/crystal-lang/crystal/blob/master/src/compiler/crystal/tools/formatter.cr)
with a technique that I saw was used in the Java formatter written for
Eclipse. The idea is to traverse the AST together with a Lexer, writing
things along the way, bumping into comments and formatting those, etc.
It works pretty well! But that's not enough for the Noir formatter
because here we also want to automatically wrap long lines (Crystal's
formatter doesn't do that). That part (wrapping) is partially based on
[this excellent blog
post](https://yorickpeterse.com/articles/how-to-write-a-code-formatter/).

Benefits:
- All code kinds are formatted. For example previously traits weren't
formatted.
- Comments are never lost.
- Lambdas appearing as the last argument of a call are formatted nicely
(this was maybe the most trickier part to get right).
- I believe the new code ends up being simpler than before, even though
it's (slightly) longer (previously is was 2138 lines, now it's 6139,
though 2608 lines are tests, so it ends up being 3531 lines, but this
new formatter does many things the old one didn't). I tried to comment
and document it well.
- Adding new formatting rules, new configurations, or enabling
formatting of new syntax should be relatively easy to do.
- There are lots and lots of tests for each of the different scenarios.
The existing overall formatter tests were kept, but with unit tests it's
easier to see how edge cases are formatted.

[Here's Aztec-Packages formatted with the new
formatter](https://github.com/AztecProtocol/aztec-packages/pull/9292/files):
- Max line width seems to be respected more (making it more readable)
- `unsafe { ... }` and `comptime { ... }` expressions will be inlined if
possible (shortening the code and making it more readable)

## Additional Context

Some things are subjective. For example Rust will put a function `where`
clause in a separate line, with each trait bound on its own line. The
new formatter does that too. But Rust will sometimes put an `impl` where
clause on the same line. It's not clear to me why. I chose to always put
`where` clauses on a separate line, but this could easily be changed if
we wanted to.

## Documentation

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[For Experimental Features]** Documentation to be submitted in a
separate PR.

# PR Checklist

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.

---------

Co-authored-by: Akosh Farkash <[email protected]>
Co-authored-by: Tom French <[email protected]>
@github-project-automation github-project-automation bot moved this from 📋 Backlog to ✅ Done in Noir Oct 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

4 participants