noir icon indicating copy to clipboard operation
noir copied to clipboard

Integer type not correctly inferred when unrelated trait impl exists

Open asterite opened this issue 8 months ago • 1 comments

Aim

This code:

trait From2<T> {
    fn from2(input: T) -> Self;
}

struct U60Repr {}

impl From2<[i32; 3]> for U60Repr {
    fn from2(_: [i32; 3]) -> Self {
        U60Repr {}
    }
}

// Comment this impl and it works fine
impl From2<i32> for U60Repr {
    fn from2(_: i32) -> Self {
        U60Repr {}
    }
}

fn main() {
    let x = [1, 2, 3];
    let _ = U60Repr::from2(x);
}

doesn't compile: it inferrs that x is [Field; 3] when it should infer that it's [i32; 3], because that's the only matching impl.

If the impl From2<i32> is removed, the code compiles fine.

Expected Behavior

The code should compile.

Bug

The code doesn't compile.

To Reproduce

Workaround

None

Workaround Description

No response

Additional Context

No response

Project Impact

None

Blocker Context

No response

Nargo Version

No response

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

asterite avatar Jun 18 '25 13:06 asterite

The problem seems to be that we resolve U60Repr::from2 to a trait method (we don't know the impl yet), then we default the integer types to Field, then we verify the trait constraints so they fail. But I feel that with the call U60Repr::from2(x) the compiler should be able to discard the non-array impl since the argument doesn't match... though I don't know how that can be done (yet).

asterite avatar Jun 18 '25 15:06 asterite