rust-lang / rust-lang/rust

Bogus "implementation of `<whatever trait you want>` is not general enough" with RPITIT + async

Open
#130,113 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-async-await A-impl-trait A-trait-system C-bug fixed-by-higher-ranked-assumptions T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

use std::future::Future;

fn bug() {
    let call_me = Wrap(CallMeImpl { value: "test" });

    assert_send(async {
        call_me.call().await;
    });
}

pub fn assert_send<F>(_future: F)
where
    F: Future + Send,
{
}

pub trait CallMe {
    fn call(&self) -> impl Future<Output = ()> + Send;
}

struct Wrap<T>(T);

impl<S> CallMe for Wrap<S>
where
    S: CallMe + Send,
{
    // adding `+ Send` to this RPIT fixes the issue
    fn call(&self) -> impl Future<Output = ()> {
        self.0.call()
    }
}

#[derive(Debug, Clone, Copy)]
pub struct CallMeImpl<T> {
    value: T,
}

impl<T> CallMe for CallMeImpl<T>
where
    // Can replace `Send` by `ToString`, `Clone`, whatever. When removing the
    // `Send` bound, the compiler produces a higher-ranked lifetime error.
    T: Send + 'static,
{
    fn call(&self) -> impl Future<Output = ()> {
        async {}
    }
}

I expected to see this happen: Compile successfully

Instead, this happened:

error: implementation of `Send` is not general enough
 --> src/lib.rs:6:5
  |
6 | /     assert_send(async {
7 | |         call_me.call().await;
8 | |     });
  | |______^ implementation of `Send` is not general enough
  |
  = note: `Send` would have to be implemented for the type `&'0 str`, for any lifetime `'0`...
  = note: ...but `Send` is actually implemented for the type `&'1 str`, for some specific lifetime `'1`
Meta

rustc --version --verbose:

rustc 1.83.0-nightly (9c01301c5 2024-09-05)
binary: rustc
commit-hash: 9c01301c52df5d2d7b6fe337707a74e011d68d6f
commit-date: 2024-09-05
host: x86_64-unknown-linux-gnu
release: 1.83.0-nightly
LLVM version: 19.1.0

happens with stable too.

I found a lot of similar-looking issues in https://github.com/rust-lang/rust/issues/110338, but none quite like this one, so I chose to report a new one instead. Maybe I overlooked an existing bug report though, sorry if I did!

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 provided reproducer from src/lib.rs with the reported rustc 1.83.0-nightly version and confirm the higher-ranked lifetime diagnostic. Read the related cases in issue #110338 and investigate the RPITIT plus async handling; done means the reproducer compiles without the bogus “implementation of Send is not general enough” error.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.