noir-lang / noir

Noir is a domain specific language for zero knowledge proofs
https://noir-lang.org
Apache License 2.0
824 stars 178 forks source link

Usage of `Trait::method` syntax on globals results in monomorphisation ICE. #5029

Open Pasifaee opened 1 month ago

Pasifaee commented 1 month ago

Aim

I wanted to have a structure with multiple trait From implementations and use From::from(...) syntax to create a global variable.

Expected Behavior

This code compiles:

struct Struct {
    field: Field
}

impl From<Field> for Struct {
    fn from(field: Field) -> Self {
        Struct { field }
    }
}

impl From<u64> for Struct {
    fn from(num: u64) -> Self {
        Struct { field: num as Field }
    }
}

global s: Struct = From::from(0);

fn main() {
    println(s);
}

Bug

The code above causes compilation panic. I also can't use Struct::from(...) syntax because it causes compiler error: 'Could not resolve 'from' in path'.

To Reproduce

  1. Try to compile the code above.

Project Impact

Nice-to-have

Impact Context

No response

Workaround

None

Workaround Description

No response

Additional Context

No response

Installation Method

Binary (noirup default)

Nargo Version

nargo version = 0.29.0 noirc version = 0.29.0+2da28997dbedce4a00d2162eddb57564bdb23546

NoirJS Version

No response

Would you like to submit a PR for this Issue?

None

Support Needs

No response

TomAFrench commented 1 month ago

This is unrelated to multiple implementations of the same trait as I get the same ICE when commenting out either of the trait impls.

Struct::from issue is due to https://github.com/noir-lang/noir/issues/4943#issuecomment-2083486013

TomAFrench commented 1 month ago

Can reproduce this with

global s: BoundedVec<Field, 2> = From::from([0]);

fn main() {
    println(s);
}

so it looks like the issue is specific to usage of Trait::method syntax in globals.

TomAFrench commented 1 month ago

For completeness the error is

% nargo compile                                                                                                        ~/aztec-packages/noir/noir-repo/temp tf/native-recursion tom-box
The application panicked (crashed).
Message:  ICE: missing trait impl - should be caught during type checking
Location: compiler/noirc_frontend/src/monomorphization/mod.rs:1027