Skip to content

"one type is more general than the other" with async, generics, Send, and lifetime bounds #90149

@duarten

Description

@duarten

Consider the following program:

impl<T: std::future::Future> FutureExt for T {}

pub trait FutureExt: Sized {
    fn with_props<I: IntoIterator<Item = &'static str>>(self, props: I) -> FutureWithProps<I> {
        FutureWithProps { props }
    }
}

pub struct FutureWithProps<I: IntoIterator<Item = &'static str>> {
    props: I,
}

impl<I: IntoIterator<Item = &'static str>> std::future::Future for FutureWithProps<I> {
    type Output = ();
    fn poll(self: std::pin::Pin<&mut Self>, _: &mut std::task::Context<'_>) -> std::task::Poll<Self::Output> {
        std::task::Poll::Pending
    }
}

unsafe impl<I: IntoIterator<Item = &'static str> + Send> Send for FutureWithProps<I> {}

pub async fn foo() -> Box<dyn std::future::Future<Output = ()> + Send> {
    Box::new(async move { async {}.with_props(vec!["foo"]).await })
}

This results in the following compiler error, using version 1.56.0:

error[E0308]: mismatched types
  --> src/main.rs:33:9
   |
33 |         Box::pin(async move { async {}.with_properties(vec!["foo"]).await })
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ one type is more general than the other
   |
   = note: expected reference `&str`
              found reference `&'static str`

The code compiles fine with any of the following modifications:

  • Changing &'static str into a String; or
  • Removing the type parameter I and using Vec<&'static str>; or
  • Removing the Send impl for FutureWithProps; or
  • Removing the Send bound in foo().

Not sure if this is a known issue, but it seems to happen with this particular mix of features.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lifetimesArea: Lifetimes / regionsC-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