rust-lang / rust-lang/rust

impl_trait_in_fn_trait_return / impl_trait_in_assoc_type doesn't resolve future correctly

Open
#122,458 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-inference C-bug F-impl_trait_in_assoc_type F-impl_trait_in_fn_trait_return T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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 https://github.com/rust-lang/rust/issues/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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by compiling the reproduction in src/main.rs on a nightly compiler, comparing the behavior with and without the o.run call. Read the interaction between impl_trait_in_fn_trait_return, impl_trait_in_assoc_type, and the related issue #106527; done means the shown call accepts the async closure without the E0308 mismatch.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.