Skip to content

Confusing error "expected type parameter, found closure" #51154

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
oberien opened this issue May 29, 2018 · 2 comments · Fixed by #76711
Closed

Confusing error "expected type parameter, found closure" #51154

oberien opened this issue May 29, 2018 · 2 comments · Fixed by #76711
Assignees
Labels
A-closures Area: Closures (`|…| { … }`) A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@oberien
Copy link
Contributor

oberien commented May 29, 2018

Minimal example:

fn test<F: FnMut()>() {
    let f: Box<F> = Box::new(|| ());
}

error[E0308]: mismatched types
 --> src/main.rs:2:30
  |
2 |     let f: Box<F> = Box::new(|| ());
  |                              ^^^^^ expected type parameter, found closure
  |
  = note: expected type `F`
             found type `[closure@src/main.rs:2:30: 2:35]`

The error is technically correct. I specified, that I want a box of F, but I create a closure. Because F is generic, it is caller-chosen and I create a new closure, which has a different type.

Still, it took me quite some time to find out what was going on in the more complicated real code. I think that it would help to add a note here explaining, that each closure has a different type, and that the inner closure does not match the type F exactly. Maybe additionally a hint could be added, stating to use Box<FnMut()> instead (i.e. the traits F is bound to).

@estebank estebank added the A-diagnostics Area: Messages for errors, warnings, and lints label May 29, 2018
@XAMPPRocky XAMPPRocky added C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 2, 2018
@estebank
Copy link
Contributor

Current output:

error[E0308]: mismatched types
 --> src/lib.rs:2:30
  |
2 |     let f: Box<F> = Box::new(|| ());
  |                              ^^^^^ expected type parameter, found closure
  |
  = note: expected type `F`
             found type `[closure@src/lib.rs:2:30: 2:35]`
  = help: type parameters must be constrained to match other types
  = note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters

It doesn't handle the closure case in any special way, and it should.

@estebank estebank added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` A-associated-items Area: Associated items (types, constants & functions) A-closures Area: Closures (`|…| { … }`) and removed A-associated-items Area: Associated items (types, constants & functions) labels Sep 26, 2019
@estebank estebank added the D-papercut Diagnostics: An error or lint that needs small tweaks. label Feb 12, 2020
@TianyiShi2001
Copy link

I’m experiencing the same problem

@davidtwco davidtwco self-assigned this Sep 14, 2020
RalfJung added a commit to RalfJung/rust that referenced this issue Sep 28, 2020
…, r=estebank

diag: improve closure/generic parameter mismatch

Fixes rust-lang#51154.

This PR improves the diagnostic when a type parameter is expected and a closure is found, noting that each closure has a distinct type and therefore could not always match the caller-chosen type of the parameter.

r? @estebank
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Sep 28, 2020
…, r=estebank

diag: improve closure/generic parameter mismatch

Fixes rust-lang#51154.

This PR improves the diagnostic when a type parameter is expected and a closure is found, noting that each closure has a distinct type and therefore could not always match the caller-chosen type of the parameter.

r? @estebank
@bors bors closed this as completed in 88ae20d Sep 28, 2020
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-closures Area: Closures (`|…| { … }`) A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. 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.

5 participants