Remove false positive "ambiguous associated type" errors

Today I was brainstorming some API ideas for a project, and I thought I had come up with a cool solution for a problem. This solution involved associated types. Unfortunately, my idea turned out to be too unergonomic, because I had neglected the fact that Rust demands fully qualified syntax on non-generic associated types. The reason the compiler provides for this requirement is that it causes ambiguity. This is a minimal example of the problem.

struct Foo; 
trait Bar {
  type UniqueTypeName; 
}
impl Bar for Foo {
  type UniqueTypeName = ();
}
type Alias = Foo::UniqueTypeName; 
error[E0223]: ambiguous associated type
 --> src/main.rs:9:14
  |
9 | type Alias = Foo::UniqueTypeName; 
  |              ^^^^^^^^^^^^^^^^^^^ help: use fully-qualified syntax: `<Foo as Trait>::UniqueTypeName`

For more information about this error, try `rustc --explain E0223`.

While this may be true some of the time, it actually isn't most of the time, and its inconsistent in relation to how analogous syntax works (String::from doesn't require fully qualified syntax). Could we get this to be less conservative with what considers ambiguous, and have it only complain when there is in fact an ambiguity?

2 Likes

This has been reported before. Someone who knows more about the compiler than I do will have to confirm, but I believe this to be blocked on lazy normalization, like inherent associated types.

2 Likes