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

Compiler panic when you initialize a slice of composite type with as value, a slice with the exact same type. #4742

Closed
pventuzelo opened this issue Apr 8, 2024 · 5 comments · Fixed by #4966
Labels
bug Something isn't working

Comments

@pventuzelo
Copy link

Aim

We (https://github.com/FuzzingLabs) found that the compiler panics when you initialize a slice of composite type with as value, a slice with the exact same type.

Expected Behavior

No panic should occur.

Bug

The previous issue reported shows that modifying a slice causes the compiler to panic. However, this issue demonstrates that initializing a slice of a composite type with another slice of the same type as its value also triggers a compiler panic.

The specific composite type or values used are irrelevant. The issue arises only when assigning an existing slice as the value of the initialized slice. If the value is a slice created within the initialization statement, the panic does not occur.

The issue seems to be due to the compiler treating the slice as an array and attempting to call AsSlice on it. However, the slice doesn’t have a fixed size like an array.

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

The application panicked (crashed).
Message:  ICE: Should be have an array length for AsSlice input
Location: compiler/noirc_evaluator/src/ssa/opt/flatten_cfg/capacity_tracker.rs:129

To Reproduce

struct strct1 {
    elem1: Field,
}

fn main() {
/* Panic when you initialize a slice of arrays with a value of a slice with the exact same type */
let var1: [[i32; 1]] = [[0]];
let var2: [[i32; 1]] = var1;

/* Still panics with other composite types */
let var1: [(i32, u8)] = [(1, 2)];
let var2: [(i32, u8)] = var1;

let var3: [strct1] = [strct1 { elem1: 1321351 }];
let var4: [strct1] = var3;

/* No panic if the slice is created in this initialization sentence, even with an existing array */
let var1: [i32; 1] = [0];
let var2: [[i32; 1]] = [var1];
}

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 Apr 8, 2024
@github-project-automation github-project-automation bot moved this to 📋 Backlog in Noir Apr 8, 2024
@vezenovm
Copy link
Contributor

vezenovm commented May 2, 2024

This looks to be due to incorrect array to slice coercion @jfecher.
With just the following lines:

    let var1: [(i32, u8)] = [(1, 2)];
    let var2: [(i32, u8)] = var1;

I get:

Message:  internal error: entered unreachable code: AsSlice called with non-array u64
Location: compiler/noirc_evaluator/src/ssa/opt/as_slice_length.rs:47

We look to be expecting an array when checking an AsSlice call, but we have two arguments and are calling as_slice on a slice.

@jfecher
Copy link
Contributor

jfecher commented May 2, 2024

and are calling as_slice on a slice.

Looks like a result of check_declaration returning the expr_type instead of the annotated type again: https://github.com/noir-lang/noir/blob/master/compiler/noirc_frontend/src/hir/type_check/stmt.rs#L352.

IIRC this used to return the annotated type. I don't remember why it was changed a while back.

@vezenovm
Copy link
Contributor

vezenovm commented May 2, 2024

IIRC this used to return the annotated type. I don't remember why it was changed a while back.

I'll test that out

@vezenovm
Copy link
Contributor

vezenovm commented May 2, 2024

Looks to be a part of this PR (#4152). I'm assuming the simpler logic was fine until this case

@vezenovm
Copy link
Contributor

vezenovm commented May 2, 2024

Switching it back fixes the issue

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

## Problem\*

Resolves #4742

## Summary\*



## Additional Context



## Documentation\*

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

# PR Checklist\*

- [ ] I have tested the changes locally.
- [ ] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
@github-project-automation github-project-automation bot moved this from 📋 Backlog to ✅ Done in Noir May 2, 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.

3 participants