rust-lang / rust-lang/rust

Universe errors involving an opaque type results in hallucinations and unused bound vars

Open
#124,472 8 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-async-await A-diagnostics A-impl-trait A-lifetimes AsyncAwait-Triaged D-confusing fixed-by-async-closures T-compiler T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code (Rust ≥2018)
use std::future::Future;

fn main() {
    consume_async(async_fn);
}

async fn async_fn(_: &u32) {}

fn consume_async<F>(_: impl FnOnce(&u32) -> F) where F: Future<Output = ()> {}
Current output
error[E0308]: mismatched types
 --> ./t.rs:4:5
  |
4 |     consume_async(async_fn);
  |     ^^^^^^^^^^^^^^^^^^^^^^^ 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
 --> ./t.rs:9:45
  |
9 | fn consume_async<F>(_: impl FnOnce(&u32) -> F) where F: Future<Output = ()> {}
  |                                             ^
Desired output
error[E0308]: mismatched types
 --> ./t.rs:4:5
  |
4 |     consume_async(async_fn);
  |     ^^^^^^^^^^^^^^^^^^^^^^^ one type is more general than the other
  |
  = note: expected `_`
             found some type `for<'a> T<'a>` which implements `Future<Output = ()>`
note: the lifetime requirement is introduced here
 --> ./t.rs:9:45
  |
9 | fn consume_async<F>(_: impl FnOnce(&u32) -> F) where F: Future<Output = ()> {}
  |                                             ^
Rationale and extra context

It's very confusing to see a second opaque type and suggestions "to await both futures" (???, there is only one). Ideally we would properly note that the generic can't name the lifetime or something.

Other cases

No response

Rust Version
rustc 1.79.0-nightly (ef8b9dcf2 2024-04-24)
binary: rustc
commit-hash: ef8b9dcf23700f2e2265317611460d3a65c19eff
commit-date: 2024-04-24
host: x86_64-unknown-linux-gnu
release: 1.79.0-nightly
LLVM version: 18.1.4
Anything else?

#124471 has a simiular test case/issue but without the opaque types.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the Rust ≥2018 example with the reported rustc 1.79.0-nightly version and compare the current and desired diagnostics. Investigate the compiler diagnostic path for opaque types and async futures; done means the misleading second opaque type and “awaiting both Futures” suggestion are removed while the lifetime requirement remains explained.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.