rust-lang / rust-lang/rust

Returning bare trait from an async function gives subpar suggestion

Open
#131,661 2 comments 0 reactions 1 assignee View on GitHub

@hirschenberger is already working on this.

Since Oct 14, 2024.

A-async-await A-diagnostics D-invalid-suggestion T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
trait Trait {}

async fn fun() -> Trait {
    todo!()
}

fn main() {}
Current output
error[E0782]: expected a type, found a trait
 --> src/main.rs:3:19
  |
3 | async fn fun() -> Trait {
  |                   ^^^^^
  |
help: you can add the `dyn` keyword if you want a trait object
  |
3 | async fn fun() -> dyn Trait {
  |                   +++
help: you might have meant to write a bound here
  |
1 | : Trait {
  | ~
Desired output
error[E0782]: expected a type, found a trait
 --> src/main.rs:3:19
  |
3 | async fn fun() -> Trait {
  |                   ^^^^^
  |
help: use `impl Trait` to return an opaque type, as long as you return a single underlying type
  |
3 | async fn fun() -> impl Trait {
  |                   ++++
help: alternatively, you can return an owned trait object
  |
3 | async fn fun() -> Box<dyn Trait> {
  |                   +++++++      +
Rationale and extra context

No response

Other cases

No response

Rust Version

rustc 1.83.0-dev
binary: rustc
commit-hash: ecf2d1fa4bd8166c696883b10f483122b1fe98a3
commit-date: 2024-10-13
host: x86_64-unknown-linux-gnu
release: 1.83.0-dev
LLVM version: 19.1.1

Anything else?

Originally reported in https://github.com/rust-lang/rust/issues/127691#issuecomment-2400169110

The non-async case was fixed in #131239

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.