Repository: rust
Issue: regression on nightly-2024-03-16
Labels: P-medium,T-compiler,C-bug,A-coroutines,F-coroutines,regression-untriaged,S-has-mcve,F-impl_trait_in_assoc_type

<!--
Thank you for filing a regression report! 🐛 A regression is something that changed between versions of Rust but was not supposed to.

Please provide a short summary of the regression, along with any information you feel is relevant to replicate it.
-->

### Code

I tried this code:

```toml

[dependencies]
futures = "0.3.30"
futures-async-stream = "0.2.10"

```

```rust
#![feature(coroutines)]
#![feature(impl_trait_in_assoc_type)]
use futures::Stream;
use futures_async_stream::try_stream;
use std::future::Future;
pub trait IterItem: Send + 'static {
    type ItemRef<'a>: Send + 'a;
}

type StorageError = ();
type StorageResult<T> = Result<T, StorageError>;

pub trait StateStoreIter<T: IterItem>: Send {
    fn try_next(
        &mut self,
    ) -> impl Future<Output = StorageResult<Option<T::ItemRef<'_>>>> + Send + '_;
}

#[try_stream(ok = O, error = StorageError)]
async fn into_stream_inner<
    T: IterItem,
    I: StateStoreIter<T>,
    O: Send,
    F: for<'a> Fn(T::ItemRef<'a>) -> StorageResult<O> + Send,
>(
    mut iter: I,
    f: F,
) {
    while let Some(item) = iter.try_next().await? {
        yield f(item)?;
    }
}

pub trait StateStoreIterExt<T: IterItem>: StateStoreIter<T> + Sized {
    type ItemStream<O: Send, F: Send>: Stream<Item = StorageResult<O>> + Send;

    fn into_stream<O: Send, F: for<'a> Fn(T::ItemRef<'a>) -> StorageResult<O> + Send>(
        self,
        f: F,
    ) -> Self::ItemStream<O, F>;
}

impl<T: IterItem, I: StateStoreIter<T>> StateStoreIterExt<T> for I {
    type ItemStream<O: Send, F: Send> = impl Stream<Item = StorageResult<O>> + Send;

    fn into_stream<O: Send, F: for<'a> Fn(T::ItemRef<'a>) -> StorageResult<O> + Send>(
        self,
        f: F,
    ) -> Self::ItemStream<O, F> {
        into_stream_inner(self, f)
    }
}

fn main() {
    println!("Hello, world!");
}

```


### Version it worked on

It compiles on `nightly-2024-03-14`

### Version with regression

ICE on `nightly-2024-03-15` https://github.com/rust-lang/rust/issues/122508

Compile error on `nightly-2024-03-16`:

```
error[E0277]: expected a `Fn(<T as IterItem>::ItemRef<'a>)` closure, found `F`
  --> src/main.rs:50:9
   |
50 |         into_stream_inner(self, f)
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^ expected an `Fn(<T as IterItem>::ItemRef<'a>)` closure, found `F`
   |
note: required by a bound in `into_stream_inner`
  --> src/main.rs:24:8
   |
20 | async fn into_stream_inner<
   |          ----------------- required by a bound in this function
...
24 |     F: for<'a> Fn(T::ItemRef<'a>) -> StorageResult<O> + Send,
   |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `into_stream_inner`
help: consider further restricting this bound
   |
44 |     type ItemStream<O: Send, F: Send + for<'a> std::ops::Fn<(<T as IterItem>::ItemRef<'a>,)>> = impl Stream<Item = StorageResult<O>> + Send;
   |                                      +++++++++++++++++++++++++++++++++++++++++++++++++++++++

```