Repository: rust
Issue: Inference poisoned by presence of unrelated impl
Labels: A-trait-system,fixed-by-next-solver

The following code [compiles fine](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=3a6815ef3678a02f350920f8d8c9c43c):


```rust
extern crate serde;
use std::borrow::Cow;

pub fn dothing<'data, V>(
) -> Cow<'data, V>
where
    V: ?Sized + ToOwned ,
    for<'de> Box<V>: serde::Deserialize<'de>,
{
    unimplemented!()
}


pub fn dothing_str<'data>(
) -> Cow<'data, str>
where
{
    dothing()
}
```

Here, `dothing_str()` simply uses return type inference to resolve `dothing()` to `dothing::<str>()`. Quite straightforward, nothing fancy.


However, if you [add in the following type](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=ff402807449ed1478d46a718779aa670), inference gets poisoned:

```rust
pub struct Poison<T: ?Sized> {
    x: T
}


impl<'de, T> serde::Deserialize<'de> for Box<Poison<T>>
where
    T: ?Sized,
    Box<T>: serde::Deserialize<'de>,
{
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        unimplemented!()
    }
}
```

Instead of trying to resolve `Box<str>: Deserialize` (reasonable), it tries to resolve `Box<Poison<Poison<Poison<Poison<Poison<Poison<Poison<Poison<Poison<Poison<Poison<Poison<...>>>>>>>>>>>>>: Deserialize`. Note that this happens even if you replace the `for<'de> ... Deserialize<'de>` with `Deserialize<'static>`, this isn't Yet Another HRTB issue.

<details><summary>Full error</summary>

```
error[E0275]: overflow evaluating the requirement `Box<[_]>: Deserialize<'_>`
  --> src/lib.rs:37:5
   |
37 |     dothing()
   |     ^^^^^^^^^
   |
   = help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`playground`)
note: required for `Box<Poison<[_]>>` to implement `Deserialize<'_>`
  --> src/lib.rs:9:14
   |
9  | impl<'de, T> serde::Deserialize<'de> for Box<Poison<T>>
   |              ^^^^^^^^^^^^^^^^^^^^^^^     ^^^^^^^^^^^^^^
...
12 |     Box<T>: serde::Deserialize<'de>,
   |             ----------------------- unsatisfied trait bound introduced here
   = note: 126 redundant requirements hidden
   = note: required for `Box<Poison<Poison<Poison<Poison<Poison<Poison<Poison<Poison<Poison<Poison<Poison<Poison<...>>>>>>>>>>>>>` to implement `for<'de> Deserialize<'de>`
note: required by a bound in `dothing`
  --> src/lib.rs:27:22
   |
23 | pub fn dothing<'data, V>(
   |        ------- required by a bound in this function
...
27 |     for<'de> Box<V>: serde::Deserialize<'de>,
   |                      ^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `dothing`
   = note: the full name for the type has been written to '/playground/target/debug/deps/playground-dd3320ba655b84a9.long-type-390055455354675765.txt'
   = note: consider using `--verbose` to print the full type name to the console
```

</details>


The poisoning is fixed by explicitly invoking `dothing::<str>()`. It's the return type inference that's getting grossly misled here, and it's really unclear as to why.