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

Incorrect compiler error when returning an array after a code block #4372

Closed
pventuzelo opened this issue Feb 14, 2024 · 0 comments · Fixed by #6401
Closed

Incorrect compiler error when returning an array after a code block #4372

pventuzelo opened this issue Feb 14, 2024 · 0 comments · Fixed by #6401
Labels
bug Something isn't working

Comments

@pventuzelo
Copy link

Aim

We (@FuzzingLabs) found that the compiler raises incorrectly an error when returning an array after a code block.

Expected Behavior

The error comes from the line 152 of this file: https://github.com/noir-lang/noir/blob/master/compiler/noirc_frontend/src/hir/type_check/errors.rs

No error should be returned.

Bug

The error occurs when attempting to return an array immediately after a code block. It persists under certain conditions, such as when using different array types, returning an array derived from a variable, or creating a code block with constructs other than an if statement, even if there are instructions inside them.

However, there are workarounds to avoid this error. For instance, you can bypass the error by directly assigning the array to a variable before the code block, inserting an instruction between the code block and the return statement, or using an array with a specified size. Additionally, loop blocks do not trigger the error.

The error occurs with the latest compiler version on the master branch. The error message is as follows:

error: Expected type Array, found type ()

To Reproduce

  1. Create a Noir project with the following command: nargo new test_issue
  2. Paste the code below into the file [main.nr](http://main.nr/)
  3. Compile the project using the following command: nargo compile
// The compiler raises an error when attempting to return an array after a code block.
fn case1() -> [Field] {
    if true {
    }
    [1]
}

// The error still occurs in the following scenarios:
// - When using another array type.
// - When returning an array of a variable.
// - When creating a code block with something other than an if statement even with instructions in them.
fn case2() -> [u8] {
    let mut var: u8 = 1;
    {
        var += 1;
    }
    [var]
}

// The error is not raised when the array is directly assigned to a variable.
fn case3() -> [Field] {
    let mut var: [Field] = [1];
    {}
    var
}

// The error is not raised if there is an instruction between the code block and the return.
fn case4() -> [Field] {
    let mut var: Field = 1;
    {}
    var += 1;
    [var]
}

// The error is not raised when using an array with a specified size.
fn case5() -> [Field; 1] {
    let mut var: Field = 1;
    {}
    [var; 1]
}

// The error is not raised with for loop blocks.
fn case6() -> [Field] {
    let mut var: Field = 1;
    for i in 0..1 {

    }
    [var]
}

fn main() {}

Project Impact

None

Impact Context

No response

Workaround

None

Workaround Description

No response

Additional Context

No response

Installation Method

None

Nargo Version

No response

NoirJS Version

No response

Would you like to submit a PR for this Issue?

None

Support Needs

No response

@pventuzelo pventuzelo added the bug Something isn't working label Feb 14, 2024
@github-project-automation github-project-automation bot moved this to 📋 Backlog in Noir Feb 14, 2024
@github-project-automation github-project-automation bot moved this from 📋 Backlog to ✅ Done in Noir Oct 30, 2024
TomAFrench added a commit that referenced this issue Oct 30, 2024
* master: (30 commits)
  chore: add regression tests for #4372 (#6401)
  chore: add regression tests for #6314 (#6381)
  chore: use array instead of Vec in keccak256 (#6395)
  fix: make keccak256 work with input lengths greater than 136 bytes (#6393)
  feat: support specifying generics on a struct when calling an associated function (#6306)
  fix: Display every bit in integer tokens (#6360)
  feat: better LSP hover for functions (#6376)
  feat: Add capacities to brillig vectors and use them in slice ops (#6332)
  feat: suggest removing `!` from macro call that doesn't return Quoted (#6384)
  fix: (formatter) correctly format quote delimiters (#6377)
  fix: allow globals in format strings (#6382)
  feat: do not increment reference counts on arrays through references (#6375)
  fix: (LSP) check visibility of module that re-exports item, if any (#6371)
  feat: let LSP suggest traits in trait bounds (#6370)
  fix: LSP auto-import would import public item inside private module (#6366)
  fix: remove assumed parent traits (#6365)
  fix: slightly better formatting of empty blocks with comments (#6367)
  fix: Fix panic in comptime code (#6361)
  feat: let the LSP import code action insert into existing use statements (#6358)
  chore: minor tweaks to comptime doc (#6357)
  ...
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.

1 participant