Skip to content

higher-ranked lifetime error #117140

Open
Open
@4r33x

Description

This is boiled down reproducible example.

I tried this code:

use std::future::Future;
pub struct Test<T, U, R, F>
where
    U: Future<Output = Option<R>>,
    F: Fn(&mut T) -> U,
{
    ty: T,
    fun: F 
}

impl<T, U, R, F> Test<T, U, R, F>
where
    U: Future<Output = Option<R>>,
    F: Fn(&mut T) -> U, 
{
    fn new(ty: T, fun: F) -> Self {
        Self { ty, fun }
    }
}
struct Ty {
    v: Vec<u32>,
}
impl Ty {
    async fn recv(&mut self) -> Option<u32> {
        self.v.pop()
    }
}

pub async fn run() {
    let ty = Ty { v: vec![] };

    let mut test = Test::new(ty, Ty::recv);
    while let Some(_v) = (test.fun)(&mut test.ty).await {}
}

Compiler throws errors:

error[E0308]: mismatched types
  --> crates\render\src\regui.rs:68:20
   |
68 |     let mut test = Test::new(ty, Ty::recv);
   |                    ^^^^^^^^^ one type is more general than the other
   |
   = note: expected opaque type `impl for<'a> Future<Output = Option<u32>>`
              found opaque type `impl Future<Output = Option<u32>>`
   = help: consider `await`ing on both `Future`s
   = note: distinct uses of `impl Trait` result in different opaque types

error: higher-ranked lifetime error
  --> crates\render\src\regui.rs:68:20
   |
68 |     let mut test = Test::new(ty, Ty::recv);
   |                    ^^^^^^^^^^^^^^^^^^^^^^^

error: higher-ranked lifetime error
  --> crates\render\src\regui.rs:68:20
   |
68 |     let mut test = Test::new(ty, Ty::recv);
   |                    ^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: could not prove `Test<Ty, impl Future<Output = Option<u32>>, u32, for<'a> fn(&'a mut Ty) -> impl Future<Output = Option<u32>> {Ty::recv}> well-formed`

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-higher-rankedArea: Higher-ranked things (e.g., lifetimes, types, trait bounds aka HRTBs)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