Repository: rust
Issue: "Associated type doesn't live long enough", even though it does
Labels: A-lifetimes,A-associated-items,C-bug

<!--
Thank you for filing a bug report! 🐛 Please provide a short summary of the bug,
along with any information you feel relevant to replicating the bug.
-->

I tried this code:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=0474ce6d89ae44d17d9ef2df99c0883f

```rust
struct Foo<T>([T]);

impl<I: core::slice::SliceIndex<[T]>, T> core::ops::Index<I> for Foo<T>
where
    I::Output: AsRef<[u8]>,
{
    type Output = [u8];

    fn index(&self, index: I) -> &Self::Output {
        self.0[index].as_ref()
    }
}
```

I expected this to compile, because lifetime elision enforces that `I::Output` will live as long as `self`, and that `SomethingElse` will live as long as `I::Output`, and by transitivity `SomethingElse` will live as long as `self`.

However, I got to following
<details>
<summary> error message </summary>

```rust
error[E0311]: the associated type `<I as SliceIndex<[T]>>::Output` may not live long enough
  --> src/lib.rs:11:9
   |
11 |         self.0[index].as_ref()
   |         ^^^^^^^^^^^^^
   |
note: the associated type `<I as SliceIndex<[T]>>::Output` must be valid for the anonymous lifetime #1 defined on the method body at 10:5...
  --> src/lib.rs:10:5
   |
10 |     fn index(&self, index: I) -> &Self::Output {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...so that the reference type `&<I as SliceIndex<[T]>>::Output` does not outlive the data it points at
  --> src/lib.rs:11:9
   |
11 |         self.0[index].as_ref()
   |         ^^^^^^^^^^^^^

error[E0311]: the associated type `<I as SliceIndex<[T]>>::Output` may not live long enough
  --> src/lib.rs:11:9
   |
11 |         self.0[index].as_ref()
   |         ^^^^^^^^^^^^^
   |
note: the associated type `<I as SliceIndex<[T]>>::Output` must be valid for the anonymous lifetime #1 defined on the method body at 10:5...
  --> src/lib.rs:10:5
   |
10 |     fn index(&self, index: I) -> &Self::Output {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...so that the type `<I as SliceIndex<[T]>>::Output` is not borrowed for too long
  --> src/lib.rs:11:9
   |
11 |         self.0[index].as_ref()
   |         ^^^^^^^^^^^^^

error[E0311]: the associated type `<I as SliceIndex<[T]>>::Output` may not live long enough
  --> src/lib.rs:11:23
   |
11 |         self.0[index].as_ref()
   |                       ^^^^^^
   |
note: the associated type `<I as SliceIndex<[T]>>::Output` must be valid for the anonymous lifetime #1 defined on the method body at 10:5...
  --> src/lib.rs:10:5
   |
10 |     fn index(&self, index: I) -> &Self::Output {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...so that the reference type `&<I as SliceIndex<[T]>>::Output` does not outlive the data it points at
  --> src/lib.rs:11:23
   |
11 |         self.0[index].as_ref()
   |                       ^^^^^^

error: aborting due to 3 previous errors

error: could not compile `playground`

To learn more, run the command again with --verbose.
```

</details>


This is not specific to `AsRef`, any function that has the following signature will fail to compile

```rust
fn method(output: &I::Output) -> &SomethingElse { ... }
```