Skip to content

HKL issues with deep associated types. #34142

Closed
@pierzchalski

Description

@pierzchalski

Using HKL and associated types, one can write constraints that should be accepted which the compiler rejects (mostly using the IntoIterator: IntoIter: Iterator: IteratorWithSomeProperty hierarchy).

'Motivating' example here and a minimal one here. The intermediate trait is required to show the error.

To reiterate what's in the comments of the first example, consider the following trait and function:

trait Tr {}
impl<A> Tr for Vec<A> {}

fn fancy_constraints<A, T: Tr>(t: T)
  where for<'a> &'a T: IntoIterator,
        for<'a> <&'a T as IntoIterator>::IntoIter: ExactSizeIterator,
{} 

Does Vec<A> satisfy these constraints? &'a Vec<A> has an IntoIterator implementation via Deref<Target=[A]> (this indirection isn't the issue: we can make a newtype wrapper around Vec<A> which directly implements IntoIterator and still exhibits the problem).

We have <&'a Vec<A> as IntoIterator>::IntoIter == Iter<'a, A>, and Iter<'a, A>: ExactSizeIterator>, so this should satisfy the constraints given in fancy_constraints above. Instead, we get the following error message:

error: the trait bound `for<'a> <&'a _ as std::iter::IntoIterator>::IntoIter: std::iter::ExactSizeIterator` is not satisfied [--explain E0277]
  --> <anon>:13:5
13 |>     fancy_constraints(vec![0u8]);
   |>     ^^^^^^^^^^^^^^^^^
help: the following implementations were found:
help:   <&'a mut I as std::iter::ExactSizeIterator>
note: required by `fancy_constraints`

The help message is somewhat confusing (I couldn't find the implementation it mentions, but I didn't look very hard), and I couldn't find a way to use it to fix the problem.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions