noir icon indicating copy to clipboard operation
noir copied to clipboard

`Field` computed to be larger than `u32::MAX` with arithmetic generics panics

Open michaeljklein opened this issue 5 months ago • 0 comments

Aim

Attempted to compile a program that adds two Field's using arithmetic generics, where the result of the computation is larger than u32::MAX:

struct Foo<let F: Field> {}

impl<let F: Field> Foo<F> {
    fn size(self) -> Field {
        F
    }
}

// 2^32 - 1
global A: Field = 4294967295;

// Avoiding overflow succeeds:
// fn foo<let A: Field>() -> Foo<A> {
fn foo<let A: Field>() -> Foo<A + A> {
    Foo {}
}

fn main() {
    // skipping 'size()' succeeds
    let C = foo::<A>();
    let C = foo::<A>().size();
}

Note that using a u64 instead of a Field fails with:

error: The only supported numeric generic types are `u1`, `u8`, `u16`, and `u32`
  ┌─ src/main.nr:5:12
  │
5 │ struct Foo<let F: u64> {}
  │            ----------
  │

Expected Behavior

Expected compilation to succeed or else fail with a user-error

Bug

❯ ~/.nargo/bin/nargo execute
The application panicked (crashed).
Message:  Non-numeric type variable used in expression expecting a value
Location: compiler/noirc_frontend/src/monomorphization/mod.rs:925

This is a bug. We may have already fixed this in newer versions of Nargo so try searching for similar issues at https://github.com/noir-lang/noir/issues/.
If there isn't an open issue for this bug, consider opening one at https://github.com/noir-lang/noir/issues/new?labels=bug&template=bug_report.yml

To Reproduce

Workaround

None

Workaround Description

No response

Additional Context

No response

Project Impact

None

Blocker Context

No response

Nargo Version

nargo version = 0.34.0 noirc version = 0.34.0+5598059576c6cbc72474aff4b18bc5e4bb9f08e1 (git version hash: 5598059576c6cbc72474aff4b18bc5e4bb9f08e1, is dirty: false)

NoirJS Version

No response

Proving Backend Tooling & Version

No response

Would you like to submit a PR for this Issue?

None

Support Needs

No response

michaeljklein avatar Sep 23 '24 14:09 michaeljklein