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

Bug while using a struct containing a slice #606

Closed
lucadonnoh opened this issue Dec 27, 2022 · 6 comments
Closed

Bug while using a struct containing a slice #606

lucadonnoh opened this issue Dec 27, 2022 · 6 comments
Assignees
Labels
bug Something isn't working

Comments

@lucadonnoh
Copy link

Description

Aim

fn main() {
    let a = id([1, 2]);
    let b = id([1, 2, 3]);

    let itWorks1 = MyStruct { data: a };
    constrain itWorks1.data[1] == 2;
    let itWorks2 = MyStruct { data: b };
    constrain itWorks2.data[1] == 2;

    let c = [1, 2];
    let itDoesnt = MyStruct { data: c };
    constrain itDoesnt.data[1] == 2;
}

fn id(x: [Field]) -> [Field] {
    x
}

struct MyStruct {
    data: [Field]
}

Expected behavior

itDoesnt should work without going through id.

Bug

>nargo build
error: Expected type [Field], found type [Field; 2]
   ┌─ /Users/donnoh/.../src/main.nr:11:20
   │
11 │     let itDoesnt = MyStruct { data: c };
   │                    --------------------

error: aborting due to 1 previous errors

To reproduce

  1. nargo build

Environment

(Specify your setup and versions of dependencies.)

  • OS: MacOS Intel
@lucadonnoh lucadonnoh added the bug Something isn't working label Dec 27, 2022
@jfecher jfecher changed the title weird behaviour with slices and structs Bug while using a struct containing a slice Jan 3, 2023
@jfecher
Copy link
Contributor

jfecher commented Jan 3, 2023

This bug is occurring because slices aren't supported in struct declarations yet. This is because currently slices are actually arrays of generic sizes, so [Field] is equivalent to [Field; N] and the struct is not generic over the N parameter, leading to issues.

Unfortunately, manually making the struct generic over N is also unsupported currently so there is no simple fix besides getting rid of the struct for now.

@lucadonnoh
Copy link
Author

is going through the id function a workaround? similar to itWorks1 and itWorks2.

@jfecher
Copy link
Contributor

jfecher commented Jan 3, 2023

Passing the array through to id may be a workaround for now, though it is only a workaround because id also interacts with this bug - it is interpreted as equivalent to

fn id<N, M>(x: [Field; N]) -> [Field; M] {
    x
}

instead of

fn id<N>(x: [Field; N]) -> [Field; N] {
    x
}

This is something we're actively working on fixing

@jfecher jfecher mentioned this issue Jan 5, 2023
5 tasks
@jfecher
Copy link
Contributor

jfecher commented Jan 5, 2023

Once #620 is merged this code can run without errors once modified to use explicit array sizes. It is worth noting though that this PR's purpose is to implement a new feature, so the old bug that occurs when array slices are used within structs will continue to be present. The workaround once this PR is merged will be to make the sizes explicit:

fn main() {
    let a = id([1, 2]);
    let b = id([1, 2, 3]);

    let itWorks1 = MyStruct { data: a };
    constrain itWorks1.data[1] == 2;
    let itWorks2 = MyStruct { data: b };
    constrain itWorks2.data[1] == 2;

    let c = [1, 2];
    let itAlsoWorks = MyStruct { data: c };
    constrain itAlsoWorks.data[1] == 2;
}

fn id<N>(x: [Field; N]) -> [Field; N] {
    x
}

struct MyStruct<N> {
    data: [Field; N]
}

@kevaundray
Copy link
Contributor

@jfecher what is the status of this issue?

@jfecher
Copy link
Contributor

jfecher commented Jul 24, 2023

@kevaundray there is a workaround for it now, and the original issue is a duplicate of the bug reported in #562, so I'm closing this one.

@jfecher jfecher closed this as completed Jul 24, 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
None yet
Development

Successfully merging a pull request may close this issue.

3 participants