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

Rust complains about mismatched types, but the types are identical #89923

Open
jimblandy opened this issue Oct 15, 2021 · 4 comments
Open

Rust complains about mismatched types, but the types are identical #89923

jimblandy opened this issue Oct 15, 2021 · 4 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@jimblandy
Copy link
Contributor

jimblandy commented Oct 15, 2021

When I compile the following code:

use std::future::Future;

pub async fn async_magic<F, R>(f: F) -> Vec<u8>
where
    F: FnOnce(&mut Vec<u8>) -> R,
    R: Future,
{
    todo!()
}

async fn push_zero(v: &mut Vec<u8>) {
    todo!()
}

pub async fn test_async_magic() {
    async_magic(push_zero).await;
}

async fn sleep() {}

I get the error message:

error[E0308]: mismatched types
  --> src/lib.rs:16:5
   |
16 |     async_magic(push_zero).await;
   |     ^^^^^^^^^^^ lifetime mismatch
   |
   = note: expected associated type `<for<'_> fn(&mut Vec<u8>) -> impl for<'_> Future {push_zero} as FnOnce<(&mut Vec<u8>,)>>::Output`
              found associated type `<for<'_> fn(&mut Vec<u8>) -> impl for<'_> Future {push_zero} as FnOnce<(&mut Vec<u8>,)>>::Output`
   = note: the required lifetime does not necessarily outlive the empty lifetime
note: the lifetime requirement is introduced here
  --> src/lib.rs:5:32
   |
5  |     F: FnOnce(&mut Vec<u8>) -> R,
   |                                ^

I'm sure the 'expected' and 'found' types are indeed different somehow, but there's nothing in the output that would allow us to distinguish them.

EDIT: Output from rustc -vV:

binary: rustc
commit-hash: c8dfcfe046a7680554bf4eb612bad840e7631c4b
commit-date: 2021-09-06
host: x86_64-unknown-linux-gnu
release: 1.55.0
LLVM version: 12.0.1
@jimblandy jimblandy 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 Oct 15, 2021
@klensy
Copy link
Contributor

klensy commented Oct 15, 2021

rustc -vV ?

@Alexendoo Alexendoo added the D-confusing Diagnostics: Confusing error or lint that should be reworked. label Oct 15, 2021
@mbartlett21
Copy link
Contributor

The difference is that it is expecting R: Future not to be related to the lifetime of the &mut Vec<u8>, while it is. There doesn't seem to be any way to actually state that in bounds :(.

@estebank
Copy link
Contributor

estebank commented Jan 5, 2023

Triage: output hasn't changed

error[E0308]: mismatched types
  --> f.rs:16:5
   |
16 |     async_magic(push_zero).await;
   |     ^^^^^^^^^^^^^^^^^^^^^^ one type is more general than the other
   |
   = note: expected trait `for<'a> <for<'a> fn(&'a mut Vec<u8>) -> impl Future<Output = ()> {push_zero} as FnOnce<(&'a mut Vec<u8>,)>>`
              found trait `for<'a> <for<'a> fn(&'a mut Vec<u8>) -> impl Future<Output = ()> {push_zero} as FnOnce<(&'a mut Vec<u8>,)>>`
note: the lifetime requirement is introduced here
  --> f.rs:5:32
   |
5  |     F: FnOnce(&mut Vec<u8>) -> R,
   |                                ^

Running with -Zverbose gives us a clue of what's happening

error[E0308]: mismatched types
  --> f.rs:16:5
   |
16 |     async_magic(push_zero).await;
   |     ^^^^^^^^^^^^^^^^^^^^^^ one type is more general than the other
   |
   = note: expected trait `FnOnce<(&RePlaceholder(Placeholder { universe: U1, name: BrNamed(DefId(0:14 ~ f[29b3]::async_magic::'_), '_) }) mut Vec<u8>,)>`
              found trait `for<Region(BrNamed(DefId(0:16 ~ f[29b3]::push_zero::'_), '_))> FnOnce<(&ReLateBound(DebruijnIndex(0), BoundRegion { var: 0, kind: BrNamed(DefId(0:16 ~ f[29b3]::push_zero::'_), '_) }) mut Vec<u8>,)>`
note: the lifetime requirement is introduced here
  --> f.rs:5:32
   |
5  |     F: FnOnce(&mut Vec<u8>) -> R,
   |                                ^

@estebank
Copy link
Contributor

Current output:

error[E0308]: mismatched types
  --> src/lib.rs:16:5
   |
16 |     async_magic(push_zero).await;
   |     ^^^^^^^^^^^^^^^^^^^^^^ one type is more general than the other
   |
   = note: expected opaque type `impl for<'a> Future<Output = ()>`
              found opaque type `impl Future<Output = ()>`
   = help: consider `await`ing on both `Future`s
   = note: distinct uses of `impl Trait` result in different opaque types
note: the lifetime requirement is introduced here
  --> src/lib.rs:5:32
   |
5  |     F: FnOnce(&mut Vec<u8>) -> R,
   |                                ^

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 D-confusing Diagnostics: Confusing error or lint that should be reworked. 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

5 participants