Skip to content

Wrong suggestion for Boxxing body of async closure when expected return type of closure to be Box #111011

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

Closed
fee1-dead opened this issue Apr 30, 2023 · 4 comments · Fixed by #111056
Assignees
Labels
A-async-closures `async || {}` 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

@fee1-dead
Copy link
Member

fee1-dead commented Apr 30, 2023

Code

playground

#![feature(async_closure)]

fn uwu<X>(x: impl FnOnce() -> Box<X>) {}

fn main() {
    uwu(async move || {})
}

Current output

Compiling playground v0.0.1 (/playground)
error[E0308]: mismatched types
 --> src/main.rs:6:23
  |
6 |     uwu(async move || {})
  |                       ^^ expected `Box<_>`, found `async` closure body
  |
  = note:            expected struct `Box<_>`
          found `async` closure body `[async closure body@src/main.rs:6:23: 6:25]`
  = note: for more on the distinction between the stack and the heap, read https://doc.rust-lang.org/book/ch15-01-box.html, https://doc.rust-lang.org/rust-by-example/std/box.html, and https://doc.rust-lang.org/std/boxed/index.html
help: store this in the heap by calling `Box::new`
  |
6 |     uwu(async move || Box::new({}))
  |                       +++++++++  +

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` (bin "playground") due to previous error

Desired output

Compiling playground v0.0.1 (/playground)
error[E0308]: mismatched types
 --> src/main.rs:6:23
  |
6 |     uwu(async move || {})
  |                       ^^ expected `Box<_>`, found `async` closure body
  |
  = note:            expected struct `Box<_>`
          found `async` closure body `[async closure body@src/main.rs:6:23: 6:25]`
  = note: for more on the distinction between the stack and the heap, read https://doc.rust-lang.org/book/ch15-01-box.html, https://doc.rust-lang.org/rust-by-example/std/box.html, and https://doc.rust-lang.org/std/boxed/index.html

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` (bin "playground") due to previous error

Rationale and extra context

No response

Other cases

No response

Anything else?

No response

@fee1-dead fee1-dead 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. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. A-async-closures `async || {}` labels Apr 30, 2023
@JohnBobbo96
Copy link
Contributor

Would this be better? This will fix the problem by returning a box with a unit type.

Compiling playground v0.0.1 (/playground)
error[E0308]: mismatched types
 --> src/main.rs:6:23
  |
6 |     uwu(async move || {})
  |                       ^^ expected `Box<_>`, found `async` closure body
  |
  = note:            expected struct `Box<_>`
          found `async` closure body `[async closure body@src/main.rs:6:23: 6:25]`
  = note: for more on the distinction between the stack and the heap, read https://doc.rust-lang.org/book/ch15-01-box.html, https://doc.rust-lang.org/rust-by-example/std/box.html, and https://doc.rust-lang.org/std/boxed/index.html
help: store this in the heap by calling `Box::new`
  |
6 |     uwu(async move || Box::new(()))
  |                       ++++++++++++

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` (bin "playground") due to previous error

This also doesn't give any suggestion for regular blocks/closures: playground link

Compiling playground v0.0.1 (/playground)
error[[E0308]](https://doc.rust-lang.org/nightly/error_codes/E0308.html): mismatched types
 --> src/main.rs:5:9
  |
5 |     foo({});
  |         ^^ expected `Box<_>`, found `()`
  |
  = note: expected struct `Box<_>`
          found unit type `()`

error[[E0308]](https://doc.rust-lang.org/nightly/error_codes/E0308.html): mismatched types
 --> src/main.rs:6:12
  |
6 |     bar(|| {});
  |            ^^ expected `Box<_>`, found `()`
  |
  = note: expected struct `Box<_>`
          found unit type `()`

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` (bin "playground") due to 2 previous errors

@JohnBobbo96
Copy link
Contributor

im going to try and fix this

@rustbot claim

@JohnBobbo96
Copy link
Contributor

Would this be better? This will fix the problem by returning a box with a unit type.

Actually this still doesn't work for async closures, weird.

@fee1-dead
Copy link
Member Author

Async closures return futures, not boxes. The suggestion is simply wrong and there is no way to actually call uwu with an async closure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-async-closures `async || {}` 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.

2 participants