Skip to content

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

New issue

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

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

Already on GitHub? # to your account

Open
GrigorenkoPV opened this issue Oct 13, 2024 · 2 comments · May be fixed by #131882
Open

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

GrigorenkoPV opened this issue Oct 13, 2024 · 2 comments · May be fixed by #131882
Assignees
Labels
A-async-await Area: Async & Await A-diagnostics Area: Messages for errors, warnings, and lints D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@GrigorenkoPV
Copy link
Contributor

GrigorenkoPV commented Oct 13, 2024

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

@GrigorenkoPV GrigorenkoPV 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 13, 2024
@VulnBandit
Copy link
Contributor

VulnBandit commented Oct 14, 2024

I don't know if it has been reported, but adding dyn to this example gives bad diagnostics too (only in async):

error[E0746]: return type cannot have an unboxed trait object
 --> src/main.rs:3:29
  |
3 |   async fn fun() -> dyn Trait {
  |  _____________________________^
4 | |     todo!()
5 | | }
  | |_^ doesn't have a size known at compile-time
  |
help: consider returning an `impl Trait` instead of a `dyn Trait`
  |
3 | async fn fun() -> dyn Trait impl {
  |                             ++++
help: alternatively, box the return type, and wrap all of the returned values in `Box::new`
  |
3 ~ async fn fun() -> dyn Trait Box<dyn {
4 |     todo!()
5 ~ }>
  |

@hirschenberger
Copy link
Contributor

@rustbot claim

@fmease fmease added A-async-await Area: Async & Await D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Oct 16, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-async-await Area: Async & Await A-diagnostics Area: Messages for errors, warnings, and lints D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants