Repository: rust
Issue: False unconstrained type error and unresolved lifetime
Labels: A-diagnostics,A-lifetimes,T-compiler,D-terse,T-types

<!--
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.
-->

Sorry about the title, but I'm not sure how else to describe it.

Taken the following code:

```rust
struct IterRef<T>(T);

impl<'a, T, Item, IntoIter> IntoIterator for IterRef<T>
where
    &'a T: IntoIterator<Item = Item, IntoIter = IntoIter>,
    IntoIter: Iterator<Item = Item>,
{
    type Item = Item;
    type IntoIter = IntoIter;

    fn into_iter(self) -> Self::IntoIter {
        let t = &self.0;

        t.into_iter()
    }
}
```

Both `Iter` and `IntoIter` generic parameters result in a "not constrained" error. However, If I do the following instead:

```rust
impl<T, Item, IntoIter> IntoIterator for IterRef<T>
where
    for<'a> &'a T: IntoIterator<Item = Item, IntoIter = IntoIter>,
    IntoIter: Iterator<Item = Item>,
{
    type Item = Item;
    type IntoIter = IntoIter;

    fn into_iter(self) -> Self::IntoIter {
        let t = &self.0;

        t.into_iter()
    }
}
```

Then this error goes away, but I then get another error.

If we add a test to the code, such as the following:
```rust
fn _test() {
    let v = vec![1, 2, 3];
    let iter_ref = IterRef(v);

    for i in iter_ref {
        println!("{i}")
    }
}
```

Then I get an error saying `Ref<T>` does not implement `IntoIterator`, which it does, as confirmed by a help message below the error. The full error is:

```
    Checking frappe-tea v0.0.0 (/home/joseprogdp/dev/rust/crates/frappe-tea/frappe-tea)
error[E0277]: the trait bound `reactive::IterRef<std::vec::Vec<{integer}>>: std::iter::IntoIterator` is not satisfied
   --> src/reactive.rs:420:14
    |
420 |     for i in iter_ref {
    |              ^^^^^^^^ the trait `std::iter::IntoIterator` is not implemented for `reactive::IterRef<std::vec::Vec<{integer}>>`
    |
    = help: the trait `std::iter::IntoIterator` is implemented for `reactive::IterRef<T>`

For more information about this error, try `rustc --explain E0277`.
error: could not compile `frappe-tea` due to previous error
[Finished running. Exit status: 101]
```

We can confirm this is true by doing the following slight modification:
```rust
impl<'a, T, Item, IntoIter> IntoIterator for &'a IterRef<T>
where
    &'a T: IntoIterator<Item = Item, IntoIter = IntoIter>,
    IntoIter: Iterator<Item = Item>,
{
    type Item = Item;
    type IntoIter = IntoIter;

    fn into_iter(self) -> Self::IntoIter {
        let t = &self.0;

        t.into_iter()
    }
}

fn _test() {
    let v = vec![1, 2, 3];
    let iter_ref = IterRef(v);

    for i in &iter_ref {
        println!("{i}")
    }
}
```

To recap these modifications, what we did is:
1. Reintroduce a `'a` lifetime which previously created an unconstrained type error
2. added the `'a` lifetime to `Ref<T>`
3. changed from `... in iter_ref` to `... in &iter_ref` to match back up with the original trait impl

I expected both
```rust
impl<'a, T, Item, IntoIter> IntoIterator for IterRef<T>
where
    &'a T: IntoIterator<Item = Item, IntoIter = IntoIter>,
    IntoIter: Iterator<Item = Item>,
{
  /* ... */
}
```

and

```rust
impl<T, Item, IntoIter> IntoIterator for IterRef<T>
where
    for<'a> &'a T: IntoIterator<Item = Item, IntoIter = IntoIter>,
    IntoIter: Iterator<Item = Item>,
{
  /* ... */
}
```

to work, and not just the 
```rust
impl<'a, T, Item, IntoIter> IntoIterator for &'a IterRef<T>
where
    &'a T: IntoIterator<Item = Item, IntoIter = IntoIter>,
    IntoIter: Iterator<Item = Item>,
{
  /* ... */
}
```

to be the only variation to work.

### Meta
<!--
If you're using the stable version of the compiler, you should also check if the
bug also exists in the beta or nightly versions.
-->

`rustc --version --verbose`:

```
rustc 1.63.0-nightly (a6b8c6954 2022-06-03)
binary: rustc
commit-hash: a6b8c6954829669a5c4fa320c3e6132edf04fcfc
commit-date: 2022-06-03
host: x86_64-unknown-linux-gnu
release: 1.63.0-nightly
LLVM version: 14.0.4
```

<!--
Include a backtrace in the code block by setting `RUST_BACKTRACE=1` in your
environment. E.g. `RUST_BACKTRACE=1 cargo build`.
-->
<details><summary>Backtrace</summary>
<p>

```
error[E0207]: the type parameter `Item` is not constrained by the impl trait, self type, or predicates
   --> src/reactive.rs:401:13
    |
401 | impl<'a, T, Item, IntoIter> IntoIterator for IterRef<T>
    |             ^^^^ unconstrained type parameter

error[E0207]: the type parameter `IntoIter` is not constrained by the impl trait, self type, or predicates
   --> src/reactive.rs:401:19
    |
401 | impl<'a, T, Item, IntoIter> IntoIterator for IterRef<T>
    |                   ^^^^^^^^ unconstrained type parameter

For more information about this error, try `rustc --explain E0207`.
error: could not compile `frappe-tea` due to 2 previous errors
```

</p>
</details>

### Disclaimer
I apologize if I filled out the bug report incorrectly. I am legally blind and might have misplaced a backtick somewhere! :P