Skip to content

impl_trait_in_fn_trait_return / impl_trait_in_assoc_type doesn't resolve future correctly #122458

@TheDan64

Description

@TheDan64
Contributor

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

Activity

added
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Mar 13, 2024
added
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Mar 13, 2024
compiler-errors

compiler-errors commented on Mar 15, 2024

@compiler-errors
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 😅

removed
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Mar 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-inferenceArea: Type inferenceC-bugCategory: 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-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @compiler-errors@TheDan64@jieyouxu@rustbot

        Issue actions

          impl_trait_in_fn_trait_return / impl_trait_in_assoc_type doesn't resolve future correctly · Issue #122458 · rust-lang/rust