Skip to content
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

Error message for wrong number of arguments to a closure is bad #21857

Closed
nrc opened this issue Feb 2, 2015 · 8 comments
Closed

Error message for wrong number of arguments to a closure is bad #21857

nrc opened this issue Feb 2, 2015 · 8 comments
Labels
A-closures Area: Closures (`|…| { … }`) 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

@nrc
Copy link
Member

nrc commented Feb 2, 2015

For example: error: type mismatch: the type `[closure /home/ncameron/rust3/src/librustc_driver/lib.rs:107:36: 107:44]` implements the trait `for<'r> core::ops::Fn<(&'r rustc::session::Session,)>`, but the trait `for<'r,'r> core::ops::Fn<(&'r rustc::session::Session, &'r getopts::Matches)>` is required (expected a tuple with 2 elements, found one with 1 elements)

It should talk about arguments rather than tuples, it should also be multi-line. It probably doesn't need to talk about the Fn trait at all.

@nrc nrc added the A-diagnostics Area: Messages for errors, warnings, and lints label Feb 2, 2015
@huonw huonw added the A-closures Area: Closures (`|…| { … }`) label Apr 14, 2015
@nagisa
Copy link
Member

nagisa commented May 12, 2016

This is a very bad issue and there’s only a single report on it?!

Need more ccs. Any thoughts on who would be good people to cc here, @nrc?

@nrc
Copy link
Member Author

nrc commented May 12, 2016

cc @jonathandturner @nikomatsakis

@sophiajt
Copy link
Contributor

This feels like a good candidate to batch with #33240

@nikomatsakis
Copy link
Contributor

Dup of #24680

@arielb1
Copy link
Contributor

arielb1 commented May 16, 2016

I am not so sure. That issue is about the HR leak_check error message being terrible, while this is about the general error message mentioning a tuple.

@arielb1 arielb1 reopened this May 16, 2016
@nikomatsakis
Copy link
Contributor

@arielb1 I suppose the original issue was highlighting a smattering of things; seems ok to separate out tuples in particular, sure.

@bluss
Copy link
Member

bluss commented Nov 23, 2016

Below are more examples of code and their bad error messages.

Expected behaviour:

  • The errors should say that the method .sort_by expects a closure with two arguments, in plain language.
error[E0281]: type mismatch: the type `[closure@<anon>:3:23: 3:39]` implements the trait `for<'r> std::ops::FnMut<(&'r {integer},)>`, but the trait `for<'r, 'r> std::ops::FnMut<(&'r {integer}, &'r {integer})>` is required (expected a tuple with 2 elements, found one with 1 elements)
 --> <anon>:3:15
  |
3 |     [1, 2, 3].sort_by(|tuple| panic!());
  |               ^^^^^^^

error[E0281]: type mismatch: the type `[closure@<anon>:3:23: 3:39]` implements the trait `for<'r> std::ops::FnOnce<(&'r {integer},)>`, but the trait `for<'r, 'r> std::ops::FnOnce<(&'r {integer}, &'r {integer})>` is required (expected a tuple with 2 elements, found one with 1 elements)
 --> <anon>:3:15
  |
3 |     [1, 2, 3].sort_by(|tuple| panic!());
  |               ^^^^^^^

error[E0277]: the trait bound `for<'r, 'r> {integer}: std::ops::FnMut<(&'r {integer}, &'r {integer})>` is not satisfied
 --> <anon>:4:15
  |
4 |     [1, 2, 3].sort_by(1);
  |               ^^^^^^^ the trait `for<'r, 'r> std::ops::FnMut<(&'r {integer}, &'r {integer})>` is not implemented for `{integer}`
  |
  = help: the following implementations were found:
  = help:   <&'a F as std::ops::FnMut<A>>
  = help:   <&'a mut F as std::ops::FnMut<A>>
  = help:   <core::str::LinesAnyMap as std::ops::FnMut<(&'a str,)>>

error[E0277]: the trait bound `for<'r, 'r> {integer}: std::ops::FnOnce<(&'r {integer}, &'r {integer})>` is not satisfied
 --> <anon>:4:15
  |
4 |     [1, 2, 3].sort_by(1);
  |               ^^^^^^^ the trait `for<'r, 'r> std::ops::FnOnce<(&'r {integer}, &'r {integer})>` is not implemented for `{integer}`
  |
  = help: the following implementations were found:
  = help:   <&'a F as std::ops::FnOnce<A>>
  = help:   <&'a mut F as std::ops::FnOnce<A>>
  = help:   <core::str::LinesAnyMap as std::ops::FnOnce<(&'a str,)>>
  = help:   <Box<std::boxed::FnBox<A, Output=R> + 'a> as std::ops::FnOnce<A>>
  = help: and 2 others
error[E0308]: mismatched types
 --> <anon>:3:24
  |
3 |     [1, 2, 3].sort_by(|(a, b)| panic!());
  |                        ^^^^^^ expected &{integer}, found tuple
  |
  = note: expected type `&{integer}`
  = note:    found type `(_, _)`

error[E0281]: type mismatch: the type `[closure@<anon>:3:23: 3:40]` implements the trait `for<'r> std::ops::FnMut<(&'r {integer},)>`, but the trait `for<'r, 'r> std::ops::FnMut<(&'r {integer}, &'r {integer})>` is required (expected a tuple with 2 elements, found one with 1 elements)
 --> <anon>:3:15
  |
3 |     [1, 2, 3].sort_by(|(a, b)| panic!());
  |               ^^^^^^^

error[E0281]: type mismatch: the type `[closure@<anon>:3:23: 3:40]` implements the trait `for<'r> std::ops::FnOnce<(&'r {integer},)>`, but the trait `for<'r, 'r> std::ops::FnOnce<(&'r {integer}, &'r {integer})>` is required (expected a tuple with 2 elements, found one with 1 elements)
 --> <anon>:3:15
  |
3 |     [1, 2, 3].sort_by(|(a, b)| panic!());
  |               ^^^^^^^

@steveklabnik steveklabnik added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Mar 9, 2017
@estebank
Copy link
Contributor

I have a PR with this proposed output for the following file:

fn main() {
    [1, 2, 3].sort_by(|tuple| panic!());
    [1, 2, 3].sort_by(|(tuple, tuple2)| panic!());
}
error[E0593]: closure takes 1 parameter but 2 parameters are required here
  --> $DIR/closure-arg-count.rs:12:15
   |
12 |     [1, 2, 3].sort_by(|tuple| panic!());
   |               ^^^^^^^ ---------------- takes 1 parameter
   |               |
   |               expected closure that takes 2 parameters

error[E0593]: closure takes 1 parameter but 2 parameters are required here
  --> $DIR/closure-arg-count.rs:12:15
   |
12 |     [1, 2, 3].sort_by(|tuple| panic!());
   |               ^^^^^^^ ---------------- takes 1 parameter
   |               |
   |               expected closure that takes 2 parameters

error[E0308]: mismatched types
  --> $DIR/closure-arg-count.rs:13:24
   |
13 |     [1, 2, 3].sort_by(|(tuple, tuple2)| panic!());
   |                        ^^^^^^^^^^^^^^^ expected &{integer}, found tuple
   |
   = note: expected type `&{integer}`
              found type `(_, _)`

error[E0593]: closure takes 1 parameter but 2 parameters are required here
  --> $DIR/closure-arg-count.rs:13:15
   |
13 |     [1, 2, 3].sort_by(|(tuple, tuple2)| panic!());
   |               ^^^^^^^ -------------------------- takes 1 parameter
   |               |
   |               expected closure that takes 2 parameters

error[E0593]: closure takes 1 parameter but 2 parameters are required here
  --> $DIR/closure-arg-count.rs:13:15
   |
13 |     [1, 2, 3].sort_by(|(tuple, tuple2)| panic!());
   |               ^^^^^^^ -------------------------- takes 1 parameter
   |               |
   |               expected closure that takes 2 parameters

error: aborting due to 5 previous errors

Any suggestions welcome.

I'm looking at removing the duplicate messages, but that might have to be a separate PR.

bors added a commit that referenced this issue May 2, 2017
Clean up callable type mismatch errors

```rust
error[E0593]: closure takes 1 argument but 2 arguments are required here
  --> ../../src/test/ui/mismatched_types/closure-arg-count.rs:13:15
   |
13 |     [1, 2, 3].sort_by(|(tuple, tuple2)| panic!());
   |               ^^^^^^^ -------------------------- takes 1 argument
   |               |
   |               expected closure that takes 2 arguments
```

instead of

```rust
error[E0281]: type mismatch: the type `[closure@../../src/test/ui/mismatched_types/closure-arg-count.rs:13:23: 13:49]` implements the trait `for<'r> std::ops::FnMut<(&'r {integer},)>`, but the trait `for<'r, 'r> std::ops::FnMut<(&'r {integer}, &'r {integer})>` is required (expected a tuple with 2 elements, found one with 1 elements)
  --> ../../src/test/ui/mismatched_types/closure-arg-count.rs:13:15
   |
13 |     [1, 2, 3].sort_by(|(tuple, tuple2)| panic!());
   |               ^^^^^^^
```

Fix #21857, re #24680.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-closures Area: Closures (`|…| { … }`) 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

No branches or pull requests

9 participants