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

Generic traits that return generic arrays don't bind array lengths #4088

Closed
Tracked by #2568
sirasistant opened this issue Jan 18, 2024 · 5 comments · Fixed by #4121
Closed
Tracked by #2568

Generic traits that return generic arrays don't bind array lengths #4088

sirasistant opened this issue Jan 18, 2024 · 5 comments · Fixed by #4121
Assignees
Labels
bug Something isn't working

Comments

@sirasistant
Copy link
Contributor

sirasistant commented Jan 18, 2024

Aim

Doing code like this:

trait Serialize<N> {
    fn serialize(self) -> [Field; N];
}

struct ValueNote {
    value: Field,
}

impl Serialize<1> for ValueNote {
    fn serialize(self) -> [Field; 1] {
        [self.value]
    }
}

fn check<N>(serialized_note: [Field; N]) {
    assert(serialized_note[0] == 0);
}

fn oopsie<Note, N>(note: Note) where Note: Serialize<N> {
    let serialized_note = Note::serialize(note);

    check(serialized_note)
}

fn main(mut note: ValueNote) {
    oopsie(note);
}

Expected Behavior

It should compile!

Bug

Panics with

The application panicked (crashed).
Message:  assertion failed: `(left == right)`
  left: `2`,
 right: `1`
Location: compiler/noirc_evaluator/src/ssa/opt/inlining.rs:420

Workaround: typing annotating the return value:

trait Serialize<N> {
    fn serialize(self) -> [Field; N];
}

struct ValueNote {
    value: Field,
}

impl Serialize<1> for ValueNote {
    fn serialize(self) -> [Field; 1] {
        [self.value]
    }
}

fn check<N>(serialized_note: [Field; N]) {
    assert(serialized_note[0] == 0);
}

fn oopsie<Note, N>(note: Note) where Note: Serialize<N> {
    let serialized_note: [Field; N] = Note::serialize(note); <== This

    check(serialized_note)
}

fn main(mut note: ValueNote) {
    oopsie(note);
}
image

To Reproduce

Installation Method

None

Nargo Version

No response

Additional Context

No response

Would you like to submit a PR for this Issue?

No

Support Needs

No response

@sirasistant sirasistant added the bug Something isn't working label Jan 18, 2024
@Thunkar
Copy link
Contributor

Thunkar commented Jan 19, 2024

This issue can also cause the following error (a lot clearer) in the monomorphization step:

Message:  Non-numeric type variable used in expression expecting a value
Location: compiler/noirc_frontend/src/monomorphization/mod.rs:720

@sirasistant
Copy link
Contributor Author

sirasistant commented Jan 22, 2024

Another possibly related issue:
This fails:

use dep::std::option::Option;

trait MySerialize<N> {
    fn serialize(self) -> [Field; N];
}

trait MyDeserialize<N> {
    fn deserialize(fields: [Field; N]) -> Self;
}

impl MySerialize<1> for Field {
    fn serialize(self) -> [Field; 1] {
        [self]
    }
}

impl MyDeserialize<1> for Field {
    fn deserialize(fields: [Field; 1]) -> Self {
        fields[0]
    }
}

#[oracle(storageRead)]
fn storage_read_oracle<N>(_storage_slot: Field, _number_of_elements: Field) -> [Field; N] {}

unconstrained fn storage_read_oracle_wrapper<N>(_storage_slot: Field) -> [Field; N] {
    storage_read_oracle(_storage_slot, N)
}

pub fn storage_read<N>(storage_slot: Field) -> [Field; N] {
    storage_read_oracle_wrapper(storage_slot)
}

struct PublicState<T> {
    storage_slot: Field,
}

impl<T> PublicState<T> {
    pub fn new(
        storage_slot: Field,
    ) -> Self {
        assert(storage_slot != 0, "Storage slot 0 not allowed. Storage slots must start from 1.");
        PublicState {
            storage_slot,
        }
    }
    pub fn read<T_SERIALIZED_LEN>(self) -> T where T: MyDeserialize<T_SERIALIZED_LEN> {
        let fields: [Field; T_SERIALIZED_LEN] = storage_read(self.storage_slot);
        T::deserialize(fields)
    }
}

