Skip to content

E0223 (ambiguous associated type) seems to be too restrictive in some contexts #35711

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
alexbool opened this issue Aug 16, 2016 · 2 comments
Closed

Comments

@alexbool
Copy link
Contributor

Consider following example:

trait MyTrait {
    type MyType: MyTrait2;

    fn do_something() -> Self::MyType::X;
}

trait MyTrait2 {
    type X;
}

It fails with

error[E0223]: ambiguous associated type
 --> <anon>:6:26
  |
6 |     fn do_something() -> Self::MyType::X;
  |                          ^^^^^^^^^^^^^^^ ambiguous associated type
  |
  = note: specify the type using the syntax `<<Self as MyTrait>::MyType as Trait>::X`

Then I open docs which say

trait MyTrait {type X; }

fn main() {
    let foo: MyTrait::X;
}

The problem here is that we're attempting to take the type of X from MyTrait.
Unfortunately, the type of X is not defined, because it's only made concrete in
implementations of the trait. A working version of this code might look like:

Whoa, in my example it seems pretty obvious which type X I mean. It works when I use the suggestion from error message, but the syntax seems too verbose for this context.
Is it a bug? Am I missing something here?

Another example:

trait MyTrait {
    type MyType: MyTrait2;

    fn do_something() -> <Self::MyType as MyTrait2>::X;
}

trait MyTrait2 {
    type X;
}

fn run<T: MyTrait>() -> T::MyType::X {
    unimplemented!();
}

Fails with

error[E0223]: ambiguous associated type
  --> <anon>:13:25
   |
13 | fn run<T: MyTrait>() -> T::MyType::X {
   |                         ^^^^^^^^^^^^ ambiguous associated type
   |
   = note: specify the type using the syntax `<<T as MyTrait>::MyType as Trait>::X`
@malbarbo
Copy link
Contributor

This seems to be a duplicated of #31372

@Mark-Simulacrum
Copy link
Member

Closing in favor of that since it's an earlier issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants