Skip to content

Confusing error message when applying binary operation on the wrong type #33877

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
gnzlbg opened this issue May 26, 2016 · 0 comments
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@gnzlbg
Copy link
Contributor

gnzlbg commented May 26, 2016

The following snippet:

let v = vec![1, 2, 3, 4, 5, 6, 7, 8, 9];
let vr = v.iter().filter(|x| x % 2 == 0); // wrong
// let vr = v.iter().filter(|&x| x % 2 == 0); // correct

produces the following error:

<anon>:10:34: 10:35 error: binary operation `%` cannot be applied to type `&&_` [E0369]
<anon>:10     let vr = v.iter().filter(|x| x % 2 == 0);
                                           ^
<anon>:10:34: 10:35 help: see the detailed explanation for E0369
<anon>:10:34: 10:35 note: an implementation of `std::ops::Rem` might be missing for `&&_`
<anon>:10     let vr = v.iter().filter(|x| x % 2 == 0);
                                           ^
error: aborting due to previous error

How is anybody supposed to extract from binary operation % cannot be applied to type &&_ that one forgot the &x in the closure? The explanation E0369 doesn't help here.

Maybe a better thing for rustc would be to "try" and see if a combination of & and &mut would have solved the issue and offer a suggestion of the form "Did you meant to make x a reference here?".

@nagisa nagisa added the A-diagnostics Area: Messages for errors, warnings, and lints label May 26, 2016
estebank added a commit to estebank/rust that referenced this issue Jul 6, 2016
```rust
let vr = v.iter().filter(|x| {
    x % 2 == 0
});
```

will now yield the following compiler output:

```bash
ERROR binary operation `%` cannot be applied to type `&&_`
NOTE this is a reference of a reference to a type that `%` can be applied to,
you need to dereference this variable once for this operation to work
NOTE an implementation of `std::ops::Rem` might be missing for `&&_`
```

The first NOTE is new.

Bug rust-lang#33877
pnkfelix pushed a commit to pnkfelix/rust that referenced this issue Dec 26, 2016
```rust
let vr = v.iter().filter(|x| {
    x % 2 == 0
});
```

will now yield the following compiler output:

```bash
ERROR binary operation `%` cannot be applied to type `&&_`
NOTE this is a reference of a reference to a type that `%` can be applied to,
you need to dereference this variable once for this operation to work
NOTE an implementation of `std::ops::Rem` might be missing for `&&_`
```

The first NOTE is new.

Bug rust-lang#33877
pnkfelix pushed a commit to pnkfelix/rust that referenced this issue Jan 3, 2017
```rust
let vr = v.iter().filter(|x| {
    x % 2 == 0
});
```

will now yield the following compiler output:

```bash
ERROR binary operation `%` cannot be applied to type `&&_`
NOTE this is a reference of a reference to a type that `%` can be applied to,
you need to dereference this variable once for this operation to work
NOTE an implementation of `std::ops::Rem` might be missing for `&&_`
```

The first NOTE is new.

Bug rust-lang#33877
steveklabnik added a commit to steveklabnik/rust that referenced this issue Jan 4, 2017
Detect double reference when applying binary op

``` rust
let vr = v.iter().filter(|x| {
    x % 2 == 0
});
```

will now yield the following compiler output:

``` bash
ERROR binary operation `%` cannot be applied to type `&&_`
NOTE this is a reference of a reference to a type that `%` can be applied to,
you need to dereference this variable once for this operation to work
NOTE an implementation of `std::ops::Rem` might be missing for `&&_`
```

The first NOTE is new.

Fix rust-lang#33877

----

Thanks to @estebank for providing the original PR rust-lang#34420 (of which this is a tweaked rebase).
alexcrichton added a commit to alexcrichton/rust that referenced this issue Jan 28, 2017
Detect double reference when applying binary op

``` rust
let vr = v.iter().filter(|x| {
    x % 2 == 0
});
```

will now yield the following compiler output:

``` bash
ERROR binary operation `%` cannot be applied to type `&&_`
NOTE this is a reference of a reference to a type that `%` can be applied to,
you need to dereference this variable once for this operation to work
NOTE an implementation of `std::ops::Rem` might be missing for `&&_`
```

The first NOTE is new.

Fix rust-lang#33877

----

Thanks to @estebank for providing the original PR rust-lang#34420 (of which this is a tweaked rebase).
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints
Projects
None yet
Development

No branches or pull requests

2 participants