Skip to content

implementation of FnOnce is not general enough regressed into closure/generator type that references itself #105396

Closed
@cynecx

Description

@cynecx

Code

I tried this code:

use core::future::Future;

trait AsyncFn<I, R>: FnMut(I) -> Self::Fut {
    type Fut: Future<Output = R>;
}

impl<F, I, R, Fut> AsyncFn<I, R> for F
where
    Fut: Future<Output = R>,
    F: FnMut(I) -> Fut,
{
    type Fut = Fut;
}

async fn call<C, R, Fn>(mut ctx: C, mut f: Fn) -> Result<R, ()>
where
    Fn: for<'a> AsyncFn<&'a mut C, Result<R, ()>>,
{
    loop {
        match f(&mut ctx).await {
            Ok(val) => return Ok(val),
            Err(_) => continue,
        }
    }
}

fn test(ctx: &mut usize) {
    call(ctx, |ctx| async move {
        let _ctx = ctx;
        Ok(1usize)
    });
}

I expected to see this happen: rustc produces implementation of `FnOnce` is not general enough

Instead, this happened: rustc produces closure/generator type that references itself

Version it worked on

1.65.0

Version with regression

1.67.0-nightly (2022-12-05 e1d8195)

@rustbot modify labels: +regression-from-stable-to-nightly -regression-untriaged

Metadata

Metadata

Labels

C-bugCategory: This is a bug.P-lowLow priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-nightlyPerformance or correctness regression from stable to nightly.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions