Skip to content

Inheriting trait with identical associated type name not rejected #11348

@aakoshh

Description

@aakoshh

Aim

Discovered testing #11346

The following code should not compile:

pub trait Foo {
    type Bar;
    fn foo(x: Self::Bar) -> Self::Bar {
        x
    }
}

pub trait Qux: Foo {
    type Bar;
    fn qux(x: Self::Bar) -> Self::Bar {
        <Self as Foo>::foo(x)
    }
}

fn main() {}

Expected Behavior

It should not compile because Qux::Bar and Foo::Bar are different types, and we are passing the wrong one to foo.

In Rust we get errors due to ambiguous types: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=ee8a01619e5dd9face63aa9859068a14

  Compiling playground v0.0.1 (/playground)
error[E0221]: ambiguous associated type `Bar` in bounds of `Self`
  --> src/main.rs:10:15
   |
 2 |     type Bar;
   |     -------- ambiguous `Bar` from `Foo`
...
 9 |     type Bar;
   |     -------- ambiguous `Bar` from `Qux`
10 |     fn qux(x: Self::Bar) -> Self::Bar {
   |               ^^^^^^^^^ ambiguous associated type `Bar`
   |
help: use fully-qualified syntax to disambiguate
   |
10 -     fn qux(x: Self::Bar) -> Self::Bar {
10 +     fn qux(x: <Self as Qux>::Bar) -> Self::Bar {
   |
help: use fully-qualified syntax to disambiguate
   |
10 -     fn qux(x: Self::Bar) -> Self::Bar {
10 +     fn qux(x: <Self as Foo>::Bar) -> Self::Bar {
   |

error[E0221]: ambiguous associated type `Bar` in bounds of `Self`
  --> src/main.rs:10:29
   |
 2 |     type Bar;
   |     -------- ambiguous `Bar` from `Foo`
...
 9 |     type Bar;
   |     -------- ambiguous `Bar` from `Qux`
10 |     fn qux(x: Self::Bar) -> Self::Bar {
   |                             ^^^^^^^^^ ambiguous associated type `Bar`
   |
help: use fully-qualified syntax to disambiguate
   |
10 -     fn qux(x: Self::Bar) -> Self::Bar {
10 +     fn qux(x: Self::Bar) -> <Self as Qux>::Bar {
   |
help: use fully-qualified syntax to disambiguate
   |
10 -     fn qux(x: Self::Bar) -> Self::Bar {
10 +     fn qux(x: Self::Bar) -> <Self as Foo>::Bar {
   |

error[E0308]: mismatched types
  --> src/main.rs:11:28
   |
11 |         <Self as Foo>::foo(x)
   |         ------------------ ^ expected `Foo::Bar`, found `Qux::Bar`
   |         |
   |         arguments to this function are incorrect
   |
   = note: expected associated type `<Self as Foo>::Bar`
              found associated type `<Self as Qux>::Bar`
   = note: an associated type was expected, but a different one was found
note: associated function defined here
  --> src/main.rs:3:8
   |
 3 |     fn foo(x: Self::Bar) -> Self::Bar {
   |        ^^^ ------------

error[E0221]: ambiguous associated type `Bar` in bounds of `Self`
  --> src/main.rs:10:29
   |
 2 |     type Bar;
   |     -------- ambiguous `Bar` from `Foo`
...
 9 |     type Bar;
   |     -------- ambiguous `Bar` from `Qux`
10 |     fn qux(x: Self::Bar) -> Self::Bar {
   |                             ^^^^^^^^^ ambiguous associated type `Bar`
   |
help: use fully-qualified syntax to disambiguate
   |
10 -     fn qux(x: Self::Bar) -> Self::Bar {
10 +     fn qux(x: Self::Bar) -> <Self as Foo>::Bar {
   |
help: use fully-qualified syntax to disambiguate
   |
10 -     fn qux(x: Self::Bar) -> Self::Bar {
10 +     fn qux(x: Self::Bar) -> <Self as Qux>::Bar {
   |

error[E0308]: mismatched types
  --> src/main.rs:11:9
   |
11 |         <Self as Foo>::foo(x)
   |         ^^^^^^^^^^^^^^^^^^^^^ expected `Qux::Bar`, found `Foo::Bar`
   |
   = note: expected associated type `<Self as Qux>::Bar`
              found associated type `<Self as Foo>::Bar`
   = note: an associated type was expected, but a different one was found

In Rust the ambiguity makes this not compile:

    pub trait Foo {
        type Bar;
        fn foo(x: Self::Bar) -> Self::Bar;
    }

    pub trait Qux: Foo {
        type Bar;
        fn qux(x: Self::Bar) -> Self::Bar;
    }

but it does compile this way:

    pub trait Foo {
        type Bar;
        fn foo(x: Self::Bar) -> Self::Bar;
    }

    pub trait Qux: Foo {
        type Bar;
        fn qux(x: <Self as Qux>::Bar) -> <Self as Qux>::Bar;
    }

Bug

cargo run -q -p nargo_cli -- compile --force && echo "Shouldn't have compiled"

Shouldn't have compiled

To Reproduce

See above.

Workaround

None

Workaround Description

No response

Additional Context

No response

Project Impact

None

Blocker Context

No response

Nargo Version

nargo version = 1.0.0-beta.18 noirc version = 1.0.0-beta.18+a796408a0d6eca324c036e59ac4d5ea1505a50c8 (git version hash: df4487f, is dirty: true)

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions