Skip to content

Returning bare trait from an async function gives subpar suggestion #131661

@GrigorenkoPV

Description

@GrigorenkoPV

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: ecf2d1f
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 #127691 (comment)

The non-async case was fixed in #131239

Metadata

Metadata

Labels

A-async-awaitArea: Async & AwaitA-diagnosticsArea: Messages for errors, warnings, and lintsD-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.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