title: How do I bound a trait with a supertrait that uses the trait's associated type as a parameter?
body: <p>I have a trait <code>Trait</code> with an associated type <code>Trait::Associated</code>. I am trying to bound the trait by requiring that it be indexable by its associated type, as shown here:</p>
  
  <pre><code>use std::ops::Index;
  
  pub trait Trait: Index&lt;Trait::Associated&gt; {
      type Associated;
  }
  </code></pre>
  
  <p>However, the compiler complains that the associated type is ambiguous</p>
  
  <blockquote>
    <pre class="lang-none prettyprint-override"><code>error[E0223]: ambiguous associated type
   --&gt; src/main.rs:3:24
    |
  3 | pub trait Trait: Index&lt;Trait::Associated&gt; {
    |                        ^^^^^^^^^^^^^^^^^ ambiguous associated type
    |
    = note: specify the type using the syntax `&lt;Type as Trait&gt;::Associated`
  </code></pre>
  </blockquote>
  
  <p>I also tried referring to the associated type as <code>Self::Associated</code>, but  then the compiler protests about a cyclic reference between the type and the trait:</p>
  
  <blockquote>
    <pre class="lang-none prettyprint-override"><code>error[E0391]: cyclic dependency detected
   --&gt; src/main.rs:3:1
    |
  3 | pub trait Trait: Index&lt;Self::Associated&gt; {
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cyclic reference
    |
  note: the cycle begins when computing the supertraits of `Trait`...
   --&gt; src/main.rs:3:1
    |
  3 | pub trait Trait: Index&lt;Self::Associated&gt; {
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    = note: ...which then again requires computing the supertraits of `Trait`, completing the cycle.
  </code></pre>
  </blockquote>
  
  <p>Finally, I also tried explicitly implementing <code>Index</code> for <code>Trait</code>:</p>
  
  <pre><code>pub trait Trait {
      type Associated;
  }
  
  impl&lt;T: Trait&gt; Index&lt;T::Associated&gt; for T {
      type Output = str;
  
      fn index(&amp;self, associated: T::Associated) -&gt; &amp;'static str {
          "sup"
      }
  }
  </code></pre>
  
  <p>Unfortunately that fails too:</p>
  
  <blockquote>
    <pre class="lang-none prettyprint-override"><code>error[E0210]: type parameter `T` must be used as the type parameter for some local type (e.g. `MyStruct&lt;T&gt;`)
   --&gt; src/main.rs:7:1
    |
  7 | impl&lt;T: Trait&gt; Index&lt;T::Associated&gt; for T {
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type parameter `T` must be used as the type parameter for some local type
    |
    = note: only traits defined in the current crate can be implemented for a type parameter
  </code></pre>
  </blockquote>
  
  <p>Am I trying to do something unreasonable here? Is there a way of achieving something similar, without having to use generics?</p>
  
  <p><a href="https://play.rust-lang.org/?gist=fd462df10e06bb6a86b29461fed3975e&amp;version=stable&amp;mode=debug" rel="noreferrer">Playground</a>.</p>