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

impl_trait_in_fn_trait_return / impl_trait_in_assoc_type doesn't resolve future correctly #122458

Open
TheDan64 opened this issue Mar 13, 2024 · 1 comment
Labels
A-inference Area: Type inference C-bug Category: This is a bug. F-impl_trait_in_assoc_type `#![feature(impl_trait_in_assoc_type)]` F-impl_trait_in_fn_trait_return `#![feature(impl_trait_in_fn_trait_return)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@TheDan64
Copy link
Contributor

TheDan64 commented Mar 13, 2024

I tried this code:

#![feature(impl_trait_in_fn_trait_return)]
#![feature(impl_trait_in_assoc_type)]

use std::future::Future;

trait Kind {
    type Func<I, R>;
    
    fn stub<I, R>() -> Self::Func<I, R>;
}

struct A;

impl Kind for A {
    type Func<I, R> = Box<dyn Fn(I) -> impl Future<Output = R>>;
    
    fn stub<I, R>() -> Self::Func<I, R> {
        Box::new(|_| async { unimplemented!() })
    }
}

struct Owner<K: Kind> {
    k: K,
}

impl<K: Kind> Owner<K> {
    fn run<I, R>(&self, _func: K::Func<I, R>) {
        // do stuff with func
    }
}

fn main() {
    let o = Owner { k: A };
    o.run(Box::new(|_| async {
        // Do stuff
    }));
}

This almost compiles. If you remove the o.run call it otherwise compiles. It seems that rust isn't able to determine that the async block impls the Future trait in main (but does work in the stub!). Think it's related to #106527. cc @compiler-errors

error[E0308]: mismatched types
  --> src/main.rs:34:24
   |
15 |       type Func<I, R> = Box<dyn Fn(I) -> impl Future<Output = R>>;
   |                                          ----------------------- the expected future
...
34 |       o.run(Box::new(|_| async {
   |  ________________________^
35 | |         // Do stuff
36 | |     }));
   | |_____^ expected future, found `async` block
   |
   = note: expected opaque type `<A as Kind>::Func<_, _>::{opaque#0}`
            found `async` block `{async block@src/main.rs:34:24: 36:6}`

Meta

rustc --version --verbose:

rustc 1.78.0-nightly (3b1717c05 2024-03-10)
binary: rustc
commit-hash: 3b1717c052de4a2dbdd3badb0e7a885f40a8ad9e
commit-date: 2024-03-10
host: x86_64-unknown-linux-gnu
release: 1.78.0-nightly
LLVM version: 18.1.0
@TheDan64 TheDan64 added the C-bug Category: This is a bug. label Mar 13, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Mar 13, 2024
@jieyouxu jieyouxu added F-impl_trait_in_fn_trait_return `#![feature(impl_trait_in_fn_trait_return)]` F-impl_trait_in_assoc_type `#![feature(impl_trait_in_assoc_type)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-inference Area: Type inference labels Mar 13, 2024
@compiler-errors
Copy link
Member

This doesn't have to do with #106527. This has to do with the fact that impl_trait_in_fn_trait_return is utterly broken -- I recommend not using it 😅

@jieyouxu jieyouxu removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Mar 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-inference Area: Type inference C-bug Category: This is a bug. F-impl_trait_in_assoc_type `#![feature(impl_trait_in_assoc_type)]` F-impl_trait_in_fn_trait_return `#![feature(impl_trait_in_fn_trait_return)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants