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

Parser parses an if statement followed by a tuple as a function call instead of a sequence #1310

Closed
iAmMichaelConnor opened this issue May 6, 2023 · 1 comment · Fixed by #1924
Labels
bug Something isn't working

Comments

@iAmMichaelConnor
Copy link
Collaborator

iAmMichaelConnor commented May 6, 2023

Aim

I've tried to distill it to a minimal example, but it was a complicated bit of aztec3 code i was writing.
I've tested this not very minimal example (but as minimal as I could cause the error) with v0.4.1. It's a horrible example. The code was doing lots of stuff, and I just started stripping things out whilst trying to retain the error.

NOTICE: when we remove the constrain liine, it causes this error. If we reinstate the constrain line, the error disappears. Not sure why I need a constrain here.

fn main(a: Field, condition: bool) -> pub bool {

    let (b, arr) = foo(a, condition);
    arr[0] == 4 + b
}

fn foo(a: Field, condition: bool) -> (Field, [Field; 2]) {

    let mut arr: [Field; 2] = [1, 2];

    if condition {
        arr[0] = a;
    } else {
        arr[0] = 4;
    }
    // NOTICE: no idea why I need this constraint here... or what the error means when you remove it.
    // constrain arr[0] == 4;

    (a, arr)
}
error: Expected a function, but found a(n) ()
   ┌─ /mnt/user-data/mike/packages/yarn-project/noir-contracts/src/contracts/mike-play/src/main.nr:11:5
   │  
11 │ ╭     if condition {
12 │ │         arr[0] = a;
13 │ │     } else {
14 │ │         arr[0] = 4;
15 │ │     }
16 │ │     // TODO: no idea why I need this constraint here... or what the error means when you remove it.
17 │ │     // constrain arr[0] == 4;
18 │ │ 
19 │ │     (a, arr)
   │ ╰────────────'

error: aborting due to 1 previous errors
Error: Failed to compile circuit

Location:
    crates/nargo_cli/src/cli/mod.rs:71:5
    ```


### Expected behavior

.

### Bug

.

### To reproduce

1.
2.
3.
4.


### Installation method

None

### Nargo version

_No response_

### @noir-lang/noir_wasm version

_No response_

### @noir-lang/barretenberg version

_No response_

### @noir-lang/aztec_backend version

_No response_

### Additional context

_No response_

### Submission Checklist

- [ ] Once I hit submit, I will assign this issue to the Project Board with the appropriate tags.
@iAmMichaelConnor iAmMichaelConnor added the bug Something isn't working label May 6, 2023
@github-project-automation github-project-automation bot moved this to 📋 Backlog in Noir May 6, 2023
@jfecher
Copy link
Contributor

jfecher commented May 8, 2023

This is an interesting one. It is parsing the whole expression as a function call:

if condition {
    arr[0] = a;
} else {
    arr[0] = 4;
}(a, arr)

Hence why the constrain statement with its semicolon afterwards fixes it. I may have to look at the rust grammar a bit for this one since this is technically an ambiguous parse, but the right solution should be to restructure the parser a bit to favor them as two separate statements.

@jfecher jfecher changed the title Error: Expected a function, but found a(n) () Parser parses an if statement followed by a tuple as a function call instead of a sequence Jul 13, 2023
@github-project-automation github-project-automation bot moved this from 📋 Backlog to ✅ Done in Noir Jul 13, 2023
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.

2 participants