Skip to content

Add section pointing out how associated item ambiguity is handled. #5

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

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions text/0000-trait-alias.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,28 @@ fn bar4(x: Box<IntIterator + Sink + 'static>) { ... } // ok (*)
The lines marked with `(*)` assume that [#24010](https://github.com/rust-lang/rust/issues/24010) is
fixed.

### Ambiguous constraints

If there are multiple associated types with the same name in a trait alias,
then it is a static error ("ambiguous associated type") to attempt to
constrain that associated type via the trait alias. For example:

```rust
trait Foo { type Assoc; }
trait Bar { type Assoc; } // same name!

// This works:
trait FooBar1 = Foo<Assoc = String> + Bar<Assoc = i32>;

// This does not work:
trait FooBar2 = Foo + Bar;
fn badness<T: FooBar2<Assoc = String>>() { } // ERROR: ambiguous associated type

// Here are ways to workaround the above error:
fn better1<T: FooBar2 + Foo<Assoc = String>>() { } // (leaves Bar::Assoc unconstrained)
fn better2<T: FooBar2 + Foo<Assoc = String> + Bar<Assoc = i32>>() { } // constrains both
```

# Teaching
[teaching]: #teaching

Expand Down