Skip to content

Implementation of trait is not general enough when boxing future returning <T as Foo<'a>>::Foo #92415

Open
Listed in
@ibraheemdev

Description

@ibraheemdev
Member

Reproduction:

use std::any::Any;
use std::future::Future;

trait Foo<'a>: Sized {
    type Error;
    fn foo(x: &'a str) -> Result<Self, Self::Error>;
}

impl<'a> Foo<'a> for &'a str {
    type Error = ();

    fn foo(x: &'a str) -> Result<Self, Self::Error> {
        Ok(x)
    }
}

async fn get_foo<'a, T>(x: &'a str) -> Result<T, <T as Foo<'a>>::Error>
where
    T: Foo<'a>,
{
    Foo::foo(x)
}

fn bar<'a>(x: &'a str) -> Box<dyn Future<Output = Result<&'a str, ()>> + Send + 'a> {
    Box::new(async move { get_foo(x).await })
}

Output on stable and nightly:

error: implementation of `Foo` is not general enough
  --> src/lib.rs:25:5
   |
25 |     Box::new(async move { get_foo(x).await })
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ implementation of `Foo` is not general enough
   |
   = note: `Foo<'1>` would have to be implemented for the type `&'0 str`, for any two lifetimes `'0` and `'1`...
   = note: ...but `Foo<'2>` is actually implemented for the type `&'2 str`, for some specific lifetime `'2

Interestingly, the code compiles without the + Send. It also compiles without the async move block. This may be related to #61949.

@rustbot label +A-async-await

Activity

compiler-errors

compiler-errors commented on Jan 1, 2022

@compiler-errors
Member

This is actually probably related to #60658 (and other issues) instead of #61949, and seems to be fixed under #92449.

eholk

eholk commented on Jan 10, 2022

@eholk
Contributor

This does sound similar to several other issues. Glad to see #92449 seems to fix it.

@rustbot label +AsyncAwait-Triaged +AsyncAwait-Polish

added
AsyncAwait-PolishAsync-await issues that are part of the "polish" area
AsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.
on Jan 10, 2022
vincenzopalazzo

vincenzopalazzo commented on Sep 23, 2023

@vincenzopalazzo
Member

@rustbot claim

OK this needs to be fixed in some way, I will try to see what it is going on

vincenzopalazzo

vincenzopalazzo commented on Sep 23, 2023

@vincenzopalazzo
Member

Ah ok this looks like a well-known issue with lifetimes, ok I think this is related to #110338 (comment)

Putting this in our board btw

removed their assignment
on Sep 24, 2023
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-async-awaitArea: Async & AwaitAsyncAwait-PolishAsync-await issues that are part of the "polish" areaAsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.C-bugCategory: This is a bug.

    Type

    No type

    Projects

    Status

    Claimed

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @eholk@compiler-errors@vincenzopalazzo@ibraheemdev@rustbot

        Issue actions

          Implementation of trait is not general enough when boxing future returning `<T as Foo<'a>>::Foo` · Issue #92415 · rust-lang/rust