Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misleading/incorrect? closure/generator type that references itself error #105401

Closed
cynecx opened this issue Dec 6, 2022 · 1 comment · Fixed by #105409
Closed

Misleading/incorrect? closure/generator type that references itself error #105401

cynecx opened this issue Dec 6, 2022 · 1 comment · Fixed by #105409
Assignees
Labels
A-closures Area: closures (`|args| { .. }`) C-bug Category: This is a bug. F-type_alias_impl_trait `#[feature(type_alias_impl_trait)]` requires-nightly This issue requires a nightly compiler in some way.

Comments

@cynecx
Copy link
Contributor

cynecx commented Dec 6, 2022

I tried this code:

#![feature(type_alias_impl_trait)]
#![feature(closure_lifetime_binder)]

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, F>(mut ctx: C, mut f: F) -> Result<R, ()>
where
    F: for<'a> AsyncFn<&'a mut C, Result<R, ()>>
{
    loop {
        match f(&mut ctx).await {
            Ok(val) => return Ok(val),
            Err(_) => continue,
        }
    }
}

trait Cap<'a> {}
impl<T> Cap<'_> for T {}

fn works(ctx: &mut usize) {
    let mut inner = 0;
    
    type Ret<'a, 'b: 'a> = impl Future<Output = Result<usize, ()>> + 'a + Cap<'b>;
    
    let callback = for<'a, 'b> |c: &'a mut &'b mut usize| -> Ret<'a, 'b> {
        inner += 1;
        async move { let _c = c; Ok(1usize) }
    };
    call(ctx, callback);
}

fn doesnt_work_but_should(ctx: &mut usize) {
    let mut inner = 0;
    
    type Ret<'a, 'b: 'a> = impl Future<Output = Result<usize, ()>> + 'a + Cap<'b>;
    
    call(ctx, for<'a, 'b> |c: &'a mut &'b mut usize| -> Ret<'a, 'b> {
        inner += 1;
        async move { let _c = c; Ok(1usize) }
    });
}

I expected to see this happen: I believe this should compile just fine but I am not 100% sure.

Instead, this happened: rustc produces the following:

Compiling playground v0.0.1 (/playground)
error[[E0644]](https://doc.rust-lang.org/nightly/error-index.html#E0644): closure/generator type that references itself
  --> src/lib.rs:50:15
   |
50 |       call(ctx, for<'a, 'b> |c: &'a mut &'b mut usize| -> Ret<'a, 'b> {
   |  _______________^
51 | |         inner += 1;
52 | |         async move { let _c = c; Ok(1usize) }
53 | |     });
   | |_____^ cyclic type of infinite size
   |
   = note: closures cannot capture themselves or take themselves as argument;
           this error may be the result of a recent compiler bug-fix,
           see issue #46062 <https://github.com/rust-lang/rust/issues/46062>
           for more information

For more information about this error, try `rustc --explain E0644`.
error: could not compile `playground` due to previous error

Permalink to the playground

rustc --version --verbose:

1.67.0-nightly (2022-12-05 e1d819583f0bf13b016b)
@cynecx cynecx added the C-bug Category: This is a bug. label Dec 6, 2022
@cynecx
Copy link
Contributor Author

cynecx commented Dec 6, 2022

This works in nightly-2022-11-04 and starts to fail in nightly-2022-11-05.

This might be #101834 (cc @compiler-errors, @lcnr), because a similar issue was found over #105396 that also produces the same error message.

@compiler-errors compiler-errors added A-closures Area: closures (`|args| { .. }`) F-type_alias_impl_trait `#[feature(type_alias_impl_trait)]` requires-nightly This issue requires a nightly compiler in some way. labels Dec 6, 2022
@compiler-errors compiler-errors self-assigned this Dec 7, 2022
@bors bors closed this as completed in 03b9e1d Jan 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-closures Area: closures (`|args| { .. }`) C-bug Category: This is a bug. F-type_alias_impl_trait `#[feature(type_alias_impl_trait)]` requires-nightly This issue requires a nightly compiler in some way.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants