noir icon indicating copy to clipboard operation
noir copied to clipboard

Trait associated types and constants shouldn't be implicit generics

Open asterite opened this issue 10 months ago • 2 comments

Aim

This code in Rust doesn't compile:

trait Trait {
    type T;
    const N: usize;

    fn foo() -> [T; N];
}

The errors:

error[E0412]: cannot find type `T` in this scope
 --> src/main.rs:5:18
  |
5 |     fn foo() -> [T; N];
  |                  ^
  |
help: you might have meant to use the associated type
  |
5 |     fn foo() -> [Self::T; N];
  |                  ++++++

error[E0425]: cannot find value `N` in this scope
 --> src/main.rs:5:21
  |
5 |     fn foo() -> [T; N];
  |                     ^
  |
help: you might have meant to use the associated `const`
  |
5 |     fn foo() -> [T; Self::N];
  |                     ++++++

By contrast, in Noir it compiles fine:

pub trait Trait {
    type T;
    let N: u32;

    fn foo() -> [T; N];
}

fn main() {}

Expected Behavior

The Noir code shouldn't compile, as associated types and constants shouldn't be implicit generics (to match Rust's behavior).

Bug

The code compiles.

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 10 '25 14:06 asterite

I don't see much of an issue with keeping this code working.

We could gate it behind Self::T but I don't think we should change associated types from being implicit generics internally - that's what they are in Rust as well.

jfecher avatar Jun 10 '25 14:06 jfecher

Unless this causes issues I'd hesitate to say its a bug either and more just a difference between Noir and Rust

jfecher avatar Jun 10 '25 14:06 jfecher