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

Wrong error message with closures on nightly #42143

Closed
mcarton opened this issue May 21, 2017 · 6 comments · Fixed by #44735
Closed

Wrong error message with closures on nightly #42143

mcarton opened this issue May 21, 2017 · 6 comments · Fixed by #44735
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-diagnostics Working group: Diagnostics

Comments

@mcarton
Copy link
Member

mcarton commented May 21, 2017

The following example

fn main() {
    let a = vec![(1, 2)];
    
    let b: Vec<_> = a.iter().map(|x: (u32, u32)| 45).collect(); 
}

gives the following error on nightly:

error[E0593]: closure takes 2 arguments but 1 argument is required
 --> src/main.rs:4:30
  |
4 |     let b: Vec<_> = a.iter().map(|x: (u32, u32)| 45).collect(); 
  |                              ^^^ ------------------ takes 2 arguments
  |                              |
  |                              expected closure that takes 1 argument

which is wrong (map is expecting a function with one argument, which is what this closure is) and the following correct message on stable/beta:

error[E0281]: type mismatch: the type `[closure@src/main.rs:4:34: 4:52]` implements the trait `std::ops::FnMut<((u32, u32),)>`, but the trait `std::ops::FnMut<(&({integer}, {integer}),)>` is required (expected reference, found tuple)
 --> src/main.rs:4:30
  |
4 |     let b: Vec<_> = a.iter().map(|x: (u32, u32)| 45).collect(); 
  |                              ^^^
@sfackler sfackler added the A-diagnostics Area: Messages for errors, warnings, and lints label May 21, 2017
@rbalicki2
Copy link

How does one fix this? I have run into this problem as well, and have tried all 8 variants of |&x: &(&u32, &u32)|, toggling each of the 3 placements of ampersands.

It looked like, at some point, that .map(|x: &(i32, i32)| 42) fixed it, but I could not get that to work for group_by of itertools. Perhaps these are separate issues.

@estebank
Copy link
Contributor

Introduced in #41488.

@Emilgardis
Copy link
Contributor

I would like to tackle this if no one already has begun doing it.

@estebank
Copy link
Contributor

@Emilgardis go ahead! You should add a new test for this case, and you'll need to add an extra check somewhere in report_selection_error. Don't hesitate to ask for help either here, in IRC or https://internals.rust-lang.org.

@Emilgardis
Copy link
Contributor

Emilgardis commented May 25, 2017

@estebank Do I add a type_mismatch .rs test or add to an existing test file?

@Emilgardis
Copy link
Contributor

pr on #42270

Emilgardis added a commit to Emilgardis/rust that referenced this issue Jun 29, 2017
Also adds test for this case.
Emilgardis added a commit to Emilgardis/rust that referenced this issue Jun 29, 2017
ref is expected but not found

Fixes rust-lang#42143

Adds E0604 for closure argument type mismatch
Emilgardis added a commit to Emilgardis/rust that referenced this issue Jul 7, 2017
when a ref is expected but not found.

Adds new error E0622

Fixes rust-lang#42143
@Mark-Simulacrum Mark-Simulacrum added C-enhancement Category: An issue proposing an enhancement or a PR with one. C-bug Category: This is a bug. and removed C-enhancement Category: An issue proposing an enhancement or a PR with one. labels Jul 26, 2017
tirr-c added a commit to tirr-c/rust that referenced this issue Sep 23, 2017
Fixes rust-lang#42143.
E0281 is totally replaced by E0631. UI tests are updated accordingly.
@nikomatsakis nikomatsakis added WG-diagnostics Working group: Diagnostics T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 25, 2017
bors added a commit that referenced this issue Sep 26, 2017
Friendlier error message for closure argument type mismatch

Rebased #42270.
Fixes #42143.

---

`test.rs`:

```rust
fn main() {
    foo(|_: i32, _: usize| ());
}

fn foo<F>(_: F) where F: Fn(&str, usize) {}
```

Before:

```
error[E0281]: type mismatch: `[closure@test.rs:2:9: 2:30]` implements the trait `std::ops::Fn<(i32, usize)>`, but the trait `for<'r> std::ops::Fn<(&'r str, usize)>` is required
 --> test.rs:2:5
  |
2 |     foo(|_: i32, _: usize| ());
  |     ^^^ --------------------- implements `std::ops::Fn<(i32, usize)>`
  |     |
  |     expected &str, found i32
  |     requires `for<'r> std::ops::Fn<(&'r str, usize)>`
  |
  = note: required by `foo`
```

After (early):

```
error[E0631]: type mismatch in closure arguments
 --> test.rs:2:5
  |
2 |     foo(|_: i32, _: usize| ());
  |     ^^^ --------------------- takes arguments of type `i32` and `usize`
  |     |
  |     expected arguments of type `&str` and `usize`
  |
  = note: required by `foo`
```

After (current):

```
error[E0631]: type mismatch in closure arguments
 --> test.rs:2:5
  |
2 |     foo(|_: i32, _: usize| ());
  |     ^^^ --------------------- found signature of `fn(i32, usize) -> _`
  |     |
  |     expected signature of `for<'r> fn(&'r str, usize) -> _`
  |
  = note: required by `foo`
```

~~Compiler output has been changed, and a few tests are failing. Help me writing/fixing tests!~~

r? @nikomatsakis
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-diagnostics Working group: Diagnostics
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants