Skip to content

rustc suggests to use impl trait in closure parameter which doesn't work #138932

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

Closed
pacak opened this issue Mar 25, 2025 · 1 comment · Fixed by #138998
Closed

rustc suggests to use impl trait in closure parameter which doesn't work #138932

pacak opened this issue Mar 25, 2025 · 1 comment · Fixed by #138998
Assignees
Labels
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.

Comments

@pacak
Copy link
Contributor

pacak commented Mar 25, 2025

Code

let a = |x: dyn Fn()| x();

Current output

--> src/lib.rs:14:17
   |
14 |     let a = |x: dyn Fn()| x();
   |                 ^^^^^^^^ doesn't have a size known at compile-time
   |
   = help: the trait `Sized` is not implemented for `(dyn Fn() + 'static)`
help: you can use `impl Trait` as the argument type
   |
14 |     let a = |x: impl Fn()| x();
   |                 ~~~~
help: function arguments must have a statically known size, borrowed types always have a known size
   |
14 |     let a = |x: &dyn Fn()| x();
   |                 +

Desired output

help: function arguments must have a statically known size, borrowed types always have a known size
   |
14 |     let a = |x: &dyn Fn()| x();
   |                 +

Rationale and extra context

We start with some obviously invalid code, rustc makes two suggestions - pass &dyn Trait by reference - this works and use impl Trait which doesn't work.

error[E0562]: `impl Trait` is not allowed in closure parameters
  --> src/lib.rs:15:17
   |
15 |     let b = |x: impl Fn()| x();
   |                 ^^^^^^^^^
   |
   = note: `impl Trait` is only allowed in arguments and return types of functions and methods

For more information about this error, try `rustc --explain E0562`.

Usually following compiler suggestions you get closer to having working code, but here this leads to a dead end.

Other cases

Rust Version

rustc 1.85.0 (4d91de4e4 2025-02-17)
binary: rustc
commit-hash: 4d91de4e48198da2e33413efdcd9cd2cc0c46688
commit-date: 2025-02-17
host: x86_64-unknown-linux-gnu
release: 1.85.0
LLVM version: 19.1.7

Anything else?

Nightly makes a similar suggestion, also broken.

@pacak pacak 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 Mar 25, 2025
@rperier
Copy link
Contributor

rperier commented Mar 26, 2025

@rustbot claim

@bors bors closed this as completed in 573ebf0 Apr 11, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Apr 11, 2025
Rollup merge of rust-lang#138998 - rperier:donot_suggest_to_use_impl_trait_in_closure_params, r=Noratrieb

Don't suggest the use of  `impl Trait` in closure parameter

Fixes rust-lang#138932
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
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.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants