Skip to content

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

Open
@jplatte

Description

@jplatte

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 #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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-async-awaitArea: Async & AwaitA-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.A-trait-systemArea: Trait systemC-bugCategory: This is a bug.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

    Issue actions