noir-lang / noir

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

Expression type is ambiguous when using traits in a global's definition #5626

Open jfecher opened 2 months ago

jfecher commented 2 months ago

Aim

use dep::std::collections::map::HashMap;
use dep::std::hash::poseidon2::Poseidon2Hasher;
use dep::std::hash::BuildHasherDefault;

global MAP: HashMap<Field, Field, 2, Poseidon2Hasher> = {
    let mut map = HashMap::default();
    map.insert(1, 2);
    map.insert(3, 4);
    map
};

fn main() {
    println(MAP);
}

Expected Behavior

The program to type check correctly

Bug

error: No matching impl found for `Poseidon2Hasher: BuildHasher<_>`
  ┌─ /.../short/src/main.nr:6:19
  │
6 │     let mut map = HashMap::default();
  │                   ---------------- No impl for `Poseidon2Hasher: BuildHasher<_>`
  │

error: Expression type is ambiguous
  ┌─ /.../short/src/main.nr:6:19
  │
6 │     let mut map = HashMap::default();
  │                   ---------------- Type must be known at this point
  │

error: Expression type is ambiguous
  ┌─ /.../short/src/main.nr:6:19
  │
6 │     let mut map = HashMap::default();
  │                   ---------------- Type must be known at this point
  │

error: No matching impl found for `Poseidon2Hasher: BuildHasher<_>`
  ┌─ /.../short/src/main.nr:7:5
  │
7 │     map.insert(1, 2);
  │     ---------------- No impl for `Poseidon2Hasher: BuildHasher<_>`
  │

error: Expression type is ambiguous
  ┌─ /.../short/src/main.nr:7:5
  │
7 │     map.insert(1, 2);
  │     ---------------- Type must be known at this point
  │

error: No matching impl found for `Poseidon2Hasher: BuildHasher<_>`
  ┌─ /.../short/src/main.nr:8:5
  │
8 │     map.insert(3, 4);
  │     ---------------- No impl for `Poseidon2Hasher: BuildHasher<_>`
  │

error: Expression type is ambiguous
  ┌─ /.../short/src/main.nr:8:5
  │
8 │     map.insert(3, 4);
  │     ---------------- Type must be known at this point
  │

Aborting due to 7 previous errors

It is probably an issue with resolving trait constraints on globals before the type constraint from their declaration is applied.

To Reproduce

1. 2. 3. 4.

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

asterite commented 2 months ago

Just a note that we get the same error if it's a local let instead of a global:

use std::collections::map::HashMap;
use std::hash::poseidon2::Poseidon2Hasher;
use std::hash::BuildHasherDefault;

fn main() {
    let map: HashMap<Field, Field, 2, Poseidon2Hasher> = {
        let mut map = HashMap::default();
        map.insert(1, 2);
        map.insert(3, 4);
        map
    };
}