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

[Struct {}, N] syntax results in a panic due to assigning LocalId(0) twice #2786

Closed
bryanwee023 opened this issue Sep 22, 2023 · 2 comments · Fixed by #3221
Closed

[Struct {}, N] syntax results in a panic due to assigning LocalId(0) twice #2786

bryanwee023 opened this issue Sep 22, 2023 · 2 comments · Fixed by #3221
Assignees
Labels
bug Something isn't working

Comments

@bryanwee023
Copy link

Aim

I'm trying to initialize an array of a user-defined struct.

Expected Behavior

If this is meant to be supported, then my code should run.
If not, then an appropriate error message should be printed.

Bug

I get the following error output.

[noir_playground] Testing test_main... The application panicked (crashed).
Message:  Variable LocalId(0) was defined twice in ssa-gen pass

To Reproduce

  1. Define a struct
  2. Initialize an array of the struct in main
  3. Call main in a test and run nargo test
struct Box {
    x: Field
}

fn main() {
    let boxes = [Box  {x: 1}; 8];
}

#[test]
fn test_main() {
    main();
}

Installation Method

Binary

Nargo Version

nargo 0.10.5 (git version hash: 91efe44, is dirty: false)

Additional Context

No response

Would you like to submit a PR for this Issue?

No

Support Needs

No response

@bryanwee023 bryanwee023 added the bug Something isn't working label Sep 22, 2023
@github-project-automation github-project-automation bot moved this to 📋 Backlog in Noir Sep 22, 2023
@bryanwee023 bryanwee023 changed the title Struct arrays give Struct arrays crash tests Sep 22, 2023
@bryanwee023 bryanwee023 changed the title Struct arrays crash tests Struct arrays crashed tests Sep 22, 2023
@TomAFrench
Copy link
Member

Interesting, thanks for opening this issue @bryanwee023. I've replicated this on current master.

Arrays of structs are supported but it looks like the sugared syntax to create an array filled with the same element doesn't, i.e. [Box {x: 1}, Box {x: 1}, ..., Box {x: 1}] will work but [Box {x: 1}; 8] won't.

@TomAFrench TomAFrench changed the title Struct arrays crashed tests [Struct {}, N] syntax results in a panic due to assigning LocalId(0) twice Sep 22, 2023
@TomAFrench
Copy link
Member

This seems to also require you to initialize the struct inside the array creation. As a workaround, I'd move this outside and use something like the below.

fn main() {
    let box = Box {x: 1};
    let boxes = [box; 8];
}

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.

3 participants