Skip to content

Fix ambiguity issue with associated_type_bounds #63393

Closed
@iluuu1994

Description

@iluuu1994

Follow up of #61738.

These two particular cases could not be refactored due to an ambiguity issue:


impl<I: DoubleEndedIterator, U, F> DoubleEndedIterator for FlatMap<I, U, F>
where F: FnMut(I::Item) -> U,
U: IntoIterator,
U::IntoIter: DoubleEndedIterator
{
#[inline]
fn next_back(&mut self) -> Option<U::Item> { self.inner.next_back() }

With #![feature(associated_type_bounds)]

impl<I: DoubleEndedIterator, U, F> DoubleEndedIterator for FlatMap<I, U, F>
where
    F: FnMut(I::Item) -> U,
    U: IntoIterator<IntoIter: DoubleEndedIterator>,
{
    #[inline]
    fn next_back(&mut self) -> Option<U::Item> { self.inner.next_back() }

Result (line numbers are off):

error[E0221]: ambiguous associated type `Item` in bounds of `U`
   --> src/libcore/iter/adapters/flatten.rs:77:39
    |
77  |     fn next_back(&mut self) -> Option<U::Item> { self.inner.next_back() }
    |                                       ^^^^^^^ ambiguous associated type `Item`
    |
   ::: src/libcore/iter/traits/iterator.rs:94:5
    |
94  |     type Item;
    |     ---------- ambiguous `Item` from `iter::traits::iterator::Iterator`
    |
   ::: src/libcore/iter/traits/collect.rs:211:5
    |
211 |     type Item;
    |     ---------- ambiguous `Item` from `iter::traits::collect::IntoIterator`

pub struct Flatten<I: Iterator>
where I::Item: IntoIterator {
inner: FlattenCompat<I, <I::Item as IntoIterator>::IntoIter>,
}

With #![feature(associated_type_bounds)]

pub struct Flatten<I: Iterator<Item: IntoIterator>> {
    inner: FlattenCompat<I, <I::Item as IntoIterator>::IntoIter>,
}

Result (line numbers are off):

error[E0221]: ambiguous associated type `Item` in bounds of `I`
   --> src/libcore/iter/adapters/flatten.rs:112:30
    |
112 |     inner: FlattenCompat<I, <I::Item as IntoIterator>::IntoIter>,
    |                              ^^^^^^^ ambiguous associated type `Item`
    |
   ::: src/libcore/iter/traits/collect.rs:211:5
    |
211 |     type Item;
    |     ---------- ambiguous `Item` from `iter::traits::collect::IntoIterator`
    |
   ::: src/libcore/iter/traits/iterator.rs:94:5
    |
94  |     type Item;
    |     ---------- ambiguous `Item` from `iter::traits::iterator::Iterator`

cc @Centril @alexreg

Metadata

Metadata

Labels

A-associated-itemsArea: Associated items (types, constants & functions)C-bugCategory: This is a bug.E-needs-mcveCall for participation: This issue has a repro, but needs a Minimal Complete and Verifiable ExampleF-associated_type_bounds`#![feature(associated_type_bounds)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions