Repository: rust
Issue: Lifetime bound not accepted for wrapped associated type of a generic trait
Labels: A-lifetimes,A-associated-items,A-borrow-checker,C-bug,T-types

In (some?) situations where the compiler needs the lifetime bound `Something<T::Type>: 'a` where `T: Trait` and `Trait` is some trait with the associated type `Type`, it does not accept that bound given explicitly.
The error message instead asks for the bound `T::Type: 'a`, so that it can infer `Something<T::Type>: 'a`.

Using the suggested bound satisfies the compiler but may not always be possible. In particular, it is not possible when the structure of the lifetime bounds are dictated by a trait that is being implemented (example at the end).

I tried this code:

```rust
trait Trait {
    type Type;
}

fn foo<'a, T, I>(_: I)
where
    T: Trait,
    I: IntoIterator<Item = &'a Option<<T as Trait>::Type>>,
    Option<<T as Trait>::Type>: 'a, // fails
    // <T as Trait>::Type: 'a, // using this instead works
{}

```
([playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=dda2da724324db5f0f3c93b29a117f47))

I expected to see this happen:
It compiles successfully. The necessary bound is `Option<<T as Trait>::Type>: 'a` and that is what it is given.

Instead, this happened:
```
error[E0309]: the associated type `<T as Trait>::Type` may not live long enough
 --> src/main.rs:8:21
  |
8 |     I: IntoIterator<Item = &'a Option<<T as Trait>::Type>>,
  |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = help: consider adding an explicit lifetime bound `<T as Trait>::Type: 'a`...
  = note: ...so that the reference type `&'a Option<<T as Trait>::Type>` does not outlive the data it points at
```

Note that bounding the lifetime of `Option<..>` does work if the inner type is not an associated type:

```rust
fn foo_works<'a, T, I>(_s: I)
where
    I: IntoIterator<Item = &'a Option<T>>,
    Option<T>: 'a,
{}
```

### Meta
Occurs on:
* [Rust Playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=dda2da724324db5f0f3c93b29a117f47) Stable 1.52.1; 2018 edition
* Rust Playground 1.54.0-nightly (2021-05-17 3e99439f4dacc8ba0d2c)
* My machine `rustc --version --verbose`:
```
rustc 1.52.1
binary: rustc
commit-hash: unknown
commit-date: unknown
host: x86_64-unknown-linux-gnu
release: 1.52.1
LLVM version: 11.1.0
```

No backtrace is generated.

### Larger Example
In which the lifetime bounds are fixed by a trait implementation so the bound suggested by the compiler cannot be applied.
<details>

```rust
pub trait Trait {
    type Type;
    
    fn foo<'a, I>(&self, _: I)
    where
        I: IntoIterator<Item = &'a Self::Type>,
        Self::Type: 'a;
}

pub struct OptionWrapper<T>(T);

impl<T: Trait> Trait for OptionWrapper<T> {
    type Type = Option<T::Type>;
    
    fn foo<'a, I>(&self, _: I)
    where
        I: IntoIterator<Item = &'a Self::Type>,
        Self::Type: 'a,
        // T::Type: 'a,  // Changes the interface for Trait::foo
    {}
}
```
([playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=10fae988a960307b366bde1aefe019aa))

</details>

More discussion here: <https://users.rust-lang.org/t/lifebound-bounds-for-an-iterator-of-generic-wrapped-associated-types/59967>

Apologies in advance if this is a duplicate. I haven't found other issues that to my understanding are the same but I don't have a good understanding of which lifetime inference issues might share a common cause. In any case, this is a simpler code example than I've seen in other lifetime inference issues.