fn main(value: Field) {
    let ps: PublicState<Field> = PublicState::new(27);

    assert(ps.read() == value);
}

with

The application panicked (crashed).
Message:  assertion failed: `(left == right)`
  left: `2`,
 right: `1`
Location: compiler/noirc_evaluator/src/ssa/opt/inlining.rs:420

The workaround consists in passing an unused sample array with the length:

use dep::aztec::context::{Context};
use dep::std::option::Option;

trait MySerialize<N> {
    fn serialize(self) -> [Field; N];
}

trait MyDeserialize<N> {
    fn deserialize(fields: [Field; N]) -> Self;
    // Such a hackkity hack!!!!
    fn empty() -> [Field; N];
}

impl MySerialize<1> for Field {
    fn serialize(self) -> [Field; 1] {
        [self]
    }
}

impl MyDeserialize<1> for Field {
    fn deserialize(fields: [Field; 1]) -> Self {
        fields[0]
    }

    fn empty() -> [Field; 1] {
        dep::std::unsafe::zeroed()
    }
}

#[oracle(storageRead)]
fn storage_read_oracle<N>(_storage_slot: Field, _number_of_elements: Field) -> [Field; N] {}

unconstrained fn storage_read_oracle_wrapper<N>(_storage_slot: Field, _sample: [Field; N]) -> [Field; N] {
    storage_read_oracle(_storage_slot, N)
}

pub fn storage_read<N>(storage_slot: Field, _sample: [Field; N]) -> [Field; N] {
    storage_read_oracle_wrapper(storage_slot, _sample)
}

struct PublicState<T> {
    storage_slot: Field,
}

impl<T> PublicState<T> {
    pub fn new(
        storage_slot: Field,
    ) -> Self {
        assert(storage_slot != 0, "Storage slot 0 not allowed. Storage slots must start from 1.");
        PublicState {
            storage_slot,
        }
    }
    pub fn read<T_SERIALIZED_LEN>(self) -> T where T: MyDeserialize<T_SERIALIZED_LEN> {
        let fields: [Field; T_SERIALIZED_LEN] = storage_read(self.storage_slot, T::empty());
        T::deserialize(fields)
    }
}

fn main(value: Field) {
    let ps: PublicState<Field> = PublicState::new(27);

    assert(ps.read() == value);
}

The ugly part of this workaround is that it requires to modify the serialize trait, which would affect users ):

@jfecher
Copy link
Contributor

jfecher commented Jan 22, 2024

Workaround: typing annotating the return value:

Another workaround is to call the trait method directly instead of using static method syntax:

trait Serialize<N> {
    fn serialize(self) -> [Field; N];
}

struct ValueNote {
    value: Field,
}

impl Serialize<1> for ValueNote {
    fn serialize(self) -> [Field; 1] {
        [self.value]
    }
}

fn check<N>(serialized_note: [Field; N]) {
    assert(serialized_note[0] == 0);
}

fn oopsie<Note, N>(note: Note) where Note: Serialize<N> {
    let serialized_note = note.serialize(); //ok
    check(serialized_note)
}

fn main(mut note: ValueNote) {
    oopsie(note);
}

@jfecher
Copy link
Contributor

jfecher commented Jan 22, 2024

@sirasistant I think your second error is a bit different. Do you mind filing a separate issue?

@sirasistant
Copy link
Contributor Author

Opened #4124

github-merge-queue bot pushed a commit that referenced this issue Jan 23, 2024
…ing identifiers (#4121)

# Description

## Problem\*

Resolves #4088

## Summary\*

We were calling `instantiate` on identifiers before applying any trait
constraints. So if we have a constraint like `Foo<Field>` (referring to
the trait `trait Foo<T> { ... }`, and an identifier `foo : forall T.
fn(T)`, we need to apply the `T = Field` constraint before instantiating
`foo` to replace `T` with a fresh type variable.

## Additional Context



## Documentation\*

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[Exceptional Case]** Documentation to be submitted in a separate
PR.

# PR Checklist\*

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.

---------

Co-authored-by: Maxim Vezenov <mvezenov@gmail.com>
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