Skip to content

Commit 5c94f49

Browse files
authored
Merge pull request #5 from pnkfelix/mult-unbound-assoc-types-with-same-name
Add section pointing out how associated item ambiguity is handled.
2 parents 2b14c14 + a2f8020 commit 5c94f49

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

text/0000-trait-alias.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,28 @@ fn bar4(x: Box<IntIterator + Sink + 'static>) { ... } // ok (*)
189189
The lines marked with `(*)` assume that [#24010](https://github.com/rust-lang/rust/issues/24010) is
190190
fixed.
191191

192+
### Ambiguous constraints
193+
194+
If there are multiple associated types with the same name in a trait alias,
195+
then it is a static error ("ambiguous associated type") to attempt to
196+
constrain that associated type via the trait alias. For example:
197+
198+
```rust
199+
trait Foo { type Assoc; }
200+
trait Bar { type Assoc; } // same name!
201+
202+
// This works:
203+
trait FooBar1 = Foo<Assoc = String> + Bar<Assoc = i32>;
204+
205+
// This does not work:
206+
trait FooBar2 = Foo + Bar;
207+
fn badness<T: FooBar2<Assoc = String>>() { } // ERROR: ambiguous associated type
208+
209+
// Here are ways to workaround the above error:
210+
fn better1<T: FooBar2 + Foo<Assoc = String>>() { } // (leaves Bar::Assoc unconstrained)
211+
fn better2<T: FooBar2 + Foo<Assoc = String> + Bar<Assoc = i32>>() { } // constrains both
212+
```
213+
192214
# Teaching
193215
[teaching]: #teaching
194216

0 commit comments

Comments
 (0)