Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Confusing HRTB mismatching diagnostic message #104484

Closed
ethe opened this issue Nov 16, 2022 · 2 comments
Closed

Confusing HRTB mismatching diagnostic message #104484

ethe opened this issue Nov 16, 2022 · 2 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ethe
Copy link

ethe commented Nov 16, 2022

Given the following code:

use std::future::Future;

async fn foo<F, R>(f: F) -> bool
where
    F: Fn(&u8) -> R,
    R: Future<Output = bool>,
{
    todo!()
}

async fn bar() {
    foo(fut).await;
}

async fn fut(param: &u8) -> bool {
    todo!()
}

The current output is:

Standard Error
   Compiling playground v0.0.1 (/playground)
error[[E0308]](https://doc.rust-lang.org/stable/error-index.html#E0308): mismatched types
  --> src/lib.rs:12:5
   |
12 |     foo(fut).await;
   |     ^^^^^^^^ one type is more general than the other
   |
   = note: expected trait `for<'r> <for<'r> fn(&'r u8) -> impl for<'r> Future<Output = bool> {fut} as FnOnce<(&'r u8,)>>`
              found trait `for<'r> <for<'r> fn(&'r u8) -> impl for<'r> Future<Output = bool> {fut} as FnOnce<(&'r u8,)>>`
note: the lifetime requirement is introduced here
  --> src/lib.rs:5:19
   |
5  |     F: Fn(&u8) -> R,
   |                   ^

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` due to previous error

the note be provided by rustc makes me confused:

for<'r> <for<'r> fn(&'r u8) -> impl for<'r> Future<Output = bool> {fut} as FnOnce<(&'r u8,)>>
!=
for<'r> <for<'r> fn(&'r u8) -> impl for<'r> Future<Output = bool> {fut} as FnOnce<(&'r u8,)>>
@ethe ethe added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 16, 2022
@ethe ethe changed the title Confusing HRTB diagnostic message Confusing HRTB mismatching diagnostic message Nov 16, 2022
@lukas-code
Copy link
Member

This is #74497 / #102400 / #104285

The fix looks like this:

use std::future::Future;

async fn foo<'a, F, R>(f: F) -> bool
where
    F: Fn(&'a u8) -> R, // <- note explicit lifetime here
    R: Future<Output = bool>,
{
    todo!()
}

async fn bar() {
    foo(fut).await;
}

async fn fut(param: &u8) -> bool {
    todo!()
}

@ethe
Copy link
Author

ethe commented Nov 17, 2022

Thanks @lukas-code , explicit lifetime parameter is a practical walkaround, however I am looking to the reason of mismatching, issues you mention is really helpful to me.

@ethe ethe closed this as completed Nov 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants