noir-lang / noir

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

Globals calling a static method on a struct in a different module fails to resolve #5271

Closed TomAFrench closed 1 week ago

TomAFrench commented 2 weeks ago

Aim

I would like to create a global by calling a static method on a struct which is defined in another module.

My program is then

global some_wrapped_value: thing::FooStruct = thing::FooStruct::new();
mod thing {
    struct FooStruct {
        inner: Field
    }

    impl FooStruct {
        pub fn new() -> Self {
            FooStruct { inner: 0 }
        }
    }
}

Expected Behavior

This program should compile successfully with some_wrapped_value being equal to FooStruct { inner: 0 }.

Bug

Compilation fails with the error below:

$ nargo execute
error: Could not resolve 'FooStruct' in path
   ┌─ /home/tom/Programming/aztec/noir/test_programs/execution_success/global_consts/src/main.nr:80:13
   │
80 │             FooStruct { inner: 0 }
   │             ---------
   │

Aborting due to 1 previous error

To Reproduce

copy-paste above code into program with an empty fn main() {} and compile

Project Impact

None

Impact Context

No response

Workaround

None

Workaround Description

No response

Additional Context

This bug seems to specifically hit static methods on structs which are being referred to using full path as opposed to being imported into the current module as the following programs compile:

FooStruct is imported into current module:

global some_wrapped_value: FooStruct = FooStruct::new();
use thing::FooStruct;
mod thing {
    struct FooStruct {
        inner: Field
    }

    impl FooStruct {
        pub fn new() -> Self {
            FooStruct { inner: 0 }
        }
    }
}

some_wrapped_value is being initialized to be equal to a FooStruct constructed in the same module as where it is defined.

global some_wrapped_value: thing::FooStruct = thing::foo_thing;
mod thing {
    global foo_thing: FooStruct = FooStruct::new();

    struct FooStruct {
        inner: Field
    }

    impl FooStruct {
        pub fn new() -> Self {
            FooStruct { inner: 0 }
        }
    }
}

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